warp-lang 1.5.0__py3-none-macosx_10_13_universal2.whl → 1.6.0__py3-none-macosx_10_13_universal2.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of warp-lang might be problematic. Click here for more details.

Files changed (132) hide show
  1. warp/__init__.py +5 -0
  2. warp/autograd.py +414 -191
  3. warp/bin/libwarp-clang.dylib +0 -0
  4. warp/bin/libwarp.dylib +0 -0
  5. warp/build.py +40 -12
  6. warp/build_dll.py +13 -6
  7. warp/builtins.py +1124 -497
  8. warp/codegen.py +261 -136
  9. warp/config.py +1 -1
  10. warp/context.py +357 -119
  11. warp/examples/assets/square_cloth.usd +0 -0
  12. warp/examples/benchmarks/benchmark_gemm.py +27 -18
  13. warp/examples/benchmarks/benchmark_interop_paddle.py +3 -3
  14. warp/examples/benchmarks/benchmark_interop_torch.py +3 -3
  15. warp/examples/core/example_torch.py +18 -34
  16. warp/examples/fem/example_apic_fluid.py +1 -0
  17. warp/examples/fem/example_mixed_elasticity.py +1 -1
  18. warp/examples/optim/example_bounce.py +1 -1
  19. warp/examples/optim/example_cloth_throw.py +1 -1
  20. warp/examples/optim/example_diffray.py +4 -15
  21. warp/examples/optim/example_drone.py +1 -1
  22. warp/examples/optim/example_softbody_properties.py +392 -0
  23. warp/examples/optim/example_trajectory.py +1 -3
  24. warp/examples/optim/example_walker.py +5 -0
  25. warp/examples/sim/example_cartpole.py +0 -2
  26. warp/examples/sim/example_cloth.py +3 -1
  27. warp/examples/sim/example_cloth_self_contact.py +260 -0
  28. warp/examples/sim/example_granular_collision_sdf.py +4 -5
  29. warp/examples/sim/example_jacobian_ik.py +0 -2
  30. warp/examples/sim/example_quadruped.py +5 -2
  31. warp/examples/tile/example_tile_cholesky.py +79 -0
  32. warp/examples/tile/example_tile_convolution.py +2 -2
  33. warp/examples/tile/example_tile_fft.py +2 -2
  34. warp/examples/tile/example_tile_filtering.py +3 -3
  35. warp/examples/tile/example_tile_matmul.py +4 -4
  36. warp/examples/tile/example_tile_mlp.py +12 -12
  37. warp/examples/tile/example_tile_nbody.py +180 -0
  38. warp/examples/tile/example_tile_walker.py +319 -0
  39. warp/fem/geometry/geometry.py +0 -2
  40. warp/math.py +147 -0
  41. warp/native/array.h +12 -0
  42. warp/native/builtin.h +0 -1
  43. warp/native/bvh.cpp +149 -70
  44. warp/native/bvh.cu +287 -68
  45. warp/native/bvh.h +195 -85
  46. warp/native/clang/clang.cpp +5 -1
  47. warp/native/coloring.cpp +5 -1
  48. warp/native/cuda_util.cpp +91 -53
  49. warp/native/cuda_util.h +5 -0
  50. warp/native/exports.h +40 -40
  51. warp/native/intersect.h +17 -0
  52. warp/native/mat.h +41 -0
  53. warp/native/mathdx.cpp +19 -0
  54. warp/native/mesh.cpp +25 -8
  55. warp/native/mesh.cu +153 -101
  56. warp/native/mesh.h +482 -403
  57. warp/native/quat.h +40 -0
  58. warp/native/solid_angle.h +7 -0
  59. warp/native/sort.cpp +85 -0
  60. warp/native/sort.cu +34 -0
  61. warp/native/sort.h +3 -1
  62. warp/native/spatial.h +11 -0
  63. warp/native/tile.h +1187 -669
  64. warp/native/tile_reduce.h +8 -6
  65. warp/native/vec.h +41 -0
  66. warp/native/warp.cpp +8 -1
  67. warp/native/warp.cu +263 -40
  68. warp/native/warp.h +19 -5
  69. warp/optim/linear.py +22 -4
  70. warp/render/render_opengl.py +130 -64
  71. warp/sim/__init__.py +6 -1
  72. warp/sim/collide.py +270 -26
  73. warp/sim/import_urdf.py +8 -8
  74. warp/sim/integrator_euler.py +25 -7
  75. warp/sim/integrator_featherstone.py +154 -35
  76. warp/sim/integrator_vbd.py +842 -40
  77. warp/sim/model.py +134 -72
  78. warp/sparse.py +1 -1
  79. warp/stubs.py +265 -132
  80. warp/tape.py +28 -30
  81. warp/tests/aux_test_module_unload.py +15 -0
  82. warp/tests/{test_sim_grad.py → flaky_test_sim_grad.py} +104 -63
  83. warp/tests/test_array.py +74 -0
  84. warp/tests/test_assert.py +242 -0
  85. warp/tests/test_codegen.py +14 -61
  86. warp/tests/test_collision.py +2 -2
  87. warp/tests/test_coloring.py +12 -2
  88. warp/tests/test_examples.py +12 -1
  89. warp/tests/test_func.py +21 -4
  90. warp/tests/test_grad_debug.py +87 -2
  91. warp/tests/test_hash_grid.py +1 -1
  92. warp/tests/test_ipc.py +116 -0
  93. warp/tests/test_lerp.py +13 -87
  94. warp/tests/test_mat.py +138 -167
  95. warp/tests/test_math.py +47 -1
  96. warp/tests/test_matmul.py +17 -16
  97. warp/tests/test_matmul_lite.py +10 -15
  98. warp/tests/test_mesh.py +84 -60
  99. warp/tests/test_mesh_query_aabb.py +165 -0
  100. warp/tests/test_mesh_query_point.py +328 -286
  101. warp/tests/test_mesh_query_ray.py +134 -121
  102. warp/tests/test_mlp.py +2 -2
  103. warp/tests/test_operators.py +43 -0
  104. warp/tests/test_overwrite.py +47 -2
  105. warp/tests/test_quat.py +77 -0
  106. warp/tests/test_reload.py +29 -0
  107. warp/tests/test_sim_grad_bounce_linear.py +204 -0
  108. warp/tests/test_smoothstep.py +17 -83
  109. warp/tests/test_static.py +19 -3
  110. warp/tests/test_tape.py +25 -0
  111. warp/tests/test_tile.py +178 -191
  112. warp/tests/test_tile_load.py +356 -0
  113. warp/tests/test_tile_mathdx.py +61 -8
  114. warp/tests/test_tile_mlp.py +17 -17
  115. warp/tests/test_tile_reduce.py +24 -18
  116. warp/tests/test_tile_shared_memory.py +66 -17
  117. warp/tests/test_tile_view.py +165 -0
  118. warp/tests/test_torch.py +35 -0
  119. warp/tests/test_utils.py +36 -24
  120. warp/tests/test_vec.py +110 -0
  121. warp/tests/unittest_suites.py +29 -4
  122. warp/tests/unittest_utils.py +30 -13
  123. warp/thirdparty/unittest_parallel.py +2 -2
  124. warp/types.py +411 -101
  125. warp/utils.py +10 -7
  126. {warp_lang-1.5.0.dist-info → warp_lang-1.6.0.dist-info}/METADATA +92 -69
  127. {warp_lang-1.5.0.dist-info → warp_lang-1.6.0.dist-info}/RECORD +130 -119
  128. {warp_lang-1.5.0.dist-info → warp_lang-1.6.0.dist-info}/WHEEL +1 -1
  129. warp/examples/benchmarks/benchmark_tile.py +0 -179
  130. warp/native/tile_gemm.h +0 -341
  131. {warp_lang-1.5.0.dist-info → warp_lang-1.6.0.dist-info}/LICENSE.md +0 -0
  132. {warp_lang-1.5.0.dist-info → warp_lang-1.6.0.dist-info}/top_level.txt +0 -0
warp/native/tile.h CHANGED
@@ -35,10 +35,6 @@
35
35
  #endif
36
36
 
37
37
  #define WP_USE_ASYNC_PIPELINE 0
38
- #if WP_USE_ASYNC_PIPELINE
39
- #include "cuda_pipeline_primitives.h"
40
- #endif // WP_USE_ASYNC_PIPELINE
41
-
42
38
  #define WP_USE_REGISTER_GEMM 0
43
39
 
