warp-lang 1.0.1__py3-none-manylinux2014_x86_64.whl → 1.1.0__py3-none-manylinux2014_x86_64.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 (346) hide show
  1. warp/__init__.py +108 -97
  2. warp/__init__.pyi +1 -1
  3. warp/bin/warp-clang.so +0 -0
  4. warp/bin/warp.so +0 -0
  5. warp/build.py +115 -113
  6. warp/build_dll.py +383 -375
  7. warp/builtins.py +3425 -3354
  8. warp/codegen.py +2878 -2792
  9. warp/config.py +40 -36
  10. warp/constants.py +45 -45
  11. warp/context.py +5194 -5102
  12. warp/dlpack.py +442 -442
  13. warp/examples/__init__.py +16 -16
  14. warp/examples/assets/bear.usd +0 -0
  15. warp/examples/assets/bunny.usd +0 -0
  16. warp/examples/assets/cartpole.urdf +110 -110
  17. warp/examples/assets/crazyflie.usd +0 -0
  18. warp/examples/assets/cube.usd +0 -0
  19. warp/examples/assets/nv_ant.xml +92 -92
  20. warp/examples/assets/nv_humanoid.xml +183 -183
  21. warp/examples/assets/quadruped.urdf +267 -267
  22. warp/examples/assets/rocks.nvdb +0 -0
  23. warp/examples/assets/rocks.usd +0 -0
  24. warp/examples/assets/sphere.usd +0 -0
  25. warp/examples/benchmarks/benchmark_api.py +383 -383
  26. warp/examples/benchmarks/benchmark_cloth.py +278 -279
  27. warp/examples/benchmarks/benchmark_cloth_cupy.py +88 -88
  28. warp/examples/benchmarks/benchmark_cloth_jax.py +97 -100
  29. warp/examples/benchmarks/benchmark_cloth_numba.py +146 -142
  30. warp/examples/benchmarks/benchmark_cloth_numpy.py +77 -77
  31. warp/examples/benchmarks/benchmark_cloth_pytorch.py +86 -86
  32. warp/examples/benchmarks/benchmark_cloth_taichi.py +112 -112
  33. warp/examples/benchmarks/benchmark_cloth_warp.py +146 -146
  34. warp/examples/benchmarks/benchmark_launches.py +295 -295
  35. warp/examples/browse.py +29 -28
  36. warp/examples/core/example_dem.py +234 -221
  37. warp/examples/core/example_fluid.py +293 -267
  38. warp/examples/core/example_graph_capture.py +144 -129
  39. warp/examples/core/example_marching_cubes.py +188 -176
  40. warp/examples/core/example_mesh.py +174 -154
  41. warp/examples/core/example_mesh_intersect.py +205 -193
  42. warp/examples/core/example_nvdb.py +176 -169
  43. warp/examples/core/example_raycast.py +105 -89
  44. warp/examples/core/example_raymarch.py +199 -178
  45. warp/examples/core/example_render_opengl.py +185 -141
  46. warp/examples/core/example_sph.py +405 -389
  47. warp/examples/core/example_torch.py +222 -181
  48. warp/examples/core/example_wave.py +263 -249
  49. warp/examples/fem/bsr_utils.py +378 -380
  50. warp/examples/fem/example_apic_fluid.py +407 -391
  51. warp/examples/fem/example_convection_diffusion.py +182 -168
  52. warp/examples/fem/example_convection_diffusion_dg.py +219 -209
  53. warp/examples/fem/example_convection_diffusion_dg0.py +204 -194
  54. warp/examples/fem/example_deformed_geometry.py +177 -159
  55. warp/examples/fem/example_diffusion.py +201 -173
  56. warp/examples/fem/example_diffusion_3d.py +177 -152
  57. warp/examples/fem/example_diffusion_mgpu.py +221 -214
  58. warp/examples/fem/example_mixed_elasticity.py +244 -222
  59. warp/examples/fem/example_navier_stokes.py +259 -243
  60. warp/examples/fem/example_stokes.py +220 -192
  61. warp/examples/fem/example_stokes_transfer.py +265 -249
  62. warp/examples/fem/mesh_utils.py +133 -109
  63. warp/examples/fem/plot_utils.py +292 -287
  64. warp/examples/optim/example_bounce.py +260 -248
  65. warp/examples/optim/example_cloth_throw.py +222 -210
  66. warp/examples/optim/example_diffray.py +566 -535
  67. warp/examples/optim/example_drone.py +864 -835
  68. warp/examples/optim/example_inverse_kinematics.py +176 -169
  69. warp/examples/optim/example_inverse_kinematics_torch.py +185 -170
  70. warp/examples/optim/example_spring_cage.py +239 -234
  71. warp/examples/optim/example_trajectory.py +223 -201
  72. warp/examples/optim/example_walker.py +306 -292
  73. warp/examples/sim/example_cartpole.py +139 -128
  74. warp/examples/sim/example_cloth.py +196 -184
  75. warp/examples/sim/example_granular.py +124 -113
  76. warp/examples/sim/example_granular_collision_sdf.py +197 -185
  77. warp/examples/sim/example_jacobian_ik.py +236 -213
  78. warp/examples/sim/example_particle_chain.py +118 -106
  79. warp/examples/sim/example_quadruped.py +193 -179
  80. warp/examples/sim/example_rigid_chain.py +197 -189
  81. warp/examples/sim/example_rigid_contact.py +189 -176
  82. warp/examples/sim/example_rigid_force.py +127 -126
  83. warp/examples/sim/example_rigid_gyroscopic.py +109 -97
  84. warp/examples/sim/example_rigid_soft_contact.py +134 -124
  85. warp/examples/sim/example_soft_body.py +190 -178
  86. warp/fabric.py +337 -335
  87. warp/fem/__init__.py +60 -27
  88. warp/fem/cache.py +401 -388
  89. warp/fem/dirichlet.py +178 -179
  90. warp/fem/domain.py +262 -263
  91. warp/fem/field/__init__.py +100 -101
  92. warp/fem/field/field.py +148 -149
  93. warp/fem/field/nodal_field.py +298 -299
  94. warp/fem/field/restriction.py +22 -21
  95. warp/fem/field/test.py +180 -181
  96. warp/fem/field/trial.py +183 -183
  97. warp/fem/geometry/__init__.py +15 -19
  98. warp/fem/geometry/closest_point.py +69 -70
  99. warp/fem/geometry/deformed_geometry.py +270 -271
  100. warp/fem/geometry/element.py +744 -744
  101. warp/fem/geometry/geometry.py +184 -186
  102. warp/fem/geometry/grid_2d.py +380 -373
  103. warp/fem/geometry/grid_3d.py +441 -435
  104. warp/fem/geometry/hexmesh.py +953 -953
  105. warp/fem/geometry/partition.py +374 -376
  106. warp/fem/geometry/quadmesh_2d.py +532 -532
  107. warp/fem/geometry/tetmesh.py +840 -840
  108. warp/fem/geometry/trimesh_2d.py +577 -577
  109. warp/fem/integrate.py +1630 -1615
  110. warp/fem/operator.py +190 -191
  111. warp/fem/polynomial.py +214 -213
  112. warp/fem/quadrature/__init__.py +2 -2
  113. warp/fem/quadrature/pic_quadrature.py +243 -245
  114. warp/fem/quadrature/quadrature.py +295 -294
  115. warp/fem/space/__init__.py +294 -292
  116. warp/fem/space/basis_space.py +488 -489
  117. warp/fem/space/collocated_function_space.py +100 -105
  118. warp/fem/space/dof_mapper.py +236 -236
  119. warp/fem/space/function_space.py +148 -145
  120. warp/fem/space/grid_2d_function_space.py +267 -267
  121. warp/fem/space/grid_3d_function_space.py +305 -306
  122. warp/fem/space/hexmesh_function_space.py +350 -352
  123. warp/fem/space/partition.py +350 -350
  124. warp/fem/space/quadmesh_2d_function_space.py +368 -369
  125. warp/fem/space/restriction.py +158 -160
  126. warp/fem/space/shape/__init__.py +13 -15
  127. warp/fem/space/shape/cube_shape_function.py +738 -738
  128. warp/fem/space/shape/shape_function.py +102 -103
  129. warp/fem/space/shape/square_shape_function.py +611 -611
  130. warp/fem/space/shape/tet_shape_function.py +565 -567
  131. warp/fem/space/shape/triangle_shape_function.py +429 -429
  132. warp/fem/space/tetmesh_function_space.py +294 -292
  133. warp/fem/space/topology.py +297 -295
  134. warp/fem/space/trimesh_2d_function_space.py +223 -221
  135. warp/fem/types.py +77 -77
  136. warp/fem/utils.py +495 -495
  137. warp/jax.py +166 -141
  138. warp/jax_experimental.py +341 -339
  139. warp/native/array.h +1072 -1025
  140. warp/native/builtin.h +1560 -1560
  141. warp/native/bvh.cpp +398 -398
  142. warp/native/bvh.cu +525 -525
  143. warp/native/bvh.h +429 -429
  144. warp/native/clang/clang.cpp +495 -464
  145. warp/native/crt.cpp +31 -31
  146. warp/native/crt.h +334 -334
  147. warp/native/cuda_crt.h +1049 -1049
  148. warp/native/cuda_util.cpp +549 -540
  149. warp/native/cuda_util.h +288 -203
  150. warp/native/cutlass_gemm.cpp +34 -34
  151. warp/native/cutlass_gemm.cu +372 -372
  152. warp/native/error.cpp +66 -66
  153. warp/native/error.h +27 -27
  154. warp/native/fabric.h +228 -228
  155. warp/native/hashgrid.cpp +301 -278
  156. warp/native/hashgrid.cu +78 -77
  157. warp/native/hashgrid.h +227 -227
  158. warp/native/initializer_array.h +32 -32
  159. warp/native/intersect.h +1204 -1204
  160. warp/native/intersect_adj.h +365 -365
  161. warp/native/intersect_tri.h +322 -322
  162. warp/native/marching.cpp +2 -2
  163. warp/native/marching.cu +497 -497
  164. warp/native/marching.h +2 -2
  165. warp/native/mat.h +1498 -1498
  166. warp/native/matnn.h +333 -333
  167. warp/native/mesh.cpp +203 -203
  168. warp/native/mesh.cu +293 -293
  169. warp/native/mesh.h +1887 -1887
  170. warp/native/nanovdb/NanoVDB.h +4782 -4782
  171. warp/native/nanovdb/PNanoVDB.h +2553 -2553
  172. warp/native/nanovdb/PNanoVDBWrite.h +294 -294
  173. warp/native/noise.h +850 -850
  174. warp/native/quat.h +1084 -1084
  175. warp/native/rand.h +299 -299
  176. warp/native/range.h +108 -108
  177. warp/native/reduce.cpp +156 -156
  178. warp/native/reduce.cu +348 -348
  179. warp/native/runlength_encode.cpp +61 -61
  180. warp/native/runlength_encode.cu +46 -46
  181. warp/native/scan.cpp +30 -30
  182. warp/native/scan.cu +36 -36
  183. warp/native/scan.h +7 -7
  184. warp/native/solid_angle.h +442 -442
  185. warp/native/sort.cpp +94 -94
  186. warp/native/sort.cu +97 -97
  187. warp/native/sort.h +14 -14
  188. warp/native/sparse.cpp +337 -337
  189. warp/native/sparse.cu +544 -544
  190. warp/native/spatial.h +630 -630
  191. warp/native/svd.h +562 -562
  192. warp/native/temp_buffer.h +30 -30
  193. warp/native/vec.h +1132 -1132
  194. warp/native/volume.cpp +297 -297
  195. warp/native/volume.cu +32 -32
  196. warp/native/volume.h +538 -538
  197. warp/native/volume_builder.cu +425 -425
  198. warp/native/volume_builder.h +19 -19
  199. warp/native/warp.cpp +1057 -1052
  200. warp/native/warp.cu +2943 -2828
  201. warp/native/warp.h +313 -305
  202. warp/optim/__init__.py +9 -9
  203. warp/optim/adam.py +120 -120
  204. warp/optim/linear.py +1104 -939
  205. warp/optim/sgd.py +104 -92
  206. warp/render/__init__.py +10 -10
  207. warp/render/render_opengl.py +3217 -3204
  208. warp/render/render_usd.py +768 -749
  209. warp/render/utils.py +152 -150
  210. warp/sim/__init__.py +52 -59
  211. warp/sim/articulation.py +685 -685
  212. warp/sim/collide.py +1594 -1590
  213. warp/sim/import_mjcf.py +489 -481
  214. warp/sim/import_snu.py +220 -221
  215. warp/sim/import_urdf.py +536 -516
  216. warp/sim/import_usd.py +887 -881
  217. warp/sim/inertia.py +316 -317
  218. warp/sim/integrator.py +234 -233
  219. warp/sim/integrator_euler.py +1956 -1956
  220. warp/sim/integrator_featherstone.py +1910 -1991
  221. warp/sim/integrator_xpbd.py +3294 -3312
  222. warp/sim/model.py +4473 -4314
  223. warp/sim/particles.py +113 -112
  224. warp/sim/render.py +417 -403
  225. warp/sim/utils.py +413 -410
  226. warp/sparse.py +1227 -1227
  227. warp/stubs.py +2109 -2469
  228. warp/tape.py +1162 -225
  229. warp/tests/__init__.py +1 -1
  230. warp/tests/__main__.py +4 -4
  231. warp/tests/assets/torus.usda +105 -105
  232. warp/tests/aux_test_class_kernel.py +26 -26
  233. warp/tests/aux_test_compile_consts_dummy.py +10 -10
  234. warp/tests/aux_test_conditional_unequal_types_kernels.py +21 -21
  235. warp/tests/aux_test_dependent.py +22 -22
  236. warp/tests/aux_test_grad_customs.py +23 -23
  237. warp/tests/aux_test_reference.py +11 -11
  238. warp/tests/aux_test_reference_reference.py +10 -10
  239. warp/tests/aux_test_square.py +17 -17
  240. warp/tests/aux_test_unresolved_func.py +14 -14
  241. warp/tests/aux_test_unresolved_symbol.py +14 -14
  242. warp/tests/disabled_kinematics.py +239 -239
  243. warp/tests/run_coverage_serial.py +31 -31
  244. warp/tests/test_adam.py +157 -157
  245. warp/tests/test_arithmetic.py +1124 -1124
  246. warp/tests/test_array.py +2417 -2326
  247. warp/tests/test_array_reduce.py +150 -150
  248. warp/tests/test_async.py +668 -656
  249. warp/tests/test_atomic.py +141 -141
  250. warp/tests/test_bool.py +204 -149
  251. warp/tests/test_builtins_resolution.py +1292 -1292
  252. warp/tests/test_bvh.py +164 -171
  253. warp/tests/test_closest_point_edge_edge.py +228 -228
  254. warp/tests/test_codegen.py +566 -553
  255. warp/tests/test_compile_consts.py +97 -101
  256. warp/tests/test_conditional.py +246 -246
  257. warp/tests/test_copy.py +232 -215
  258. warp/tests/test_ctypes.py +632 -632
  259. warp/tests/test_dense.py +67 -67
  260. warp/tests/test_devices.py +91 -98
  261. warp/tests/test_dlpack.py +530 -529
  262. warp/tests/test_examples.py +400 -378
  263. warp/tests/test_fabricarray.py +955 -955
  264. warp/tests/test_fast_math.py +62 -54
  265. warp/tests/test_fem.py +1277 -1278
  266. warp/tests/test_fp16.py +130 -130
  267. warp/tests/test_func.py +338 -337
  268. warp/tests/test_generics.py +571 -571
  269. warp/tests/test_grad.py +746 -640
  270. warp/tests/test_grad_customs.py +333 -336
  271. warp/tests/test_hash_grid.py +210 -164
  272. warp/tests/test_import.py +39 -39
  273. warp/tests/test_indexedarray.py +1134 -1134
  274. warp/tests/test_intersect.py +67 -67
  275. warp/tests/test_jax.py +307 -307
  276. warp/tests/test_large.py +167 -164
  277. warp/tests/test_launch.py +354 -354
  278. warp/tests/test_lerp.py +261 -261
  279. warp/tests/test_linear_solvers.py +191 -171
  280. warp/tests/test_lvalue.py +421 -493
  281. warp/tests/test_marching_cubes.py +65 -65
  282. warp/tests/test_mat.py +1801 -1827
  283. warp/tests/test_mat_lite.py +115 -115
  284. warp/tests/test_mat_scalar_ops.py +2907 -2889
  285. warp/tests/test_math.py +126 -193
  286. warp/tests/test_matmul.py +500 -499
  287. warp/tests/test_matmul_lite.py +410 -410
  288. warp/tests/test_mempool.py +188 -190
  289. warp/tests/test_mesh.py +284 -324
  290. warp/tests/test_mesh_query_aabb.py +228 -241
  291. warp/tests/test_mesh_query_point.py +692 -702
  292. warp/tests/test_mesh_query_ray.py +292 -303
  293. warp/tests/test_mlp.py +276 -276
  294. warp/tests/test_model.py +110 -110
  295. warp/tests/test_modules_lite.py +39 -39
  296. warp/tests/test_multigpu.py +163 -163
  297. warp/tests/test_noise.py +248 -248
  298. warp/tests/test_operators.py +250 -250
  299. warp/tests/test_options.py +123 -125
  300. warp/tests/test_peer.py +133 -137
  301. warp/tests/test_pinned.py +78 -78
  302. warp/tests/test_print.py +54 -54
  303. warp/tests/test_quat.py +2086 -2086
  304. warp/tests/test_rand.py +288 -288
  305. warp/tests/test_reload.py +217 -217
  306. warp/tests/test_rounding.py +179 -179
  307. warp/tests/test_runlength_encode.py +190 -190
  308. warp/tests/test_sim_grad.py +243 -0
  309. warp/tests/test_sim_kinematics.py +91 -97
  310. warp/tests/test_smoothstep.py +168 -168
  311. warp/tests/test_snippet.py +305 -266
  312. warp/tests/test_sparse.py +468 -460
  313. warp/tests/test_spatial.py +2148 -2148
  314. warp/tests/test_streams.py +486 -473
  315. warp/tests/test_struct.py +710 -675
  316. warp/tests/test_tape.py +173 -148
  317. warp/tests/test_torch.py +743 -743
  318. warp/tests/test_transient_module.py +87 -87
  319. warp/tests/test_types.py +556 -659
  320. warp/tests/test_utils.py +490 -499
  321. warp/tests/test_vec.py +1264 -1268
  322. warp/tests/test_vec_lite.py +73 -73
  323. warp/tests/test_vec_scalar_ops.py +2099 -2099
  324. warp/tests/test_verify_fp.py +94 -94
  325. warp/tests/test_volume.py +737 -736
  326. warp/tests/test_volume_write.py +255 -265
  327. warp/tests/unittest_serial.py +37 -37
  328. warp/tests/unittest_suites.py +363 -359
  329. warp/tests/unittest_utils.py +603 -578
  330. warp/tests/unused_test_misc.py +71 -71
  331. warp/tests/walkthrough_debug.py +85 -85
  332. warp/thirdparty/appdirs.py +598 -598
  333. warp/thirdparty/dlpack.py +143 -143
  334. warp/thirdparty/unittest_parallel.py +566 -561
  335. warp/torch.py +321 -295
  336. warp/types.py +4504 -4450
  337. warp/utils.py +1008 -821
  338. {warp_lang-1.0.1.dist-info → warp_lang-1.1.0.dist-info}/LICENSE.md +126 -126
  339. {warp_lang-1.0.1.dist-info → warp_lang-1.1.0.dist-info}/METADATA +338 -400
  340. warp_lang-1.1.0.dist-info/RECORD +352 -0
  341. warp/examples/assets/cube.usda +0 -42
  342. warp/examples/assets/sphere.usda +0 -56
  343. warp/examples/assets/torus.usda +0 -105
  344. warp_lang-1.0.1.dist-info/RECORD +0 -352
  345. {warp_lang-1.0.1.dist-info → warp_lang-1.1.0.dist-info}/WHEEL +0 -0
  346. {warp_lang-1.0.1.dist-info → warp_lang-1.1.0.dist-info}/top_level.txt +0 -0
