1 : |
jhr |
5597 |
# Data Layout in Diderot
|
2 : |
|
|
|
3 : |
|
|
This note describes the data layout conventions used in the Diderot
|
4 : |
|
|
implementation for images and tensors.
|
5 : |
|
|
|
6 : |
|
|
## Tensors
|
7 : |
|
|
|
8 : |
|
|
The axes of tensors and tensor fields are listed from slowest to fastest (which is the opposite of nrrd files).
|
9 : |
|
|
|
10 : |
|
|
### Tensor construction
|
11 : |
|
|
|
12 : |
|
|
The CFG IR expression
|
13 : |
|
|
|
14 : |
|
|
```
|
15 : |
|
|
T = CONS[T_1, ..., T_d]
|
16 : |
|
|
```
|
17 : |
|
|
|
18 : |
|
|
will construct a tensor with type `tensor[d_1,...,d_n,d]`, where the `T_i`
|
19 : |
|
|
tensors have the type `tensor[d_1,...,d_n]`. Thus the dimensions of a tensor
|
20 : |
|
|
are listed from slowest to fastest (the opposite of Nrrd file headers).
|
21 : |
|
|
|
22 : |
|
|
### Tensor indexing
|
23 : |
|
|
|
24 : |
|
|
If we have
|
25 : |
|
|
|
26 : |
|
|
````diderot
|
27 : |
|
|
tensor[d_1,...,d_n] T;
|
28 : |
|
|
````
|
29 : |
|
|
|
30 : |
|
|
then the expression
|
31 : |
|
|
|
32 : |
|
|
````diderot
|
33 : |
|
|
T[i_1,...,i_n]
|
34 : |
|
|
````
|
35 : |
|
|
|
36 : |
|
|
is translated to the following address arithmetic:
|
37 : |
|
|
|
38 : |
|
|
````
|
39 : |
|
|
T + i_n + d_n * (i_{n-1} + d_{n-1} * ( ... d_2 * i_1) ... )
|
40 : |
|
|
````
|
41 : |
|
|
|
42 : |
|
|
## Differentiation
|
43 : |
|
|
|
44 : |
|
|
The gradiant operator returns a field of higher order than its argument.
|
45 : |
|
|
For example
|
46 : |
|
|
|
47 : |
|
|
````diderot
|
48 : |
|
|
field#2(3)[2] F;
|
49 : |
|
|
field#1(3)[2,3] G = ∇ F;
|
50 : |
|
|
tensor[2,3] T = G(x);
|
51 : |
|
|
````
|
52 : |
|
|
|
53 : |
|
|
Again, the dimensions are slowest to fastest, so T can be thought of
|
54 : |
|
|
as a two-element array of three-vectors.
|
55 : |
|
|
|
56 : |
|
|
## Images and Nrrd Files
|
57 : |
|
|
|
58 : |
|
|
Image values are represented using the [Nrrd file format](http://teem.sourceforge.net/nrrd/format.html)
|
59 : |
|
|
from teem.
|
60 : |
|
|
|
61 : |
|
|
### `LoadVoxels`
|
62 : |
|
|
|
63 : |
|
|
The nrrd convention is to list indices from fastest to slowest (opposite
|
64 : |
|
|
the C convention). If we are sampling a `4x4` grid of voxels from an image
|
65 : |
|
|
with type `image(2)[2])`, then the data will have the following layout in the nrrd file:
|
66 : |
|
|
````
|
67 : |
|
|
... x00 y00 x10 y10 x20 y20 x30 y30 ...
|
68 : |
|
|
... x01 y01 x11 y11 x21 y21 x31 y31 ...
|
69 : |
|
|
... x02 y02 x12 y12 x22 y22 x32 y32 ...
|
70 : |
|
|
... x03 y03 x13 y13 x23 y23 x33 y33 ...
|
71 : |
|
|
````
|
72 : |
|
|
|
73 : |
|
|
When we load these voxels into memory, however, we swizzle them to fit the type `tensor[2,4,4]`.
|
74 : |
|
|
The following IR expression shows how the voxel tensor would be constructed:
|
75 : |
|
|
|
76 : |
|
|
```
|
77 : |
|
|
CONS[
|
78 : |
|
|
CONS[
|
79 : |
|
|
CONS[x00, x10, x20, x30],
|
80 : |
|
|
CONS[x01, x11, x21, x31],
|
81 : |
|
|
CONS[x02, x12, x22, x32],
|
82 : |
|
|
CONS[x03, x13, x23, x33]],
|
83 : |
|
|
CONS[
|
84 : |
|
|
CONS[y00, y10, y20, y30],
|
85 : |
|
|
CONS[y01, y11, y21, y31],
|
86 : |
|
|
CONS[y02, y12, y22, y32],
|
87 : |
|
|
CONS[y03, y13, y23, y33]]
|
88 : |
|
|
]
|
89 : |
|
|
```
|