Writing Dense Arrays¶
In this tutorial you will learn how to write to dense arrays. It is highly recommended that you read the tutorials on dense arrays and tiling first, as well as writing sparse arrays.
Program |
Links |
|
|
|
|
|
|
|
|
|
Basic concepts and definitions¶
Writing to a dense array¶
Let us revisit the quickstart_dense
example of tutorial Dense Arrays.
Here is how we wrote to the array:
This writes values 1, 2, ..., 16
to the 4x4
dense TileDB array.
You can write to any subarray in a dense array,
i.e., you do not have to populate the entire array at once
(see also the section on multiple writes below). For instance,
the code below would just populate only two rows of the above
array, i.e., subarray [1,2], [1,4]
:
The figure below depicts the array contents when varying the query layout. Observe that the layout is always specified with respect to the query subarray.
Empty space / Padding¶
Since TileDB allows you to write to any subarray of your array,
what happens if your array has empty areas? We demonstrate
with example writing_dense_padding
. We first create a 4x4
array
with 2x2
space tiling. We then write only subarray
[2,3], [12]
, leaving the rest of the array unpopulated:
The array looks like in the figure below.
The example then reads the entire array (i.e., [1,4], [1,4]
)
in row-major order and
prints the cell values on the screen. Here is the output after
running the program:
Observe that for every empty cell in [1,4], [1,4]
, TileDB returned value
-2147483648
. This is the default fill value, which is is equal to
the minumum value stored in an integer variable.
The table below shows the default fill values for all supported
attribute data types.
Data type |
Fill value |
|
Minimum |
|
Minimum |
|
Maximum |
|
Minimum |
|
Maximum |
|
Minimum |
|
Maximum |
|
Minimum |
|
Maximum |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Note
In a future release, you will be able to define your own fill values for each of your arrays.
But you may wonder, how does TileDB handles empty spaces at the physical level?
Note
TileDB does not materialize empty cells for dense arrays, except for the ones belonging to partially written tiles.
Let us explain the above with our running example. The array has 2x2
space
tiling, which means that we have two partially written tiles (the upper left
and lower left), and two completely empty tiles (upper right and lower right).
TileDB distinguishes between partially written tiles and completely empty tiles.
We mentioned in an earlier tutorial that TileDB always writes integral tiles
on the file, i.e., it cannot write just 2 out of 4 cells in our example.
Hence, TileDB will explicitly store the fill value for each empty cell in
a partially written tile. In contrast, it does not materialize any cells for
the completely
empty tiles (i.e., it entirely ignores empty tiles). This is depicted
in the figure below.
Multiple writes / Updates¶
You can write to a dense array multiple times, similar to the case we
described for sparse arrays in an earlier tutorial. Each write in
row-major or column-major layout creates a new subfolder/fragment
in the array directory (we explain unordered and global layout in
the subsections below). Consider the following two writes to the
dense array we have been using in the examples above (see
code example writing_dense_multiple
):
The first writes to subarray [1,2], [1,2]
, whereas the second
to [2,3], [1,4]
. The figure below depicts the two writes,
as well as the collective logical view of the array after the second write.
After running the program, we get the following output. Observe that the full read (in row-major) order results in retrieving the cell values as depicted in the collective logical view of the array (again, retrieving the default fill values for the empty cells).
Listing the array directory, you can see that there are two subfolders/fragments created:
$ ls -l multiple_writes_dense_array/
total 8
drwx------ 4 stavros staff 128 Jun 25 15:49 __1561492148493_1561492148493_52634f26a295445ca6c6dcfdafc8a967
drwx------ 4 stavros staff 128 Jun 25 15:49 __1561492148506_1561492148506_fef381c0326b49a59d6e74816416dfa1
-rwx------ 1 stavros staff 149 Jun 25 15:49 __array_schema.tdb
-rwx------ 1 stavros staff 0 Jun 25 15:49 __lock.tdb
drwx------ 2 stavros staff 64 Jun 25 15:49 __meta
Writing sparse cells¶
Warning
Currently sparse writes to dense arrays are not supported in the Python API.
One exciting feature about dense arrays is that you can write sparse cells to them, i.e., you can write multiple cells that do not necessary fall in the same hyper-rectangular subarray in a single write query. The sparse writes in dense arrays are identical to those of sparse arrays, i.e., you need to add an extra buffer that holds the explicit coordinates of the cells you are writing into. Also here is where the unordered layout is relevant again. Everything discussed about writes in Writing Sparse Arrays (e.g., even writing in global order) holds here as well.
Let us demonstrate with code example writing_dense_sparse
. Here is how we write
some sparse cells to the dense array of the previous examples:
The array resulting from the above write looks as follows:
Compiling and running the program gives the output shown below. Observe that, contrary to sparse arrays, when slicing dense arrays, TileDB returns fill values for empty areas as explain earlier in this tutorial. Recall that, in the case of sparse arrays, you get back only the values of the non-empty cells; no cell is ever materialized for sparse arrays (neither in writes nor reads). Also notice that you can explicitly request the cell coordinates even in dense arrays.
$ g++ -std=c++11 writing_dense_sparse.cc -o writing_dense_sparse_cpp -ltiledb
$ ./writing_dense_sparse_cpp
Cell (1, 1) has data -2147483648
Cell (1, 2) has data 1
Cell (1, 3) has data -2147483648
Cell (1, 4) has data 4
Cell (2, 1) has data 2
Cell (2, 2) has data -2147483648
Cell (2, 3) has data -2147483648
Cell (2, 4) has data -2147483648
Cell (3, 1) has data -2147483648
Cell (3, 2) has data -2147483648
Cell (3, 3) has data -2147483648
Cell (3, 4) has data -2147483648
Cell (4, 1) has data -2147483648
Cell (4, 2) has data -2147483648
Cell (4, 3) has data 3
Cell (4, 4) has data -2147483648
Let us inspect the contents of the dense array after the write:
$ ls -l writing_dense_sparse_array/
total 8
drwx------ 5 stavros staff 160 Jun 25 15:50 __1561492235844_1561492235844_c033cea7bbc34f2bb425969a497f7bab
-rwx------ 1 stavros staff 149 Jun 25 15:50 __array_schema.tdb
-rwx------ 1 stavros staff 0 Jun 25 15:50 __lock.tdb
drwx------ 2 stavros staff 64 Jun 25 15:50 __meta
$ ls -l writing_dense_sparse_array/__1561492235844_1561492235844_c033cea7bbc34f2bb425969a497f7bab/
total 24
-rwx------ 1 stavros staff 114 Jun 25 15:50 __coords.tdb
-rwx------ 1 stavros staff 610 Jun 25 15:50 __fragment_metadata.tdb
-rwx------ 1 stavros staff 36 Jun 25 15:50 a.tdb
Observe that the
coordinates were written explicitly in file __coords.tdb
inside
the fragment, similarly to the sparse case. Note that here the
sparse format is adopted, i.e., no empty cell is materialized.
This is in contrast to the padding technique explained earlier in this
tutorial for dense fragments. In other words, when writing sparse cells to
dense arrays, TileDB creates a sparse fragment, which is treated as a
sparse array snapshot! We provide more information on fragments in a later
tutorial.
Writing in global layout¶
Warning
Currently writing in global layout is not supported in the Python API.
TileDB allows you to write in global order similar to the case of sparse arrays. This generally leads to better performance, but comes at the expense of extra usage complexity. The limitation with this layout is that you need to always write integral tiles, i.e., the subarray you set to the query (and write into) must not partially intersect tiles, but instead encompass them entirely.
The following code writes to subarray [1,4], [1,2]
(see example
writing_dense_global
). Observe that, similar
to the case of sparse arrays, we can submit the same query multiple
times, effectively appending to the same fragment (list the contents
of the resulting array to verify this). The difference here is that
we update the contents of the buffer we already set, without needing
to reset the buffer to the query in the second write. Either way works
here (resetting a new buffer, or updating the contents of the set buffer
without resetting). Important: do not forget to finalize the query
when you are done writing/appending in global order
and before closing the array.
The resulting array is depicted below. Note that the subarray we wrote into contains exactly two tiles. Any attempt to write to partial tiles in global order would have failed (and the behavior can be unexpected).
Writing in global order mode must be done with extra care in case some tile extent does not divide the respective dimension domain. As we have explained in an earlier tutorial, this results in internal domain expansion. Moreover, TileDB does not allow you to write outside your defined domain. Therefore, if your domain contains partial tiles, you will not be able to write to them in global order.
We illustrate with an example. Consider you have a 4x3
array
with 2x2
space tiling, as shown in the figure below. The
domain contains two entire tiles (upper left and lower left)
and two partial tiles (upper right and lower right). In this
case, you can write in global order in subarray [1,4], [1,2]
,
but choose another layout (e.g., row-major) for [1,4], [3,3]
.
This is done in code example writingdenseglobalexpansioncpp
in two
writes, which are also shown in the figure below.
Writing and performance¶
The writing performance can be affected by various factors, such as the tiling, filters and query layout. See the Introduction to Performance tutorial for more information about the TileDB performance.