@@ -1,1134 +1,1134 @@
1
- # Copyright (c) 2023 NVIDIA CORPORATION. All rights reserved.
2
- # NVIDIA CORPORATION and its licensors retain all intellectual property
3
- # and proprietary rights in and to this software, related documentation
4
- # and any modifications thereto. Any use, reproduction, disclosure or
5
- # distribution of this software and related documentation without an express
6
- # license agreement from NVIDIA CORPORATION is strictly prohibited.
7
-
8
- import unittest
9
- from typing import Any
10
-
11
- import numpy as np
12
-
13
- import warp as wp
14
- from warp.tests.test_array import FillStruct
15
- from warp.tests.unittest_utils import *
16
-
17
- wp.init()
18
-
19
-
20
- @wp.kernel
21
- def kernel_1d(a: wp.indexedarray(dtype=float), expected: wp.array(dtype=float)):
22
- i = wp.tid()
23
-
24
- wp.expect_eq(a[i], expected[i])
25
-
26
- a[i] = 2.0 * a[i]
27
-
28
- wp.atomic_add(a, i, 1.0)
29
-
30
- wp.expect_eq(a[i], 2.0 * expected[i] + 1.0)
31
-
32
-
33
- def test_indexedarray_1d(test, device):
34
- values = np.arange(10, dtype=np.float32)
35
- arr = wp.array(data=values, device=device)
36
-
37
- indices = wp.array([1, 3, 5, 7, 9], dtype=int, device=device)
38
-
39
- iarr = wp.indexedarray1d(arr, [indices])
40
-
41
- test.assertEqual(iarr.dtype, arr.dtype)
42
- test.assertEqual(iarr.ndim, 1)
43
- test.assertEqual(iarr.shape, (5,))
44
- test.assertEqual(iarr.size, 5)
45
-
46
- expected_arr = wp.array(data=[1, 3, 5, 7, 9], dtype=float, device=device)
47
-
48
- wp.launch(kernel_1d, dim=iarr.size, inputs=[iarr, expected_arr], device=device)
49
-
50
-
51
- @wp.kernel
52
- def kernel_2d(a: wp.indexedarray2d(dtype=float), expected: wp.array2d(dtype=float)):
53
- i, j = wp.tid()
54
-
55
- # check expected values
56
- wp.expect_eq(a[i, j], expected[i, j])
57
-
58
- # test wp.view()
59
- wp.expect_eq(a[i][j], a[i, j])
60
-
61
- a[i, j] = 2.0 * a[i, j]
62
-
63
- wp.atomic_add(a, i, j, 1.0)
64
-
65
- wp.expect_eq(a[i, j], 2.0 * expected[i, j] + 1.0)
66
-
67
-
68
- def test_indexedarray_2d(test, device):
69
- values = np.arange(100, dtype=np.float32).reshape((10, 10))
70
- arr = wp.array(data=values, device=device)
71
-
72
- indices0 = wp.array([1, 3], dtype=int, device=device)
73
- indices1 = wp.array([2, 4, 8], dtype=int, device=device)
74
-
75
- iarr = wp.indexedarray2d(arr, [indices0, indices1])
76
-
77
- test.assertEqual(iarr.dtype, arr.dtype)
78
- test.assertEqual(iarr.ndim, 2)
79
- test.assertEqual(iarr.shape, (2, 3))
80
- test.assertEqual(iarr.size, 6)
81
-
82
- expected_values = [[12, 14, 18], [32, 34, 38]]
83
- expected_arr = wp.array(data=expected_values, dtype=float, device=device)
84
-
85
- wp.launch(kernel_2d, dim=iarr.shape, inputs=[iarr, expected_arr], device=device)
86
-
87
-
88
- @wp.kernel
89
- def kernel_3d(a: wp.indexedarray3d(dtype=float), expected: wp.array3d(dtype=float)):
90
- i, j, k = wp.tid()
91
-
92
- # check expected values
93
- wp.expect_eq(a[i, j, k], expected[i, j, k])
94
-
95
- # test wp.view()
96
- wp.expect_eq(a[i][j][k], a[i, j, k])
97
- wp.expect_eq(a[i, j][k], a[i, j, k])
98
- wp.expect_eq(a[i][j, k], a[i, j, k])
99
-
100
- a[i, j, k] = 2.0 * a[i, j, k]
101
-
102
- wp.atomic_add(a, i, j, k, 1.0)
103
-
104
- wp.expect_eq(a[i, j, k], 2.0 * expected[i, j, k] + 1.0)
105
-
106
-
107
- def test_indexedarray_3d(test, device):
108
- values = np.arange(1000, dtype=np.float32).reshape((10, 10, 10))
109
- arr = wp.array(data=values, device=device)
110
-
111
- indices0 = wp.array([1, 3], dtype=int, device=device)
112
- indices1 = wp.array([2, 4, 8], dtype=int, device=device)
113
- indices2 = wp.array([0, 5], dtype=int, device=device)
114
-
115
- iarr = wp.indexedarray3d(arr, [indices0, indices1, indices2])
116
-
117
- test.assertEqual(iarr.dtype, arr.dtype)
118
- test.assertEqual(iarr.ndim, 3)
119
- test.assertEqual(iarr.shape, (2, 3, 2))
120
- test.assertEqual(iarr.size, 12)
121
-
122
- expected_values = [
123
- [[120, 125], [140, 145], [180, 185]],
124
- [[320, 325], [340, 345], [380, 385]],
125
- ]
126
- expected_arr = wp.array(data=expected_values, dtype=float, device=device)
127
-
128
- wp.launch(kernel_3d, dim=iarr.shape, inputs=[iarr, expected_arr], device=device)
129
-
130
-
131
- @wp.kernel
132
- def kernel_4d(a: wp.indexedarray4d(dtype=float), expected: wp.array4d(dtype=float)):
133
- i, j, k, l = wp.tid()
134
-
135
- # check expected values
136
- wp.expect_eq(a[i, j, k, l], expected[i, j, k, l])
137
-
138
- # test wp.view()
139
- wp.expect_eq(a[i][j][k][l], a[i, j, k, l])
140
- wp.expect_eq(a[i][j, k, l], a[i, j, k, l])
141
- wp.expect_eq(a[i, j][k, l], a[i, j, k, l])
142
- wp.expect_eq(a[i, j, k][l], a[i, j, k, l])
143
-
144
- a[i, j, k, l] = 2.0 * a[i, j, k, l]
145
-
146
- wp.atomic_add(a, i, j, k, l, 1.0)
147
-
148
- wp.expect_eq(a[i, j, k, l], 2.0 * expected[i, j, k, l] + 1.0)
149
-
150
-
151
- def test_indexedarray_4d(test, device):
152
- values = np.arange(10000, dtype=np.float32).reshape((10, 10, 10, 10))
153
- arr = wp.array(data=values, device=device)
154
-
155
- indices0 = wp.array([1, 3], dtype=int, device=device)
156
- indices1 = wp.array([2, 4, 8], dtype=int, device=device)
157
- indices2 = wp.array([0, 5], dtype=int, device=device)
158
- indices3 = wp.array([6, 7, 9], dtype=int, device=device)
159
-
160
- iarr = wp.indexedarray4d(arr, [indices0, indices1, indices2, indices3])
161
-
162
- test.assertEqual(iarr.dtype, arr.dtype)
163
- test.assertEqual(iarr.ndim, 4)
164
- test.assertEqual(iarr.shape, (2, 3, 2, 3))
165
- test.assertEqual(iarr.size, 36)
166
-
167
- expected_values = [
168
- [
169
- [[1206, 1207, 1209], [1256, 1257, 1259]],
170
- [[1406, 1407, 1409], [1456, 1457, 1459]],
171
- [[1806, 1807, 1809], [1856, 1857, 1859]],
172
- ],
173
- [
174
- [[3206, 3207, 3209], [3256, 3257, 3259]],
175
- [[3406, 3407, 3409], [3456, 3457, 3459]],
176
- [[3806, 3807, 3809], [3856, 3857, 3859]],
177
- ],
178
- ]
179
- expected_arr = wp.array(data=expected_values, dtype=float, device=device)
180
-
181
- wp.launch(kernel_4d, dim=iarr.shape, inputs=[iarr, expected_arr], device=device)
182
-
183
-
184
- def test_indexedarray_mixed(test, device):
185
- # [[[ 0, 1, 2, 3],
186
- # [ 4, 5, 6, 7],
187
- # [ 8, 9, 10, 11],
188
- # [12, 13, 14, 15]],
189
- # [[16, 17, 18, 19],
190
- # [20, 21, 22, 23],
191
- # [24, 25, 26, 27],
192
- # [28, 29, 30, 31]],
193
- # [[32, 33, 34, 35],
194
- # [36, 37, 38, 39],
195
- # [40, 41, 42, 43],
196
- # [44, 45, 46, 47],
197
- # [[48, 49, 50, 51],
198
- # [52, 53, 54, 55],
199
- # [56, 57, 58, 59],
200
- # [60, 61, 62, 63]]]]
201
- values = np.arange(64, dtype=np.float32).reshape((4, 4, 4))
202
-
203
- indices = wp.array([0, 3], dtype=int, device=device)
204
-
205
- # -----
206
-
207
- arr = wp.array(data=values, device=device)
208
- iarr = wp.indexedarray(arr, [indices, None, None])
209
- test.assertEqual(iarr.dtype, arr.dtype)
210
- test.assertEqual(iarr.ndim, 3)
211
- test.assertEqual(iarr.shape, (2, 4, 4))
212
- test.assertEqual(iarr.size, 32)
213
-
214
- expected_values = [
215
- [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11], [12, 13, 14, 15]],
216
- [[48, 49, 50, 51], [52, 53, 54, 55], [56, 57, 58, 59], [60, 61, 62, 63]],
217
- ]
218
- expected_arr = wp.array(data=expected_values, dtype=float, device=device)
219
-
220
- wp.launch(kernel_3d, dim=iarr.shape, inputs=[iarr, expected_arr], device=device)
221
-
222
- # -----
223
-
224
- arr = wp.array(data=values, device=device)
225
- iarr = wp.indexedarray(arr, [indices, indices, None])
226
- test.assertEqual(iarr.dtype, arr.dtype)
227
- test.assertEqual(iarr.ndim, 3)
228
- test.assertEqual(iarr.shape, (2, 2, 4))
229
- test.assertEqual(iarr.size, 16)
230
-
231
- expected_values = [[[0, 1, 2, 3], [12, 13, 14, 15]], [[48, 49, 50, 51], [60, 61, 62, 63]]]
232
- expected_arr = wp.array(data=expected_values, dtype=float, device=device)
233
-
234
- wp.launch(kernel_3d, dim=iarr.shape, inputs=[iarr, expected_arr], device=device)
235
-
236
- # -----
237
-
238
- arr = wp.array(data=values, device=device)
239
- iarr = wp.indexedarray(arr, [indices, None, indices])
240
- test.assertEqual(iarr.dtype, arr.dtype)
241
- test.assertEqual(iarr.ndim, 3)
242
- test.assertEqual(iarr.shape, (2, 4, 2))
243
- test.assertEqual(iarr.size, 16)
244
-
245
- expected_values = [[[0, 3], [4, 7], [8, 11], [12, 15]], [[48, 51], [52, 55], [56, 59], [60, 63]]]
246
- expected_arr = wp.array(data=expected_values, dtype=float, device=device)
247
-
248
- wp.launch(kernel_3d, dim=iarr.shape, inputs=[iarr, expected_arr], device=device)
249
-
250
- # -----
251
-
252
- arr = wp.array(data=values, device=device)
253
- iarr = wp.indexedarray(arr, [None, indices, indices])
254
- test.assertEqual(iarr.dtype, arr.dtype)
255
- test.assertEqual(iarr.ndim, 3)
256
- test.assertEqual(iarr.shape, (4, 2, 2))
257
- test.assertEqual(iarr.size, 16)
258
-
259
- expected_values = [[[0, 3], [12, 15]], [[16, 19], [28, 31]], [[32, 35], [44, 47]], [[48, 51], [60, 63]]]
260
- expected_arr = wp.array(data=expected_values, dtype=float, device=device)
261
-
262
- wp.launch(kernel_3d, dim=iarr.shape, inputs=[iarr, expected_arr], device=device)
263
-
264
-
265
- vec2i = wp.types.vector(length=2, dtype=wp.int32)
266
- vec3i = wp.types.vector(length=3, dtype=wp.int32)
267
- vec4i = wp.types.vector(length=4, dtype=wp.int32)
268
-
269
-
270
- @wp.kernel
271
- def shape_kernel_1d(arr: wp.indexedarray1d(dtype=float), expected: int):
272
- wp.expect_eq(arr.shape[0], expected)
273
-
274
-
275
- @wp.kernel
276
- def shape_kernel_2d(arr: wp.indexedarray2d(dtype=float), expected: vec2i):
277
- wp.expect_eq(arr.shape[0], expected[0])
278
- wp.expect_eq(arr.shape[1], expected[1])
279
-
280
- # 1d slice
281
- view = arr[0]
282
- wp.expect_eq(view.shape[0], expected[1])
283
-
284
-
285
- @wp.kernel
286
- def shape_kernel_3d(arr: wp.indexedarray3d(dtype=float), expected: vec3i):
287
- wp.expect_eq(arr.shape[0], expected[0])
288
- wp.expect_eq(arr.shape[1], expected[1])
289
- wp.expect_eq(arr.shape[2], expected[2])
290
-
291
- # 2d slice
292
- view2 = arr[0]
293
- wp.expect_eq(view2.shape[0], expected[1])
294
- wp.expect_eq(view2.shape[1], expected[2])
295
-
296
- # 1d slice
297
- view1 = arr[0, 0]
298
- wp.expect_eq(view1.shape[0], expected[2])
299
-
300
-
301
- @wp.kernel
302
- def shape_kernel_4d(arr: wp.indexedarray4d(dtype=float), expected: vec4i):
303
- wp.expect_eq(arr.shape[0], expected[0])
304
- wp.expect_eq(arr.shape[1], expected[1])
305
- wp.expect_eq(arr.shape[2], expected[2])
306
- wp.expect_eq(arr.shape[3], expected[3])
307
-
308
- # 3d slice
309
- view3 = arr[0]
310
- wp.expect_eq(view3.shape[0], expected[1])
311
- wp.expect_eq(view3.shape[1], expected[2])
312
- wp.expect_eq(view3.shape[2], expected[3])
313
-
314
- # 2d slice
315
- view2 = arr[0, 0]
316
- wp.expect_eq(view2.shape[0], expected[2])
317
- wp.expect_eq(view2.shape[1], expected[3])
318
-
319
- # 1d slice
320
- view1 = arr[0, 0, 0]
321
- wp.expect_eq(view1.shape[0], expected[3])
322
-
323
-
324
- def test_indexedarray_shape(test, device):
325
- with wp.ScopedDevice(device):
326
- data1 = wp.zeros(10, dtype=float)
327
- data2 = wp.zeros((10, 20), dtype=float)
328
- data3 = wp.zeros((10, 20, 30), dtype=float)
329
- data4 = wp.zeros((10, 20, 30, 40), dtype=float)
330
-
331
- indices1 = wp.array(data=[2, 7], dtype=int)
332
- indices2 = wp.array(data=[2, 7, 12, 17], dtype=int)
333
- indices3 = wp.array(data=[2, 7, 12, 17, 22, 27], dtype=int)
334
- indices4 = wp.array(data=[2, 7, 12, 17, 22, 27, 32, 37], dtype=int)
335
-
336
- ia1 = wp.indexedarray(data1, [indices1])
337
- wp.launch(shape_kernel_1d, dim=1, inputs=[ia1, 2])
338
-
339
- ia2_1 = wp.indexedarray(data2, [indices1, None])
340
- ia2_2 = wp.indexedarray(data2, [None, indices2])
341
- ia2_3 = wp.indexedarray(data2, [indices1, indices2])
342
- wp.launch(shape_kernel_2d, dim=1, inputs=[ia2_1, vec2i(2, 20)])
343
- wp.launch(shape_kernel_2d, dim=1, inputs=[ia2_2, vec2i(10, 4)])
344
- wp.launch(shape_kernel_2d, dim=1, inputs=[ia2_3, vec2i(2, 4)])
345
-
346
- ia3_1 = wp.indexedarray(data3, [indices1, None, None])
347
- ia3_2 = wp.indexedarray(data3, [None, indices2, None])
348
- ia3_3 = wp.indexedarray(data3, [None, None, indices3])
349
- ia3_4 = wp.indexedarray(data3, [indices1, indices2, None])
350
- ia3_5 = wp.indexedarray(data3, [indices1, None, indices3])
351
- ia3_6 = wp.indexedarray(data3, [None, indices2, indices3])
352
- ia3_7 = wp.indexedarray(data3, [indices1, indices2, indices3])
353
- wp.launch(shape_kernel_3d, dim=1, inputs=[ia3_1, vec3i(2, 20, 30)])
354
- wp.launch(shape_kernel_3d, dim=1, inputs=[ia3_2, vec3i(10, 4, 30)])
355
- wp.launch(shape_kernel_3d, dim=1, inputs=[ia3_3, vec3i(10, 20, 6)])
356
- wp.launch(shape_kernel_3d, dim=1, inputs=[ia3_4, vec3i(2, 4, 30)])
357
- wp.launch(shape_kernel_3d, dim=1, inputs=[ia3_5, vec3i(2, 20, 6)])
358
- wp.launch(shape_kernel_3d, dim=1, inputs=[ia3_6, vec3i(10, 4, 6)])
359
- wp.launch(shape_kernel_3d, dim=1, inputs=[ia3_7, vec3i(2, 4, 6)])
360
-
361
- ia4_1 = wp.indexedarray(data4, [indices1, None, None, None])
362
- ia4_2 = wp.indexedarray(data4, [indices1, None, None, indices4])
363
- ia4_3 = wp.indexedarray(data4, [None, indices2, indices3, None])
364
- ia4_4 = wp.indexedarray(data4, [indices1, indices2, indices3, indices4])
365
- wp.launch(shape_kernel_4d, dim=1, inputs=[ia4_1, vec4i(2, 20, 30, 40)])
366
- wp.launch(shape_kernel_4d, dim=1, inputs=[ia4_2, vec4i(2, 20, 30, 8)])
367
- wp.launch(shape_kernel_4d, dim=1, inputs=[ia4_3, vec4i(10, 4, 6, 40)])
368
- wp.launch(shape_kernel_4d, dim=1, inputs=[ia4_4, vec4i(2, 4, 6, 8)])
369
-
370
- wp.synchronize_device(device)
371
-
372
-
373
- def test_indexedarray_getitem(test, device):
374
- with wp.ScopedDevice(device):
375
- data = wp.array(data=np.arange(1000, dtype=np.int32).reshape((10, 10, 10)))
376
-
377
- I = wp.array(data=[0, 1, 2], dtype=int)
378
-
379
- # use constructor
380
- a1 = wp.indexedarray(data, [None, None, I])
381
- a2 = wp.indexedarray(data, [None, I])
382
- a3 = wp.indexedarray(data, [None, I, I])
383
- a4 = wp.indexedarray(data, [I])
384
- a5 = wp.indexedarray(data, [I, None, I])
385
- a6 = wp.indexedarray(data, [I, I])
386
- a7 = wp.indexedarray(data, [I, I, I])
387
-
388
- # use array.__getitem__()
389
- b1 = data[:, :, I]
390
- b2 = data[:, I]
391
- b3 = data[:, I, I]
392
- b4 = data[I]
393
- b5 = data[I, :, I]
394
- b6 = data[I, I]
395
- b7 = data[I, I, I]
396
-
397
- test.assertEqual(type(a1), type(b1))
398
- test.assertEqual(type(a2), type(b2))
399
- test.assertEqual(type(a3), type(b3))
400
- test.assertEqual(type(a4), type(b4))
401
- test.assertEqual(type(a5), type(b5))
402
- test.assertEqual(type(a6), type(b6))
403
- test.assertEqual(type(a7), type(b7))
404
-
405
- assert_np_equal(a1.numpy(), b1.numpy())
406
- assert_np_equal(a2.numpy(), b2.numpy())
407
- assert_np_equal(a3.numpy(), b3.numpy())
408
- assert_np_equal(a4.numpy(), b4.numpy())
409
- assert_np_equal(a5.numpy(), b5.numpy())
410
- assert_np_equal(a6.numpy(), b6.numpy())
411
- assert_np_equal(a7.numpy(), b7.numpy())
412
-
413
-
414
- def test_indexedarray_slicing(test, device):
415
- with wp.ScopedDevice(device):
416
- data = wp.array(data=np.arange(1000, dtype=np.int32).reshape((10, 10, 10)))
417
-
418
- # test equivalence of slicing and indexing the same range
419
- s = slice(0, 3)
420
- I = wp.array(data=[0, 1, 2], dtype=int)
421
-
422
- a0 = data[s, s, s]
423
- test.assertEqual(type(a0), wp.array)
424
- a1 = data[s, s, I]
425
- test.assertEqual(type(a1), wp.indexedarray)
426
- a2 = data[s, I, s]
427
- test.assertEqual(type(a2), wp.indexedarray)
428
- a3 = data[s, I, I]
429
- test.assertEqual(type(a3), wp.indexedarray)
430
- a4 = data[I, s, s]
431
- test.assertEqual(type(a4), wp.indexedarray)
432
- a5 = data[I, s, I]
433
- test.assertEqual(type(a5), wp.indexedarray)
434
- a6 = data[I, I, s]
435
- test.assertEqual(type(a6), wp.indexedarray)
436
- a7 = data[I, I, I]
437
- test.assertEqual(type(a7), wp.indexedarray)
438
-
439
- expected = a0.numpy()
440
-
441
- assert_np_equal(a1.numpy(), expected)
442
- assert_np_equal(a2.numpy(), expected)
443
- assert_np_equal(a3.numpy(), expected)
444
- assert_np_equal(a4.numpy(), expected)
445
- assert_np_equal(a5.numpy(), expected)
446
- assert_np_equal(a6.numpy(), expected)
447
- assert_np_equal(a7.numpy(), expected)
448
-
449
-
450
- # generic increment kernels that work with any array (regular or indexed)
451
- @wp.kernel
452
- def inc_1d(a: Any):
453
- i = wp.tid()
454
- a[i] = a[i] + 1
455
-
456
-
457
- @wp.kernel
458
- def inc_2d(a: Any):
459
- i, j = wp.tid()
460
- a[i, j] = a[i, j] + 1
461
-
462
-
463
- @wp.kernel
464
- def inc_3d(a: Any):
465
- i, j, k = wp.tid()
466
- a[i, j, k] = a[i, j, k] + 1
467
-
468
-
469
- @wp.kernel
470
- def inc_4d(a: Any):
471
- i, j, k, l = wp.tid()
472
- a[i, j, k, l] = a[i, j, k, l] + 1
473
-
474
-
475
- # optional overloads to avoid module reloading
476
- wp.overload(inc_1d, [wp.array1d(dtype=int)])
477
- wp.overload(inc_2d, [wp.array2d(dtype=int)])
478
- wp.overload(inc_3d, [wp.array3d(dtype=int)])
479
- wp.overload(inc_4d, [wp.array4d(dtype=int)])
480
-
481
- wp.overload(inc_1d, [wp.indexedarray1d(dtype=int)])
482
- wp.overload(inc_2d, [wp.indexedarray2d(dtype=int)])
483
- wp.overload(inc_3d, [wp.indexedarray3d(dtype=int)])
484
- wp.overload(inc_4d, [wp.indexedarray4d(dtype=int)])
485
-
486
-
487
- def test_indexedarray_generics(test, device):
488
- with wp.ScopedDevice(device):
489
- data1 = wp.zeros((5,), dtype=int)
490
- data2 = wp.zeros((5, 5), dtype=int)
491
- data3 = wp.zeros((5, 5, 5), dtype=int)
492
- data4 = wp.zeros((5, 5, 5, 5), dtype=int)
493
-
494
- indices = wp.array(data=[0, 4], dtype=int)
495
-
496
- ia1 = wp.indexedarray(data1, [indices])
497
- ia2 = wp.indexedarray(data2, [indices, indices])
498
- ia3 = wp.indexedarray(data3, [indices, indices, indices])
499
- ia4 = wp.indexedarray(data4, [indices, indices, indices, indices])
500
-
501
- wp.launch(inc_1d, dim=data1.shape, inputs=[data1])
502
- wp.launch(inc_2d, dim=data2.shape, inputs=[data2])
503
- wp.launch(inc_3d, dim=data3.shape, inputs=[data3])
504
- wp.launch(inc_4d, dim=data4.shape, inputs=[data4])
505
-
506
- wp.launch(inc_1d, dim=ia1.shape, inputs=[ia1])
507
- wp.launch(inc_2d, dim=ia2.shape, inputs=[ia2])
508
- wp.launch(inc_3d, dim=ia3.shape, inputs=[ia3])
509
- wp.launch(inc_4d, dim=ia4.shape, inputs=[ia4])
510
-
511
- expected1 = np.ones(5, dtype=np.int32)
512
- expected1[0] = 2
513
- expected1[4] = 2
514
-
515
- expected2 = np.ones((5, 5), dtype=np.int32)
516
- expected2[0, 0] = 2
517
- expected2[0, 4] = 2
518
- expected2[4, 0] = 2
519
- expected2[4, 4] = 2
520
-
521
- expected3 = np.ones((5, 5, 5), dtype=np.int32)
522
- expected3[0, 0, 0] = 2
523
- expected3[0, 0, 4] = 2
524
- expected3[0, 4, 0] = 2
525
- expected3[0, 4, 4] = 2
526
- expected3[4, 0, 0] = 2
527
- expected3[4, 0, 4] = 2
528
- expected3[4, 4, 0] = 2
529
- expected3[4, 4, 4] = 2
530
-
531
- expected4 = np.ones((5, 5, 5, 5), dtype=np.int32)
532
- expected4[0, 0, 0, 0] = 2
533
- expected4[0, 0, 0, 4] = 2
534
- expected4[0, 0, 4, 0] = 2
535
- expected4[0, 0, 4, 4] = 2
536
- expected4[0, 4, 0, 0] = 2
537
- expected4[0, 4, 0, 4] = 2
538
- expected4[0, 4, 4, 0] = 2
539
- expected4[0, 4, 4, 4] = 2
540
- expected4[4, 0, 0, 0] = 2
541
- expected4[4, 0, 0, 4] = 2
542
- expected4[4, 0, 4, 0] = 2
543
- expected4[4, 0, 4, 4] = 2
544
- expected4[4, 4, 0, 0] = 2
545
- expected4[4, 4, 0, 4] = 2
546
- expected4[4, 4, 4, 0] = 2
547
- expected4[4, 4, 4, 4] = 2
548
-
549
- assert_np_equal(data1.numpy(), expected1)
550
- assert_np_equal(data2.numpy(), expected2)
551
- assert_np_equal(data3.numpy(), expected3)
552
- assert_np_equal(data4.numpy(), expected4)
553
-
554
- assert_np_equal(ia1.numpy(), np.full((2,), 2, dtype=np.int32))
555
- assert_np_equal(ia2.numpy(), np.full((2, 2), 2, dtype=np.int32))
556
- assert_np_equal(ia3.numpy(), np.full((2, 2, 2), 2, dtype=np.int32))
557
- assert_np_equal(ia4.numpy(), np.full((2, 2, 2, 2), 2, dtype=np.int32))
558
-
559
-
560
- def test_indexedarray_empty(test, device):
561
- # Test whether common operations work with empty (zero-sized) indexed arrays
562
- # without throwing exceptions.
563
-
564
- def test_empty_ops(ndim, nrows, ncols, wptype, nptype):
565
- data_shape = (1,) * ndim
566
- dtype_shape = ()
567
-
568
- if wptype in wp.types.scalar_types:
569
- # scalar, vector, or matrix
570
- if ncols > 0:
571
- if nrows > 0:
572
- wptype = wp.types.matrix((nrows, ncols), wptype)
573
- else:
574
- wptype = wp.types.vector(ncols, wptype)
575
- dtype_shape = wptype._shape_
576
- fill_value = wptype(42)
577
- else:
578
- # struct
579
- fill_value = wptype()
580
-
581
- # create a data array
582
- data = wp.empty(data_shape, dtype=wptype, device=device, requires_grad=True)
583
-
584
- # create a zero-sized array of indices
585
- indices = wp.empty(0, dtype=int, device=device)
586
-
587
- a = data[indices]
588
-
589
- # we expect dim to be zero for the empty indexed array, unchanged otherwise
590
- expected_shape = (0, *data_shape[1:])
591
-
592
- test.assertEqual(a.size, 0)
593
- test.assertEqual(a.shape, expected_shape)
594
-
595
- # all of these methods should succeed with zero-sized arrays
596
- a.zero_()
597
- a.fill_(fill_value)
598
- b = a.contiguous()
599
-
600
- b = wp.empty_like(a)
601
- b = wp.zeros_like(a)
602
- b = wp.full_like(a, fill_value)
603
- b = wp.clone(a)
604
-
605
- wp.copy(a, b)
606
- a.assign(b)
607
-
608
- na = a.numpy()
609
- test.assertEqual(na.size, 0)
610
- test.assertEqual(na.shape, (*expected_shape, *dtype_shape))
611
- test.assertEqual(na.dtype, nptype)
612
-
613
- test.assertEqual(a.list(), [])
614
-
615
- for ndim in range(1, 5):
616
- # test with scalars, vectors, and matrices
617
- for nptype, wptype in wp.types.np_dtype_to_warp_type.items():
618
- # scalars
619
- test_empty_ops(ndim, 0, 0, wptype, nptype)
620
-
621
- for ncols in [2, 3, 4, 5]:
622
- # vectors
623
- test_empty_ops(ndim, 0, ncols, wptype, nptype)
624
- # square matrices
625
- test_empty_ops(ndim, ncols, ncols, wptype, nptype)
626
-
627
- # non-square matrices
628
- test_empty_ops(ndim, 2, 3, wptype, nptype)
629
- test_empty_ops(ndim, 3, 2, wptype, nptype)
630
- test_empty_ops(ndim, 3, 4, wptype, nptype)
631
- test_empty_ops(ndim, 4, 3, wptype, nptype)
632
-
633
- # test with structs
634
- test_empty_ops(ndim, 0, 0, FillStruct, FillStruct.numpy_dtype())
635
-
636
-
637
- def test_indexedarray_fill_scalar(test, device):
638
- dim_x = 4
639
-
640
- for nptype, wptype in wp.types.np_dtype_to_warp_type.items():
641
- data1 = wp.zeros(dim_x, dtype=wptype, device=device)
642
- data2 = wp.zeros((dim_x, dim_x), dtype=wptype, device=device)
643
- data3 = wp.zeros((dim_x, dim_x, dim_x), dtype=wptype, device=device)
644
- data4 = wp.zeros((dim_x, dim_x, dim_x, dim_x), dtype=wptype, device=device)
645
-
646
- indices = wp.array(np.arange(0, dim_x, 2, dtype=np.int32), device=device)
647
-
648
- a1 = data1[indices]
649
- a2 = data2[indices]
650
- a3 = data3[indices]
651
- a4 = data4[indices]
652
-
653
- assert_np_equal(a1.numpy(), np.zeros(a1.shape, dtype=nptype))
654
- assert_np_equal(a2.numpy(), np.zeros(a2.shape, dtype=nptype))
655
- assert_np_equal(a3.numpy(), np.zeros(a3.shape, dtype=nptype))
656
- assert_np_equal(a4.numpy(), np.zeros(a4.shape, dtype=nptype))
657
-
658
- # fill with int value
659
- fill_value = 42
660
-
661
- a1.fill_(fill_value)
662
- a2.fill_(fill_value)
663
- a3.fill_(fill_value)
664
- a4.fill_(fill_value)
665
-
666
- assert_np_equal(a1.numpy(), np.full(a1.shape, fill_value, dtype=nptype))
667
- assert_np_equal(a2.numpy(), np.full(a2.shape, fill_value, dtype=nptype))
668
- assert_np_equal(a3.numpy(), np.full(a3.shape, fill_value, dtype=nptype))
669
- assert_np_equal(a4.numpy(), np.full(a4.shape, fill_value, dtype=nptype))
670
-
671
- a1.zero_()
672
- a2.zero_()
673
- a3.zero_()
674
- a4.zero_()
675
-
676
- assert_np_equal(a1.numpy(), np.zeros(a1.shape, dtype=nptype))
677
- assert_np_equal(a2.numpy(), np.zeros(a2.shape, dtype=nptype))
678
- assert_np_equal(a3.numpy(), np.zeros(a3.shape, dtype=nptype))
679
- assert_np_equal(a4.numpy(), np.zeros(a4.shape, dtype=nptype))
680
-
681
- if wptype in wp.types.float_types:
682
- # fill with float value
683
- fill_value = 13.37
684
-
685
- a1.fill_(fill_value)
686
- a2.fill_(fill_value)
687
- a3.fill_(fill_value)
688
- a4.fill_(fill_value)
689
-
690
- assert_np_equal(a1.numpy(), np.full(a1.shape, fill_value, dtype=nptype))
691
- assert_np_equal(a2.numpy(), np.full(a2.shape, fill_value, dtype=nptype))
692
- assert_np_equal(a3.numpy(), np.full(a3.shape, fill_value, dtype=nptype))
693
- assert_np_equal(a4.numpy(), np.full(a4.shape, fill_value, dtype=nptype))
694
-
695
- # fill with Warp scalar value
696
- fill_value = wptype(17)
697
-
698
- a1.fill_(fill_value)
699
- a2.fill_(fill_value)
700
- a3.fill_(fill_value)
701
- a4.fill_(fill_value)
702
-
703
- assert_np_equal(a1.numpy(), np.full(a1.shape, fill_value.value, dtype=nptype))
704
- assert_np_equal(a2.numpy(), np.full(a2.shape, fill_value.value, dtype=nptype))
705
- assert_np_equal(a3.numpy(), np.full(a3.shape, fill_value.value, dtype=nptype))
706
- assert_np_equal(a4.numpy(), np.full(a4.shape, fill_value.value, dtype=nptype))
707
-
708
-
709
- def test_indexedarray_fill_vector(test, device):
710
- # test filling a vector array with scalar or vector values (vec_type, list, or numpy array)
711
-
712
- dim_x = 4
713
-
714
- for nptype, wptype in wp.types.np_dtype_to_warp_type.items():
715
- # vector types
716
- vector_types = [
717
- wp.types.vector(2, wptype),
718
- wp.types.vector(3, wptype),
719
- wp.types.vector(4, wptype),
720
- wp.types.vector(5, wptype),
721
- ]
722
-
723
- for vec_type in vector_types:
724
- vec_len = vec_type._length_
725
-
726
- data1 = wp.zeros(dim_x, dtype=vec_type, device=device)
727
- data2 = wp.zeros((dim_x, dim_x), dtype=vec_type, device=device)
728
- data3 = wp.zeros((dim_x, dim_x, dim_x), dtype=vec_type, device=device)
729
- data4 = wp.zeros((dim_x, dim_x, dim_x, dim_x), dtype=vec_type, device=device)
730
-
731
- indices = wp.array(np.arange(0, dim_x, 2, dtype=np.int32), device=device)
732
-
733
- a1 = data1[indices]
734
- a2 = data2[indices]
735
- a3 = data3[indices]
736
- a4 = data4[indices]
737
-
738
- assert_np_equal(a1.numpy(), np.zeros((*a1.shape, vec_len), dtype=nptype))
739
- assert_np_equal(a2.numpy(), np.zeros((*a2.shape, vec_len), dtype=nptype))
740
- assert_np_equal(a3.numpy(), np.zeros((*a3.shape, vec_len), dtype=nptype))
741
- assert_np_equal(a4.numpy(), np.zeros((*a4.shape, vec_len), dtype=nptype))
742
-
743
- # fill with int scalar
744
- fill_value = 42
745
-
746
- a1.fill_(fill_value)
747
- a2.fill_(fill_value)
748
- a3.fill_(fill_value)
749
- a4.fill_(fill_value)
750
-
751
- assert_np_equal(a1.numpy(), np.full((*a1.shape, vec_len), fill_value, dtype=nptype))
752
- assert_np_equal(a2.numpy(), np.full((*a2.shape, vec_len), fill_value, dtype=nptype))
753
- assert_np_equal(a3.numpy(), np.full((*a3.shape, vec_len), fill_value, dtype=nptype))
754
- assert_np_equal(a4.numpy(), np.full((*a4.shape, vec_len), fill_value, dtype=nptype))
755
-
756
- # test zeroing
757
- a1.zero_()
758
- a2.zero_()
759
- a3.zero_()
760
- a4.zero_()
761
-
762
- assert_np_equal(a1.numpy(), np.zeros((*a1.shape, vec_len), dtype=nptype))
763
- assert_np_equal(a2.numpy(), np.zeros((*a2.shape, vec_len), dtype=nptype))
764
- assert_np_equal(a3.numpy(), np.zeros((*a3.shape, vec_len), dtype=nptype))
765
- assert_np_equal(a4.numpy(), np.zeros((*a4.shape, vec_len), dtype=nptype))
766
-
767
- # vector values can be passed as a list, numpy array, or Warp vector instance
768
- fill_list = [17, 42, 99, 101, 127][:vec_len]
769
- fill_arr = np.array(fill_list, dtype=nptype)
770
- fill_vec = vec_type(fill_list)
771
-
772
- expected1 = np.tile(fill_arr, a1.size).reshape((*a1.shape, vec_len))
773
- expected2 = np.tile(fill_arr, a2.size).reshape((*a2.shape, vec_len))
774
- expected3 = np.tile(fill_arr, a3.size).reshape((*a3.shape, vec_len))
775
- expected4 = np.tile(fill_arr, a4.size).reshape((*a4.shape, vec_len))
776
-
777
- # fill with list of vector length
778
- a1.fill_(fill_list)
779
- a2.fill_(fill_list)
780
- a3.fill_(fill_list)
781
- a4.fill_(fill_list)
782
-
783
- assert_np_equal(a1.numpy(), expected1)
784
- assert_np_equal(a2.numpy(), expected2)
785
- assert_np_equal(a3.numpy(), expected3)
786
- assert_np_equal(a4.numpy(), expected4)
787
-
788
- # clear
789
- a1.zero_()
790
- a2.zero_()
791
- a3.zero_()
792
- a4.zero_()
793
-
794
- # fill with numpy array of vector length
795
- a1.fill_(fill_arr)
796
- a2.fill_(fill_arr)
797
- a3.fill_(fill_arr)
798
- a4.fill_(fill_arr)
799
-
800
- assert_np_equal(a1.numpy(), expected1)
801
- assert_np_equal(a2.numpy(), expected2)
802
- assert_np_equal(a3.numpy(), expected3)
803
- assert_np_equal(a4.numpy(), expected4)
804
-
805
- # clear
806
- a1.zero_()
807
- a2.zero_()
808
- a3.zero_()
809
- a4.zero_()
810
-
811
- # fill with vec instance
812
- a1.fill_(fill_vec)
813
- a2.fill_(fill_vec)
814
- a3.fill_(fill_vec)
815
- a4.fill_(fill_vec)
816
-
817
- assert_np_equal(a1.numpy(), expected1)
818
- assert_np_equal(a2.numpy(), expected2)
819
- assert_np_equal(a3.numpy(), expected3)
820
- assert_np_equal(a4.numpy(), expected4)
821
-
822
- if wptype in wp.types.float_types:
823
- # fill with float scalar
824
- fill_value = 13.37
825
-
826
- a1.fill_(fill_value)
827
- a2.fill_(fill_value)
828
- a3.fill_(fill_value)
829
- a4.fill_(fill_value)
830
-
831
- assert_np_equal(a1.numpy(), np.full((*a1.shape, vec_len), fill_value, dtype=nptype))
832
- assert_np_equal(a2.numpy(), np.full((*a2.shape, vec_len), fill_value, dtype=nptype))
833
- assert_np_equal(a3.numpy(), np.full((*a3.shape, vec_len), fill_value, dtype=nptype))
834
- assert_np_equal(a4.numpy(), np.full((*a4.shape, vec_len), fill_value, dtype=nptype))
835
-
836
- # fill with float list of vector length
837
- fill_list = [-2.5, -1.25, 1.25, 2.5, 5.0][:vec_len]
838
-
839
- a1.fill_(fill_list)
840
- a2.fill_(fill_list)
841
- a3.fill_(fill_list)
842
- a4.fill_(fill_list)
843
-
844
- expected1 = np.tile(np.array(fill_list, dtype=nptype), a1.size).reshape((*a1.shape, vec_len))
845
- expected2 = np.tile(np.array(fill_list, dtype=nptype), a2.size).reshape((*a2.shape, vec_len))
846
- expected3 = np.tile(np.array(fill_list, dtype=nptype), a3.size).reshape((*a3.shape, vec_len))
847
- expected4 = np.tile(np.array(fill_list, dtype=nptype), a4.size).reshape((*a4.shape, vec_len))
848
-
849
- assert_np_equal(a1.numpy(), expected1)
850
- assert_np_equal(a2.numpy(), expected2)
851
- assert_np_equal(a3.numpy(), expected3)
852
- assert_np_equal(a4.numpy(), expected4)
853
-
854
-
855
- def test_indexedarray_fill_matrix(test, device):
856
- # test filling a matrix array with scalar or matrix values (mat_type, nested list, or 2d numpy array)
857
-
858
- dim_x = 4
859
-
860
- for nptype, wptype in wp.types.np_dtype_to_warp_type.items():
861
- # matrix types
862
- matrix_types = [
863
- # square matrices
864
- wp.types.matrix((2, 2), wptype),
865
- wp.types.matrix((3, 3), wptype),
866
- wp.types.matrix((4, 4), wptype),
867
- wp.types.matrix((5, 5), wptype),
868
- # non-square matrices
869
- wp.types.matrix((2, 3), wptype),
870
- wp.types.matrix((3, 2), wptype),
871
- wp.types.matrix((3, 4), wptype),
872
- wp.types.matrix((4, 3), wptype),
873
- ]
874
-
875
- for mat_type in matrix_types:
876
- mat_len = mat_type._length_
877
- mat_shape = mat_type._shape_
878
-
879
- data1 = wp.zeros(dim_x, dtype=mat_type, device=device)
880
- data2 = wp.zeros((dim_x, dim_x), dtype=mat_type, device=device)
881
- data3 = wp.zeros((dim_x, dim_x, dim_x), dtype=mat_type, device=device)
882
- data4 = wp.zeros((dim_x, dim_x, dim_x, dim_x), dtype=mat_type, device=device)
883
-
884
- indices = wp.array(np.arange(0, dim_x, 2, dtype=np.int32), device=device)
885
-
886
- a1 = data1[indices]
887
- a2 = data2[indices]
888
- a3 = data3[indices]
889
- a4 = data4[indices]
890
-
891
- assert_np_equal(a1.numpy(), np.zeros((*a1.shape, *mat_shape), dtype=nptype))
892
- assert_np_equal(a2.numpy(), np.zeros((*a2.shape, *mat_shape), dtype=nptype))
893
- assert_np_equal(a3.numpy(), np.zeros((*a3.shape, *mat_shape), dtype=nptype))
894
- assert_np_equal(a4.numpy(), np.zeros((*a4.shape, *mat_shape), dtype=nptype))
895
-
896
- # fill with scalar
897
- fill_value = 42
898
-
899
- a1.fill_(fill_value)
900
- a2.fill_(fill_value)
901
- a3.fill_(fill_value)
902
- a4.fill_(fill_value)
903
-
904
- assert_np_equal(a1.numpy(), np.full((*a1.shape, *mat_shape), fill_value, dtype=nptype))
905
- assert_np_equal(a2.numpy(), np.full((*a2.shape, *mat_shape), fill_value, dtype=nptype))
906
- assert_np_equal(a3.numpy(), np.full((*a3.shape, *mat_shape), fill_value, dtype=nptype))
907
- assert_np_equal(a4.numpy(), np.full((*a4.shape, *mat_shape), fill_value, dtype=nptype))
908
-
909
- # test zeroing
910
- a1.zero_()
911
- a2.zero_()
912
- a3.zero_()
913
- a4.zero_()
914
-
915
- assert_np_equal(a1.numpy(), np.zeros((*a1.shape, *mat_shape), dtype=nptype))
916
- assert_np_equal(a2.numpy(), np.zeros((*a2.shape, *mat_shape), dtype=nptype))
917
- assert_np_equal(a3.numpy(), np.zeros((*a3.shape, *mat_shape), dtype=nptype))
918
- assert_np_equal(a4.numpy(), np.zeros((*a4.shape, *mat_shape), dtype=nptype))
919
-
920
- # matrix values can be passed as a 1d numpy array, 2d numpy array, flat list, nested list, or Warp matrix instance
921
- if wptype != wp.bool:
922
- fill_arr1 = np.arange(mat_len, dtype=nptype)
923
- else:
924
- fill_arr1 = np.ones(mat_len, dtype=nptype)
925
- fill_arr2 = fill_arr1.reshape(mat_shape)
926
- fill_list1 = list(fill_arr1)
927
- fill_list2 = [list(row) for row in fill_arr2]
928
- fill_mat = mat_type(fill_arr1)
929
-
930
- expected1 = np.tile(fill_arr1, a1.size).reshape((*a1.shape, *mat_shape))
931
- expected2 = np.tile(fill_arr1, a2.size).reshape((*a2.shape, *mat_shape))
932
- expected3 = np.tile(fill_arr1, a3.size).reshape((*a3.shape, *mat_shape))
933
- expected4 = np.tile(fill_arr1, a4.size).reshape((*a4.shape, *mat_shape))
934
-
935
- # fill with 1d numpy array
936
- a1.fill_(fill_arr1)
937
- a2.fill_(fill_arr1)
938
- a3.fill_(fill_arr1)
939
- a4.fill_(fill_arr1)
940
-
941
- assert_np_equal(a1.numpy(), expected1)
942
- assert_np_equal(a2.numpy(), expected2)
943
- assert_np_equal(a3.numpy(), expected3)
944
- assert_np_equal(a4.numpy(), expected4)
945
-
946
- # clear
947
- a1.zero_()
948
- a2.zero_()
949
- a3.zero_()
950
- a4.zero_()
951
-
952
- # fill with 2d numpy array
953
- a1.fill_(fill_arr2)
954
- a2.fill_(fill_arr2)
955
- a3.fill_(fill_arr2)
956
- a4.fill_(fill_arr2)
957
-
958
- assert_np_equal(a1.numpy(), expected1)
959
- assert_np_equal(a2.numpy(), expected2)
960
- assert_np_equal(a3.numpy(), expected3)
961
- assert_np_equal(a4.numpy(), expected4)
962
-
963
- # clear
964
- a1.zero_()
965
- a2.zero_()
966
- a3.zero_()
967
- a4.zero_()
968
-
969
- # fill with flat list
970
- a1.fill_(fill_list1)
971
- a2.fill_(fill_list1)
972
- a3.fill_(fill_list1)
973
- a4.fill_(fill_list1)
974
-
975
- assert_np_equal(a1.numpy(), expected1)
976
- assert_np_equal(a2.numpy(), expected2)
977
- assert_np_equal(a3.numpy(), expected3)
978
- assert_np_equal(a4.numpy(), expected4)
979
-
980
- # clear
981
- a1.zero_()
982
- a2.zero_()
983
- a3.zero_()
984
- a4.zero_()
985
-
986
- # fill with nested list
987
- a1.fill_(fill_list2)
988
- a2.fill_(fill_list2)
989
- a3.fill_(fill_list2)
990
- a4.fill_(fill_list2)
991
-
992
- assert_np_equal(a1.numpy(), expected1)
993
- assert_np_equal(a2.numpy(), expected2)
994
- assert_np_equal(a3.numpy(), expected3)
995
- assert_np_equal(a4.numpy(), expected4)
996
-
997
- # clear
998
- a1.zero_()
999
- a2.zero_()
1000
- a3.zero_()
1001
- a4.zero_()
1002
-
1003
- # fill with mat instance
1004
- a1.fill_(fill_mat)
1005
- a2.fill_(fill_mat)
1006
- a3.fill_(fill_mat)
1007
- a4.fill_(fill_mat)
1008
-
1009
- assert_np_equal(a1.numpy(), expected1)
1010
- assert_np_equal(a2.numpy(), expected2)
1011
- assert_np_equal(a3.numpy(), expected3)
1012
- assert_np_equal(a4.numpy(), expected4)
1013
-
1014
-
1015
- def test_indexedarray_fill_struct(test, device):
1016
- dim_x = 8
1017
-
1018
- nptype = FillStruct.numpy_dtype()
1019
-
1020
- data1 = wp.zeros(dim_x, dtype=FillStruct, device=device)
1021
- data2 = wp.zeros((dim_x, dim_x), dtype=FillStruct, device=device)
1022
- data3 = wp.zeros((dim_x, dim_x, dim_x), dtype=FillStruct, device=device)
1023
- data4 = wp.zeros((dim_x, dim_x, dim_x, dim_x), dtype=FillStruct, device=device)
1024
-
1025
- indices = wp.array(np.arange(0, dim_x, 2, dtype=np.int32), device=device)
1026
-
1027
- a1 = data1[indices]
1028
- a2 = data2[indices]
1029
- a3 = data3[indices]
1030
- a4 = data4[indices]
1031
-
1032
- assert_np_equal(a1.numpy(), np.zeros(a1.shape, dtype=nptype))
1033
- assert_np_equal(a2.numpy(), np.zeros(a2.shape, dtype=nptype))
1034
- assert_np_equal(a3.numpy(), np.zeros(a3.shape, dtype=nptype))
1035
- assert_np_equal(a4.numpy(), np.zeros(a4.shape, dtype=nptype))
1036
-
1037
- s = FillStruct()
1038
-
1039
- # fill with default struct value (should be all zeros)
1040
- a1.fill_(s)
1041
- a2.fill_(s)
1042
- a3.fill_(s)
1043
- a4.fill_(s)
1044
-
1045
- assert_np_equal(a1.numpy(), np.zeros(a1.shape, dtype=nptype))
1046
- assert_np_equal(a2.numpy(), np.zeros(a2.shape, dtype=nptype))
1047
- assert_np_equal(a3.numpy(), np.zeros(a3.shape, dtype=nptype))
1048
- assert_np_equal(a4.numpy(), np.zeros(a4.shape, dtype=nptype))
1049
-
1050
- # scalars
1051
- s.i1 = -17
1052
- s.i2 = 42
1053
- s.i4 = 99
1054
- s.i8 = 101
1055
- s.f2 = -1.25
1056
- s.f4 = 13.37
1057
- s.f8 = 0.125
1058
- # vectors
1059
- s.v2 = [21, 22]
1060
- s.v3 = [31, 32, 33]
1061
- s.v4 = [41, 42, 43, 44]
1062
- s.v5 = [51, 52, 53, 54, 55]
1063
- # matrices
1064
- s.m2 = [[61, 62]] * 2
1065
- s.m3 = [[71, 72, 73]] * 3
1066
- s.m4 = [[81, 82, 83, 84]] * 4
1067
- s.m5 = [[91, 92, 93, 94, 95]] * 5
1068
- # arrays
1069
- s.a1 = wp.zeros((2,) * 1, dtype=float, device=device)
1070
- s.a2 = wp.zeros((2,) * 2, dtype=float, device=device)
1071
- s.a3 = wp.zeros((2,) * 3, dtype=float, device=device)
1072
- s.a4 = wp.zeros((2,) * 4, dtype=float, device=device)
1073
-
1074
- # fill with custom struct value
1075
- a1.fill_(s)
1076
- a2.fill_(s)
1077
- a3.fill_(s)
1078
- a4.fill_(s)
1079
-
1080
- ns = s.numpy_value()
1081
-
1082
- expected1 = np.empty(a1.shape, dtype=nptype)
1083
- expected2 = np.empty(a2.shape, dtype=nptype)
1084
- expected3 = np.empty(a3.shape, dtype=nptype)
1085
- expected4 = np.empty(a4.shape, dtype=nptype)
1086
-
1087
- expected1.fill(ns)
1088
- expected2.fill(ns)
1089
- expected3.fill(ns)
1090
- expected4.fill(ns)
1091
-
1092
- assert_np_equal(a1.numpy(), expected1)
1093
- assert_np_equal(a2.numpy(), expected2)
1094
- assert_np_equal(a3.numpy(), expected3)
1095
- assert_np_equal(a4.numpy(), expected4)
1096
-
1097
- # test clearing
1098
- a1.zero_()
1099
- a2.zero_()
1100
- a3.zero_()
1101
- a4.zero_()
1102
-
1103
- assert_np_equal(a1.numpy(), np.zeros(a1.shape, dtype=nptype))
1104
- assert_np_equal(a2.numpy(), np.zeros(a2.shape, dtype=nptype))
1105
- assert_np_equal(a3.numpy(), np.zeros(a3.shape, dtype=nptype))
1106
- assert_np_equal(a4.numpy(), np.zeros(a4.shape, dtype=nptype))
1107
-
1108
-
1109
- devices = get_test_devices()
1110
-
1111
-
1112
- class TestIndexedArray(unittest.TestCase):
1113
- pass
1114
-
1115
-
1116
- add_function_test(TestIndexedArray, "test_indexedarray_1d", test_indexedarray_1d, devices=devices)
1117
- add_function_test(TestIndexedArray, "test_indexedarray_2d", test_indexedarray_2d, devices=devices)
1118
- add_function_test(TestIndexedArray, "test_indexedarray_3d", test_indexedarray_3d, devices=devices)
1119
- add_function_test(TestIndexedArray, "test_indexedarray_4d", test_indexedarray_4d, devices=devices)
1120
- add_function_test(TestIndexedArray, "test_indexedarray_mixed", test_indexedarray_mixed, devices=devices)
1121
- add_function_test(TestIndexedArray, "test_indexedarray_shape", test_indexedarray_shape, devices=devices)
1122
- add_function_test(TestIndexedArray, "test_indexedarray_getitem", test_indexedarray_getitem, devices=devices)
1123
- add_function_test(TestIndexedArray, "test_indexedarray_slicing", test_indexedarray_slicing, devices=devices)
1124
- add_function_test(TestIndexedArray, "test_indexedarray_generics", test_indexedarray_generics, devices=devices)
1125
- add_function_test(TestIndexedArray, "test_indexedarray_empty", test_indexedarray_empty, devices=devices)
1126
- add_function_test(TestIndexedArray, "test_indexedarray_fill_scalar", test_indexedarray_fill_scalar, devices=devices)
1127
- add_function_test(TestIndexedArray, "test_indexedarray_fill_vector", test_indexedarray_fill_vector, devices=devices)
1128
- add_function_test(TestIndexedArray, "test_indexedarray_fill_matrix", test_indexedarray_fill_matrix, devices=devices)
1129
- add_function_test(TestIndexedArray, "test_indexedarray_fill_struct", test_indexedarray_fill_struct, devices=devices)
1130
-
1131
-
1132
- if __name__ == "__main__":
1133
- wp.build.clear_kernel_cache()
1134
- unittest.main(verbosity=2)
1
+ # Copyright (c) 2023 NVIDIA CORPORATION. All rights reserved.
2
+ # NVIDIA CORPORATION and its licensors retain all intellectual property
3
+ # and proprietary rights in and to this software, related documentation
4
+ # and any modifications thereto. Any use, reproduction, disclosure or
5
+ # distribution of this software and related documentation without an express
6
+ # license agreement from NVIDIA CORPORATION is strictly prohibited.
7
+
8
+ import unittest
9
+ from typing import Any
10
+
11
+ import numpy as np
12
+
13
+ import warp as wp
14
+ from warp.tests.test_array import FillStruct
15
+ from warp.tests.unittest_utils import *
16
+
17
+ wp.init()
18
+
19
+
20
+ @wp.kernel
21
+ def kernel_1d(a: wp.indexedarray(dtype=float), expected: wp.array(dtype=float)):
22
+ i = wp.tid()
23
+
24
+ wp.expect_eq(a[i], expected[i])
25
+
26
+ a[i] = 2.0 * a[i]
27
+
28
+ wp.atomic_add(a, i, 1.0)
29
+
30
+ wp.expect_eq(a[i], 2.0 * expected[i] + 1.0)
31
+
32
+
33
+ def test_indexedarray_1d(test, device):
34
+ values = np.arange(10, dtype=np.float32)
35
+ arr = wp.array(data=values, device=device)
36
+
37
+ indices = wp.array([1, 3, 5, 7, 9], dtype=int, device=device)
38
+
39
+ iarr = wp.indexedarray1d(arr, [indices])
40
+
41
+ test.assertEqual(iarr.dtype, arr.dtype)
42
+ test.assertEqual(iarr.ndim, 1)
43
+ test.assertEqual(iarr.shape, (5,))
44
+ test.assertEqual(iarr.size, 5)
45
+
46
+ expected_arr = wp.array(data=[1, 3, 5, 7, 9], dtype=float, device=device)
47
+
48
+ wp.launch(kernel_1d, dim=iarr.size, inputs=[iarr, expected_arr], device=device)
49
+
50
+
51
+ @wp.kernel
52
+ def kernel_2d(a: wp.indexedarray2d(dtype=float), expected: wp.array2d(dtype=float)):
53
+ i, j = wp.tid()
54
+
55
+ # check expected values
56
+ wp.expect_eq(a[i, j], expected[i, j])
57
+
58
+ # test wp.view()
59
+ wp.expect_eq(a[i][j], a[i, j])
60
+
61
+ a[i, j] = 2.0 * a[i, j]
62
+
63
+ wp.atomic_add(a, i, j, 1.0)
64
+
65
+ wp.expect_eq(a[i, j], 2.0 * expected[i, j] + 1.0)
66
+
67
+
68
+ def test_indexedarray_2d(test, device):
69
+ values = np.arange(100, dtype=np.float32).reshape((10, 10))
70
+ arr = wp.array(data=values, device=device)
71
+
72
+ indices0 = wp.array([1, 3], dtype=int, device=device)
73
+ indices1 = wp.array([2, 4, 8], dtype=int, device=device)
74
+
75
+ iarr = wp.indexedarray2d(arr, [indices0, indices1])
76
+
77
+ test.assertEqual(iarr.dtype, arr.dtype)
78
+ test.assertEqual(iarr.ndim, 2)
79
+ test.assertEqual(iarr.shape, (2, 3))
80
+ test.assertEqual(iarr.size, 6)
81
+
82
+ expected_values = [[12, 14, 18], [32, 34, 38]]
83
+ expected_arr = wp.array(data=expected_values, dtype=float, device=device)
84
+
85
+ wp.launch(kernel_2d, dim=iarr.shape, inputs=[iarr, expected_arr], device=device)
86
+
87
+
88
+ @wp.kernel
89
+ def kernel_3d(a: wp.indexedarray3d(dtype=float), expected: wp.array3d(dtype=float)):
90
+ i, j, k = wp.tid()
91
+
92
+ # check expected values
93
+ wp.expect_eq(a[i, j, k], expected[i, j, k])
94
+
95
+ # test wp.view()
96
+ wp.expect_eq(a[i][j][k], a[i, j, k])
97
+ wp.expect_eq(a[i, j][k], a[i, j, k])
98
+ wp.expect_eq(a[i][j, k], a[i, j, k])
99
+
100
+ a[i, j, k] = 2.0 * a[i, j, k]
101
+
102
+ wp.atomic_add(a, i, j, k, 1.0)
103
+
104
+ wp.expect_eq(a[i, j, k], 2.0 * expected[i, j, k] + 1.0)
105
+
106
+
107
+ def test_indexedarray_3d(test, device):
108
+ values = np.arange(1000, dtype=np.float32).reshape((10, 10, 10))
109
+ arr = wp.array(data=values, device=device)
110
+
111
+ indices0 = wp.array([1, 3], dtype=int, device=device)
112
+ indices1 = wp.array([2, 4, 8], dtype=int, device=device)
113
+ indices2 = wp.array([0, 5], dtype=int, device=device)
114
+
115
+ iarr = wp.indexedarray3d(arr, [indices0, indices1, indices2])
116
+
117
+ test.assertEqual(iarr.dtype, arr.dtype)
118
+ test.assertEqual(iarr.ndim, 3)
119
+ test.assertEqual(iarr.shape, (2, 3, 2))
120
+ test.assertEqual(iarr.size, 12)
121
+
122
+ expected_values = [
123
+ [[120, 125], [140, 145], [180, 185]],
124
+ [[320, 325], [340, 345], [380, 385]],
125
+ ]
126
+ expected_arr = wp.array(data=expected_values, dtype=float, device=device)
127
+
128
+ wp.launch(kernel_3d, dim=iarr.shape, inputs=[iarr, expected_arr], device=device)
129
+
130
+
131
+ @wp.kernel
132
+ def kernel_4d(a: wp.indexedarray4d(dtype=float), expected: wp.array4d(dtype=float)):
133
+ i, j, k, l = wp.tid()
134
+
135
+ # check expected values
136
+ wp.expect_eq(a[i, j, k, l], expected[i, j, k, l])
137
+
138
+ # test wp.view()
139
+ wp.expect_eq(a[i][j][k][l], a[i, j, k, l])
140
+ wp.expect_eq(a[i][j, k, l], a[i, j, k, l])
141
+ wp.expect_eq(a[i, j][k, l], a[i, j, k, l])
142
+ wp.expect_eq(a[i, j, k][l], a[i, j, k, l])
143
+
144
+ a[i, j, k, l] = 2.0 * a[i, j, k, l]
145
+
146
+ wp.atomic_add(a, i, j, k, l, 1.0)
147
+
148
+ wp.expect_eq(a[i, j, k, l], 2.0 * expected[i, j, k, l] + 1.0)
149
+
150
+
151
+ def test_indexedarray_4d(test, device):
152
+ values = np.arange(10000, dtype=np.float32).reshape((10, 10, 10, 10))
153
+ arr = wp.array(data=values, device=device)
154
+
155
+ indices0 = wp.array([1, 3], dtype=int, device=device)
156
+ indices1 = wp.array([2, 4, 8], dtype=int, device=device)
157
+ indices2 = wp.array([0, 5], dtype=int, device=device)
158
+ indices3 = wp.array([6, 7, 9], dtype=int, device=device)
159
+
160
+ iarr = wp.indexedarray4d(arr, [indices0, indices1, indices2, indices3])
161
+
162
+ test.assertEqual(iarr.dtype, arr.dtype)
163
+ test.assertEqual(iarr.ndim, 4)
164
+ test.assertEqual(iarr.shape, (2, 3, 2, 3))
165
+ test.assertEqual(iarr.size, 36)
166
+
167
+ expected_values = [
168
+ [
169
+ [[1206, 1207, 1209], [1256, 1257, 1259]],
170
+ [[1406, 1407, 1409], [1456, 1457, 1459]],
171
+ [[1806, 1807, 1809], [1856, 1857, 1859]],
172
+ ],
173
+ [
174
+ [[3206, 3207, 3209], [3256, 3257, 3259]],
175
+ [[3406, 3407, 3409], [3456, 3457, 3459]],
176
+ [[3806, 3807, 3809], [3856, 3857, 3859]],
177
+ ],
178
+ ]
179
+ expected_arr = wp.array(data=expected_values, dtype=float, device=device)
180
+
181
+ wp.launch(kernel_4d, dim=iarr.shape, inputs=[iarr, expected_arr], device=device)
182
+
183
+
184
+ def test_indexedarray_mixed(test, device):
185
+ # [[[ 0, 1, 2, 3],
186
+ # [ 4, 5, 6, 7],
187
+ # [ 8, 9, 10, 11],
188
+ # [12, 13, 14, 15]],
189
+ # [[16, 17, 18, 19],
190
+ # [20, 21, 22, 23],
191
+ # [24, 25, 26, 27],
192
+ # [28, 29, 30, 31]],
193
+ # [[32, 33, 34, 35],
194
+ # [36, 37, 38, 39],
195
+ # [40, 41, 42, 43],
196
+ # [44, 45, 46, 47],
197
+ # [[48, 49, 50, 51],
198
+ # [52, 53, 54, 55],
199
+ # [56, 57, 58, 59],
200
+ # [60, 61, 62, 63]]]]
201
+ values = np.arange(64, dtype=np.float32).reshape((4, 4, 4))
202
+
203
+ indices = wp.array([0, 3], dtype=int, device=device)
204
+
205
+ # -----
206
+
207
+ arr = wp.array(data=values, device=device)
208
+ iarr = wp.indexedarray(arr, [indices, None, None])
209
+ test.assertEqual(iarr.dtype, arr.dtype)
210
+ test.assertEqual(iarr.ndim, 3)
211
+ test.assertEqual(iarr.shape, (2, 4, 4))
212
+ test.assertEqual(iarr.size, 32)
213
+
214
+ expected_values = [
215
+ [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11], [12, 13, 14, 15]],
216
+ [[48, 49, 50, 51], [52, 53, 54, 55], [56, 57, 58, 59], [60, 61, 62, 63]],
217
+ ]
218
+ expected_arr = wp.array(data=expected_values, dtype=float, device=device)
219
+
220
+ wp.launch(kernel_3d, dim=iarr.shape, inputs=[iarr, expected_arr], device=device)
221
+
222
+ # -----
223
+
224
+ arr = wp.array(data=values, device=device)
225
+ iarr = wp.indexedarray(arr, [indices, indices, None])
226
+ test.assertEqual(iarr.dtype, arr.dtype)
227
+ test.assertEqual(iarr.ndim, 3)
228
+ test.assertEqual(iarr.shape, (2, 2, 4))
229
+ test.assertEqual(iarr.size, 16)
230
+
231
+ expected_values = [[[0, 1, 2, 3], [12, 13, 14, 15]], [[48, 49, 50, 51], [60, 61, 62, 63]]]
232
+ expected_arr = wp.array(data=expected_values, dtype=float, device=device)
233
+
234
+ wp.launch(kernel_3d, dim=iarr.shape, inputs=[iarr, expected_arr], device=device)
235
+
236
+ # -----
237
+
238
+ arr = wp.array(data=values, device=device)
239
+ iarr = wp.indexedarray(arr, [indices, None, indices])
240
+ test.assertEqual(iarr.dtype, arr.dtype)
241
+ test.assertEqual(iarr.ndim, 3)
242
+ test.assertEqual(iarr.shape, (2, 4, 2))
243
+ test.assertEqual(iarr.size, 16)
244
+
245
+ expected_values = [[[0, 3], [4, 7], [8, 11], [12, 15]], [[48, 51], [52, 55], [56, 59], [60, 63]]]
246
+ expected_arr = wp.array(data=expected_values, dtype=float, device=device)
247
+
248
+ wp.launch(kernel_3d, dim=iarr.shape, inputs=[iarr, expected_arr], device=device)
249
+
250
+ # -----
251
+
252
+ arr = wp.array(data=values, device=device)
253
+ iarr = wp.indexedarray(arr, [None, indices, indices])
254
+ test.assertEqual(iarr.dtype, arr.dtype)
255
+ test.assertEqual(iarr.ndim, 3)
256
+ test.assertEqual(iarr.shape, (4, 2, 2))
257
+ test.assertEqual(iarr.size, 16)
258
+
259
+ expected_values = [[[0, 3], [12, 15]], [[16, 19], [28, 31]], [[32, 35], [44, 47]], [[48, 51], [60, 63]]]
260
+ expected_arr = wp.array(data=expected_values, dtype=float, device=device)
261
+
262
+ wp.launch(kernel_3d, dim=iarr.shape, inputs=[iarr, expected_arr], device=device)
263
+
264
+
265
+ vec2i = wp.types.vector(length=2, dtype=wp.int32)
266
+ vec3i = wp.types.vector(length=3, dtype=wp.int32)
267
+ vec4i = wp.types.vector(length=4, dtype=wp.int32)
268
+
269
+
270
+ @wp.kernel
271
+ def shape_kernel_1d(arr: wp.indexedarray1d(dtype=float), expected: int):
272
+ wp.expect_eq(arr.shape[0], expected)
273
+
274
+
275
+ @wp.kernel
276
+ def shape_kernel_2d(arr: wp.indexedarray2d(dtype=float), expected: vec2i):
277
+ wp.expect_eq(arr.shape[0], expected[0])
278
+ wp.expect_eq(arr.shape[1], expected[1])
279
+
280
+ # 1d slice
281
+ view = arr[0]
282
+ wp.expect_eq(view.shape[0], expected[1])
283
+
284
+
285
+ @wp.kernel
286
+ def shape_kernel_3d(arr: wp.indexedarray3d(dtype=float), expected: vec3i):
287
+ wp.expect_eq(arr.shape[0], expected[0])
288
+ wp.expect_eq(arr.shape[1], expected[1])
289
+ wp.expect_eq(arr.shape[2], expected[2])
290
+
291
+ # 2d slice
292
+ view2 = arr[0]
293
+ wp.expect_eq(view2.shape[0], expected[1])
294
+ wp.expect_eq(view2.shape[1], expected[2])
295
+
296
+ # 1d slice
297
+ view1 = arr[0, 0]
298
+ wp.expect_eq(view1.shape[0], expected[2])
299
+
300
+
301
+ @wp.kernel
302
+ def shape_kernel_4d(arr: wp.indexedarray4d(dtype=float), expected: vec4i):
303
+ wp.expect_eq(arr.shape[0], expected[0])
304
+ wp.expect_eq(arr.shape[1], expected[1])
305
+ wp.expect_eq(arr.shape[2], expected[2])
306
+ wp.expect_eq(arr.shape[3], expected[3])
307
+
308
+ # 3d slice
309
+ view3 = arr[0]
310
+ wp.expect_eq(view3.shape[0], expected[1])
311
+ wp.expect_eq(view3.shape[1], expected[2])
312
+ wp.expect_eq(view3.shape[2], expected[3])
313
+
314
+ # 2d slice
315
+ view2 = arr[0, 0]
316
+ wp.expect_eq(view2.shape[0], expected[2])
317
+ wp.expect_eq(view2.shape[1], expected[3])
318
+
319
+ # 1d slice
320
+ view1 = arr[0, 0, 0]
321
+ wp.expect_eq(view1.shape[0], expected[3])
322
+
323
+
324
+ def test_indexedarray_shape(test, device):
325
+ with wp.ScopedDevice(device):
326
+ data1 = wp.zeros(10, dtype=float)
327
+ data2 = wp.zeros((10, 20), dtype=float)
328
+ data3 = wp.zeros((10, 20, 30), dtype=float)
329
+ data4 = wp.zeros((10, 20, 30, 40), dtype=float)
330
+
331
+ indices1 = wp.array(data=[2, 7], dtype=int)
332
+ indices2 = wp.array(data=[2, 7, 12, 17], dtype=int)
333
+ indices3 = wp.array(data=[2, 7, 12, 17, 22, 27], dtype=int)
334
+ indices4 = wp.array(data=[2, 7, 12, 17, 22, 27, 32, 37], dtype=int)
335
+
336
+ ia1 = wp.indexedarray(data1, [indices1])
337
+ wp.launch(shape_kernel_1d, dim=1, inputs=[ia1, 2])
338
+
339
+ ia2_1 = wp.indexedarray(data2, [indices1, None])
340
+ ia2_2 = wp.indexedarray(data2, [None, indices2])
341
+ ia2_3 = wp.indexedarray(data2, [indices1, indices2])
342
+ wp.launch(shape_kernel_2d, dim=1, inputs=[ia2_1, vec2i(2, 20)])
343
+ wp.launch(shape_kernel_2d, dim=1, inputs=[ia2_2, vec2i(10, 4)])
344
+ wp.launch(shape_kernel_2d, dim=1, inputs=[ia2_3, vec2i(2, 4)])
345
+
346
+ ia3_1 = wp.indexedarray(data3, [indices1, None, None])
347
+ ia3_2 = wp.indexedarray(data3, [None, indices2, None])
348
+ ia3_3 = wp.indexedarray(data3, [None, None, indices3])
349
+ ia3_4 = wp.indexedarray(data3, [indices1, indices2, None])
350
+ ia3_5 = wp.indexedarray(data3, [indices1, None, indices3])
351
+ ia3_6 = wp.indexedarray(data3, [None, indices2, indices3])
352
+ ia3_7 = wp.indexedarray(data3, [indices1, indices2, indices3])
353
+ wp.launch(shape_kernel_3d, dim=1, inputs=[ia3_1, vec3i(2, 20, 30)])
354
+ wp.launch(shape_kernel_3d, dim=1, inputs=[ia3_2, vec3i(10, 4, 30)])
355
+ wp.launch(shape_kernel_3d, dim=1, inputs=[ia3_3, vec3i(10, 20, 6)])
356
+ wp.launch(shape_kernel_3d, dim=1, inputs=[ia3_4, vec3i(2, 4, 30)])
357
+ wp.launch(shape_kernel_3d, dim=1, inputs=[ia3_5, vec3i(2, 20, 6)])
358
+ wp.launch(shape_kernel_3d, dim=1, inputs=[ia3_6, vec3i(10, 4, 6)])
359
+ wp.launch(shape_kernel_3d, dim=1, inputs=[ia3_7, vec3i(2, 4, 6)])
360
+
361
+ ia4_1 = wp.indexedarray(data4, [indices1, None, None, None])
362
+ ia4_2 = wp.indexedarray(data4, [indices1, None, None, indices4])
363
+ ia4_3 = wp.indexedarray(data4, [None, indices2, indices3, None])
364
+ ia4_4 = wp.indexedarray(data4, [indices1, indices2, indices3, indices4])
365
+ wp.launch(shape_kernel_4d, dim=1, inputs=[ia4_1, vec4i(2, 20, 30, 40)])
366
+ wp.launch(shape_kernel_4d, dim=1, inputs=[ia4_2, vec4i(2, 20, 30, 8)])
367
+ wp.launch(shape_kernel_4d, dim=1, inputs=[ia4_3, vec4i(10, 4, 6, 40)])
368
+ wp.launch(shape_kernel_4d, dim=1, inputs=[ia4_4, vec4i(2, 4, 6, 8)])
369
+
370
+ wp.synchronize_device(device)
371
+
372
+
373
+ def test_indexedarray_getitem(test, device):
374
+ with wp.ScopedDevice(device):
375
+ data = wp.array(data=np.arange(1000, dtype=np.int32).reshape((10, 10, 10)))
376
+
377
+ I = wp.array(data=[0, 1, 2], dtype=int)
378
+
379
+ # use constructor
380
+ a1 = wp.indexedarray(data, [None, None, I])
381
+ a2 = wp.indexedarray(data, [None, I])
382
+ a3 = wp.indexedarray(data, [None, I, I])
383
+ a4 = wp.indexedarray(data, [I])
384
+ a5 = wp.indexedarray(data, [I, None, I])
385
+ a6 = wp.indexedarray(data, [I, I])
386
+ a7 = wp.indexedarray(data, [I, I, I])
387
+
388
+ # use array.__getitem__()
389
+ b1 = data[:, :, I]
390
+ b2 = data[:, I]
391
+ b3 = data[:, I, I]
392
+ b4 = data[I]
393
+ b5 = data[I, :, I]
394
+ b6 = data[I, I]
395
+ b7 = data[I, I, I]
396
+
397
+ test.assertEqual(type(a1), type(b1))
398
+ test.assertEqual(type(a2), type(b2))
399
+ test.assertEqual(type(a3), type(b3))
400
+ test.assertEqual(type(a4), type(b4))
401
+ test.assertEqual(type(a5), type(b5))
402
+ test.assertEqual(type(a6), type(b6))
403
+ test.assertEqual(type(a7), type(b7))
404
+
405
+ assert_np_equal(a1.numpy(), b1.numpy())
406
+ assert_np_equal(a2.numpy(), b2.numpy())
407
+ assert_np_equal(a3.numpy(), b3.numpy())
408
+ assert_np_equal(a4.numpy(), b4.numpy())
409
+ assert_np_equal(a5.numpy(), b5.numpy())
410
+ assert_np_equal(a6.numpy(), b6.numpy())
411
+ assert_np_equal(a7.numpy(), b7.numpy())
412
+
413
+
414
+ def test_indexedarray_slicing(test, device):
415
+ with wp.ScopedDevice(device):
416
+ data = wp.array(data=np.arange(1000, dtype=np.int32).reshape((10, 10, 10)))
417
+
418
+ # test equivalence of slicing and indexing the same range
419
+ s = slice(0, 3)
420
+ I = wp.array(data=[0, 1, 2], dtype=int)
421
+
422
+ a0 = data[s, s, s]
423
+ test.assertEqual(type(a0), wp.array)
424
+ a1 = data[s, s, I]
425
+ test.assertEqual(type(a1), wp.indexedarray)
426
+ a2 = data[s, I, s]
427
+ test.assertEqual(type(a2), wp.indexedarray)
428
+ a3 = data[s, I, I]
429
+ test.assertEqual(type(a3), wp.indexedarray)
430
+ a4 = data[I, s, s]
431
+ test.assertEqual(type(a4), wp.indexedarray)
432
+ a5 = data[I, s, I]
433
+ test.assertEqual(type(a5), wp.indexedarray)
434
+ a6 = data[I, I, s]
435
+ test.assertEqual(type(a6), wp.indexedarray)
436
+ a7 = data[I, I, I]
437
+ test.assertEqual(type(a7), wp.indexedarray)
438
+
439
+ expected = a0.numpy()
440
+
441
+ assert_np_equal(a1.numpy(), expected)
442
+ assert_np_equal(a2.numpy(), expected)
443
+ assert_np_equal(a3.numpy(), expected)
444
+ assert_np_equal(a4.numpy(), expected)
445
+ assert_np_equal(a5.numpy(), expected)
446
+ assert_np_equal(a6.numpy(), expected)
447
+ assert_np_equal(a7.numpy(), expected)
448
+
449
+
450
+ # generic increment kernels that work with any array (regular or indexed)
451
+ @wp.kernel
452
+ def inc_1d(a: Any):
453
+ i = wp.tid()
454
+ a[i] = a[i] + 1
455
+
456
+
457
+ @wp.kernel
458
+ def inc_2d(a: Any):
459
+ i, j = wp.tid()
460
+ a[i, j] = a[i, j] + 1
461
+
462
+
463
+ @wp.kernel
464
+ def inc_3d(a: Any):
465
+ i, j, k = wp.tid()
466
+ a[i, j, k] = a[i, j, k] + 1
467
+
468
+
469
+ @wp.kernel
470
+ def inc_4d(a: Any):
471
+ i, j, k, l = wp.tid()
472
+ a[i, j, k, l] = a[i, j, k, l] + 1
473
+
474
+
475
+ # optional overloads to avoid module reloading
476
+ wp.overload(inc_1d, [wp.array1d(dtype=int)])
477
+ wp.overload(inc_2d, [wp.array2d(dtype=int)])
478
+ wp.overload(inc_3d, [wp.array3d(dtype=int)])
479
+ wp.overload(inc_4d, [wp.array4d(dtype=int)])
480
+
481
+ wp.overload(inc_1d, [wp.indexedarray1d(dtype=int)])
482
+ wp.overload(inc_2d, [wp.indexedarray2d(dtype=int)])
483
+ wp.overload(inc_3d, [wp.indexedarray3d(dtype=int)])
484
+ wp.overload(inc_4d, [wp.indexedarray4d(dtype=int)])
485
+
486
+
487
+ def test_indexedarray_generics(test, device):
488
+ with wp.ScopedDevice(device):
489
+ data1 = wp.zeros((5,), dtype=int)
490
+ data2 = wp.zeros((5, 5), dtype=int)
491
+ data3 = wp.zeros((5, 5, 5), dtype=int)
492
+ data4 = wp.zeros((5, 5, 5, 5), dtype=int)
493
+
494
+ indices = wp.array(data=[0, 4], dtype=int)
495
+
496
+ ia1 = wp.indexedarray(data1, [indices])
497
+ ia2 = wp.indexedarray(data2, [indices, indices])
498
+ ia3 = wp.indexedarray(data3, [indices, indices, indices])
499
+ ia4 = wp.indexedarray(data4, [indices, indices, indices, indices])
500
+
501
+ wp.launch(inc_1d, dim=data1.shape, inputs=[data1])
502
+ wp.launch(inc_2d, dim=data2.shape, inputs=[data2])
503
+ wp.launch(inc_3d, dim=data3.shape, inputs=[data3])
504
+ wp.launch(inc_4d, dim=data4.shape, inputs=[data4])
505
+
506
+ wp.launch(inc_1d, dim=ia1.shape, inputs=[ia1])
507
+ wp.launch(inc_2d, dim=ia2.shape, inputs=[ia2])
508
+ wp.launch(inc_3d, dim=ia3.shape, inputs=[ia3])
509
+ wp.launch(inc_4d, dim=ia4.shape, inputs=[ia4])
510
+
511
+ expected1 = np.ones(5, dtype=np.int32)
512
+ expected1[0] = 2
513
+ expected1[4] = 2
514
+
515
+ expected2 = np.ones((5, 5), dtype=np.int32)
516
+ expected2[0, 0] = 2
517
+ expected2[0, 4] = 2
518
+ expected2[4, 0] = 2
519
+ expected2[4, 4] = 2
520
+
521
+ expected3 = np.ones((5, 5, 5), dtype=np.int32)
522
+ expected3[0, 0, 0] = 2
523
+ expected3[0, 0, 4] = 2
524
+ expected3[0, 4, 0] = 2
525
+ expected3[0, 4, 4] = 2
526
+ expected3[4, 0, 0] = 2
527
+ expected3[4, 0, 4] = 2
528
+ expected3[4, 4, 0] = 2
529
+ expected3[4, 4, 4] = 2
530
+
531
+ expected4 = np.ones((5, 5, 5, 5), dtype=np.int32)
532
+ expected4[0, 0, 0, 0] = 2
533
+ expected4[0, 0, 0, 4] = 2
534
+ expected4[0, 0, 4, 0] = 2
535
+ expected4[0, 0, 4, 4] = 2
536
+ expected4[0, 4, 0, 0] = 2
537
+ expected4[0, 4, 0, 4] = 2
538
+ expected4[0, 4, 4, 0] = 2
539
+ expected4[0, 4, 4, 4] = 2
540
+ expected4[4, 0, 0, 0] = 2
541
+ expected4[4, 0, 0, 4] = 2
542
+ expected4[4, 0, 4, 0] = 2
543
+ expected4[4, 0, 4, 4] = 2
544
+ expected4[4, 4, 0, 0] = 2
545
+ expected4[4, 4, 0, 4] = 2
546
+ expected4[4, 4, 4, 0] = 2
547
+ expected4[4, 4, 4, 4] = 2
548
+
549
+ assert_np_equal(data1.numpy(), expected1)
550
+ assert_np_equal(data2.numpy(), expected2)
551
+ assert_np_equal(data3.numpy(), expected3)
552
+ assert_np_equal(data4.numpy(), expected4)
553
+
554
+ assert_np_equal(ia1.numpy(), np.full((2,), 2, dtype=np.int32))
555
+ assert_np_equal(ia2.numpy(), np.full((2, 2), 2, dtype=np.int32))
556
+ assert_np_equal(ia3.numpy(), np.full((2, 2, 2), 2, dtype=np.int32))
557
+ assert_np_equal(ia4.numpy(), np.full((2, 2, 2, 2), 2, dtype=np.int32))
558
+
559
+
560
+ def test_indexedarray_empty(test, device):
561
+ # Test whether common operations work with empty (zero-sized) indexed arrays
562
+ # without throwing exceptions.
563
+
564
+ def test_empty_ops(ndim, nrows, ncols, wptype, nptype):
565
+ data_shape = (1,) * ndim
566
+ dtype_shape = ()
567
+
568
+ if wptype in wp.types.scalar_types:
569
+ # scalar, vector, or matrix
570
+ if ncols > 0:
571
+ if nrows > 0:
572
+ wptype = wp.types.matrix((nrows, ncols), wptype)
573
+ else:
574
+ wptype = wp.types.vector(ncols, wptype)
575
+ dtype_shape = wptype._shape_
576
+ fill_value = wptype(42)
577
+ else:
578
+ # struct
579
+ fill_value = wptype()
580
+
581
+ # create a data array
582
+ data = wp.empty(data_shape, dtype=wptype, device=device, requires_grad=True)
583
+
584
+ # create a zero-sized array of indices
585
+ indices = wp.empty(0, dtype=int, device=device)
586
+
587
+ a = data[indices]
588
+
589
+ # we expect dim to be zero for the empty indexed array, unchanged otherwise
590
+ expected_shape = (0, *data_shape[1:])
591
+
592
+ test.assertEqual(a.size, 0)
593
+ test.assertEqual(a.shape, expected_shape)
594
+
595
+ # all of these methods should succeed with zero-sized arrays
596
+ a.zero_()
597
+ a.fill_(fill_value)
598
+ b = a.contiguous()
599
+
600
+ b = wp.empty_like(a)
601
+ b = wp.zeros_like(a)
602
+ b = wp.full_like(a, fill_value)
603
+ b = wp.clone(a)
604
+
605
+ wp.copy(a, b)
606
+ a.assign(b)
607
+
608
+ na = a.numpy()
609
+ test.assertEqual(na.size, 0)
610
+ test.assertEqual(na.shape, (*expected_shape, *dtype_shape))
611
+ test.assertEqual(na.dtype, nptype)
612
+
613
+ test.assertEqual(a.list(), [])
614
+
615
+ for ndim in range(1, 5):
616
+ # test with scalars, vectors, and matrices
617
+ for nptype, wptype in wp.types.np_dtype_to_warp_type.items():
618
+ # scalars
619
+ test_empty_ops(ndim, 0, 0, wptype, nptype)
620
+
621
+ for ncols in [2, 3, 4, 5]:
622
+ # vectors
623
+ test_empty_ops(ndim, 0, ncols, wptype, nptype)
624
+ # square matrices
625
+ test_empty_ops(ndim, ncols, ncols, wptype, nptype)
626
+
627
+ # non-square matrices
628
+ test_empty_ops(ndim, 2, 3, wptype, nptype)
629
+ test_empty_ops(ndim, 3, 2, wptype, nptype)
630
+ test_empty_ops(ndim, 3, 4, wptype, nptype)
631
+ test_empty_ops(ndim, 4, 3, wptype, nptype)
632
+
633
+ # test with structs
634
+ test_empty_ops(ndim, 0, 0, FillStruct, FillStruct.numpy_dtype())
635
+
636
+
637
+ def test_indexedarray_fill_scalar(test, device):
638
+ dim_x = 4
639
+
640
+ for nptype, wptype in wp.types.np_dtype_to_warp_type.items():
641
+ data1 = wp.zeros(dim_x, dtype=wptype, device=device)
642
+ data2 = wp.zeros((dim_x, dim_x), dtype=wptype, device=device)
643
+ data3 = wp.zeros((dim_x, dim_x, dim_x), dtype=wptype, device=device)
644
+ data4 = wp.zeros((dim_x, dim_x, dim_x, dim_x), dtype=wptype, device=device)
645
+
646
+ indices = wp.array(np.arange(0, dim_x, 2, dtype=np.int32), device=device)
647
+
648
+ a1 = data1[indices]
649
+ a2 = data2[indices]
650
+ a3 = data3[indices]
651
+ a4 = data4[indices]
652
+
653
+ assert_np_equal(a1.numpy(), np.zeros(a1.shape, dtype=nptype))
654
+ assert_np_equal(a2.numpy(), np.zeros(a2.shape, dtype=nptype))
655
+ assert_np_equal(a3.numpy(), np.zeros(a3.shape, dtype=nptype))
656
+ assert_np_equal(a4.numpy(), np.zeros(a4.shape, dtype=nptype))
657
+
658
+ # fill with int value
659
+ fill_value = 42
660
+
661
+ a1.fill_(fill_value)
662
+ a2.fill_(fill_value)
663
+ a3.fill_(fill_value)
664
+ a4.fill_(fill_value)
665
+
666
+ assert_np_equal(a1.numpy(), np.full(a1.shape, fill_value, dtype=nptype))
667
+ assert_np_equal(a2.numpy(), np.full(a2.shape, fill_value, dtype=nptype))
668
+ assert_np_equal(a3.numpy(), np.full(a3.shape, fill_value, dtype=nptype))
669
+ assert_np_equal(a4.numpy(), np.full(a4.shape, fill_value, dtype=nptype))
670
+
671
+ a1.zero_()
672
+ a2.zero_()
673
+ a3.zero_()
674
+ a4.zero_()
675
+
676
+ assert_np_equal(a1.numpy(), np.zeros(a1.shape, dtype=nptype))
677
+ assert_np_equal(a2.numpy(), np.zeros(a2.shape, dtype=nptype))
678
+ assert_np_equal(a3.numpy(), np.zeros(a3.shape, dtype=nptype))
679
+ assert_np_equal(a4.numpy(), np.zeros(a4.shape, dtype=nptype))
680
+
681
+ if wptype in wp.types.float_types:
682
+ # fill with float value
683
+ fill_value = 13.37
684
+
685
+ a1.fill_(fill_value)
686
+ a2.fill_(fill_value)
687
+ a3.fill_(fill_value)
688
+ a4.fill_(fill_value)
689
+
690
+ assert_np_equal(a1.numpy(), np.full(a1.shape, fill_value, dtype=nptype))
691
+ assert_np_equal(a2.numpy(), np.full(a2.shape, fill_value, dtype=nptype))
692
+ assert_np_equal(a3.numpy(), np.full(a3.shape, fill_value, dtype=nptype))
693
+ assert_np_equal(a4.numpy(), np.full(a4.shape, fill_value, dtype=nptype))
694
+
695
+ # fill with Warp scalar value
696
+ fill_value = wptype(17)
697
+
698
+ a1.fill_(fill_value)
699
+ a2.fill_(fill_value)
700
+ a3.fill_(fill_value)
701
+ a4.fill_(fill_value)
702
+
703
+ assert_np_equal(a1.numpy(), np.full(a1.shape, fill_value.value, dtype=nptype))
704
+ assert_np_equal(a2.numpy(), np.full(a2.shape, fill_value.value, dtype=nptype))
705
+ assert_np_equal(a3.numpy(), np.full(a3.shape, fill_value.value, dtype=nptype))
706
+ assert_np_equal(a4.numpy(), np.full(a4.shape, fill_value.value, dtype=nptype))
707
+
708
+
709
+ def test_indexedarray_fill_vector(test, device):
710
+ # test filling a vector array with scalar or vector values (vec_type, list, or numpy array)
711
+
712
+ dim_x = 4
713
+
714
+ for nptype, wptype in wp.types.np_dtype_to_warp_type.items():
715
+ # vector types
716
+ vector_types = [
717
+ wp.types.vector(2, wptype),
718
+ wp.types.vector(3, wptype),
719
+ wp.types.vector(4, wptype),
720
+ wp.types.vector(5, wptype),
721
+ ]
722
+
723
+ for vec_type in vector_types:
724
+ vec_len = vec_type._length_
725
+
726
+ data1 = wp.zeros(dim_x, dtype=vec_type, device=device)
727
+ data2 = wp.zeros((dim_x, dim_x), dtype=vec_type, device=device)
728
+ data3 = wp.zeros((dim_x, dim_x, dim_x), dtype=vec_type, device=device)
729
+ data4 = wp.zeros((dim_x, dim_x, dim_x, dim_x), dtype=vec_type, device=device)
730
+
731
+ indices = wp.array(np.arange(0, dim_x, 2, dtype=np.int32), device=device)
732
+
733
+ a1 = data1[indices]
734
+ a2 = data2[indices]
735
+ a3 = data3[indices]
736
+ a4 = data4[indices]
737
+
738
+ assert_np_equal(a1.numpy(), np.zeros((*a1.shape, vec_len), dtype=nptype))
739
+ assert_np_equal(a2.numpy(), np.zeros((*a2.shape, vec_len), dtype=nptype))
740
+ assert_np_equal(a3.numpy(), np.zeros((*a3.shape, vec_len), dtype=nptype))
741
+ assert_np_equal(a4.numpy(), np.zeros((*a4.shape, vec_len), dtype=nptype))
742
+
743
+ # fill with int scalar
744
+ fill_value = 42
745
+
746
+ a1.fill_(fill_value)
747
+ a2.fill_(fill_value)
748
+ a3.fill_(fill_value)
749
+ a4.fill_(fill_value)
750
+
751
+ assert_np_equal(a1.numpy(), np.full((*a1.shape, vec_len), fill_value, dtype=nptype))
752
+ assert_np_equal(a2.numpy(), np.full((*a2.shape, vec_len), fill_value, dtype=nptype))
753
+ assert_np_equal(a3.numpy(), np.full((*a3.shape, vec_len), fill_value, dtype=nptype))
754
+ assert_np_equal(a4.numpy(), np.full((*a4.shape, vec_len), fill_value, dtype=nptype))
755
+
756
+ # test zeroing
757
+ a1.zero_()
758
+ a2.zero_()
759
+ a3.zero_()
760
+ a4.zero_()
761
+
762
+ assert_np_equal(a1.numpy(), np.zeros((*a1.shape, vec_len), dtype=nptype))
763
+ assert_np_equal(a2.numpy(), np.zeros((*a2.shape, vec_len), dtype=nptype))
764
+ assert_np_equal(a3.numpy(), np.zeros((*a3.shape, vec_len), dtype=nptype))
765
+ assert_np_equal(a4.numpy(), np.zeros((*a4.shape, vec_len), dtype=nptype))
766
+
767
+ # vector values can be passed as a list, numpy array, or Warp vector instance
768
+ fill_list = [17, 42, 99, 101, 127][:vec_len]
769
+ fill_arr = np.array(fill_list, dtype=nptype)
770
+ fill_vec = vec_type(fill_list)
771
+
772
+ expected1 = np.tile(fill_arr, a1.size).reshape((*a1.shape, vec_len))
773
+ expected2 = np.tile(fill_arr, a2.size).reshape((*a2.shape, vec_len))
774
+ expected3 = np.tile(fill_arr, a3.size).reshape((*a3.shape, vec_len))
775
+ expected4 = np.tile(fill_arr, a4.size).reshape((*a4.shape, vec_len))
776
+
777
+ # fill with list of vector length
778
+ a1.fill_(fill_list)
779
+ a2.fill_(fill_list)
780
+ a3.fill_(fill_list)
781
+ a4.fill_(fill_list)
782
+
783
+ assert_np_equal(a1.numpy(), expected1)
784
+ assert_np_equal(a2.numpy(), expected2)
785
+ assert_np_equal(a3.numpy(), expected3)
786
+ assert_np_equal(a4.numpy(), expected4)
787
+
788
+ # clear
789
+ a1.zero_()
790
+ a2.zero_()
791
+ a3.zero_()
792
+ a4.zero_()
793
+
794
+ # fill with numpy array of vector length
795
+ a1.fill_(fill_arr)
796
+ a2.fill_(fill_arr)
797
+ a3.fill_(fill_arr)
798
+ a4.fill_(fill_arr)
799
+
800
+ assert_np_equal(a1.numpy(), expected1)
801
+ assert_np_equal(a2.numpy(), expected2)
802
+ assert_np_equal(a3.numpy(), expected3)
803
+ assert_np_equal(a4.numpy(), expected4)
804
+
805
+ # clear
806
+ a1.zero_()
807
+ a2.zero_()
808
+ a3.zero_()
809
+ a4.zero_()
810
+
811
+ # fill with vec instance
812
+ a1.fill_(fill_vec)
813
+ a2.fill_(fill_vec)
814
+ a3.fill_(fill_vec)
815
+ a4.fill_(fill_vec)
816
+
817
+ assert_np_equal(a1.numpy(), expected1)
818
+ assert_np_equal(a2.numpy(), expected2)
819
+ assert_np_equal(a3.numpy(), expected3)
820
+ assert_np_equal(a4.numpy(), expected4)
821
+
822
+ if wptype in wp.types.float_types:
823
+ # fill with float scalar
824
+ fill_value = 13.37
825
+
826
+ a1.fill_(fill_value)
827
+ a2.fill_(fill_value)
828
+ a3.fill_(fill_value)
829
+ a4.fill_(fill_value)
830
+
831
+ assert_np_equal(a1.numpy(), np.full((*a1.shape, vec_len), fill_value, dtype=nptype))
832
+ assert_np_equal(a2.numpy(), np.full((*a2.shape, vec_len), fill_value, dtype=nptype))
833
+ assert_np_equal(a3.numpy(), np.full((*a3.shape, vec_len), fill_value, dtype=nptype))
834
+ assert_np_equal(a4.numpy(), np.full((*a4.shape, vec_len), fill_value, dtype=nptype))
835
+
836
+ # fill with float list of vector length
837
+ fill_list = [-2.5, -1.25, 1.25, 2.5, 5.0][:vec_len]
838
+
839
+ a1.fill_(fill_list)
840
+ a2.fill_(fill_list)
841
+ a3.fill_(fill_list)
842
+ a4.fill_(fill_list)
843
+
844
+ expected1 = np.tile(np.array(fill_list, dtype=nptype), a1.size).reshape((*a1.shape, vec_len))
845
+ expected2 = np.tile(np.array(fill_list, dtype=nptype), a2.size).reshape((*a2.shape, vec_len))
846
+ expected3 = np.tile(np.array(fill_list, dtype=nptype), a3.size).reshape((*a3.shape, vec_len))
847
+ expected4 = np.tile(np.array(fill_list, dtype=nptype), a4.size).reshape((*a4.shape, vec_len))
848
+
849
+ assert_np_equal(a1.numpy(), expected1)
850
+ assert_np_equal(a2.numpy(), expected2)
851
+ assert_np_equal(a3.numpy(), expected3)
852
+ assert_np_equal(a4.numpy(), expected4)
853
+
854
+
855
+ def test_indexedarray_fill_matrix(test, device):
856
+ # test filling a matrix array with scalar or matrix values (mat_type, nested list, or 2d numpy array)
857
+
858
+ dim_x = 4
859
+
860
+ for nptype, wptype in wp.types.np_dtype_to_warp_type.items():
861
+ # matrix types
862
+ matrix_types = [
863
+ # square matrices
864
+ wp.types.matrix((2, 2), wptype),
865
+ wp.types.matrix((3, 3), wptype),
866
+ wp.types.matrix((4, 4), wptype),
867
+ wp.types.matrix((5, 5), wptype),
868
+ # non-square matrices
869
+ wp.types.matrix((2, 3), wptype),
870
+ wp.types.matrix((3, 2), wptype),
871
+ wp.types.matrix((3, 4), wptype),
872
+ wp.types.matrix((4, 3), wptype),
873
+ ]
874
+
875
+ for mat_type in matrix_types:
876
+ mat_len = mat_type._length_
877
+ mat_shape = mat_type._shape_
878
+
879
+ data1 = wp.zeros(dim_x, dtype=mat_type, device=device)
880
+ data2 = wp.zeros((dim_x, dim_x), dtype=mat_type, device=device)
881
+ data3 = wp.zeros((dim_x, dim_x, dim_x), dtype=mat_type, device=device)
882
+ data4 = wp.zeros((dim_x, dim_x, dim_x, dim_x), dtype=mat_type, device=device)
883
+
884
+ indices = wp.array(np.arange(0, dim_x, 2, dtype=np.int32), device=device)
885
+
886
+ a1 = data1[indices]
887
+ a2 = data2[indices]
888
+ a3 = data3[indices]
889
+ a4 = data4[indices]
890
+
891
+ assert_np_equal(a1.numpy(), np.zeros((*a1.shape, *mat_shape), dtype=nptype))
892
+ assert_np_equal(a2.numpy(), np.zeros((*a2.shape, *mat_shape), dtype=nptype))
893
+ assert_np_equal(a3.numpy(), np.zeros((*a3.shape, *mat_shape), dtype=nptype))
894
+ assert_np_equal(a4.numpy(), np.zeros((*a4.shape, *mat_shape), dtype=nptype))
895
+
896
+ # fill with scalar
897
+ fill_value = 42
898
+
899
+ a1.fill_(fill_value)
900
+ a2.fill_(fill_value)
901
+ a3.fill_(fill_value)
902
+ a4.fill_(fill_value)
903
+
904
+ assert_np_equal(a1.numpy(), np.full((*a1.shape, *mat_shape), fill_value, dtype=nptype))
905
+ assert_np_equal(a2.numpy(), np.full((*a2.shape, *mat_shape), fill_value, dtype=nptype))
906
+ assert_np_equal(a3.numpy(), np.full((*a3.shape, *mat_shape), fill_value, dtype=nptype))
907
+ assert_np_equal(a4.numpy(), np.full((*a4.shape, *mat_shape), fill_value, dtype=nptype))
908
+
909
+ # test zeroing
910
+ a1.zero_()
911
+ a2.zero_()
912
+ a3.zero_()
913
+ a4.zero_()
914
+
915
+ assert_np_equal(a1.numpy(), np.zeros((*a1.shape, *mat_shape), dtype=nptype))
916
+ assert_np_equal(a2.numpy(), np.zeros((*a2.shape, *mat_shape), dtype=nptype))
917
+ assert_np_equal(a3.numpy(), np.zeros((*a3.shape, *mat_shape), dtype=nptype))
918
+ assert_np_equal(a4.numpy(), np.zeros((*a4.shape, *mat_shape), dtype=nptype))
919
+
920
+ # matrix values can be passed as a 1d numpy array, 2d numpy array, flat list, nested list, or Warp matrix instance
921
+ if wptype != wp.bool:
922
+ fill_arr1 = np.arange(mat_len, dtype=nptype)
923
+ else:
924
+ fill_arr1 = np.ones(mat_len, dtype=nptype)
925
+ fill_arr2 = fill_arr1.reshape(mat_shape)
926
+ fill_list1 = list(fill_arr1)
927
+ fill_list2 = [list(row) for row in fill_arr2]
928
+ fill_mat = mat_type(fill_arr1)
929
+
930
+ expected1 = np.tile(fill_arr1, a1.size).reshape((*a1.shape, *mat_shape))
931
+ expected2 = np.tile(fill_arr1, a2.size).reshape((*a2.shape, *mat_shape))
932
+ expected3 = np.tile(fill_arr1, a3.size).reshape((*a3.shape, *mat_shape))
933
+ expected4 = np.tile(fill_arr1, a4.size).reshape((*a4.shape, *mat_shape))
934
+
935
+ # fill with 1d numpy array
936
+ a1.fill_(fill_arr1)
937
+ a2.fill_(fill_arr1)
938
+ a3.fill_(fill_arr1)
939
+ a4.fill_(fill_arr1)
940
+
941
+ assert_np_equal(a1.numpy(), expected1)
942
+ assert_np_equal(a2.numpy(), expected2)
943
+ assert_np_equal(a3.numpy(), expected3)
944
+ assert_np_equal(a4.numpy(), expected4)
945
+
946
+ # clear
947
+ a1.zero_()
948
+ a2.zero_()
949
+ a3.zero_()
950
+ a4.zero_()
951
+
952
+ # fill with 2d numpy array
953
+ a1.fill_(fill_arr2)
954
+ a2.fill_(fill_arr2)
955
+ a3.fill_(fill_arr2)
956
+ a4.fill_(fill_arr2)
957
+
958
+ assert_np_equal(a1.numpy(), expected1)
959
+ assert_np_equal(a2.numpy(), expected2)
960
+ assert_np_equal(a3.numpy(), expected3)
961
+ assert_np_equal(a4.numpy(), expected4)
962
+
963
+ # clear
964
+ a1.zero_()
965
+ a2.zero_()
966
+ a3.zero_()
967
+ a4.zero_()
968
+
969
+ # fill with flat list
970
+ a1.fill_(fill_list1)
971
+ a2.fill_(fill_list1)
972
+ a3.fill_(fill_list1)
973
+ a4.fill_(fill_list1)
974
+
975
+ assert_np_equal(a1.numpy(), expected1)
976
+ assert_np_equal(a2.numpy(), expected2)
977
+ assert_np_equal(a3.numpy(), expected3)
978
+ assert_np_equal(a4.numpy(), expected4)
979
+
980
+ # clear
981
+ a1.zero_()
982
+ a2.zero_()
983
+ a3.zero_()
984
+ a4.zero_()
985
+
986
+ # fill with nested list
987
+ a1.fill_(fill_list2)
988
+ a2.fill_(fill_list2)
989
+ a3.fill_(fill_list2)
990
+ a4.fill_(fill_list2)
991
+
992
+ assert_np_equal(a1.numpy(), expected1)
993
+ assert_np_equal(a2.numpy(), expected2)
994
+ assert_np_equal(a3.numpy(), expected3)
995
+ assert_np_equal(a4.numpy(), expected4)
996
+
997
+ # clear
998
+ a1.zero_()
999
+ a2.zero_()
1000
+ a3.zero_()
1001
+ a4.zero_()
1002
+
1003
+ # fill with mat instance
1004
+ a1.fill_(fill_mat)
1005
+ a2.fill_(fill_mat)
1006
+ a3.fill_(fill_mat)
1007
+ a4.fill_(fill_mat)
1008
+
1009
+ assert_np_equal(a1.numpy(), expected1)
1010
+ assert_np_equal(a2.numpy(), expected2)
1011
+ assert_np_equal(a3.numpy(), expected3)
1012
+ assert_np_equal(a4.numpy(), expected4)
1013
+
1014
+
1015
+ def test_indexedarray_fill_struct(test, device):
1016
+ dim_x = 8
1017
+
1018
+ nptype = FillStruct.numpy_dtype()
1019
+
1020
+ data1 = wp.zeros(dim_x, dtype=FillStruct, device=device)
1021
+ data2 = wp.zeros((dim_x, dim_x), dtype=FillStruct, device=device)
1022
+ data3 = wp.zeros((dim_x, dim_x, dim_x), dtype=FillStruct, device=device)
1023
+ data4 = wp.zeros((dim_x, dim_x, dim_x, dim_x), dtype=FillStruct, device=device)
1024
+
1025
+ indices = wp.array(np.arange(0, dim_x, 2, dtype=np.int32), device=device)
1026
+
1027
+ a1 = data1[indices]
1028
+ a2 = data2[indices]
1029
+ a3 = data3[indices]
1030
+ a4 = data4[indices]
1031
+
1032
+ assert_np_equal(a1.numpy(), np.zeros(a1.shape, dtype=nptype))
1033
+ assert_np_equal(a2.numpy(), np.zeros(a2.shape, dtype=nptype))
1034
+ assert_np_equal(a3.numpy(), np.zeros(a3.shape, dtype=nptype))
1035
+ assert_np_equal(a4.numpy(), np.zeros(a4.shape, dtype=nptype))
1036
+
1037
+ s = FillStruct()
1038
+
1039
+ # fill with default struct value (should be all zeros)
1040
+ a1.fill_(s)
1041
+ a2.fill_(s)
1042
+ a3.fill_(s)
1043
+ a4.fill_(s)
1044
+
1045
+ assert_np_equal(a1.numpy(), np.zeros(a1.shape, dtype=nptype))
1046
+ assert_np_equal(a2.numpy(), np.zeros(a2.shape, dtype=nptype))
1047
+ assert_np_equal(a3.numpy(), np.zeros(a3.shape, dtype=nptype))
1048
+ assert_np_equal(a4.numpy(), np.zeros(a4.shape, dtype=nptype))
1049
+
1050
+ # scalars
1051
+ s.i1 = -17
1052
+ s.i2 = 42
1053
+ s.i4 = 99
1054
+ s.i8 = 101
1055
+ s.f2 = -1.25
1056
+ s.f4 = 13.37
1057
+ s.f8 = 0.125
1058
+ # vectors
1059
+ s.v2 = [21, 22]
1060
+ s.v3 = [31, 32, 33]
1061
+ s.v4 = [41, 42, 43, 44]
1062
+ s.v5 = [51, 52, 53, 54, 55]
1063
+ # matrices
1064
+ s.m2 = [[61, 62]] * 2
1065
+ s.m3 = [[71, 72, 73]] * 3
1066
+ s.m4 = [[81, 82, 83, 84]] * 4
1067
+ s.m5 = [[91, 92, 93, 94, 95]] * 5
1068
+ # arrays
1069
+ s.a1 = wp.zeros((2,) * 1, dtype=float, device=device)
1070
+ s.a2 = wp.zeros((2,) * 2, dtype=float, device=device)
1071
+ s.a3 = wp.zeros((2,) * 3, dtype=float, device=device)
1072
+ s.a4 = wp.zeros((2,) * 4, dtype=float, device=device)
1073
+
1074
+ # fill with custom struct value
1075
+ a1.fill_(s)
1076
+ a2.fill_(s)
1077
+ a3.fill_(s)
1078
+ a4.fill_(s)
1079
+
1080
+ ns = s.numpy_value()
1081
+
1082
+ expected1 = np.empty(a1.shape, dtype=nptype)
1083
+ expected2 = np.empty(a2.shape, dtype=nptype)
1084
+ expected3 = np.empty(a3.shape, dtype=nptype)
1085
+ expected4 = np.empty(a4.shape, dtype=nptype)
1086
+
1087
+ expected1.fill(ns)
1088
+ expected2.fill(ns)
1089
+ expected3.fill(ns)
1090
+ expected4.fill(ns)
1091
+
1092
+ assert_np_equal(a1.numpy(), expected1)
1093
+ assert_np_equal(a2.numpy(), expected2)
1094
+ assert_np_equal(a3.numpy(), expected3)
1095
+ assert_np_equal(a4.numpy(), expected4)
1096
+
1097
+ # test clearing
1098
+ a1.zero_()
1099
+ a2.zero_()
1100
+ a3.zero_()
1101
+ a4.zero_()
1102
+
1103
+ assert_np_equal(a1.numpy(), np.zeros(a1.shape, dtype=nptype))
1104
+ assert_np_equal(a2.numpy(), np.zeros(a2.shape, dtype=nptype))
1105
+ assert_np_equal(a3.numpy(), np.zeros(a3.shape, dtype=nptype))
1106
+ assert_np_equal(a4.numpy(), np.zeros(a4.shape, dtype=nptype))
1107
+
1108
+
1109
+ devices = get_test_devices()
1110
+
1111
+
1112
+ class TestIndexedArray(unittest.TestCase):
1113
+ pass
1114
+
1115
+
1116
+ add_function_test(TestIndexedArray, "test_indexedarray_1d", test_indexedarray_1d, devices=devices)
1117
+ add_function_test(TestIndexedArray, "test_indexedarray_2d", test_indexedarray_2d, devices=devices)
1118
+ add_function_test(TestIndexedArray, "test_indexedarray_3d", test_indexedarray_3d, devices=devices)
1119
+ add_function_test(TestIndexedArray, "test_indexedarray_4d", test_indexedarray_4d, devices=devices)
1120
+ add_function_test(TestIndexedArray, "test_indexedarray_mixed", test_indexedarray_mixed, devices=devices)
1121
+ add_function_test(TestIndexedArray, "test_indexedarray_shape", test_indexedarray_shape, devices=devices)
1122
+ add_function_test(TestIndexedArray, "test_indexedarray_getitem", test_indexedarray_getitem, devices=devices)
1123
+ add_function_test(TestIndexedArray, "test_indexedarray_slicing", test_indexedarray_slicing, devices=devices)
1124
+ add_function_test(TestIndexedArray, "test_indexedarray_generics", test_indexedarray_generics, devices=devices)
1125
+ add_function_test(TestIndexedArray, "test_indexedarray_empty", test_indexedarray_empty, devices=devices)
1126
+ add_function_test(TestIndexedArray, "test_indexedarray_fill_scalar", test_indexedarray_fill_scalar, devices=devices)
1127
+ add_function_test(TestIndexedArray, "test_indexedarray_fill_vector", test_indexedarray_fill_vector, devices=devices)
1128
+ add_function_test(TestIndexedArray, "test_indexedarray_fill_matrix", test_indexedarray_fill_matrix, devices=devices)
1129
+ add_function_test(TestIndexedArray, "test_indexedarray_fill_struct", test_indexedarray_fill_struct, devices=devices)
1130
+
1131
+
1132
+ if __name__ == "__main__":
1133
+ wp.build.clear_kernel_cache()
1134
+ unittest.main(verbosity=2)