-
Notifications
You must be signed in to change notification settings - Fork 6
/
ipt.hpp
354 lines (297 loc) · 7.72 KB
/
ipt.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/* ipt.hpp - In-Place Transposition
*
* When transitioning between different media,
* e.g. CPU to GPU, CPU to Network, CPU to disk,
* it's often necessary to physically transpose
* multi-dimensional arrays to reformat as C or
* Fortran order. Tranposing matrices is also
* a common action in linear algebra, but often
* you can get away with just changing the strides.
*
* An out-of-place transposition is easy to write,
* often faster, but will spike peak memory consumption.
*
* This library grants the user the option of performing
* an in-place transposition which trades CPU time for
* peak memory usage.
*
* Author: William Silversmith
* Date: Feb. 2019
*/
#include <algorithm>
#include <cstdint>
#include <vector>
#ifndef IN_PLACE_TRANSPOSE_H
#define IN_PLACE_TRANSPOSE_H
// ipt = in-place transpose
// call as:
// 2d: ipt::ipt<T>(arr, sx, sy);
// 3d: ipt::ipt<T>(arr, sx, sy, sz);
// 4d: ipt::ipt<T>(arr, sx, sy, sz, sw);
namespace ipt {
template <typename T>
void square_ipt(T* arr, const size_t sx, const size_t sy) {
T tmp = 0;
size_t k = 0;
size_t next_k = 0;
size_t base_k = 0; // just for going faster
for (size_t y = 0; y < sy; y++) {
base_k = sx * y;
for (size_t x = y; x < sx; x++) {
k = x + base_k;
next_k = y + sy * x;
tmp = arr[next_k];
arr[next_k] = arr[k];
arr[k] = tmp;
}
}
}
/* A permutation, P(k), is a mapping of
* one arrangement of numbers to another.
* For an m x n array, the permuatation
* mapping from C to Fortran order is:
*
* P(k) := mk mod mn - 1
* iP(k) := nk mod mn - 1 (the inverse)
*
* Where does this come from? Assume we are
* going from C to Fortran order (it doesn't
* matter either way). The indicies are defined
* as:
*
* k = C(x,y) = x + sx * y
* F(x,y) = y + sy * x
*
* The permutation P(k) is the transformation:
*
* P(C(x,y)) = F(x,y)
*
* 1. P(x + sx * y) = y + sx * x
* 2. sy (x + sx y) = sy x + sx sy y
* 3. Let q = (sx sy - 1)
* 4. sy x + sx sy y % q
* 5. ((sy x % q) + (sx sy y % q)) % q by distributive identity
* 6. sy x is identical b/c q is always bigger
* 7. sx sy y reduces to y
* 8 q is always bigger than sy x + y so it disappears
*
* ==> P(k) = y + sy * x = F(x,y)
* ==> P(k) = sy * k % (sx sy - 1)
*
* Note that P(0) and P(q) are always 0 and q respectively.
*
* Now we need a way to implement this insight.
* How can we move the data around without using too
* much extra space? A simple algorithm is
* "follow-the-cycles". Each time you try moving a
* k to P(k), it displaces the resident tile. Eventually,
* this forms a cycle. When you reach the end of a cycle,
* you can stop processing and move to unvisited parts of
* the array. This requires storing a packed bit representation
* of where we've visited to make sure we get everything.
* This means we need to store between 2.0x and 1.016x
* memory in the size of the original array depending on its
* data type (2.0x would be a transpose of another bit packed
* array and 1.016x would be 64-bit data types).
*
* There are fancier algorithms that use divide-and-conquer,
* and SIMD tricks, and near zero extra memory, but
* this is a good place to start. Fwiw, the bit vector
* has an O(nm) time complexity (really 2nm) while the
* sans-bit vector algorithms are O(nm log nm).
*/
template <typename T>
void rect_ipt(T* arr, const size_t sx, const size_t sy) {
const size_t sxy = sx * sy;
std::vector<bool> visited;
visited.resize(sxy);
visited[0] = true;
visited[sxy - 1] = true;
const size_t q = sxy - 1;
size_t k, next_k;
T tmp1, tmp2;
for (size_t i = 1; i < q; i++) {
if (visited[i]) {
continue;
}
k = i;
tmp1 = arr[k];
next_k = sy * k - q * (k / sx); // P(k)
while (!visited[next_k]) {
tmp2 = arr[next_k];
arr[next_k] = tmp1;
tmp1 = tmp2;
visited[next_k] = true;
k = next_k;
next_k = sy * k - q * (k / sx); // P(k)
}
}
}
// note: sx == sy == sz... find better convention?
// still good for mutliple-dispatch.
template <typename T>
void square_ipt(
T* arr,
const size_t sx, const size_t sy, const size_t sz
) {
T tmp = 0;
const size_t sxy = sx * sy;
const size_t syz = sy * sz;
size_t k = 0;
size_t next_k = 0;
size_t base_k = 0;
for (size_t z = 0; z < sz; z++) {
for (size_t y = 0; y < sy; y++) {
base_k = sx * y + sxy * z;
for (size_t x = z; x < sx; x++) {
k = x + base_k;
next_k = z + sz * y + syz * x;
tmp = arr[next_k];
arr[next_k] = arr[k];
arr[k] = tmp;
}
}
}
}
inline size_t P_3d(
const size_t k,
const size_t sx, const size_t sy, const size_t sz
) {
const size_t sxy = sx * sy;
// k = x + sx y + sx sy z
size_t z = k / sxy;
size_t y = (k - (z * sxy)) / sx;
size_t x = k - sx * (y + z * sy);
return z + sz * (y + sy * x);
}
template <typename T>
void rect_ipt(
T* arr,
const size_t sx, const size_t sy, const size_t sz
) {
const size_t sxy = sx * sy;
const size_t N = sxy * sz;
std::vector<bool> visited;
visited.resize(N);
visited[0] = true;
visited[N - 1] = true;
size_t k, next_k;
T tmp1 = 0, tmp2 = 0;
for (size_t i = 1; i < (N - 1); i++) {
if (visited[i]) {
continue;
}
k = i;
tmp1 = arr[k];
next_k = P_3d(k, sx, sy, sz);
while (!visited[next_k]) {
tmp2 = arr[next_k];
arr[next_k] = tmp1;
tmp1 = tmp2;
visited[next_k] = true;
k = next_k;
next_k = P_3d(k, sx, sy, sz);
}
}
}
inline size_t P_4d(
const size_t k,
const size_t sx, const size_t sy, const size_t sz, const size_t sw
) {
const size_t sxy = sx * sy;
const size_t sxyz = sxy * sz;
// k = x + sx y + sx sy z + sx sy sz w
size_t w = k / sxyz;
size_t z = (k - w * sxyz) / sxy;
size_t y = (k - (w * sxyz) - (z * sxy)) / sx;
size_t x = k - (w * sxyz) - (z * sxy) - y * sx;
return w + sw * (z + sz * (y + sy * x));
}
template <typename T>
void rect_ipt(
T* arr,
const size_t sx, const size_t sy, const size_t sz, const size_t sw
) {
const size_t N = sx * sy * sz * sw;
std::vector<bool> visited;
visited.resize(N);
visited[0] = true;
visited[N - 1] = true;
size_t k, next_k;
T tmp1 = 0, tmp2 = 0;
for (size_t i = 1; i < (N - 1); i++) {
if (visited[i]) {
continue;
}
k = i;
tmp1 = arr[k];
next_k = P_4d(k, sx, sy, sz, sw);
while (!visited[next_k]) {
tmp2 = arr[next_k];
arr[next_k] = tmp1;
tmp1 = tmp2;
visited[next_k] = true;
k = next_k;
next_k = P_4d(k, sx, sy, sz, sw);
}
}
}
template <typename T>
void ipt(T* arr, const size_t sx) {
return;
}
template <typename T>
void ipt(T* arr, const size_t sx, const size_t sy) {
if (sx * sy <= 1) {
return;
}
if (sx == sy) {
square_ipt(arr, sx, sy);
}
else {
rect_ipt(arr, sx, sy);
}
}
template <typename T>
void ipt(T* arr, const size_t sx, const size_t sy, const size_t sz) {
if (sx * sy * sz <= 1) {
return;
}
if (sx == sy && sy == sz) {
square_ipt(arr, sx, sy, sz);
}
else {
rect_ipt(arr, sx, sy, sz);
}
}
template <typename T>
void ipt(
T* arr,
const size_t sx, const size_t sy,
const size_t sz, const size_t sw
) {
if (sx * sy * sz * sw <= 1) {
return;
}
rect_ipt(arr, sx, sy, sz, sw);
}
};
namespace pyipt {
template <typename T>
void _ipt2d(T* arr, const size_t sx, const size_t sy) {
ipt::ipt(arr, sx, sy);
}
template <typename T>
void _ipt3d(T* arr, const size_t sx, const size_t sy, const size_t sz) {
ipt::ipt(arr, sx, sy, sz);
}
template <typename T>
void _ipt4d(
T* arr,
const size_t sx, const size_t sy,
const size_t sz, const size_t sw
) {
ipt::ipt(arr, sx, sy, sz, sw);
}
};
#endif