44
40
  /* Tile Expressions
@@ -171,50 +167,300 @@ struct is_same<T, T> {
171
167
  };
172
168
 
173
169
 
174
- template <typename Tile>
175
- constexpr int tile_size(Tile& t) { return Tile::M*Tile::N; }
170
+ template <int N>
171
+ struct tile_coord_t
172
+ {
173
+ int indices[N];
174
+
175
+ CUDA_CALLABLE inline int operator[](int i) const { assert(0 <= 1 && i < N); return indices[i]; }
176
+ CUDA_CALLABLE inline int& operator[](int i) { assert(0 <= 1 && i < N); return indices[i]; }
177
+
178
+ CUDA_CALLABLE inline tile_coord_t<N> operator + (const tile_coord_t<N>& c) const
179
+ {
180
+ tile_coord_t<N> out;
181
+ for (int i=0; i < N; ++i)
182
+ {
183
+ out.indices[i] = indices[i] + c.indices[i];
184
+ }
185
+ return out;
186
+ }
187
+ };
188
+
189
+ // This function deduces N = sizeof...(Ints)
190
+ template <typename... Ints>
191
+ constexpr tile_coord_t<sizeof...(Ints)> tile_coord(Ints... idxs)
192
+ {
193
+ constexpr int N = sizeof...(Ints);
194
+
195
+ // Create the result
196
+ tile_coord_t<N> result{};
197
+
198
+ // Capture all arguments in a local array
199
+ int arr[] = { static_cast<int>(idxs)... };
200
+
201
+ // C++14 or later: 'for' is allowed in a constexpr context
202
+ for (int i = 0; i < N; ++i)
203
+ {
204
+ result.indices[i] = arr[i];
205
+ }
176
206
 
177
- constexpr int tile_regcount(int m, int n) {
178
- return (m*n + WP_TILE_BLOCK_DIM - 1) / WP_TILE_BLOCK_DIM;
207
+ return result;
208
+ }
209
+
210
+ // helpers to construct a coord from a set of indices
211
+ auto tile_coord(int i)
212
+ {
213
+ auto c = tile_coord_t<1>();
214
+ c.indices[0] = i;
215
+ return c;
216
+ }
217
+
218
+ auto tile_coord(int i, int j)
219
+ {
220
+ auto c = tile_coord_t<2>();
221
+ c.indices[0] = i;
222
+ c.indices[1] = j;
223
+ return c;
179
224
  }
180
225
 
181
- struct coord_t
226
+ auto tile_coord(int i, int j, int k)
227
+ {
228
+ auto c = tile_coord_t<3>();
229
+ c.indices[0] = i;
230
+ c.indices[1] = j;
231
+ c.indices[2] = k;
232
+ return c;
233
+ }
234
+
235
+ auto tile_coord(int i, int j, int k, int l)
182
236
  {
183
- int i;
184
- int j;
237
+ auto c = tile_coord_t<4>();
238
+ c.indices[0] = i;
239
+ c.indices[1] = j;
240
+ c.indices[2] = k;
241
+ c.indices[3] = l;
242
+ return c;
243
+ }
244
+
245
+ // represents a compile time int tuple for strides/shapes/coords
246
+ template <int... V>
247
+ struct tile_tuple_t
248
+ {
249
+ static constexpr int N = sizeof...(V);
250
+ static_assert(N > 0);
251
+
252
+ static constexpr int data[N] = { V... };
253
+
254
+ static constexpr int dim(int i) { assert(i < N); return data[i]; }
255
+ static constexpr int size()
256
+ {
257
+ int res = data[0];
258
+ for (int i=1; i < N; ++i)
259
+ res *= data[i];
260
+
261
+ return res;
262
+ }
185
263
  };
186
264
 
265
+ // simple helper to compute strides from a shape up to 4d
266
+ template <typename Shape>
267
+ struct compute_strides;
268
+
269
+ // 1D
270
+ template <int D0>
271
+ struct compute_strides< tile_tuple_t<D0> > { using Stride = tile_tuple_t<1>; };
272
+ // 2D
273
+ template <int D0, int D1>
274
+ struct compute_strides< tile_tuple_t<D0, D1> > { using Stride = tile_tuple_t<D1, 1>; };
275
+ // 3D
276
+ template <int D0, int D1, int D2>
277
+ struct compute_strides< tile_tuple_t<D0, D1, D2> > { using Stride = tile_tuple_t<(D1 * D2), D2, 1>; };
278
+ // 4D
279
+ template <int D0, int D1, int D2, int D3>
280
+ struct compute_strides< tile_tuple_t<D0, D1, D2, D3> > { using Stride = tile_tuple_t<(D1 * D2 * D3), (D2 * D3), D3, 1>; };
281
+
282
+
283
+ // alias of tuple to represent shapes
284
+ template <int... V>
285
+ using tile_shape_t = tile_tuple_t<V...>;
286
+
287
+ // alias of tuple to represent stride
288
+ template <int... V>
289
+ using tile_stride_t = tile_tuple_t<V...>;
290
+
187
291
 
188
292
  // represents a tile stored in global memory with dynamic strides
189
- // only used to represent the source for tile loads to register/shared
190
- template <typename T>
191
- struct tile_global_t
293
+ // used to represent the source and offset for tile loads to register/shared
294
+ template <typename T, typename Shape_>
295
+ struct tile_global_t
192
296
  {
193
297
  using Type = T;
298
+ using Shape = Shape_;
299
+ using Coord = tile_coord_t<Shape::N>;
194
300
 
195
301
  array_t<T> data;
196
- int x;
197
- int y;
302
+ Coord offset;
303
+
304
+ tile_global_t(array_t<T>& a, const Coord& c) : data(a), offset(c)
305
+ {
306
+ }
307
+
308
+ inline CUDA_CALLABLE int index_from_coord(const Coord& coord) const
309
+ {
310
+ // element index
311
+ int index = 0;
312
+
313
+ WP_PRAGMA_UNROLL
314
+ for (int i=0; i < Shape::N; ++i)
315
+ {
316
+ // global = offset + coord
317
+ int c = offset[i] + coord[i];
318
+ index += data.strides[i]*c;
319
+ }
198
320
 
199
- tile_global_t(array_t<T>& a, int x, int y) : data(a), x(x), y(y)
321
+ return index/sizeof(T);
322
+ }
323
+
324
+ inline CUDA_CALLABLE bool index(const Coord& coord, int& out) const
200
325
  {
326
+ // element index
327
+ int index = 0;
328
+
329
+ WP_PRAGMA_UNROLL
330
+ for (int i=0; i < Shape::N; ++i)
331
+ {
332
+ // global = offset + coord
333
+ int c = offset[i] + coord[i];
334
+
335
+ // handle out of bounds case
336
+ if (c >= data.shape[i])
337
+ return false;
338
+ else
339
+ index += data.strides[i]*c;
340
+ }
341
+
342
+ // array strides are in bytes so we convert to elements
343
+ out = index / sizeof(T);
344
+ return true;
345
+ }
346
+
347
+ inline CUDA_CALLABLE T load(const Coord& coord) const
348
+ {
349
+ int i;
350
+ if (index(coord, i))
351
+ return data.data[i];
352
+ else
353
+ return T(0);
354
+ }
355
+
356
+ inline CUDA_CALLABLE T load_grad(const Coord& coord) const
357
+ {
358
+ int i;
359
+ if (index(coord, i))
360
+ return data.grad[i];
361
+ else
362
+ return T(0);
363
+ }
364
+
365
+ inline CUDA_CALLABLE void store(const Coord& coord, const T& x) const
366
+ {
367
+ int i;
368
+ if (index(coord, i))
369
+ data.data[i] = x;
370
+ }
371
+
372
+ inline CUDA_CALLABLE T atomic_add(const Coord& coord, const T& value) const
373
+ {
374
+ int i;
375
+ if (index(coord, i))
376
+ return wp::atomic_add(&data.data[i], value);
377
+ else
378
+ return T(0);
379
+ }
380
+
381
+ inline CUDA_CALLABLE T atomic_add_grad(const Coord& coord, const T& grad) const
382
+ {
383
+ int i;
384
+ if (index(coord, i))
385
+ return wp::atomic_add(&data.grad[i], grad);
386
+ else
387
+ return T(0);
201
388
  }
202
389
  };
203
390
 
391
+ template <typename Shape_>
392
+ struct tile_layout_register_t
393
+ {
394
+ using Shape = Shape_;
395
+ using Coord = tile_coord_t<Shape::N>;
396
+
397
+ static constexpr int Size = Shape::size();
398
+ static constexpr int NumRegs = (Size + WP_TILE_BLOCK_DIM - 1) / WP_TILE_BLOCK_DIM;
399
+ static constexpr bool Aligned = Size%WP_TILE_BLOCK_DIM == 0;
400
+
401
+ static inline CUDA_CALLABLE int linear_from_register(int reg)
402
+ {
403
+ return threadIdx.x + reg*WP_TILE_BLOCK_DIM;
404
+ }
405
+
406
+ static inline CUDA_CALLABLE int linear_from_coord(Coord c)
407
+ {
408
+ int linear = 0;
409
+ int stride = 1;
410
+
411
+ WP_PRAGMA_UNROLL
412
+ for (int i=Shape::N-1; i >= 0; --i)
413
+ {
414
+ linear += c[i] * stride;
415
+ stride *= Shape::dim(i);
416
+ }
417
+ return linear;
418
+ }
419
+
420
+ static inline CUDA_CALLABLE auto coord_from_linear(int linear)
421
+ {
422
+ Coord c;
423
+
424
+ WP_PRAGMA_UNROLL
425
+ for (int i=Shape::N-1; i >= 0; --i)
426
+ {
427
+ c[i] = linear%Shape::dim(i);
428
+ linear /= Shape::dim(i);
429
+ }
430
+
431
+ return c;
432
+ }
433
+
434
+ static inline CUDA_CALLABLE int thread_from_linear(int linear)
435
+ {
436
+ const int thread = linear%WP_TILE_BLOCK_DIM;
437
+ return thread;
438
+ }
439
+
440
+ static inline CUDA_CALLABLE int register_from_linear(int linear)
441
+ {
442
+ const int reg = linear/WP_TILE_BLOCK_DIM;
443
+ return reg;
444
+ }
445
+
446
+ static inline CUDA_CALLABLE bool valid(int linear)
447
+ {
448
+ if (Aligned || linear < Size)
449
+ return true;
450
+ else
451
+ return false;
452
+ }
453
+
454
+ };
455
+
204
456
  // represents a tile stored in registers across a block
205
- template <typename T, int M_, int N_>
457
+ template <typename T, typename L>
206
458
  struct tile_register_t
207
459
  {
208
460
  using Type = T;
209
- static constexpr int M = M_;
210
- static constexpr int N = N_;
211
- static constexpr int Size = M*N;
212
-
213
- static constexpr int NumRegs = tile_regcount(M, N);
461
+ using Layout = L;
214
462
 
215
- static constexpr bool Aligned = Size%WP_TILE_BLOCK_DIM == 0;
216
-
217
- T data[NumRegs];
463
+ T data[Layout::NumRegs];
218
464
 
219
465
  inline CUDA_CALLABLE tile_register_t(T value=T(0.0))
220
466
  {
@@ -224,52 +470,34 @@ struct tile_register_t
224
470
  // in backwards pass and letting default constructor
225
471
  // avoid initialization
226
472
 
227
- for (int i=0; i < NumRegs; ++i)
473
+ for (int i=0; i < Layout::NumRegs; ++i)
228
474
  data[i] = value;
229
475
  }
230
476
 
231
- inline CUDA_CALLABLE auto& operator=(const tile_global_t<T>& t)
477
+ inline CUDA_CALLABLE auto& operator=(const tile_global_t<T, typename Layout::Shape>& t)
232
478
  {
233
- if (t.data.ndim == 1)
234
- copy_from_global(t.data, t.x); // 1d load
235
- else
236
- copy_from_global(t.data, t.x, t.y); // 2d load
237
-
479
+ copy_from_global(t);
238
480
  return *this;
239
-
240
481
  }
241
482
 
242
483
  // define the += operator which is used during backward pass codegen
243
484
  // when returning a register tile from a user defined function
244
- inline CUDA_CALLABLE auto& operator += (tile_register_t<T, M, N>& rhs)
485
+ inline CUDA_CALLABLE auto& operator += (tile_register_t<T, Layout>& rhs)
245
486
  {
246
- this->grad_add(rhs);
487
+ grad_add(rhs);
247
488
  return *this;
248
489
  }
249
490
 
250
- inline CUDA_CALLABLE T& operator()(int index)
491
+ inline CUDA_CALLABLE T& operator()(int reg)
251
492
  {
252
- assert(index < NumRegs);
253
- return data[index];
493
+ assert(reg < Layout::NumRegs);
494
+ return data[reg];
254
495
  }
255
496
 
256
- inline CUDA_CALLABLE const T& operator()(int index) const
497
+ inline CUDA_CALLABLE const T& operator()(int reg) const
257
498
  {
258
- assert(index < NumRegs);
259
- return data[index];
260
- }
261
-
262
-
263
- // compute linear tile index from a local register index
264
- inline CUDA_CALLABLE int index(int reg) const
265
- {
266
- return threadIdx.x + reg*WP_TILE_BLOCK_DIM;
267
- }
268
-
269
- // compute tile coordinate from linear index
270
- inline CUDA_CALLABLE coord_t coord(int index) const
271
- {
272
- return {index/N, index%N};
499
+ assert(reg < Layout::NumRegs);
500
+ return data[reg];
273
501
  }
274
502
 
275
503
  // Returns the number of valid registers for this tile
@@ -278,29 +506,29 @@ struct tile_register_t
278
506
  // some of the trailing registers may lie outside the valid range
279
507
  inline CUDA_CALLABLE int valid() const
280
508
  {
281
- return (Size - threadIdx.x)/WP_TILE_BLOCK_DIM;
509
+ return (int)floor(float(Size - threadIdx.x - 1)/WP_TILE_BLOCK_DIM) + 1;
282
510
  }
283
511
 
284
- inline CUDA_CALLABLE void assign(const tile_register_t<T, M, N>& tile)
512
+ inline CUDA_CALLABLE void assign(const tile_register_t<T, Layout>& tile)
285
513
  {
286
- for (int i=0; i < NumRegs; ++i)
514
+ for (int i=0; i < Layout::NumRegs; ++i)
287
515
  data[i] = tile.data[i];
288
516
  }
289
517
 
290
518
  inline CUDA_CALLABLE void zero()
291
519
  {
292
- for (int i=0; i < NumRegs; ++i)
293
- data[i] = T(0);
520
+ for (int i=0; i < Layout::NumRegs; ++i)
521
+ data[i] = T(0);
294
522
  }
295
523
 
296
524
  // extract a single tile element to a native type
297
- inline CUDA_CALLABLE Type extract(int i, int j)
525
+ template <typename Coord>
526
+ inline CUDA_CALLABLE Type extract(const Coord& c)
298
527
  {
299
528
  // map from logical coords (i, j) -> (thread, reg)
300
- const int linear = i*N + j;
301
-
302
- const int thread = linear/NumRegs;
303
- const int reg = linear%NumRegs;
529
+ const int linear = Layout::linear_from_coord(c);
530
+ const int thread = Layout::thread_from_linear(linear);
531
+ const int reg = Layout::register_from_linear(linear);
304
532
 
305
533
  WP_TILE_SHARED Type scratch;
306
534
 
@@ -320,13 +548,13 @@ struct tile_register_t
320
548
 
321
549
 
322
550
  // backward version of scalar extract
323
- inline CUDA_CALLABLE void adj_extract(int i, int j, Type adj_ret)
551
+ template <typename Coord>
552
+ inline CUDA_CALLABLE void adj_extract(const Coord& c, Type adj_ret)
324
553
  {
325
554
  // map from logical coords (i, j) -> (thread, reg)
326
- const int linear = i*N + j;
327
-
328
- const int thread = linear/NumRegs;
329
- const int reg = linear%NumRegs;
555
+ const int linear = Layout::linear_from_coord(c);
556
+ const int thread = Layout::thread_from_linear(linear);
557
+ const int reg = Layout::register_from_linear(linear);
330
558
 
331
559
  if (threadIdx.x == thread)
332
560
  {
@@ -348,6 +576,24 @@ struct tile_register_t
348
576
  return *this;
349
577
  }
350
578
 
579
+ // apply a lambda to all valid entries in the tile
580
+ // Op should be a functor that takes a register index and tile_coord_t as input
581
+ template <typename Op>
582
+ void apply(Op op)
583
+ {
584
+ WP_PRAGMA_UNROLL
585
+ for (int i=0; i < Layout::NumRegs; ++i)
586
+ {
587
+ int linear = Layout::linear_from_register(i);
588
+ if (!Layout::valid(linear))
589
+ break;
590
+
591
+ auto c = Layout::coord_from_linear(linear);
592
+ op(i, c);
593
+ }
594
+ }
595
+
596
+
351
597
  // in-place gradient zero
352
598
  inline CUDA_CALLABLE void grad_zero()
353
599
  {
@@ -355,118 +601,77 @@ struct tile_register_t
355
601
  }
356
602
 
357
603
  // accumulate gradients onto this tile
358
- inline CUDA_CALLABLE void grad_add(const tile_register_t<T, M, N>& tile)
604
+ inline CUDA_CALLABLE void grad_add(const tile_register_t<T, Layout>& tile)
359
605
  {
360
- for (int i=0; i < NumRegs; ++i)
606
+ for (int i=0; i < Layout::NumRegs; ++i)
361
607
  data[i] += tile.data[i];
362
608
  }
363
609
 
364
- // copy shared tile to register
610
+ CUDA_CALLABLE void grad_add(const tile_global_t<T, typename Layout::Shape>& global)
611
+ {
612
+ apply([&](int reg, auto c) {data[reg] = global.load_grad(c);});
613
+
614
+ }
615
+
365
616
  inline CUDA_CALLABLE auto& grad_to_register()
366
617
  {
618
+ // nop for register tiles
367
619
  return *this;
368
620
  }
369
621
 
370
- void copy_to_global(array_t<T> dest, int x)
622
+ template <typename Global>
623
+ inline CUDA_CALLABLE void copy_to_global(const Global& dest)
371
624
  {
372
- assert(dest.ndim == 1);
373
-
374
- const int tile_i = x*N;
375
-
376
- WP_PRAGMA_UNROLL
377
- for (int i=0; i < NumRegs; ++i)
378
- {
379
- // handle case where tile size is not
380
- // aligned to block dimensions
381
- int linear = index(i);
382
- if (!Aligned && linear >= Size)
383
- break;
384
-
385
- wp::index(dest, tile_i + linear) = data[i];
386
- }
625
+ apply([&](int reg, auto c) { dest.store(c, data[reg]); });
387
626
  }
388
627
 
389
- void copy_to_global(array_t<T> dest, int x, int y)
628
+ template <typename Global>
629
+ inline CUDA_CALLABLE void copy_from_global(const Global& src)
390
630
  {
391
- assert(dest.ndim == 2);
392
-
393
- const int tile_i = x*M;
394
- const int tile_j = y*N;
395
-
396
- // wp.array() indexing generates poor code due to char* casting
397
- // here we unroll some of the ops, note this assumes byte strides are
398
- // aligned to the element size
399
- T* ptr = &wp::index(dest, tile_i, tile_j);
400
- const int stride_i = dest.strides[0]/sizeof(T);
401
- const int stride_j = dest.strides[1]/sizeof(T);
402
-
403
- WP_PRAGMA_UNROLL
404
- for (int i=0; i < NumRegs; ++i)
405
- {
406
- // handle case where tile size is not
407
- // aligned to block dimensions
408
- int linear = index(i);
409
- if (!Aligned && linear >= Size)
410
- break;
411
-
412
- coord_t c = coord(linear);
413
- ptr[c.i*stride_i + c.j*stride_j] = data[i];
414
- }
631
+ apply([&](int reg, auto c) { data[reg] = src.load(c); });
415
632
  }
416
633
 
417
- inline CUDA_CALLABLE void copy_from_global(const array_t<T>& src, int x)
634
+ // add a register tile to a global array
635
+ template <typename Global>
636
+ inline CUDA_CALLABLE auto atomic_add(const Global& dest)
418
637
  {
419
- // todo: use async pipelines or TMA here
420
- const int tile_i = x*N;
638
+ // allocate a tile to hold previous dest value
639
+ auto previous = *this;
421
640
 
422
- WP_PRAGMA_UNROLL
423
- for (int i=0; i < NumRegs; ++i)
424
- {
425
- int linear = index(i);
426
- if (!Aligned && linear >= Size)
427
- break;
428
-
429
- data[i] = wp::index(src, tile_i + linear);
430
- }
641
+ apply([&](int reg, auto c) { previous.data[reg] = dest.atomic_add(c, data[reg]); });
642
+ return previous;
431
643
  }
432
644
 
433
- inline CUDA_CALLABLE void copy_from_global(const array_t<T>& src, int x, int y)
645
+ // add a register tile to the gradient of a global array
646
+ template <typename Global>
647
+ inline CUDA_CALLABLE auto atomic_add_grad(const Global& dest)
434
648
  {
435
- // todo: use async pipelines or TMA here
436
- const int tile_i = x*M;
437
- const int tile_j = y*N;
438
-
439
- // wp.array() indexing generates poor code due to char* casting
440
- // here we unroll some of the ops, note this assumes array byte strides are
441
- // aligned to the element size
442
- const T* ptr = &wp::index(src, tile_i, tile_j);
443
-
444
- assert(src.strides[0]%sizeof(T) == 0);
445
- assert(src.strides[1]%sizeof(T) == 0);
446
-
447
- const int stride_i = src.strides[0]/sizeof(T);
448
- const int stride_j = src.strides[1]/sizeof(T);
449
-
450
- WP_PRAGMA_UNROLL
451
- for (int i=0; i < NumRegs; ++i)
452
- {
453
- int linear = index(i);
454
- if (!Aligned && linear >= Size)
455
- break;
649
+ // allocate a tile to hold previous dest value
650
+ auto previous = *this;
456
651
 
457
- coord_t c = coord(linear);
458
- data[i] = ptr[c.i*stride_i + c.j*stride_j];
459
- }
460
- }
652
+ apply([&](int reg, auto c) { previous.data[reg] = dest.atomic_add_grad(c, data[reg]); });
653
+ return previous;
654
+ }
461
655
  };
462
656
 
657
+
463
658
  // helper to allocate a register tile like another tile
659
+ // users can either specify a template explicitly or
660
+ // pass in another concrete instance
464
661
  template<typename Tile>
465
- auto tile_register_like()
662
+ auto tile_register_like(Tile* t=NULL)
466
663
  {
467
664
  using T = typename Tile::Type;
665
+ using L = typename Tile::Layout;
468
666
 
469
- return tile_register_t<T, Tile::M, Tile::N>(T(0.0));
667
+ return tile_register_t<T, tile_layout_register_t<typename L::Shape>>(T(0.0));
668
+ }
669
+
670
+ // helper to construct a register tile from a type and a list of dims
671
+ template <typename T, int... Dims>
672
+ auto tile_register()
673
+ {
674
+ return tile_register_t<T, tile_layout_register_t<tile_shape_t<Dims...>>>();
470
675
  }
471
676
 
472
677
  inline CUDA_CALLABLE int tile_align(int num_bytes)
@@ -474,7 +679,10 @@ inline CUDA_CALLABLE int tile_align(int num_bytes)
474
679
  // note this much match value in Python types.py
475
680
  const int alignment = 16;
476
681
 
477
- return ((num_bytes + alignment - 1) / alignment) * alignment;
682
+ const int num_bytes_abs = num_bytes < 0 ? - num_bytes : num_bytes;
683
+ const int sign = num_bytes < 0 ? - 1 : 1;
684
+
685
+ return sign * ((num_bytes_abs + alignment - 1) / alignment) * alignment;
478
686
  }
479
687
 
480
688
  inline CUDA_CALLABLE void* tile_alloc_shared(int num_bytes, bool init=false)
@@ -502,20 +710,78 @@ inline CUDA_CALLABLE void* tile_alloc_shared(int num_bytes, bool init=false)
502
710
  }
503
711
 
504
712
 
505
-
506
- template <typename T, int M_, int N_, int StrideM_=N_, int StrideN_=1, bool Owner_=true>
507
- struct tile_shared_t
713
+ template <typename Shape_, typename Stride_= typename compute_strides<Shape_>::Stride>
714
+ struct tile_layout_strided_t
508
715
  {
509
- using Type = T;
510
- static constexpr int M = M_;
511
- static constexpr int N = N_;
512
- static constexpr int Size = M*N;
716
+ using Shape = Shape_;
717
+ using Stride = Stride_;
718
+ using Coord = tile_coord_t<Shape::N>;
513
719
 
514
- static constexpr int StrideM = StrideM_;
515
- static constexpr int StrideN = StrideN_;
516
-
720
+ static constexpr int Size = Shape::size();
517
721
  static constexpr bool Aligned = Size%WP_TILE_BLOCK_DIM == 0;
518
- static constexpr bool Unique = (StrideM >= N) && (StrideN >= 1);
722
+
723
+ static inline CUDA_CALLABLE auto coord_from_linear(int linear)
724
+ {
725
+ assert(linear < Size);
726
+
727
+ Coord c;
728
+
729
+ WP_PRAGMA_UNROLL
730
+ for (int d=Shape::N-1; d >= 0; --d)
731
+ {
732
+ c[d] = linear%Shape::dim(d);
733
+ linear /= Shape::dim(d);
734
+ }
735
+
736
+ return c;
737
+ }
738
+
739
+ static inline CUDA_CALLABLE int index_from_coord(Coord c)
740
+ {
741
+ int index = 0;
742
+
743
+ WP_PRAGMA_UNROLL
744
+ for (int d=0; d < Shape::N; ++d)
745
+ {
746
+ assert(c[d] < Shape::dim(d));
747
+
748
+ index += c[d]*Stride::dim(d);
749
+ }
750
+
751
+ return index;
752
+ }
753
+
754
+ // checks whether a strided layout is unique, i.e.: if memory locations are only
755
+ // every referred to by one element in the tile, this is a basic test that only
756
+ // checks for broadcast dimensions, it would be possible to do the full check
757
+ // using sorted shape/strides in Python and add it as a template parameter to the type
758
+ static constexpr bool is_unique()
759
+ {
760
+ constexpr int N = Shape::N;
761
+
762
+ // check for any broadcast dimensions
763
+ for (int i=0; i < N; ++i)
764
+ if (Stride::dim(i) == 0)
765
+ return false;
766
+
767
+ return true;
768
+ }
769
+
770
+ static constexpr bool Unique = is_unique();
771
+
772
+ static inline CUDA_CALLABLE bool valid(int linear)
773
+ {
774
+ return linear < Size;
775
+ }
776
+
777
+ };
778
+
779
+
780
+ template <typename T, typename L, bool Owner_=true>
781
+ struct tile_shared_t
782
+ {
783
+ using Type = T;
784
+ using Layout = L;
519
785
  static constexpr bool Owner = Owner_;
520
786
 
521
787
  struct Storage
@@ -524,55 +790,60 @@ struct tile_shared_t
524
790
 
525
791
  Storage(T* p) : ptr(p) {}
526
792
 
527
- inline CUDA_CALLABLE T& operator()(int i, int j)
793
+ inline CUDA_CALLABLE T& operator()(typename Layout::Coord c)
528
794
  {
529
- assert(i < M);
530
- assert(j < N);
795
+ assert(ptr);
531
796
 
532
- return ptr[i*StrideM + j*StrideN];
797
+ int index = Layout::index_from_coord(c);
798
+ return ptr[index];
533
799
  }
534
800
 
535
- inline CUDA_CALLABLE const T& operator()(int i, int j) const
536
- {
537
- assert(i < M);
538
- assert(j < N);
801
+ inline CUDA_CALLABLE const T& operator()(typename Layout::Coord c) const
802
+ {
803
+ assert(ptr);
539
804
 
540
- return ptr[i*StrideM + j*StrideN];
805
+ int index = Layout::index_from_coord(c);
806
+ return ptr[index];
541
807
  }
542
808
 
543
- inline CUDA_CALLABLE T& operator()(int index)
809
+ inline CUDA_CALLABLE T& operator()(int linear)
544
810
  {
545
- assert(index < M*N);
811
+ assert(ptr);
812
+ assert(Layout::valid(linear));
546
813
 
547
- // unravel
548
- int i = index/N;
549
- int j = index%N;
550
-
551
- return (*this)(i,j);
814
+ auto c = Layout::coord_from_linear(linear);
815
+ return (*this)(c);
552
816
  }
553
817
 
554
- inline CUDA_CALLABLE const T& operator()(int index) const
818
+ inline CUDA_CALLABLE const T& operator()(int linear) const
555
819
  {
556
- assert(index < M*N);
557
-
558
- // unravel
559
- int i = index/N;
560
- int j = index%N;
820
+ assert(ptr);
821
+ assert(Layout::valid(linear));
561
822
 
562
- return (*this)(i,j);
823
+ auto c = Layout::coord_from_linear(linear);
824
+ return (*this)(c);
563
825
  }
564
826
  };
565
827
 
566
828
  Storage data;
567
829
  Storage grad;
568
830
 
831
+ // we need to track whether or not this tile's data has been initialized.
832
+ // once true, any re-initialization of data that follows needs a WP_TILE_SYNC()
833
+ // call to precede it, to allow threads that are still reading from this tile
834
+ // to complete their work. e.g, in a dynamic loop:
835
+ // for i in range(x):
836
+ // tile = wp.tile_load(arr, i, TILE_SIZE, storage="shared")
837
+ // # read from tile...
838
+ bool initialized;
839
+
569
840
  // default initialization (non-initialized)
570
- inline CUDA_CALLABLE tile_shared_t() : data(NULL), grad(NULL)
841
+ inline CUDA_CALLABLE tile_shared_t() : data(NULL), grad(NULL), initialized(false)
571
842
  {
572
843
  }
573
844
 
574
845
  // initialize from an existing tile's memory
575
- inline CUDA_CALLABLE tile_shared_t(T* data, T* grad=NULL) : data(data), grad(grad)
846
+ inline CUDA_CALLABLE tile_shared_t(T* data, T* grad=NULL, bool initialized=true) : data(data), grad(grad), initialized(initialized)
576
847
  {
577
848
  }
578
849
 
@@ -582,10 +853,10 @@ struct tile_shared_t
582
853
  {
583
854
  // update our per-thread shared memory allocator
584
855
  if (data.ptr)
585
- tile_alloc_shared(-M*N*int(sizeof(T)));
856
+ tile_alloc_shared(-Layout::Size*int(sizeof(T)));
586
857
 
587
858
  if (grad.ptr)
588
- tile_alloc_shared(-M*N*int(sizeof(T)));
859
+ tile_alloc_shared(-Layout::Size*int(sizeof(T)));
589
860
  }
590
861
  }
591
862
 
@@ -597,12 +868,13 @@ struct tile_shared_t
597
868
  return *this;
598
869
  }
599
870
 
871
+
600
872
  // construct from another shared tile, this constructor
601
873
  // is invoked for reshape operations like `wp.tile_transpose()`
602
- template <typename OtherT, int OtherM, int OtherN, int OtherStrideM, int OtherStrideN>
603
- inline CUDA_CALLABLE auto& operator=(const tile_shared_t<OtherT, OtherM, OtherN, OtherStrideM, OtherStrideN>& rhs)
874
+ template <typename OtherT, typename OtherLayout>
875
+ inline CUDA_CALLABLE auto& operator=(const tile_shared_t<OtherT, OtherLayout>& rhs)
604
876
  {
605
- using OtherTile = tile_shared_t<OtherT, OtherM, OtherN, OtherStrideM, OtherStrideN>;
877
+ using OtherTile = tile_shared_t<OtherT, OtherLayout>;
606
878
 
607
879
  // check dimensions are compatible
608
880
  static_assert(Size == OtherTile::Size);
@@ -610,89 +882,89 @@ struct tile_shared_t
610
882
  // alias tile directly
611
883
  data = rhs.data;
612
884
  grad = rhs.grad;
885
+ initialized = rhs.initialized;
613
886
 
614
887
  return *this;
615
888
  }
616
889
 
617
890
  // assign from a global tile (load)
618
- inline CUDA_CALLABLE auto& operator=(const tile_global_t<T>& t)
891
+ inline CUDA_CALLABLE auto& operator=(const tile_global_t<T, typename Layout::Shape>& t)
619
892
  {
620
- if (t.data.ndim == 1)
621
- copy_from_global(t.data, t.x); // 1d load
622
- else
623
- copy_from_global(t.data, t.x, t.y); // 2d load
624
-
625
- // synchronization happens in copy functions above
626
-
893
+ copy_from_global(t);
627
894
  return *this;
628
895
  }
629
896
 
630
897
  // assign from a constant value
631
898
  inline CUDA_CALLABLE auto& operator=(const T& x)
632
899
  {
633
- for (int i=threadIdx.x; i < M*N; i+= WP_TILE_BLOCK_DIM)
900
+ // sync if we are re-initializing data so that any threads that are still
901
+ // reading from this tile can complete their work, e.g.: if re-assigning
902
+ // to a tile during a dynamic loop
903
+ if (initialized)
904
+ WP_TILE_SYNC();
905
+
906
+ for (int i=threadIdx.x; i < Layout::Size; i+= WP_TILE_BLOCK_DIM)
634
907
  data(i) = x;
635
908
 
909
+ initialized = true;
636
910
  WP_TILE_SYNC();
637
911
  return *this;
638
912
  }
639
913
 
640
-
641
- // compute tile coordinate from linear index
642
- inline CUDA_CALLABLE coord_t coord(int index) const
643
- {
644
- return {index/N, index%N};
645
- }
646
-
647
914
  // in-place zero
648
915
  inline CUDA_CALLABLE void zero()
649
916
  {
650
- for (int i=threadIdx.x; i < M*N; i+= WP_TILE_BLOCK_DIM)
917
+ for (int i=threadIdx.x; i < Layout::Size; i+= WP_TILE_BLOCK_DIM)
651
918
  data(i) = T(0);
652
919
 
653
920
  WP_TILE_SYNC();
654
921
  }
655
922
 
656
923
  // extract a single tile element to a native type
657
- inline CUDA_CALLABLE Type extract(int i, int j)
924
+ inline CUDA_CALLABLE Type extract(const typename Layout::Coord& c)
658
925
  {
659
- return data(i, j);
926
+ return data(c);
660
927
  }
661
928
 
662
929
  // backward of scalar extraction
663
- inline CUDA_CALLABLE void adj_extract(int i, int j, Type adj_ret)
930
+ inline CUDA_CALLABLE void adj_extract(const typename Layout::Coord& c, Type adj_ret)
664
931
  {
665
- if (threadIdx.x == 0)
666
- data(i, j) += adj_ret;
667
-
668
- WP_TILE_SYNC();
932
+ // since multiple threads may extract the same element
933
+ // we need to accumulate using atomic operations
934
+ wp::atomic_add(&grad(c), adj_ret);
935
+
936
+ WP_TILE_SYNC();
669
937
  }
670
938
 
671
939
 
672
940
  // copy register tile to shared
673
- inline CUDA_CALLABLE void assign(const tile_register_t<T, M, N>& tile)
941
+ template <typename Tile>
942
+ inline CUDA_CALLABLE void assign(const Tile& tile)
674
943
  {
944
+ if (initialized)
945
+ WP_TILE_SYNC();
946
+
675
947
  WP_PRAGMA_UNROLL
676
- for (int i=0; i < tile.NumRegs; ++i)
948
+ for (int i=0; i < Tile::Layout::NumRegs; ++i)
677
949
  {
678
- const int linear = tile.index(i);
950
+ const int linear = Tile::Layout::linear_from_register(i);
679
951
 
680
952
  // handle case where tile size is not
681
953
  // aligned to block dimensions
682
- if (!Aligned && linear >= Size)
683
- break;
954
+ if (!Tile::Layout::valid(linear))
955
+ break;
684
956
 
685
957
  data(linear) = tile.data[i];
686
958
  }
687
959
 
960
+ initialized = true;
688
961
  WP_TILE_SYNC();
689
962
  }
690
963
 
691
964
  // in-place gradient zero
692
965
  inline CUDA_CALLABLE void grad_zero()
693
966
  {
694
- // todo: make this subtile (stride aware)
695
- for (int i=threadIdx.x; i < M*N; i+= WP_TILE_BLOCK_DIM)
967
+ for (int i=threadIdx.x; i < Layout::Size; i+= WP_TILE_BLOCK_DIM)
696
968
  grad(i) = T(0);
697
969
 
698
970
  WP_TILE_SYNC();
@@ -700,44 +972,73 @@ struct tile_shared_t
700
972
 
701
973
 
702
974
  // accumulate gradients onto this tile
703
- inline CUDA_CALLABLE void grad_add(const tile_register_t<T, M, N>& tile)
975
+ template <typename Tile>
976
+ inline CUDA_CALLABLE void grad_add(const Tile& tile)
704
977
  {
705
978
  WP_PRAGMA_UNROLL
706
- for (int i=0; i < tile.NumRegs; ++i)
979
+ for (int i=0; i < Tile::Layout::NumRegs; ++i)
707
980
  {
708
- const int linear = tile.index(i);
981
+ const int linear = Tile::Layout::linear_from_register(i);
709
982
 
710
983
  // handle case where tile size is not
711
984
  // aligned to block dimensions
712
- if (!Aligned && linear >= Size)
985
+ if (!Tile::Layout::valid(linear))
713
986
  break;
714
987
 
715
- if (Unique)
988
+ // if the destination layout is unique (no broadcast dimensions)
989
+ // then we can use regular non-atomic accmulation
990
+ if (Layout::Unique)
716
991
  grad(linear) += tile.data[i];
717
992
  else
718
993
  // use shared memory atomics to accumulate gradients
719
994
  // since for broadcast tiles (e.g.: a bias vector) multiple incoming threads
720
995
  // may map to a single location in shared memory
721
- atomic_add(&grad(linear), tile.data[i]);
996
+ wp::atomic_add(&grad(linear), tile.data[i]);
722
997
 
723
998
  }
724
999
 
725
1000
  WP_TILE_SYNC();
726
1001
  }
727
1002
 
1003
+ // accumulate gradient onto this tile from a global array
1004
+ CUDA_CALLABLE void grad_add(const tile_global_t<T, typename Layout::Shape>& global)
1005
+ {
1006
+ WP_PRAGMA_UNROLL
1007
+ for (int i=threadIdx.x; i < Layout::Size; i += WP_TILE_BLOCK_DIM)
1008
+ {
1009
+ auto c = Layout::coord_from_linear(i);
1010
+ T g = global.load_grad(c);
1011
+
1012
+ if (Layout::Unique)
1013
+ {
1014
+ // if the destination layout is unique (no broadcast dimensions)
1015
+ // then we can use regular non-atomic accumulation
1016
+ grad(c) += g;
1017
+ }
1018
+ else
1019
+ {
1020
+ // use shared memory atomics to accumulate gradients
1021
+ // since for broadcast tiles (e.g.: a bias vector) multiple incoming threads
1022
+ // may map to a single location in shared memory
1023
+ wp::atomic_add(&grad(c), g);
1024
+ }
1025
+ }
1026
+
1027
+ WP_TILE_SYNC();
1028
+ }
1029
+
728
1030
  // copy shared tile to register
729
- inline CUDA_CALLABLE tile_register_t<T, M, N> grad_to_register()
1031
+ inline CUDA_CALLABLE auto grad_to_register()
730
1032
  {
731
- tile_register_t<T, M, N> out;
1033
+ using Tile = tile_register_t<T, tile_layout_register_t<typename Layout::Shape>>;
1034
+ Tile out;
732
1035
 
733
1036
  WP_PRAGMA_UNROLL
734
- for (int i=0; i < out.NumRegs; ++i)
1037
+ for (int i=0; i < Tile::Layout::NumRegs; ++i)
735
1038
  {
736
- const int linear = out.index(i);
1039
+ const int linear = Tile::Layout::linear_from_register(i);
737
1040
 
738
- // handle case where tile size is not
739
- // aligned to block dimensions
740
- if (!Aligned && linear >= Size)
1041
+ if (!Tile::Layout::valid(linear))
741
1042
  break;
742
1043
 
743
1044
  out(i) = grad(linear);
@@ -746,40 +1047,20 @@ struct tile_shared_t
746
1047
  return out;
747
1048
  }
748
1049
 
749
- inline CUDA_CALLABLE void print() const
750
- {
751
- if (threadIdx.x == 0)
752
- {
753
- printf("tile(m=%d, n=%d, storage=shared) = [", M, N);
754
- for (int i=0; i < M; ++i)
755
- {
756
- printf("%*s[", i>0, "");
757
- for (int j=0; j < N; ++j)
758
- {
759
- printf("%g ", double(data(i, j)));
760
- }
761
-
762
- if (i == M-1)
763
- printf("]]\n");
764
- else
765
- printf("]\n");
766
- }
767
- }
768
- }
769
-
770
1050
  // copy shared tile to register
771
- inline CUDA_CALLABLE tile_register_t<T, M, N> copy_to_register() const
1051
+ inline CUDA_CALLABLE auto copy_to_register() const
772
1052
  {
773
- tile_register_t<T, M, N> out;
1053
+
1054
+ auto out = tile_register_like(this);
1055
+
1056
+ using Layout = typename decltype(out)::Layout;
774
1057
 
775
1058
  WP_PRAGMA_UNROLL
776
- for (int i=0; i < out.NumRegs; ++i)
1059
+ for (int i=0; i < Layout::NumRegs; ++i)
777
1060
  {
778
- const int linear = out.index(i);
1061
+ const int linear = Layout::linear_from_register(i);
779
1062
 
780
- // handle case where tile size is not
781
- // aligned to block dimensions
782
- if (!Aligned && linear >= Size)
1063
+ if (!Layout::valid(linear))
783
1064
  break;
784
1065
 
785
1066
  out(i) = data(linear);
@@ -788,220 +1069,354 @@ struct tile_shared_t
788
1069
  return out;
789
1070
  }
790
1071
 
791
- inline CUDA_CALLABLE void copy_to_global(array_t<T> dest, int x) const
792
- {
793
- assert(dest.ndim == 1);
1072
+ template <typename Global>
1073
+ inline CUDA_CALLABLE void copy_to_global(const Global& dest)
1074
+ {
1075
+ // vectorized loads for specific input/output shapes
1076
+ if constexpr (Layout::Shape::N == 2)
1077
+ {
1078
+ constexpr int lastdim = Layout::Shape::N-1;
1079
+ constexpr bool contiguous_src = Layout::Stride::dim(lastdim) == 1;
1080
+ const bool contiguous_dest = dest.data.strides[lastdim] == sizeof(T);
1081
+ const int elements = (dest.data.shape[lastdim] - dest.offset[lastdim]);
1082
+ const bool aligned = (elements*sizeof(T))%sizeof(float4) == 0;
1083
+
1084
+ if (contiguous_dest && contiguous_src && aligned)
1085
+ {
1086
+ constexpr int M = Layout::Shape::dim(0);
1087
+ constexpr int N = (Layout::Shape::dim(1)*sizeof(T))/sizeof(float4);
1088
+
1089
+ // alias of shared tile with 128bit type
1090
+ using SrcLayout = tile_layout_strided_t<tile_shape_t<M, N>>;
1091
+ tile_shared_t<float4, SrcLayout> src128((float4*)data.ptr);
1092
+ float4* dest128 = (float4*)&dest.data.data[dest.index_from_coord(tile_coord(0,0))];
1093
+
1094
+ assert(((uint64_t)(data.ptr))%sizeof(float4) == 0);
1095
+ assert(((uint64_t)(ptr))%sizeof(float4) == 0);
1096
+
1097
+ const int stride_i = dest.data.strides[0]/sizeof(float4);
1098
+ const int stride_j = 1;
1099
+
1100
+ WP_PRAGMA_UNROLL
1101
+ for (int i=threadIdx.x; i < SrcLayout::Size; i += WP_TILE_BLOCK_DIM)
1102
+ {
1103
+ auto c = SrcLayout::coord_from_linear(i);
1104
+
1105
+ dest128[stride_i*c[0] + stride_j*c[1]] = src128.data(i);
1106
+ }
794
1107
 
795
- // todo: use TMA here
796
- const int tile_i = x*N;
1108
+ return;
1109
+ }
1110
+ }
797
1111
 
1112
+ // scalar bounds checked path
798
1113
  WP_PRAGMA_UNROLL
799
- for (int i=threadIdx.x; i < Size; i += WP_TILE_BLOCK_DIM)
1114
+ for (int i=threadIdx.x; i < Layout::Size; i += WP_TILE_BLOCK_DIM)
800
1115
  {
801
- wp::index(dest, tile_i + i) = data(i);
1116
+ auto c = Layout::coord_from_linear(i);
1117
+ dest.store(c, data(i));
802
1118
  }
803
1119
  }
804
1120
 
805
- inline CUDA_CALLABLE void copy_to_global(array_t<T> dest, int x, int y)
1121
+ __device__ __forceinline__
1122
+ void cp_async_global_to_shared_128(float4* shared_dest, const float4* global_src)
806
1123
  {
807
- // todo: use TMA here
808
- const int tile_i = x*M;
809
- const int tile_j = y*N;
810
-
811
- // check each row is contiguous and 128bit aligned
812
- if (StrideN == 1 && dest.strides[1] == sizeof(T) && (N*sizeof(T))%sizeof(float4) == 0)
813
- {
814
- constexpr int num_rows = M;
815
- constexpr int num_cols = (N*sizeof(T))/sizeof(float4);
816
-
817
- tile_shared_t<float4, num_rows, num_cols> src128((float4*)data.ptr);
818
-
819
- // alias of shared tile with 128bit type
820
- float4* ptr = (float4*)&wp::index(dest, tile_i, tile_j);
1124
+ #if defined(__CUDA_ARCH__) && (__CUDA_ARCH__ >= 800)
1125
+
1126
+ unsigned long long saddr = 0ULL;
1127
+ unsigned long long gaddr = 0ULL;
1128
+
1129
+ asm volatile("cvta.to.shared.u64 %0, %1;" : "=l"(saddr) : "l"(shared_dest));
1130
+ asm volatile("cvta.to.global.u64 %0, %1;" : "=l"(gaddr) : "l"(global_src));
1131
+
1132
+ // Use cp.async on newer architectures
1133
+ asm volatile(
1134
+ "cp.async.ca.shared.global [%0], [%1], 16;\n"
1135
+ :
1136
+ : "l"(saddr), "l"(gaddr)
1137
+ );
1138
+ #else
1139
+ // use regular load/store through register on older arches
1140
+ *shared_dest = *global_src;
1141
+ #endif
1142
+ }
821
1143
 
822
- assert(((uint64_t)(data.ptr))%sizeof(float4) == 0);
823
- assert(((uint64_t)(ptr))%sizeof(float4) == 0);
1144
+ __device__ __forceinline__
1145
+ void cp_async_commit_and_wait_all_128()
1146
+ {
1147
+ #if defined(__CUDA_ARCH__) && (__CUDA_ARCH__ >= 800)
1148
+ asm volatile(
1149
+ "cp.async.commit_group;\n"
1150
+ "cp.async.wait_group 0;\n" ::);
1151
+ #endif
1152
+ }
1153
+
1154
+ template <typename Global>
1155
+ inline CUDA_CALLABLE void copy_from_global(const Global& src)
1156
+ {
1157
+ if (initialized)
1158
+ WP_TILE_SYNC();
1159
+
1160
+ // vectorized loads for specific input/output shapes
1161
+ if constexpr (Layout::Shape::N == 2)
1162
+ {
1163
+ constexpr int lastdim = Layout::Shape::N-1;
1164
+ constexpr bool contiguous_dest = Layout::Stride::dim(lastdim) == 1;
1165
+ const bool contiguous_src = src.data.strides[lastdim] == sizeof(T);
1166
+ const int elements = (src.data.shape[lastdim] - src.offset[lastdim]);
1167
+ const bool aligned = (elements*sizeof(T))%sizeof(float4) == 0;
1168
+
1169
+ if (contiguous_dest && contiguous_src && aligned)
1170
+ {
1171
+ constexpr int M = Layout::Shape::dim(0);
1172
+ constexpr int N = (Layout::Shape::dim(1)*sizeof(T))/sizeof(float4);
1173
+
1174
+ // alias of shared tile with 128bit type
1175
+ using DestLayout = tile_layout_strided_t<tile_shape_t<M, N>>;
1176
+ tile_shared_t<float4, DestLayout> dest128((float4*)data.ptr);
1177
+ float4* src128 = (float4*)&src.data.data[src.index_from_coord(tile_coord(0,0))];
1178
+
1179
+ assert(((uint64_t)(dest128.data.ptr))%sizeof(float4) == 0);
1180
+ assert(((uint64_t)(src128))%sizeof(float4) == 0);
1181
+
1182
+ const int stride_i = src.data.strides[0]/sizeof(float4);
1183
+ const int stride_j = 1;
1184
+
1185
+ WP_PRAGMA_UNROLL
1186
+ for (int i=threadIdx.x; i < DestLayout::Size; i += WP_TILE_BLOCK_DIM)
1187
+ {
1188
+ auto c = DestLayout::coord_from_linear(i);
1189
+
1190
+ #if WP_USE_ASYNC_PIPELINE
1191
+ cp_async_global_to_shared_128(&dest128.data(i), &src128[stride_i*c[0] + stride_j*c[1]]);
1192
+ #else
1193
+ dest128.data(i) = src128[stride_i*c[0] + stride_j*c[1]];
1194
+ #endif // WP_USE_ASYNC_PIPELINE
1195
+ }
824
1196
 
825
- const int stride_i = dest.strides[0]/sizeof(float4);
826
- const int stride_j = 1;
1197
+ #if WP_USE_ASYNC_PIPELINE
1198
+ cp_async_commit_and_wait_all_128();
1199
+ #endif // WP_USE_ASYNC_PIPELINE
827
1200
 
828
- WP_PRAGMA_UNROLL
829
- for (int i=threadIdx.x; i < src128.Size; i += WP_TILE_BLOCK_DIM)
830
- {
831
- coord_t c = src128.coord(i);
832
- ptr[c.i*stride_i + c.j*stride_j] = src128.data(i);
1201
+ initialized = true;
1202
+ WP_TILE_SYNC();
1203
+ return;
833
1204
  }
834
1205
  }
835
- else
836
- {
837
- // wp.array() indexing generates poor code due to char* casting
838
- // here we unroll some of the ops, note this assumes byte strides are
839
- // aligned to the element size
840
- T* ptr = &wp::index(dest, tile_i, tile_j);
841
- const int stride_i = dest.strides[0]/sizeof(T);
842
- const int stride_j = dest.strides[1]/sizeof(T);
843
-
844
- WP_PRAGMA_UNROLL
845
- for (int i=threadIdx.x; i < Size; i += WP_TILE_BLOCK_DIM)
846
- {
847
- coord_t c = coord(i);
848
- ptr[c.i*stride_i + c.j*stride_j] = data(c.i, c.j);
849
- }
850
- }
851
- }
852
-
853
- inline CUDA_CALLABLE void copy_from_global(const array_t<T>& src, int x)
854
- {
855
- // todo: use async pipelines or TMA here
856
- const int tile_i = x*N;
857
1206
 
1207
+ // scalar bounds checked path
858
1208
  WP_PRAGMA_UNROLL
859
- for (int i=threadIdx.x; i < Size; i += WP_TILE_BLOCK_DIM)
1209
+ for (int i=threadIdx.x; i < Layout::Size; i += WP_TILE_BLOCK_DIM)
860
1210
  {
861
- data(i) = wp::index(src, tile_i + i);
1211
+ auto c = Layout::coord_from_linear(i);
1212
+ data(i) = src.load(c);
862
1213
  }
863
1214
 
1215
+ initialized = true;
864
1216
  WP_TILE_SYNC();
865
1217
  }
866
1218
 
867
- inline CUDA_CALLABLE void copy_from_global(const array_t<T>& src, int x, int y)
1219
+ template <typename Global>
1220
+ inline CUDA_CALLABLE auto atomic_add(Global& dest)
868
1221
  {
869
- // todo: use async pipelines or TMA here
870
- const int tile_i = x*M;
871
- const int tile_j = y*N;
1222
+ copy_to_register().atomic_add(dest);
1223
+ }
872
1224
 
873
- // check each row is contiguous and 128bit aligned
874
- if (StrideN == 1 && src.strides[1] == sizeof(T) && (N*sizeof(T))%sizeof(float4) == 0)
875
- {
876
- constexpr int num_rows = M;
877
- constexpr int num_cols = (N*sizeof(T))/sizeof(float4);
1225
+ template <typename Global>
1226
+ inline CUDA_CALLABLE auto atomic_add_grad(Global& dest)
1227
+ {
1228
+ grad_to_register().atomic_add_grad(dest);
1229
+ }
878
1230
 
879
- // alias of shared tile with 128bit type
880
- tile_shared_t<float4, num_rows, num_cols> dest128((float4*)data.ptr);
1231
+ // overload for integral types
1232
+ inline CUDA_CALLABLE void print_value(int x) const
1233
+ {
1234
+ printf("%d", x);
1235
+ }
881
1236
 
882
- const float4* ptr = (const float4*)&wp::index(src, tile_i, tile_j);
1237
+ // overload for floating point types
1238
+ template <typename ValueType>
1239
+ inline CUDA_CALLABLE void print_value(ValueType x) const
1240
+ {
1241
+ printf("%g", x);
1242
+ }
883
1243
 
884
- assert(((uint64_t)(data.ptr))%sizeof(float4) == 0);
885
- assert(((uint64_t)(ptr))%sizeof(float4) == 0);
1244
+ template <int Level = 0>
1245
+ inline CUDA_CALLABLE void print_values(const Storage& storage, int index=0) const
1246
+ {
1247
+ using Shape = typename Layout::Shape;
886
1248
 
887
- const int stride_i = src.strides[0]/sizeof(float4);
888
- //const int stride_j = 1;
1249
+ if constexpr (Level < Shape::N)
1250
+ {
1251
+ if constexpr (Level == Shape::N - 1)
1252
+ {
1253
+ // Special handling for 1D case
1254
+ printf("[");
1255
+ for (int i = 0; i < Shape::dim(Level); ++i)
1256
+ {
1257
+ print_value(storage(index + i));
889
1258
 
890
- WP_PRAGMA_UNROLL
891
- for (int i=threadIdx.x; i < dest128.Size; i += WP_TILE_BLOCK_DIM)
892
- {
893
- coord_t c = dest128.coord(i);
894
-
895
- #if WP_USE_ASYNC_PIPELINE
896
- __pipeline_memcpy_async(&dest128.data(i),
897
- &ptr[c.i*stride_i + c.j],
898
- sizeof(float4));
899
- #else
900
- dest128.data(i) = ptr[c.i*stride_i + c.j];
901
- #endif // WP_USE_ASYNC_PIPELINE
1259
+ if (i < Shape::dim(Level) - 1)
1260
+ {
1261
+ printf(" ");
1262
+ }
1263
+ }
1264
+ printf("]");
1265
+ }
1266
+ else if constexpr (Level == Shape::N - 2)
1267
+ {
1268
+ // Special handling for 2D case
1269
+ printf("[");
1270
+ for (int i = 0; i < Shape::dim(Level); ++i)
1271
+ {
1272
+ printf("[");
1273
+ for (int j=0; j < Shape::dim(Level+1); ++j)
1274
+ {
1275
+ print_value(storage(index));
1276
+
1277
+ if (j < Shape::dim(Level+1) - 1)
1278
+ {
1279
+ printf(" ");
1280
+ }
1281
+
1282
+ ++index;
1283
+ }
1284
+
1285
+ printf("]");
1286
+
1287
+ // next row
1288
+ if (i < Shape::dim(Level)-1)
1289
+ {
1290
+ printf("\n");
1291
+
1292
+ // indent next row
1293
+ for (int i=0; i <= Shape::N-2; ++i)
1294
+ printf(" ");
1295
+
1296
+ }
1297
+ }
1298
+ printf("]");
1299
+ }
1300
+ else
1301
+ {
1302
+ printf("[");
1303
+ for (int i = 0; i < Shape::dim(Level); ++i)
1304
+ {
1305
+ print_values<Level + 1>(storage, index + i * Shape::dim(Level));
1306
+ if (i < Shape::dim(Level) - 1)
1307
+ {
1308
+ printf("\n\n");
1309
+
1310
+ // indent next row
1311
+ for (int i=0; i <= Level; ++i)
1312
+ printf(" ");
1313
+ }
1314
+ }
1315
+ printf("]");
902
1316
  }
1317
+ }
1318
+ }
903
1319
 
904
- #if WP_USE_ASYNC_PIPELINE
905
- __pipeline_commit();
906
- #endif // WP_USE_ASYNC_PIPELINE
1320
+ inline CUDA_CALLABLE void print(bool reverse=false) const
1321
+ {
1322
+ if (threadIdx.x != 0)
1323
+ return;
907
1324
 
908
- }
1325
+ if (reverse)
1326
+ print_values(grad);
909
1327
  else
1328
+ print_values(data);
1329
+
1330
+ printf(" = tile(shape=(");
1331
+ for (int i=0; i < Layout::Shape::N; ++i)
910
1332
  {
911
- // wp.array() indexing generates poor code due to char* casting
912
- // here we unroll some of the ops, note this assumes array byte strides are
913
- // aligned to the element size
914
- const T* ptr = &wp::index(src, tile_i, tile_j);
915
-
916
- assert(src.strides[0]%sizeof(T) == 0);
917
- assert(src.strides[1]%sizeof(T) == 0);
918
-
919
- const int stride_i = src.strides[0]/sizeof(T);
920
- const int stride_j = src.strides[1]/sizeof(T);
921
-
922
- WP_PRAGMA_UNROLL
923
- for (int i=threadIdx.x; i < Size; i += WP_TILE_BLOCK_DIM)
924
- {
925
- coord_t c = coord(i);
926
- data(c.i, c.j) = ptr[c.i*stride_i + c.j*stride_j];
927
- }
1333
+ printf("%d", Layout::Shape::dim(i));
1334
+ if (i != Layout::Shape::N-1)
1335
+ printf(",");
928
1336
  }
929
1337
 
930
- #if !WP_USE_ASYNC_PIPELINE
931
- WP_TILE_SYNC();
932
- #endif
933
-
934
- }
1338
+ printf("), storage=shared)\n");
1339
+ }
935
1340
  };
936
1341
 
937
- template <typename T, int M, int N>
938
- void tile_register_t<T, M, N>::print() const
1342
+
1343
+ template <typename T, typename L>
1344
+ void tile_register_t<T, L>::print() const
939
1345
  {
940
1346
  // create a temporary shared tile so that
941
1347
  // we can print it deterministically
942
- WP_TILE_SHARED T smem[M*N];
943
-
944
- tile_shared_t<T, M, N> scratch(smem, NULL);
1348
+ WP_TILE_SHARED T smem[L::Size];
1349
+ tile_shared_t<T, tile_layout_strided_t<typename L::Shape>> scratch(smem, NULL);
1350
+
945
1351
  scratch.assign(*this);
946
1352
 
947
1353
  WP_TILE_SYNC();
948
1354
 
949
1355
  if (threadIdx.x == 0)
950
1356
  {
951
- printf("tile(m=%d, n=%d, storage=register) = [", M, N);
952
- for (int i=0; i < M; ++i)
953
- {
954
- printf("%*s[", i>0, "");
955
- for (int j=0; j < N; ++j)
956
- {
957
- printf("%g ", double(scratch.data(i, j)));
958
- }
1357
+ scratch.print_values(scratch.data, 0);
959
1358
 
960
- if (i == M-1)
961
- printf("]]\n");
962
- else
963
- printf("]\n");
1359
+ printf(" = tile(shape=(");
1360
+ for (int i=0; i < L::Shape::N; ++i)
1361
+ {
1362
+ printf("%d", L::Shape::dim(i));
1363
+ if (i != L::Shape::N-1)
1364
+ printf(",");
964
1365
  }
1366
+
1367
+ printf("), storage=register)\n");
965
1368
  }
966
1369
 
967
1370
  WP_TILE_SYNC();
968
1371
  }
969
1372
 
970
- template <typename T, int M, int N>
971
- inline CUDA_CALLABLE void print(const tile_register_t<T, M, N>& t)
1373
+ // print entry points
1374
+ template <typename T, typename L>
1375
+ inline CUDA_CALLABLE void print(const tile_register_t<T, L>& t) { t.print(); }
1376
+ template <typename T, typename L, bool Owner>
1377
+ inline CUDA_CALLABLE void print(const tile_shared_t<T, L, Owner>& t) { t.print(); }
1378
+
1379
+ template <typename T, typename L, bool O>
1380
+ inline CUDA_CALLABLE int len(const tile_shared_t<T, L, O>& t)
972
1381
  {
973
- t.print();
1382
+ return Tile::Layout::Shape::dim(0);
974
1383
  }
975
1384
 
976
- template <typename T, int M, int N>
977
- inline CUDA_CALLABLE void adj_print(const tile_register_t<T, M, N>& t, const tile_register_t<T, M, N>& a)
1385
+ template <typename T, typename L, bool O, typename AdjTile>
1386
+ inline CUDA_CALLABLE void adj_len(const tile_shared_t<T,L,O>& t, const AdjTile& a, int& adj_ret)
978
1387
  {
979
- a.print();
980
1388
  }
981
1389
 
982
- template <typename T, int M, int N, int StrideM, int StrideN, bool Owner>
983
- inline CUDA_CALLABLE void print(const tile_shared_t<T, M, N, StrideM, StrideN, Owner>& t)
1390
+ template <typename T, typename L>
1391
+ inline CUDA_CALLABLE int len(const tile_register_t<T, L>& t)
984
1392
  {
985
- t.print();
1393
+ return Tile::Layout::Shape::dim(0);
986
1394
  }
987
1395
 
988
- template <typename T, int M, int N, int StrideM, int StrideN, bool Owner>
989
- inline CUDA_CALLABLE void adj_print(const tile_shared_t<T, M, N, StrideM, StrideN, Owner>& t, const tile_shared_t<T, M, N, StrideM, StrideN, Owner>& a)
1396
+ template <typename T, typename L, typename AdjTile>
1397
+ inline CUDA_CALLABLE void adj_len(const tile_register_t<T,L>& t, const AdjTile& a, int& adj_ret)
990
1398
  {
991
- a.print();
992
1399
  }
993
1400
 
1401
+
1402
+ template <typename T, typename L>
1403
+ inline CUDA_CALLABLE void adj_print(const tile_register_t<T, L>& t, const tile_register_t<T, L>& a) { a.print(); }
1404
+ template <typename T, typename L, bool Owner>
1405
+ inline CUDA_CALLABLE void adj_print(const tile_shared_t<T, L, Owner>& t, const tile_shared_t<T, L, Owner>& a) { a.print(true); }
1406
+
1407
+
1408
+
994
1409
  // helpers to allocate shared tiles
995
- template <typename T, int M, int N, bool RequiresGrad>
1410
+ template <typename T, typename Shape, bool RequiresGrad>
996
1411
  inline CUDA_CALLABLE auto tile_alloc_empty()
997
1412
 
998
- { constexpr int Len = M*N;
999
- T* data = (T*)tile_alloc_shared(Len*sizeof(T));
1413
+ { constexpr int size = Shape::size();
1414
+ T* data = (T*)tile_alloc_shared(size*sizeof(T));
1000
1415
  T* grad = NULL;
1001
1416
 
1002
1417
  #if FP_CHECK
1003
1418
 
1004
- for (int i=threadIdx.x; i < Len; i+= WP_TILE_BLOCK_DIM)
1419
+ for (int i=threadIdx.x; i < size; i+= WP_TILE_BLOCK_DIM)
1005
1420
  data[i] = T(nanf(""));
1006
1421
 
1007
1422
  WP_TILE_SYNC();
@@ -1011,15 +1426,15 @@ inline CUDA_CALLABLE auto tile_alloc_empty()
1011
1426
 
1012
1427
  if (RequiresGrad)
1013
1428
  {
1014
- grad = (T*)tile_alloc_shared(Len*sizeof(T));
1429
+ grad = (T*)tile_alloc_shared(size*sizeof(T));
1015
1430
 
1016
- for (int i=threadIdx.x; i < Len; i+= WP_TILE_BLOCK_DIM)
1431
+ for (int i=threadIdx.x; i < size; i+= WP_TILE_BLOCK_DIM)
1017
1432
  grad[i] = T(0);
1018
1433
 
1019
1434
  WP_TILE_SYNC();
1020
1435
  }
1021
1436
 
1022
- return tile_shared_t<T, M, N>(data, grad);
1437
+ return tile_shared_t<T, tile_layout_strided_t<Shape>>(data, grad);
1023
1438
  }
1024
1439
 
1025
1440
  template <typename T, int M, int N, bool RequiresGrad>
@@ -1043,7 +1458,7 @@ inline CUDA_CALLABLE auto tile_alloc_zeros()
1043
1458
 
1044
1459
  WP_TILE_SYNC();
1045
1460
 
1046
- return tile_shared_t<T, M, N, StrideM, StrideN>(data, grad);
1461
+ return tile_shared_t<T, tile_layout_strided_t<tile_shape_t<M, N>>(data, grad);
1047
1462
  }
1048
1463
 
1049
1464
 
@@ -1054,9 +1469,10 @@ inline CUDA_CALLABLE auto tile_alloc_zeros()
1054
1469
  template <typename T>
1055
1470
  inline CUDA_CALLABLE auto tile(const T& x)
1056
1471
  {
1057
- tile_register_t<T, 1, WP_TILE_BLOCK_DIM> result;
1472
+ tile_register_t<T, tile_layout_register_t<tile_shape_t<WP_TILE_BLOCK_DIM>>> result;
1058
1473
 
1059
- static_assert(result.NumRegs == 1);
1474
+ using Layout = typename decltype(result)::Layout;
1475
+ static_assert(Layout::NumRegs == 1);
1060
1476
 
1061
1477
  result.data[0] = x;
1062
1478
  return result;
@@ -1066,9 +1482,10 @@ inline CUDA_CALLABLE auto tile(const T& x)
1066
1482
  template <typename T, unsigned Length>
1067
1483
  inline CUDA_CALLABLE auto tile(const wp::vec_t<Length, T>& x)
1068
1484
  {
1069
- tile_register_t<T, Length, WP_TILE_BLOCK_DIM> result;
1485
+ tile_register_t<T, tile_layout_register_t<tile_shape_t<Length, WP_TILE_BLOCK_DIM>>> result;
1070
1486
 
1071
- static_assert(result.NumRegs == Length);
1487
+ using Layout = typename decltype(result)::Layout;
1488
+ static_assert(Layout::NumRegs == Length);
1072
1489
 
1073
1490
  for (int i=0; i < Length; ++i)
1074
1491
  result.data[i] = x[i];
@@ -1080,8 +1497,8 @@ inline CUDA_CALLABLE auto tile(const wp::vec_t<Length, T>& x)
1080
1497
  template <typename T, typename AdjTile>
1081
1498
  inline CUDA_CALLABLE void adj_tile(const T& x, T& adj_x, AdjTile& adj_ret)
1082
1499
  {
1083
- static_assert(AdjTile::M == 1);
1084
- static_assert(AdjTile::N == WP_TILE_BLOCK_DIM);
1500
+ static_assert(AdjTile::Layout::Shape::N == 1);
1501
+ static_assert(AdjTile::Layout::Shape::dim(0) == WP_TILE_BLOCK_DIM);
1085
1502
 
1086
1503
  auto adj_reg = adj_ret.copy_to_register();
1087
1504
 
@@ -1091,8 +1508,9 @@ inline CUDA_CALLABLE void adj_tile(const T& x, T& adj_x, AdjTile& adj_ret)
1091
1508
  template <typename T, unsigned Length, typename AdjTile>
1092
1509
  inline CUDA_CALLABLE void adj_tile(const wp::vec_t<Length, T>& x, wp::vec_t<Length, T>& adj_x, AdjTile& adj_ret)
1093
1510
  {
1094
- static_assert(AdjTile::M == Length);
1095
- static_assert(AdjTile::N == WP_TILE_BLOCK_DIM);
1511
+ static_assert(AdjTile::Layout::Shape::N == 2);
1512
+ static_assert(AdjTile::Layout::Shape::dim(0) == Length);
1513
+ static_assert(AdjTile::Layout::Shape::dim(1) == WP_TILE_BLOCK_DIM);
1096
1514
 
1097
1515
  auto adj_reg = adj_ret.copy_to_register();
1098
1516
 
@@ -1108,76 +1526,82 @@ inline CUDA_CALLABLE auto untile(Tile& tile)
1108
1526
  // there is exactly one value per-thread
1109
1527
  auto reg = tile.copy_to_register();
1110
1528
 
1529
+ constexpr int N = Tile::Layout::Shape::N;
1530
+
1111
1531
  // scalar case
1112
- if constexpr(Tile::M == 1)
1532
+ if constexpr(N == 1)
1113
1533
  {
1114
1534
  return reg.data[0];
1115
1535
  }
1116
1536
 
1117
1537
  // vector case
1118
- if constexpr(Tile::M > 1)
1538
+ if constexpr(N == 2)
1119
1539
  {
1120
- wp::vec_t<Tile::M, typename Tile::Type> v;
1121
- for (int i=0; i < Tile::M; ++i)
1540
+ constexpr int Length = Tile::Layout::Shape::dim(0);
1541
+ wp::vec_t<Length, typename Tile::Type> v;
1542
+ for (int i=0; i < Length; ++i)
1122
1543
  v[i] = reg.data[i];
1123
1544
 
1124
1545
  return v;
1125
1546
  }
1126
1547
  }
1127
1548
 
1128
-
1129
-
1130
1549
  template <typename Tile, typename Value>
1131
1550
  inline CUDA_CALLABLE void adj_untile(Tile& tile, Tile& adj_tile, Value& adj_ret)
1132
1551
  {
1133
1552
  auto adj = adj_tile.copy_to_register();
1134
1553
 
1554
+ constexpr int N = Tile::Layout::Shape::N;
1555
+
1135
1556
  // scalar case
1136
- if constexpr(Tile::M == 1)
1557
+ if constexpr(N == 1)
1137
1558
  {
1138
1559
  adj.data[0] += adj_ret;
1139
1560
  }
1140
1561
 
1141
1562
  // vector case
1142
- if constexpr(Tile::M > 1)
1563
+ if constexpr(N == 2)
1143
1564
  {
1144
- for (int i=0; i < Tile::M; ++i)
1145
- adj.data[i] = adj_ret[i];
1565
+ constexpr int Length = Tile::Layout::Shape::dim(0);
1566
+ for (int i=0; i < Length; ++i)
1567
+ adj.data[i] += adj_ret[i];
1146
1568
  }
1147
1569
 
1148
1570
  adj_tile.assign(adj);
1149
1571
  }
1150
1572
 
1151
1573
  // zero initialized tile
1152
- template <typename T, int M, int N>
1574
+ template <typename T, unsigned... Shape>
1153
1575
  inline CUDA_CALLABLE auto tile_zeros()
1154
1576
  {
1155
1577
  // tile variable assignment operator will handle initialization (since lhs could be shared/register tile)
1156
1578
  return T(0);
1157
1579
  }
1158
1580
 
1159
- // zero initialized tile
1160
- template <typename T, int M, int N>
1581
+ // one-initialized tile
1582
+ template <typename T, unsigned... Shape>
1161
1583
  inline CUDA_CALLABLE auto tile_ones()
1162
1584
  {
1163
1585
  // tile variable assignment operator will handle initialization (since lhs could be shared/register tile)
1164
1586
  return T(1);
1165
1587
  }
1166
1588
 
1167
- // zero initialized tile
1168
- template <typename T, int M, int N>
1589
+ // tile with evenly spaced values
1590
+ template <typename T, int Len>
1169
1591
  inline CUDA_CALLABLE auto tile_arange(T start, T stop, T step)
1170
1592
  {
1171
- tile_register_t<T, M, N> out;
1593
+ auto out = tile_register<T, Len>();
1594
+
1595
+ using Layout = typename decltype(out)::Layout;
1172
1596
 
1173
1597
  WP_PRAGMA_UNROLL
1174
- for (int i=0; i < out.NumRegs; ++i)
1598
+ for (int i=0; i < Layout::NumRegs; ++i)
1175
1599
  {
1176
- const int linear = out.index(i);
1600
+ const int linear = Layout::linear_from_register(i);
1177
1601
 
1178
1602
  // handle case where tile size is not
1179
1603
  // aligned to block dimensions
1180
- if (!out.Aligned && linear >= out.Size)
1604
+ if (!Layout::valid(linear))
1181
1605
  break;
1182
1606
 
1183
1607
  out.data[i] = start + linear*step;
@@ -1190,192 +1614,106 @@ template <typename T, typename AdjTile>
1190
1614
  inline CUDA_CALLABLE void adj_tile_arange(T start, T stop, T step,
1191
1615
  T& adj_start, T& adj_stop, T& adj_step, AdjTile& adj_ret) {}
1192
1616
 
1193
- // entry point for 1d load
1194
- template <typename T, int N>
1195
- inline CUDA_CALLABLE auto tile_load(array_t<T>& src, int x)
1617
+ // entry point for load operations, these just return a reference to a global memory array + coordinate
1618
+ template <unsigned... Shape, typename... Indices, typename T>
1619
+ inline CUDA_CALLABLE auto tile_load(array_t<T>& src, Indices... offset)
1196
1620
  {
1197
- return tile_global_t<T>(src, x, 0);
1621
+ return tile_global_t<T, tile_shape_t<Shape...>>(src, tile_coord(offset...));
1198
1622
  }
1199
1623
 
1200
- // entry point for 2d load
1201
- template <typename T, int M, int N>
1202
- inline CUDA_CALLABLE auto tile_load(array_t<T>& src, int x, int y)
1203
- {
1204
- return tile_global_t<T>(src, x, y);
1205
- }
1624
+ // // entry point for tile store operations
1625
+ // template <typename... Indices, typename T, typename Tile>
1626
+ // inline CUDA_CALLABLE void tile_store(array_t<T>& dest, Tile& src, Indices... x)
1627
+ // {
1628
+ // src.copy_to_global(tile_global_t<T, typename Tile::Layout::Shape>(dest, tile_coord(x)));
1629
+ // }
1206
1630
 
1207
- // entry point for 1d store
1631
+ // entry point for tile store operations
1208
1632
  template <typename T, typename Tile>
1209
- inline CUDA_CALLABLE void tile_store(array_t<T>& dest, int x, Tile& src)
1210
- {
1211
- // dispatch to tile type
1212
- src.copy_to_global(dest, x);
1213
- }
1214
-
1215
- // entry point for 2d store
1633
+ inline CUDA_CALLABLE void tile_store(array_t<T>& dest, int x, Tile& src) { src.copy_to_global(tile_global_t<T, typename Tile::Layout::Shape>(dest, tile_coord(x))); }
1216
1634
  template <typename T, typename Tile>
1217
- inline CUDA_CALLABLE void tile_store(array_t<T>& dest, int x, int y, Tile& src)
1218
- {
1219
- // dispatch to tile type
1220
- src.copy_to_global(dest, x, y);
1221
- }
1222
-
1223
- // entry point for store
1635
+ inline CUDA_CALLABLE void tile_store(array_t<T>& dest, int x, int y, Tile& src) { src.copy_to_global(tile_global_t<T, typename Tile::Layout::Shape>(dest, tile_coord(x, y))); }
1224
1636
  template <typename T, typename Tile>
1225
- inline CUDA_CALLABLE auto tile_atomic_add(array_t<T>& dest, int x, int y, Tile& src)
1226
- {
1227
- auto src_reg = src.copy_to_register();
1228
-
1229
- const int tile_i = x*src_reg.M;
1230
- const int tile_j = y*src_reg.N;
1231
-
1232
- tile_register_t<T, src_reg.M, src_reg.N> previous;
1233
-
1234
- WP_PRAGMA_UNROLL
1235
- for (int i=0; i < src_reg.NumRegs; ++i)
1236
- {
1237
- // handle case where tile size is not
1238
- // aligned to block dimensions
1239
- int linear = src_reg.index(i);
1240
- if (!src_reg.Aligned && linear >= src_reg.Size)
1241
- break;
1637
+ inline CUDA_CALLABLE void tile_store(array_t<T>& dest, int x, int y, int z, Tile& src) { src.copy_to_global(tile_global_t<T, typename Tile::Layout::Shape>(dest, tile_coord(x, y, z))); }
1638
+ template <typename T, typename Tile>
1639
+ inline CUDA_CALLABLE void tile_store(array_t<T>& dest, int x, int y, int z, int w, Tile& src) { src.copy_to_global(tile_global_t<T, typename Tile::Layout::Shape>(dest, tile_coord(x, y, z, w))); }
1242
1640
 
1243
- coord_t c = src_reg.coord(linear);
1244
- previous.data[i] = atomic_add(dest, tile_i + c.i, tile_j + c.j, src_reg.data[i]);
1245
- }
1246
1641
 
1247
- return previous;
1248
- }
1249
1642
 
1643
+ template <typename T, typename Tile>
1644
+ inline CUDA_CALLABLE auto tile_atomic_add(array_t<T>& dest, int x, Tile& src) { return src.atomic_add(tile_global_t<T, typename Tile::Layout::Shape>(dest, tile_coord(x))); }
1645
+ template <typename T, typename Tile>
1646
+ inline CUDA_CALLABLE auto tile_atomic_add(array_t<T>& dest, int x, int y, Tile& src) { return src.atomic_add(tile_global_t<T, typename Tile::Layout::Shape>(dest, tile_coord(x, y)));}
1647
+ template <typename T, typename Tile>
1648
+ inline CUDA_CALLABLE auto tile_atomic_add(array_t<T>& dest, int x, int y, int z, Tile& src) { return src.atomic_add(tile_global_t<T, typename Tile::Layout::Shape>(dest, tile_coord(x, y, z)));}
1649
+ template <typename T, typename Tile>
1650
+ inline CUDA_CALLABLE auto tile_atomic_add(array_t<T>& dest, int x, int y, int z, int w, Tile& src) { return src.atomic_add(tile_global_t<T, typename Tile::Layout::Shape>(dest, tile_coord(x, y, z, w)));}
1250
1651
 
1251
1652
 
1252
1653
  //-------------------------------------
1253
1654
  // Adjoints
1254
1655
 
1255
- template <typename T, typename AdjTile>
1256
- inline CUDA_CALLABLE void adj_tile_load(array_t<T>& src, int x,
1257
- array_t<T>& adj_src, int adj_x,
1656
+ template <typename T, typename AdjTile, typename Coord>
1657
+ inline CUDA_CALLABLE void adj_tile_load(array_t<T>& src, Coord c,
1658
+ array_t<T>& adj_src, Coord adj_c,
1258
1659
  AdjTile& adj_ret)
1259
1660
  {
1260
- // early out
1261
- // if (!src.grad)
1262
- // return;
1263
-
1264
- auto adj_reg = adj_ret.grad_to_register();
1265
-
1266
- const int tile_i = x*adj_reg.N;
1267
-
1268
- // add gradients to src array
1269
- WP_PRAGMA_UNROLL
1270
- for (int i=0; i < adj_reg.NumRegs; ++i)
1271
- {
1272
- int linear = adj_reg.index(i);
1273
- if (!adj_reg.Aligned && linear >= adj_reg.Size)
1274
- break;
1275
-
1276
- auto grad = adj_reg.data[i];
1661
+ tile_global_t<T, typename AdjTile::Layout::Shape> dest(src, c);
1662
+
1663
+ // we allow users to override grad of src
1664
+ if (adj_src.data)
1665
+ dest.data.grad = adj_src.data;
1277
1666
 
1278
- if (adj_src.data)
1279
- adj_atomic_add(&index(adj_src, tile_i + linear), grad);
1280
- else if (src.grad)
1281
- adj_atomic_add(&index_grad(src, tile_i + linear), grad);
1282
- }
1667
+ adj_ret.atomic_add_grad(dest);
1283
1668
  }
1284
1669
 
1285
- template <typename T, typename AdjTile>
1286
- inline CUDA_CALLABLE void adj_tile_load(array_t<T>& src, int x, int y,
1287
- array_t<T>& adj_src, int adj_x, int adj_y,
1288
- AdjTile& adj_ret)
1289
- {
1290
- // early out
1291
- // if (!src.grad)
1292
- // return;
1293
-
1294
- auto adj_reg = adj_ret.grad_to_register();
1295
-
1296
- const int tile_i = x*adj_reg.M;
1297
- const int tile_j = y*adj_reg.N;
1298
-
1299
- // add gradients to src array
1300
- WP_PRAGMA_UNROLL
1301
- for (int i=0; i < adj_reg.NumRegs; ++i)
1302
- {
1303
- int linear = adj_reg.index(i);
1304
- if (!adj_reg.Aligned && linear >= adj_reg.Size)
1305
- break;
1306
-
1307
- coord_t coord = adj_reg.coord(linear);
1308
1670
 
1309
- auto grad = adj_reg.data[i];
1671
+ template <typename T, typename AdjTile>
1672
+ inline CUDA_CALLABLE void adj_tile_load(array_t<T>& src, int x, array_t<T>& adj_src, int adj_x, AdjTile& adj_ret) { adj_tile_load( src, tile_coord(x), adj_src, tile_coord(0), adj_ret); }
1673
+ template <typename T, typename AdjTile>
1674
+ inline CUDA_CALLABLE void adj_tile_load(array_t<T>& src, int x, int y, array_t<T>& adj_src, int adj_x, int adj_y, AdjTile& adj_ret) { adj_tile_load( src, tile_coord(x, y), adj_src, tile_coord(0,0), adj_ret); }
1675
+ template <typename T, typename AdjTile>
1676
+ inline CUDA_CALLABLE void adj_tile_load(array_t<T>& src, int x, int y, int z, array_t<T>& adj_src, int adj_x, int adj_y, int adj_z, AdjTile& adj_ret) { adj_tile_load( src, tile_coord(x, y, z), adj_src, tile_coord(0,0,0), adj_ret); }
1677
+ template <typename T, typename AdjTile>
1678
+ inline CUDA_CALLABLE void adj_tile_load(array_t<T>& src, int x, int y, int z, int w, array_t<T>& adj_src, int adj_x, int adj_y, int adj_z, int adj_w, AdjTile& adj_ret) { adj_tile_load( src, tile_coord(x, y, z, w), adj_src, tile_coord(0,0,0,0), adj_ret); }
1310
1679
 
1311
- if (adj_src.data)
1312
- adj_atomic_add(&index(adj_src, tile_i + coord.i, tile_j + coord.j), grad);
1313
- else if (src.grad)
1314
- adj_atomic_add(&index_grad(src, tile_i + coord.i, tile_j + coord.j), grad);
1315
- }
1316
- }
1317
1680
 
1318
1681
 
1319
- template <typename T, typename Tile, typename AdjTile>
1320
- inline CUDA_CALLABLE void adj_tile_store(array_t<T>& dest, int x, Tile& t, array_t<T>& adj_dest, int adj_x, AdjTile& adj_t)
1682
+ template <typename T, typename Tile, typename AdjTile, typename Coord>
1683
+ inline CUDA_CALLABLE void adj_tile_store(array_t<T>& dest, Coord c, Tile& t, array_t<T>& adj_dest, Coord adj_c, AdjTile& adj_t)
1321
1684
  {
1322
- // convert to register if necessary
1323
- tile_register_t<T, AdjTile::M, AdjTile::N> adj_reg;
1324
-
1325
- const int tile_i = x*adj_reg.N;
1326
-
1327
- // load gradients from output
1328
- WP_PRAGMA_UNROLL
1329
- for (int i=0; i < adj_reg.NumRegs; ++i)
1330
- {
1331
- int linear = adj_reg.index(i);
1332
- if (!adj_reg.Aligned && linear >= adj_reg.Size)
1333
- break;
1685
+ tile_global_t<T, typename AdjTile::Layout::Shape> src(dest, c);
1686
+
1687
+ // we allow users to override grad of src
1688
+ if (adj_dest.data)
1689
+ src.data.grad = adj_dest.data;
1334
1690
 
1335
- if (adj_dest.data)
1336
- adj_reg.data[i] = index(adj_dest, tile_i + linear);
1337
- else if (dest.grad)
1338
- adj_reg.data[i] = index_grad(dest, tile_i + linear);
1339
- }
1691
+ if (src.data.grad == NULL)
1692
+ return;
1340
1693
 
1341
- // store adjoint back to tile
1342
- adj_t.grad_add(adj_reg);
1694
+ adj_t.grad_add(src);
1343
1695
  }
1344
1696
 
1345
1697
  template <typename T, typename Tile, typename AdjTile>
1346
- inline CUDA_CALLABLE void adj_tile_store(array_t<T>& dest, int x, int y, Tile& t, array_t<T>& adj_dest, int adj_x, int adj_y, AdjTile& adj_t)
1347
- {
1348
- // allocate register tile to load grads into
1349
- tile_register_t<T, AdjTile::M, AdjTile::N> adj_reg;
1350
-
1351
- const int tile_i = x*adj_reg.M;
1352
- const int tile_j = y*adj_reg.N;
1353
-
1354
- // load gradients from output
1355
- WP_PRAGMA_UNROLL
1356
- for (int i=0; i < adj_reg.NumRegs; ++i)
1357
- {
1358
- int linear = adj_reg.index(i);
1359
- if (!adj_reg.Aligned && linear >= adj_reg.Size)
1360
- break;
1361
-
1362
- coord_t coord = adj_reg.coord(linear);
1698
+ inline CUDA_CALLABLE void adj_tile_store(array_t<T>& dest, int x, Tile& t, array_t<T>& adj_dest, int adj_x, AdjTile& adj_t) { adj_tile_store(dest, tile_coord(x), t, adj_dest, tile_coord(0), adj_t); }
1699
+ template <typename T, typename Tile, typename AdjTile>
1700
+ inline CUDA_CALLABLE void adj_tile_store(array_t<T>& dest, int x, int y, Tile& t, array_t<T>& adj_dest, int adj_x, int adj_y, AdjTile& adj_t) { adj_tile_store(dest, tile_coord(x, y), t, adj_dest, tile_coord(0,0), adj_t); }
1701
+ template <typename T, typename Tile, typename AdjTile>
1702
+ inline CUDA_CALLABLE void adj_tile_store(array_t<T>& dest, int x, int y, int z, Tile& t, array_t<T>& adj_dest, int adj_x, int adj_y, int adj_z, AdjTile& adj_t) { adj_tile_store(dest, tile_coord(x, y, z), t, adj_dest, tile_coord(0,0,0), adj_t); }
1703
+ template <typename T, typename Tile, typename AdjTile>
1704
+ inline CUDA_CALLABLE void adj_tile_store(array_t<T>& dest, int x, int y, int z, int w, Tile& t, array_t<T>& adj_dest, int adj_x, int adj_y, int adj_z, int adj_w, AdjTile& adj_t) { adj_tile_store(dest, tile_coord(x, y, z, w), t, adj_dest, tile_coord(0,0,0,0), adj_t); }
1363
1705
 
1364
- if (adj_dest.data)
1365
- adj_reg.data[i] = index(adj_dest, tile_i + coord.i, tile_j + coord.j);
1366
- else if (dest.grad)
1367
- adj_reg.data[i] = index_grad(dest, tile_i + coord.i, tile_j + coord.j);
1368
- }
1369
1706
 
1370
- // store adjoint back to tile
1371
- adj_t.grad_add(adj_reg);
1372
- }
1373
1707
 
1708
+ // adj_tile_atomic_add is an alias for adj_tile_store
1374
1709
  template <typename T, typename Tile, typename AdjTile, typename AdjRet>
1375
- inline CUDA_CALLABLE void adj_tile_atomic_add(array_t<T>& dest, int x, int y, Tile& t, array_t<T>& adj_dest, int adj_x, int adj_y, AdjTile& adj_t, AdjRet& adj_ret)
1376
- {
1377
- adj_tile_store(dest, x, y, t, adj_dest, adj_x, adj_y, adj_t);
1378
- }
1710
+ inline CUDA_CALLABLE void adj_tile_atomic_add(array_t<T>& dest, int x, Tile& t, array_t<T>& adj_dest, int adj_x, AdjTile& adj_t, AdjRet& adj_ret) { adj_tile_store(dest, tile_coord(x), t, adj_dest, tile_coord(adj_x), adj_t); }
1711
+ template <typename T, typename Tile, typename AdjTile, typename AdjRet>
1712
+ inline CUDA_CALLABLE void adj_tile_atomic_add(array_t<T>& dest, int x, int y, Tile& t, array_t<T>& adj_dest, int adj_x, int adj_y, AdjTile& adj_t, AdjRet& adj_ret) { adj_tile_store(dest, tile_coord(x, y), t, adj_dest, tile_coord(adj_x, adj_y), adj_t); }
1713
+ template <typename T, typename Tile, typename AdjTile, typename AdjRet>
1714
+ inline CUDA_CALLABLE void adj_tile_atomic_add(array_t<T>& dest, int x, int y, int z, Tile& t, array_t<T>& adj_dest, int adj_x, int adj_y, int adj_z, AdjTile& adj_t, AdjRet& adj_ret) { adj_tile_store(dest, tile_coord(x, y, z), t, adj_dest, tile_coord(adj_x, adj_y, adj_z), adj_t); }
1715
+ template <typename T, typename Tile, typename AdjTile, typename AdjRet>
1716
+ inline CUDA_CALLABLE void adj_tile_atomic_add(array_t<T>& dest, int x, int y, int z, int w, Tile& t, array_t<T>& adj_dest, int adj_x, int adj_y, int adj_z, int adj_w, AdjTile& adj_t, AdjRet& adj_ret) { adj_tile_store(dest, tile_coord(x, y, z, w), t, adj_dest, tile_coord(adj_x, adj_y, adj_z, adj_w), adj_t); }
1379
1717
 
1380
1718
 
1381
1719
  // unary map
@@ -1383,11 +1721,13 @@ template <typename Tile, typename Fwd>
1383
1721
  inline CUDA_CALLABLE auto tile_map(Fwd op,
1384
1722
  Tile &a)
1385
1723
  {
1386
- auto out = tile_register_t<typename Tile::Type, Tile::M, Tile::N>();
1724
+ auto out = tile_register_like<Tile>();
1387
1725
  auto a_reg = a.copy_to_register();
1726
+
1727
+ using Layout = typename decltype(out)::Layout;
1388
1728
 
1389
1729
  WP_PRAGMA_UNROLL
1390
- for (int i=0; i < out.NumRegs; ++i)
1730
+ for (int i=0; i < Layout::NumRegs; ++i)
1391
1731
  {
1392
1732
  out.data[i] = op(a_reg.data[i]);
1393
1733
  }
@@ -1407,8 +1747,10 @@ inline CUDA_CALLABLE void adj_tile_map(Fwd op,
1407
1747
  auto adj_a_reg = tile_register_like<Tile>();
1408
1748
  auto adj_ret_reg = adj_ret.grad_to_register();
1409
1749
 
1750
+ using Layout = typename decltype(a_reg)::Layout;
1751
+
1410
1752
  WP_PRAGMA_UNROLL
1411
- for (int i=0; i < a_reg.NumRegs; ++i)
1753
+ for (int i=0; i < Layout::NumRegs; ++i)
1412
1754
  {
1413
1755
  adj_op(a_reg.data[i], adj_a_reg.data[i], adj_ret_reg.data[i]);
1414
1756
  }
@@ -1423,14 +1765,18 @@ inline CUDA_CALLABLE auto tile_map(Fwd op,
1423
1765
  TileA& a,
1424
1766
  TileB& b)
1425
1767
  {
1426
- auto out = tile_register_t<typename TileA::Type, TileA::M, TileA::N>();
1768
+ auto out = tile_register_like<TileA>();
1427
1769
 
1428
1770
  auto a_reg = a.copy_to_register();
1429
1771
  auto b_reg = b.copy_to_register();
1430
1772
 
1773
+ using Layout = typename decltype(out)::Layout;
1774
+
1431
1775
  WP_PRAGMA_UNROLL
1432
- for (int i=0; i < out.NumRegs; ++i)
1776
+ for (int i=0; i < Layout::NumRegs; ++i)
1777
+ {
1433
1778
  out.data[i] = op(a_reg.data[i], b_reg.data[i]);
1779
+ }
1434
1780
 
1435
1781
  return out;
1436
1782
  }
@@ -1454,8 +1800,10 @@ inline CUDA_CALLABLE void adj_tile_map(Fwd op,
1454
1800
 
1455
1801
  auto adj_ret_reg = adj_ret.grad_to_register();
1456
1802
 
1803
+ using Layout = typename decltype(a_reg)::Layout;
1804
+
1457
1805
  WP_PRAGMA_UNROLL
1458
- for (int i=0; i < a_reg.NumRegs; ++i)
1806
+ for (int i=0; i < Layout::NumRegs; ++i)
1459
1807
  {
1460
1808
  adj_op(a_reg.data[i], b_reg.data[i], adj_a_reg.data[i], adj_b_reg.data[i], adj_ret_reg.data[i]);
1461
1809
  }
@@ -1488,26 +1836,32 @@ inline CUDA_CALLABLE auto tile_add(TileA& a, TileB& b)
1488
1836
  return tile_binary_map(add, a, b);
1489
1837
  }
1490
1838
 
1491
- // // tile + tile, we implement this
1492
- // template <typename TileA, typename TileB>
1493
- // inline CUDA_CALLABLE auto add(TileA& a, TileB& b)
1494
- // {
1495
- // return tile_binary_map(add, a, b);
1496
- // }
1497
-
1498
-
1499
1839
  template <typename TileA, typename TileB, typename AdjTileA, typename AdjTileB, typename AdjTile>
1500
1840
  inline CUDA_CALLABLE void adj_tile_add(TileA& a, TileB& b, AdjTileA& adj_a, AdjTileB& adj_b, AdjTile& adj_c)
1501
1841
  {
1502
1842
  adj_tile_binary_map(add, a, b, adj_add, adj_a, adj_b, adj_c);
1503
1843
  }
1504
1844
 
1845
+ // tile - tile
1846
+ template <typename TileA, typename TileB>
1847
+ inline CUDA_CALLABLE auto tile_sub(TileA& a, TileB& b)
1848
+ {
1849
+ return tile_binary_map(sub, a, b);
1850
+ }
1851
+
1852
+ template <typename TileA, typename TileB, typename AdjTileA, typename AdjTileB, typename AdjTile>
1853
+ inline CUDA_CALLABLE void adj_tile_sub(TileA& a, TileB& b, AdjTileA& adj_a, AdjTileB& adj_b, AdjTile& adj_c)
1854
+ {
1855
+ adj_tile_binary_map(sub, a, b, adj_sub, adj_a, adj_b, adj_c);
1856
+ }
1857
+
1858
+
1505
1859
  // tile*scalar
1506
1860
  template <typename Tile>
1507
1861
  inline CUDA_CALLABLE auto tile_mul(Tile& a, const typename Tile::Type& s)
1508
1862
  {
1509
1863
  // promote scalar to a constant tile
1510
- auto s_tile = tile_register_t<typename Tile::Type, Tile::M, Tile::N>(s);
1864
+ auto s_tile = tile_register_t<typename Tile::Type, tile_layout_register_t<typename Tile::Layout::Shape>>(s);
1511
1865
 
1512
1866
  return tile_binary_map(mul, a, s_tile);
1513
1867
  }
@@ -1517,12 +1871,17 @@ inline CUDA_CALLABLE void adj_tile_mul(Tile& a, const typename Tile::Type& s,
1517
1871
  Tile& adj_a, typename Tile::Type& adj_s,
1518
1872
  AdjTile& adj_c)
1519
1873
  {
1520
- auto s_tile = tile_register_t<typename Tile::Type, Tile::M, Tile::N>(s);
1521
- auto adj_s_tile = tile_register_t<typename Tile::Type, Tile::M, Tile::N>();
1874
+ auto s_tile = tile_register_like<Tile>();
1875
+ auto adj_s_tile = tile_register_like<Tile>();
1876
+
1877
+ using Layout = typename decltype(adj_s_tile)::Layout;
1878
+
1879
+ // initialize to constant
1880
+ s_tile = s;
1522
1881
 
1523
1882
  adj_tile_binary_map(mul, a, s_tile, adj_mul, adj_a, adj_s_tile, adj_c);
1524
1883
 
1525
- for (int i=0; i < adj_s_tile.NumRegs; ++i)
1884
+ for (int i=0; i < Layout::NumRegs; ++i)
1526
1885
  {
1527
1886
  adj_s += adj_s_tile.data[i];
1528
1887
  }
@@ -1533,10 +1892,7 @@ inline CUDA_CALLABLE void adj_tile_mul(Tile& a, const typename Tile::Type& s,
1533
1892
  template <typename Tile>
1534
1893
  inline CUDA_CALLABLE auto tile_mul(const typename Tile::Type& s, Tile& a)
1535
1894
  {
1536
- // promote scalar to a constant tile
1537
- auto s_tile = tile_register_t<typename Tile::Type, Tile::M, Tile::N>(s);
1538
-
1539
- return tile_binary_map(mul, s_tile, a);
1895
+ return tile_mul(a, s);
1540
1896
  }
1541
1897
 
1542
1898
  template <typename Tile, typename AdjTile>
@@ -1544,36 +1900,30 @@ inline CUDA_CALLABLE void adj_tile_mul(const typename Tile::Type& s, Tile& a,
1544
1900
  typename Tile::Type& adj_s, Tile& adj_a,
1545
1901
  AdjTile& adj_c)
1546
1902
  {
1547
- auto s_tile = tile_register_t<typename Tile::Type, Tile::M, Tile::N>(s);
1548
- auto adj_s_tile = tile_register_t<typename Tile::Type, Tile::M, Tile::N>();
1549
-
1550
- adj_tile_binary_map(mul, s_tile, a, adj_mul, adj_s_tile, adj_a, adj_c);
1551
-
1552
- for (int i=0; i < adj_s_tile.NumRegs; ++i)
1553
- {
1554
- adj_s += adj_s_tile.data[i];
1555
- }
1903
+ adj_tile_mul(a, s, adj_a, adj_s, adj_c);
1556
1904
  }
1557
1905
 
1558
1906
 
1559
-
1560
1907
  template<typename Tile>
1561
- typename Tile::Type tile_extract(Tile& t, int i, int j)
1562
- {
1563
- assert(i < Tile::M);
1564
- assert(j < Tile::N);
1908
+ typename Tile::Type tile_extract(Tile& t, int i) { return t.extract(tile_coord(i)); }
1909
+ template<typename Tile>
1910
+ typename Tile::Type tile_extract(Tile& t, int i, int j) { return t.extract(tile_coord(i,j)); }
1911
+ template<typename Tile>
1912
+ typename Tile::Type tile_extract(Tile& t, int i, int j, int k) { return t.extract(tile_coord(i,j,k)); }
1913
+ template<typename Tile>
1914
+ typename Tile::Type tile_extract(Tile& t, int i, int j, int k, int l) { return t.extract(tile_coord(i,j,k,l)); }
1565
1915
 
1566
- return t.extract(i, j);
1567
- }
1568
1916
 
1569
1917
  template<typename Tile, typename AdjTile>
1570
- void adj_tile_extract(Tile& t, int i, int j, AdjTile& adj_t, int adj_i, int adj_j, typename Tile::Type adj_ret)
1571
- {
1572
- assert(i < Tile::M);
1573
- assert(j < Tile::N);
1918
+ void adj_tile_extract(Tile& t, int i, AdjTile& adj_t, int adj_i, typename Tile::Type adj_ret) { adj_t.adj_extract(tile_coord(i), adj_ret); }
1919
+ template<typename Tile, typename AdjTile>
1920
+ void adj_tile_extract(Tile& t, int i, int j, AdjTile& adj_t, int adj_i, int adj_j, typename Tile::Type adj_ret) { adj_t.adj_extract(tile_coord(i, j), adj_ret); }
1921
+ template<typename Tile, typename AdjTile>
1922
+ void adj_tile_extract(Tile& t, int i, int j, int k, AdjTile& adj_t, int adj_i, int adj_j, int adj_k, typename Tile::Type adj_ret) { adj_t.adj_extract(tile_coord(i, j, k), adj_ret); }
1923
+ template<typename Tile, typename AdjTile>
1924
+ void adj_tile_extract(Tile& t, int i, int j, int k, int l, AdjTile& adj_t, int adj_i, int adj_j, int adj_k, int adj_l, typename Tile::Type adj_ret) { adj_t.adj_extract(tile_coord(i, j, k, l), adj_ret); }
1574
1925
 
1575
- adj_t.adj_extract(i, j, adj_ret);
1576
- }
1926
+ #if WP_USE_REGISTER_GEMM
1577
1927
 
1578
1928
  namespace partitioned_gemm
1579
1929
  {
@@ -1595,7 +1945,7 @@ struct partition_t
1595
1945
  {
1596
1946
  static constexpr int M = PartitionM;
1597
1947
  static constexpr int N = PartitionN;
1598
- static constexpr int Stride = Tile::N;
1948
+ static constexpr int Stride = Tile::Layout::Shape::dim(1);
1599
1949
 
1600
1950
  using T = typename Tile::Type;
1601
1951
 
@@ -1604,8 +1954,8 @@ struct partition_t
1604
1954
  data = A.data.ptr;
1605
1955
 
1606
1956
  // todo: do ceil div for non-multiples of M,N
1607
- shape[0] = Tile::M/PartitionM;
1608
- shape[1] = Tile::N/PartitionN;
1957
+ shape[0] = Tile::Layout::Shape::dim(0)/PartitionM;
1958
+ shape[1] = Tile::Layout::Shape::dim(1)/PartitionN;
1609
1959
  }
1610
1960
 
1611
1961
  // underlying data
@@ -1643,7 +1993,7 @@ inline auto partition_load(const Partition& tile, int i, int j)
1643
1993
  WP_PRAGMA_UNROLL
1644
1994
  for (int j=0; j < Partition::N; ++j)
1645
1995
  {
1646
- out.data[i][j] = index(tile.data, tile_i + i, tile_j + j, Partition::Stride);
1996
+ out.data[i][j] = partitioned_gemm::index(tile.data, tile_i + i, tile_j + j, Partition::Stride);
1647
1997
  }
1648
1998
  }
1649
1999
 
@@ -1667,6 +2017,7 @@ inline void partition_store(const Partition& tile, int i, int j, const Value& va
1667
2017
  }
1668
2018
  }
1669
2019
 
2020
+
1670
2021
  template <typename TileA, typename TileB, typename TileC>
1671
2022
  inline CUDA_CALLABLE void matmul(TileA& A, TileB& B, TileC& out)
1672
2023
  {
@@ -1703,15 +2054,26 @@ inline CUDA_CALLABLE void matmul(TileA& A, TileB& B, TileC& out)
1703
2054
 
1704
2055
  } // namespace partition_gemm
1705
2056
 
2057
+ #endif // WP_USE_REGISTER_GEMM
2058
+
2059
+
1706
2060
  template <int Add, typename Fwd, typename AdjA, typename AdjB, typename TileA, typename TileB, typename TileC>
1707
2061
  TileC& tile_matmul(Fwd fun_forward, AdjA fun_backward_A, AdjB fun_backward_B, TileA& A, TileB& B, TileC& C)
1708
2062
  {
1709
- using T = typename TileA::Type;
2063
+ using ShapeA = typename TileA::Layout::Shape;
2064
+ using ShapeB = typename TileB::Layout::Shape;
2065
+ using ShapeC = typename TileC::Layout::Shape;
1710
2066
 
1711
- #if WP_USE_ASYNC_PIPELINE
1712
- __pipeline_wait_prior(0);
1713
- WP_TILE_SYNC();
1714
- #endif
2067
+ static_assert(ShapeA::N == 2);
2068
+ static_assert(ShapeB::N == 2);
2069
+ static_assert(ShapeC::N == 2);
2070
+
2071
+ static_assert(ShapeA::dim(1) == ShapeB::dim(0));
2072
+ static_assert(ShapeC::dim(0) == ShapeA::dim(0));
2073
+ static_assert(ShapeC::dim(1) == ShapeB::dim(1));
2074
+
2075
+
2076
+ using T = typename TileA::Type;
1715
2077
 
1716
2078
  #if WP_USE_REGISTER_GEMM
1717
2079
  partitioned_gemm::matmul(A, B, C);
@@ -1749,11 +2111,11 @@ void adj_tile_matmul(Fwd fun_forward, AdjA fun_backward_A, AdjB fun_backward_B,
1749
2111
  }
1750
2112
 
1751
2113
  // TODO(lcambier): use a properly overaligned complex type that matches cuFFTDx's expectation
1752
- // TODO(lcambier): use dynamic smem
2114
+ // and remove the need for __align__(16) dtypes data[...]
1753
2115
  #define tile_fft(function_name, dtype, shared_memory_size, batch_size, ept, Xinout) \
1754
2116
  do { \
1755
2117
  void function_name(dtype*, dtype*); \
1756
- WP_TILE_SHARED __align__(16) char buffer[shared_memory_size]; \
2118
+ char* buffer = (char*)wp::tile_alloc_shared(shared_memory_size); \
1757
2119
  __align__(16) dtype data[ept]; \
1758
2120
  for(int b = 0; b < (int)batch_size; b++) { \
1759
2121
  dtype* inout = Xinout.data + (int)b * (int)ept; \
@@ -1762,6 +2124,7 @@ void adj_tile_matmul(Fwd fun_forward, AdjA fun_backward_A, AdjB fun_backward_B,
1762
2124
  memcpy(inout, data, sizeof(dtype) * ept); \
1763
2125
  WP_TILE_SYNC(); \
1764
2126
  } \
2127
+ wp::tile_alloc_shared(-shared_memory_size); \
1765
2128
  } while (0)
1766
2129
 
1767
2130
  #define tile_ifft tile_fft
@@ -1782,12 +2145,78 @@ void adj_tile_matmul(Fwd fun_forward, AdjA fun_backward_A, AdjB fun_backward_B,
1782
2145
  tile_fft(function_name, dtype, shared_memory_size, batch_size, ept, adj_Xinout); \
1783
2146
  } while (0)
1784
2147
 
2148
+ template <typename Fwd, typename TileA, typename TileL>
2149
+ TileL& tile_cholesky(Fwd fun_forward, TileA& A, TileL& L)
2150
+ {
2151
+ // Copy to L
2152
+ L = A;
2153
+
2154
+ // Call cholesky on L
2155
+ WP_TILE_SYNC();
2156
+
2157
+ fun_forward(L.data.ptr, TileL::Layout::Shape::dim(0));
2158
+
2159
+ WP_TILE_SYNC();
2160
+
2161
+ // Zero-out the upper triangular part of L
2162
+
2163
+ WP_PRAGMA_UNROLL
2164
+ for (int i=threadIdx.x; i < TileL::Layout::Size; i += WP_TILE_BLOCK_DIM)
2165
+ {
2166
+ auto c = TileL::Layout::coord_from_linear(i);
2167
+
2168
+ if(c[0] < c[1])
2169
+ L.data(c) = 0.0;
2170
+ }
2171
+
2172
+ WP_TILE_SYNC();
2173
+
2174
+ return L;
2175
+ }
2176
+
2177
+ #define adj_tile_cholesky(function_name, A, L, \
2178
+ adj_function_name, adj_A, adj_L, adj_ret) \
2179
+ do { \
2180
+ assert(false); \
2181
+ } while (0)
2182
+
2183
+ template <typename Fwd, typename TileL, typename TileX, typename TileY>
2184
+ TileY& tile_cholesky_solve(Fwd fun_forward, TileL& L, TileX& X, TileY& Y)
2185
+ {
2186
+ // Copy x to y
2187
+
2188
+ Y = X;
2189
+
2190
+ // Call cholesky solve on L & y
2191
+
2192
+ WP_TILE_SYNC();
2193
+
2194
+ fun_forward(L.data.ptr, Y.data.ptr); \
2195
+
2196
+ WP_TILE_SYNC();
2197
+
2198
+ return Y;
2199
+ }
2200
+
2201
+ #define adj_tile_cholesky_solve(function_name, L, X, Y, \
2202
+ adj_function_name, adj_L, adj_X, adj_Y, adj_ret) \
2203
+ do { \
2204
+ assert(false); \
2205
+ } while (0)
1785
2206
 
1786
2207
  template <typename Tile>
1787
2208
  inline CUDA_CALLABLE auto tile_transpose(Tile& t)
1788
2209
  {
2210
+ static_assert(Tile::Layout::Shape::N == 2);
2211
+
1789
2212
  // alias incoming tile
1790
- return tile_shared_t<typename Tile::Type, Tile::N, Tile::M, Tile::StrideN, Tile::StrideM, false>(t.data.ptr, t.grad.ptr);
2213
+ constexpr int M = Tile::Layout::Shape::dim(0);
2214
+ constexpr int N = Tile::Layout::Shape::dim(1);
2215
+
2216
+ constexpr int StrideM = Tile::Layout::Stride::dim(0);
2217
+ constexpr int StrideN = Tile::Layout::Stride::dim(1);
2218
+
2219
+ return tile_shared_t<typename Tile::Type, tile_layout_strided_t<tile_shape_t<N,M>, tile_stride_t<StrideN, StrideM>>, false>(t.data.ptr, t.grad.ptr);
1791
2220
  }
1792
2221
 
1793
2222
  template <typename Tile, typename AdjTile>
@@ -1803,55 +2232,144 @@ template <int M, int N, int StrideM, int StrideN, typename Tile>
1803
2232
  inline CUDA_CALLABLE auto tile_broadcast(Tile& t)
1804
2233
  {
1805
2234
  // alias incoming tile with new strides
1806
- return tile_shared_t<typename Tile::Type, M, N, StrideM, StrideN, false>(t.data.ptr, t.grad.ptr);
2235
+ return tile_shared_t<typename Tile::Type, tile_layout_strided_t<tile_shape_t<M, N>, tile_stride_t<StrideM, StrideN>>, false>(t.data.ptr, t.grad.ptr);
1807
2236
  }
1808
2237
 
1809
2238
  template <typename Tile, typename AdjTile>
1810
2239
  inline CUDA_CALLABLE void adj_tile_broadcast(Tile& t, Tile& adj_t, AdjTile& adj_ret)
1811
2240
  {
1812
2241
  // nop, since memory is aliased grads already accumulated
2242
+ }
2243
+
2244
+ template <typename ReturnType, typename Tile, typename... Indices>
2245
+ inline CUDA_CALLABLE auto tile_view(Tile& t, Indices... indices)
2246
+ {
2247
+ auto c = tile_coord(indices...);
2248
+
2249
+ // return new tile with same strides
2250
+ typename Tile::Type* data_ptr = &t.data(c);
2251
+ typename Tile::Type* grad_ptr = NULL;
2252
+
2253
+ if (t.grad.ptr)
2254
+ grad_ptr = &t.grad(c);
1813
2255
 
2256
+ return ReturnType(data_ptr, grad_ptr);
1814
2257
  }
1815
2258
 
1816
- template <int M, int N, typename Tile>
1817
- inline CUDA_CALLABLE auto tile_view(Tile& t, int i, int j)
1818
- {
1819
- // alias incoming tile with new strides
1820
- return tile_shared_t<typename Tile::Type, M, N, Tile::StrideM, Tile::StrideN, false>(&t.data(i, j), &t.grad(i, j));
2259
+
2260
+ template <typename TileA, typename Scalar>
2261
+ inline CUDA_CALLABLE void assign(TileA& dest, int i, const Scalar& src)
2262
+ {
2263
+ dest.data(tile_coord(i)) = src;
2264
+ WP_TILE_SYNC();
1821
2265
  }
1822
2266
 
1823
- template <typename Tile, typename AdjTile>
1824
- inline CUDA_CALLABLE void adj_tile_view(Tile& t, int i, int j, Tile& adj_t, int adj_i, int adj_j, AdjTile& adj_ret)
2267
+ template <typename TileA, typename Scalar>
2268
+ inline CUDA_CALLABLE void assign(TileA& dest, int i, int j, const Scalar& src)
1825
2269
  {
1826
- // nop, since memory is aliased grads already accumulated
2270
+ dest.data(tile_coord(i, j)) = src;
2271
+ WP_TILE_SYNC();
2272
+ }
1827
2273
 
2274
+ template <typename TileA, typename Scalar>
2275
+ inline CUDA_CALLABLE void assign(TileA& dest, int i, int j, int k, const Scalar& src)
2276
+ {
2277
+ dest.data(tile_coord(i, j, k)) = src;
2278
+ WP_TILE_SYNC();
1828
2279
  }
1829
2280
 
1830
- template <typename TileA, typename TileB>
1831
- inline CUDA_CALLABLE void tile_assign(TileA& dest, int i, int j, TileB& src)
2281
+ template <typename TileA, typename Scalar>
2282
+ inline CUDA_CALLABLE void assign(TileA& dest, int i, int j, int k, int l, const Scalar& src)
2283
+ {
2284
+ dest.data(tile_coord(i, j, k, l)) = src;
2285
+ WP_TILE_SYNC();
2286
+ }
2287
+
2288
+
2289
+
2290
+
2291
+ template <typename TileA, typename TileB, typename Coord>
2292
+ inline CUDA_CALLABLE void tile_assign(TileA& dest, TileB& src, const Coord& offset)
1832
2293
  {
1833
- for (int t=threadIdx.x; t < src.Size; t += WP_TILE_BLOCK_DIM)
2294
+ using Layout = typename TileB::Layout;
2295
+
2296
+ for (int t=threadIdx.x; t < Layout::Size; t += WP_TILE_BLOCK_DIM)
1834
2297
  {
1835
- coord_t c = src.coord(t);
1836
- dest.data(i + c.i, j + c.j) = src.data(c.i, c.j);
2298
+ auto c = Layout::coord_from_linear(t);
2299
+ dest.data(c + offset) = src.data(c);
1837
2300
  }
1838
2301
 
1839
2302
  WP_TILE_SYNC();
1840
2303
  }
1841
2304
 
1842
- template <typename TileA, typename TileB, typename AdjTileA, typename AdjTileB>
1843
- inline CUDA_CALLABLE void adj_tile_assign(TileA& dest, int i, int j, TileB& src,
1844
- AdjTileA& adj_dest, int adj_i, int adj_j, AdjTileB& adj_src)
2305
+ template <typename TileA, typename TileB, typename AdjTileA, typename AdjTileB, typename Coord, typename AdjCoord>
2306
+ inline CUDA_CALLABLE void adj_tile_assign(TileA& dest, TileB& src, Coord offset,
2307
+ AdjTileA& adj_dest, AdjTileB& adj_src, AdjCoord adj_offset)
1845
2308
  {
1846
- for (int t=threadIdx.x; t < src.Size; t += WP_TILE_BLOCK_DIM)
2309
+ using Layout = typename TileB::Layout;
2310
+
2311
+ for (int t=threadIdx.x; t < Layout::Size; t += WP_TILE_BLOCK_DIM)
1847
2312
  {
1848
- coord_t c = src.coord(t);
1849
- src.grad(c.i, c.j) += dest.grad(i + c.i, j + c.j);
2313
+ auto c = Layout::coord_from_linear(t);
2314
+ src.grad(c) += dest.grad(c + offset);
1850
2315
  }
1851
2316
 
1852
2317
  WP_TILE_SYNC();
1853
2318
  }
1854
2319
 
1855
2320
 
2321
+ // codegen entry points, which emit calls like `tile_assign(dest, src, i, j, k)`
2322
+ // a better approach here would be for codegen to just directly generate `tile_assign(dest, src, tile_coord(i, j, k))`
2323
+ // i.e.: call the above implementation methods directly, then we could remove these overloads
2324
+ template <typename TileA, typename TileB>
2325
+ inline CUDA_CALLABLE void tile_assign(TileA& dest, TileB& src, int i) { tile_assign(dest, src, tile_coord(i)); }
2326
+ template <typename TileA, typename TileB>
2327
+ inline CUDA_CALLABLE void tile_assign(TileA& dest, TileB& src, int i, int j) { tile_assign(dest, src, tile_coord(i, j)); }
2328
+ template <typename TileA, typename TileB>
2329
+ inline CUDA_CALLABLE void tile_assign(TileA& dest, TileB& src, int i, int j, int k) { tile_assign(dest, src, tile_coord(i, j, k)); }
2330
+ template <typename TileA, typename TileB>
2331
+ inline CUDA_CALLABLE void tile_assign(TileA& dest, TileB& src, int i, int j, int k, int l) { tile_assign(dest, src, tile_coord(i, j, k, l)); }
2332
+
2333
+ template <typename TileA, typename TileB, typename AdjTileA, typename AdjTileB>
2334
+ inline CUDA_CALLABLE void adj_tile_assign(TileA& dest, TileB& src, int i, AdjTileA& adj_dest, AdjTileB& adj_src, int) { adj_tile_assign(dest, src, tile_coord(i), adj_dest, adj_src, tile_coord(0)); }
2335
+ template <typename TileA, typename TileB, typename AdjTileA, typename AdjTileB>
2336
+ inline CUDA_CALLABLE void adj_tile_assign(TileA& dest, TileB& src, int i, int j, AdjTileA& adj_dest, AdjTileB& adj_src, int, int) { adj_tile_assign(dest, src, tile_coord(i,j), adj_dest, adj_src, tile_coord(0)); }
2337
+ template <typename TileA, typename TileB, typename AdjTileA, typename AdjTileB>
2338
+ inline CUDA_CALLABLE void adj_tile_assign(TileA& dest, TileB& src, int i, int j, int k, AdjTileA& adj_dest, AdjTileB& adj_src, int, int, int) { adj_tile_assign(dest, src, tile_coord(i,j,k), adj_dest, adj_src, tile_coord(0)); }
2339
+ template <typename TileA, typename TileB, typename AdjTileA, typename AdjTileB>
2340
+ inline CUDA_CALLABLE void adj_tile_assign(TileA& dest, TileB& src, int i, int j, int k, int l, AdjTileA& adj_dest, AdjTileB& adj_src, int, int, int, int) { adj_tile_assign(dest, src, tile_coord(i,j,k,l), adj_dest, adj_src, tile_coord(0)); }
2341
+
2342
+
2343
+ template <typename TileA, typename TileB, typename TileC>
2344
+ inline CUDA_CALLABLE TileC& tile_diag_add(TileA& a, TileB& b, TileC& c)
2345
+ {
2346
+ using ShapeA = typename TileA::Layout::Shape;
2347
+ using ShapeB = typename TileB::Layout::Shape;
2348
+ using ShapeC = typename TileC::Layout::Shape;
2349
+
2350
+ static_assert(ShapeA::dim(0) == ShapeA::dim(1));
2351
+ static_assert(ShapeB::dim(0) == ShapeA::dim(0));
2352
+ static_assert(ShapeC::dim(0) == ShapeA::dim(0));
2353
+ static_assert(ShapeC::dim(0) == ShapeC::dim(1));
2354
+
2355
+ c = a;
2356
+
2357
+ for (int t=threadIdx.x; t < ShapeA::dim(0); t += WP_TILE_BLOCK_DIM)
2358
+ {
2359
+ c.data(tile_coord(t, t)) += b.data(tile_coord(t));
2360
+ }
2361
+
2362
+ WP_TILE_SYNC();
2363
+
2364
+ return c;
2365
+ }
2366
+
2367
+ template <typename TileA, typename TileB, typename TileC, typename AdjTileA, typename AdjTileB, typename AdjTileC>
2368
+ inline CUDA_CALLABLE void adj_tile_diag_add(TileA& a, TileB& b, TileC& c, AdjTileA& adj_a, AdjTileB& adj_b, AdjTileC& adj_c, AdjTileC& adj_ret)
2369
+ {
2370
+ assert(false);
2371
+ }
2372
+
1856
2373
 
1857
2374
  } // namespace wp
2375
+