contiguous

Undocumented in source. Be warned that the author may not have intended to support it.
version(grain_cuda)
contiguous
(
T
size_t dim
)

Examples

test cudnnTransformTensor with array ptr manipulations

1 import std.stdio;
2 // skipping stride 2
3 {
4     auto x = [1f, 0f, 2f, 0f, 3f].variable;
5     x.strides = [2];
6     x.shape = [3];
7     auto y = x.to!DeviceStorage.contiguous.to!HostStorage;
8     assert(y.data == [1f, 2f, 3f]);
9     assert(y.strides == [1]);
10     assert(y.shape == [3]);
11 }
12 // reverse skipping stride -2
13 {
14     auto x = [1f, 0f, 2f, 0f, 3f].variable;
15     x.strides = [-2];
16     x.shape = [3];
17     auto dx = x.to!DeviceStorage;
18     dx.data.ptr += 4 * float.sizeof;
19     scope(exit) dx.data.ptr -= 4 * float.sizeof;
20     auto y = dx.contiguous.to!HostStorage;
21     assert(y.data == [3f, 2f, 1f]);
22     assert(y.strides == [1]);
23     assert(y.shape == [3]);
24 }
25 // multi-dim transposed stride [3, 1]
26 {
27     auto x = [[1f, 0f, 2f],
28               [0f, 3f, 0f]].variable;
29     x.strides = [1, 3];
30     x.shape = [3, 2];
31     auto dx = x.to!DeviceStorage;
32     auto y = dx.contiguous.to!HostStorage;
33     assert(y.sliced == [[1f, 0f], [0f, 3f], [2f, 0f]]);
34     assert(y.strides == [2, 1]);
35     assert(y.shape == [3, 2]);
36 }
37 // multi-dim skipping stride [3, 2]
38 {
39     auto x = [[1f, 0f, 2f],
40               [0f, 3f, 0f]].variable;
41     x.strides = [3, 2];
42     x.shape = [2, 2];
43     auto dx = x.to!DeviceStorage;
44     auto y = dx.contiguous.to!HostStorage;
45     assert(y.sliced == [[1f, 2f],  [0f, 0f]]);
46     assert(y.strides == [2, 1]);
47     assert(y.shape == [2, 2]);
48 }
49 // multi-dim transposed skipping stride [2, 3]
50 {
51     auto x = [[1f, 0f, 2f],
52               [0f, 3f, 0f]].variable;
53     x.strides = [2, 3];
54     x.shape = [2, 2];
55     auto dx = x.to!DeviceStorage;
56     // dx.data.ptr += (2 * 3 - 1) * float.sizeof;
57     // scope(exit) dx.data.ptr -= (2 * 3 - 1) * float.sizeof;
58     auto y = dx.contiguous.to!HostStorage;
59     assert(y.sliced == [[1f, 0f],  [2f, 0f]]);
60     assert(y.strides == [2, 1]);
61     assert(y.shape == [2, 2]);
62 }
63 // multi-dim transposed reverse skipping stride [-2, -3]
64 {
65     auto x = [[1f, 0f, 2f],
66               [0f, 3f, 0f]].variable;
67     x.strides = [-2, -3];
68     x.shape = [2, 2];
69     auto dx = x.to!DeviceStorage;
70     dx.data.ptr += (2 * 3 - 1) * float.sizeof;
71     scope(exit) dx.data.ptr -= (2 * 3 - 1) * float.sizeof;
72     auto y = dx.contiguous.to!HostStorage;
73     assert(y.sliced == [[0f, 2f],  [0f, 1f]]);
74     assert(y.strides == [2, 1]);
75     assert(y.shape == [2, 2]);
76 }

Meta