warp-lang 1.7.0__py3-none-manylinux_2_34_aarch64.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 (429) hide show
  1. warp/__init__.py +139 -0
  2. warp/__init__.pyi +1 -0
  3. warp/autograd.py +1142 -0
  4. warp/bin/warp-clang.so +0 -0
  5. warp/bin/warp.so +0 -0
  6. warp/build.py +557 -0
  7. warp/build_dll.py +405 -0
  8. warp/builtins.py +6855 -0
  9. warp/codegen.py +3969 -0
  10. warp/config.py +158 -0
  11. warp/constants.py +57 -0
  12. warp/context.py +6812 -0
  13. warp/dlpack.py +462 -0
  14. warp/examples/__init__.py +24 -0
  15. warp/examples/assets/bear.usd +0 -0
  16. warp/examples/assets/bunny.usd +0 -0
  17. warp/examples/assets/cartpole.urdf +110 -0
  18. warp/examples/assets/crazyflie.usd +0 -0
  19. warp/examples/assets/cube.usd +0 -0
  20. warp/examples/assets/nonuniform.usd +0 -0
  21. warp/examples/assets/nv_ant.xml +92 -0
  22. warp/examples/assets/nv_humanoid.xml +183 -0
  23. warp/examples/assets/nvidia_logo.png +0 -0
  24. warp/examples/assets/pixel.jpg +0 -0
  25. warp/examples/assets/quadruped.urdf +268 -0
  26. warp/examples/assets/rocks.nvdb +0 -0
  27. warp/examples/assets/rocks.usd +0 -0
  28. warp/examples/assets/sphere.usd +0 -0
  29. warp/examples/assets/square_cloth.usd +0 -0
  30. warp/examples/benchmarks/benchmark_api.py +389 -0
  31. warp/examples/benchmarks/benchmark_cloth.py +296 -0
  32. warp/examples/benchmarks/benchmark_cloth_cupy.py +96 -0
  33. warp/examples/benchmarks/benchmark_cloth_jax.py +105 -0
  34. warp/examples/benchmarks/benchmark_cloth_numba.py +161 -0
  35. warp/examples/benchmarks/benchmark_cloth_numpy.py +85 -0
  36. warp/examples/benchmarks/benchmark_cloth_paddle.py +94 -0
  37. warp/examples/benchmarks/benchmark_cloth_pytorch.py +94 -0
  38. warp/examples/benchmarks/benchmark_cloth_taichi.py +120 -0
  39. warp/examples/benchmarks/benchmark_cloth_warp.py +153 -0
  40. warp/examples/benchmarks/benchmark_gemm.py +164 -0
  41. warp/examples/benchmarks/benchmark_interop_paddle.py +166 -0
  42. warp/examples/benchmarks/benchmark_interop_torch.py +166 -0
  43. warp/examples/benchmarks/benchmark_launches.py +301 -0
  44. warp/examples/benchmarks/benchmark_tile_load_store.py +103 -0
  45. warp/examples/browse.py +37 -0
  46. warp/examples/core/example_cupy.py +86 -0
  47. warp/examples/core/example_dem.py +241 -0
  48. warp/examples/core/example_fluid.py +299 -0
  49. warp/examples/core/example_graph_capture.py +150 -0
  50. warp/examples/core/example_marching_cubes.py +194 -0
  51. warp/examples/core/example_mesh.py +180 -0
  52. warp/examples/core/example_mesh_intersect.py +211 -0
  53. warp/examples/core/example_nvdb.py +182 -0
  54. warp/examples/core/example_raycast.py +111 -0
  55. warp/examples/core/example_raymarch.py +205 -0
  56. warp/examples/core/example_render_opengl.py +193 -0
  57. warp/examples/core/example_sample_mesh.py +300 -0
  58. warp/examples/core/example_sph.py +411 -0
  59. warp/examples/core/example_torch.py +211 -0
  60. warp/examples/core/example_wave.py +269 -0
  61. warp/examples/fem/example_adaptive_grid.py +286 -0
  62. warp/examples/fem/example_apic_fluid.py +423 -0
  63. warp/examples/fem/example_burgers.py +261 -0
  64. warp/examples/fem/example_convection_diffusion.py +178 -0
  65. warp/examples/fem/example_convection_diffusion_dg.py +204 -0
  66. warp/examples/fem/example_deformed_geometry.py +172 -0
  67. warp/examples/fem/example_diffusion.py +196 -0
  68. warp/examples/fem/example_diffusion_3d.py +225 -0
  69. warp/examples/fem/example_diffusion_mgpu.py +220 -0
  70. warp/examples/fem/example_distortion_energy.py +228 -0
  71. warp/examples/fem/example_magnetostatics.py +240 -0
  72. warp/examples/fem/example_mixed_elasticity.py +291 -0
  73. warp/examples/fem/example_navier_stokes.py +261 -0
  74. warp/examples/fem/example_nonconforming_contact.py +298 -0
  75. warp/examples/fem/example_stokes.py +213 -0
  76. warp/examples/fem/example_stokes_transfer.py +262 -0
  77. warp/examples/fem/example_streamlines.py +352 -0
  78. warp/examples/fem/utils.py +1000 -0
  79. warp/examples/interop/example_jax_callable.py +116 -0
  80. warp/examples/interop/example_jax_ffi_callback.py +132 -0
  81. warp/examples/interop/example_jax_kernel.py +205 -0
  82. warp/examples/optim/example_bounce.py +266 -0
  83. warp/examples/optim/example_cloth_throw.py +228 -0
  84. warp/examples/optim/example_diffray.py +561 -0
  85. warp/examples/optim/example_drone.py +870 -0
  86. warp/examples/optim/example_fluid_checkpoint.py +497 -0
  87. warp/examples/optim/example_inverse_kinematics.py +182 -0
  88. warp/examples/optim/example_inverse_kinematics_torch.py +191 -0
  89. warp/examples/optim/example_softbody_properties.py +400 -0
  90. warp/examples/optim/example_spring_cage.py +245 -0
  91. warp/examples/optim/example_trajectory.py +227 -0
  92. warp/examples/sim/example_cartpole.py +143 -0
  93. warp/examples/sim/example_cloth.py +225 -0
  94. warp/examples/sim/example_cloth_self_contact.py +322 -0
  95. warp/examples/sim/example_granular.py +130 -0
  96. warp/examples/sim/example_granular_collision_sdf.py +202 -0
  97. warp/examples/sim/example_jacobian_ik.py +244 -0
  98. warp/examples/sim/example_particle_chain.py +124 -0
  99. warp/examples/sim/example_quadruped.py +203 -0
  100. warp/examples/sim/example_rigid_chain.py +203 -0
  101. warp/examples/sim/example_rigid_contact.py +195 -0
  102. warp/examples/sim/example_rigid_force.py +133 -0
  103. warp/examples/sim/example_rigid_gyroscopic.py +115 -0
  104. warp/examples/sim/example_rigid_soft_contact.py +140 -0
  105. warp/examples/sim/example_soft_body.py +196 -0
  106. warp/examples/tile/example_tile_cholesky.py +87 -0
  107. warp/examples/tile/example_tile_convolution.py +66 -0
  108. warp/examples/tile/example_tile_fft.py +55 -0
  109. warp/examples/tile/example_tile_filtering.py +113 -0
  110. warp/examples/tile/example_tile_matmul.py +85 -0
  111. warp/examples/tile/example_tile_mlp.py +383 -0
  112. warp/examples/tile/example_tile_nbody.py +199 -0
  113. warp/examples/tile/example_tile_walker.py +327 -0
  114. warp/fabric.py +355 -0
  115. warp/fem/__init__.py +106 -0
  116. warp/fem/adaptivity.py +508 -0
  117. warp/fem/cache.py +572 -0
  118. warp/fem/dirichlet.py +202 -0
  119. warp/fem/domain.py +411 -0
  120. warp/fem/field/__init__.py +125 -0
  121. warp/fem/field/field.py +619 -0
  122. warp/fem/field/nodal_field.py +326 -0
  123. warp/fem/field/restriction.py +37 -0
  124. warp/fem/field/virtual.py +848 -0
  125. warp/fem/geometry/__init__.py +32 -0
  126. warp/fem/geometry/adaptive_nanogrid.py +857 -0
  127. warp/fem/geometry/closest_point.py +84 -0
  128. warp/fem/geometry/deformed_geometry.py +221 -0
  129. warp/fem/geometry/element.py +776 -0
  130. warp/fem/geometry/geometry.py +362 -0
  131. warp/fem/geometry/grid_2d.py +392 -0
  132. warp/fem/geometry/grid_3d.py +452 -0
  133. warp/fem/geometry/hexmesh.py +911 -0
  134. warp/fem/geometry/nanogrid.py +571 -0
  135. warp/fem/geometry/partition.py +389 -0
  136. warp/fem/geometry/quadmesh.py +663 -0
  137. warp/fem/geometry/tetmesh.py +855 -0
  138. warp/fem/geometry/trimesh.py +806 -0
  139. warp/fem/integrate.py +2335 -0
  140. warp/fem/linalg.py +419 -0
  141. warp/fem/operator.py +293 -0
  142. warp/fem/polynomial.py +229 -0
  143. warp/fem/quadrature/__init__.py +17 -0
  144. warp/fem/quadrature/pic_quadrature.py +299 -0
  145. warp/fem/quadrature/quadrature.py +591 -0
  146. warp/fem/space/__init__.py +228 -0
  147. warp/fem/space/basis_function_space.py +468 -0
  148. warp/fem/space/basis_space.py +667 -0
  149. warp/fem/space/dof_mapper.py +251 -0
  150. warp/fem/space/function_space.py +309 -0
  151. warp/fem/space/grid_2d_function_space.py +177 -0
  152. warp/fem/space/grid_3d_function_space.py +227 -0
  153. warp/fem/space/hexmesh_function_space.py +257 -0
  154. warp/fem/space/nanogrid_function_space.py +201 -0
  155. warp/fem/space/partition.py +367 -0
  156. warp/fem/space/quadmesh_function_space.py +223 -0
  157. warp/fem/space/restriction.py +179 -0
  158. warp/fem/space/shape/__init__.py +143 -0
  159. warp/fem/space/shape/cube_shape_function.py +1105 -0
  160. warp/fem/space/shape/shape_function.py +133 -0
  161. warp/fem/space/shape/square_shape_function.py +926 -0
  162. warp/fem/space/shape/tet_shape_function.py +834 -0
  163. warp/fem/space/shape/triangle_shape_function.py +672 -0
  164. warp/fem/space/tetmesh_function_space.py +271 -0
  165. warp/fem/space/topology.py +424 -0
  166. warp/fem/space/trimesh_function_space.py +194 -0
  167. warp/fem/types.py +99 -0
  168. warp/fem/utils.py +420 -0
  169. warp/jax.py +187 -0
  170. warp/jax_experimental/__init__.py +16 -0
  171. warp/jax_experimental/custom_call.py +351 -0
  172. warp/jax_experimental/ffi.py +698 -0
  173. warp/jax_experimental/xla_ffi.py +602 -0
  174. warp/math.py +244 -0
  175. warp/native/array.h +1145 -0
  176. warp/native/builtin.h +1800 -0
  177. warp/native/bvh.cpp +492 -0
  178. warp/native/bvh.cu +791 -0
  179. warp/native/bvh.h +554 -0
  180. warp/native/clang/clang.cpp +536 -0
  181. warp/native/coloring.cpp +613 -0
  182. warp/native/crt.cpp +51 -0
  183. warp/native/crt.h +362 -0
  184. warp/native/cuda_crt.h +1058 -0
  185. warp/native/cuda_util.cpp +646 -0
  186. warp/native/cuda_util.h +307 -0
  187. warp/native/error.cpp +77 -0
  188. warp/native/error.h +36 -0
  189. warp/native/exports.h +1878 -0
  190. warp/native/fabric.h +245 -0
  191. warp/native/hashgrid.cpp +311 -0
  192. warp/native/hashgrid.cu +87 -0
  193. warp/native/hashgrid.h +240 -0
  194. warp/native/initializer_array.h +41 -0
  195. warp/native/intersect.h +1230 -0
  196. warp/native/intersect_adj.h +375 -0
  197. warp/native/intersect_tri.h +339 -0
  198. warp/native/marching.cpp +19 -0
  199. warp/native/marching.cu +514 -0
  200. warp/native/marching.h +19 -0
  201. warp/native/mat.h +2220 -0
  202. warp/native/mathdx.cpp +87 -0
  203. warp/native/matnn.h +343 -0
  204. warp/native/mesh.cpp +266 -0
  205. warp/native/mesh.cu +404 -0
  206. warp/native/mesh.h +1980 -0
  207. warp/native/nanovdb/GridHandle.h +366 -0
  208. warp/native/nanovdb/HostBuffer.h +590 -0
  209. warp/native/nanovdb/NanoVDB.h +6624 -0
  210. warp/native/nanovdb/PNanoVDB.h +3390 -0
  211. warp/native/noise.h +859 -0
  212. warp/native/quat.h +1371 -0
  213. warp/native/rand.h +342 -0
  214. warp/native/range.h +139 -0
  215. warp/native/reduce.cpp +174 -0
  216. warp/native/reduce.cu +364 -0
  217. warp/native/runlength_encode.cpp +79 -0
  218. warp/native/runlength_encode.cu +61 -0
  219. warp/native/scan.cpp +47 -0
  220. warp/native/scan.cu +53 -0
  221. warp/native/scan.h +23 -0
  222. warp/native/solid_angle.h +466 -0
  223. warp/native/sort.cpp +251 -0
  224. warp/native/sort.cu +277 -0
  225. warp/native/sort.h +33 -0
  226. warp/native/sparse.cpp +378 -0
  227. warp/native/sparse.cu +524 -0
  228. warp/native/spatial.h +657 -0
  229. warp/native/svd.h +702 -0
  230. warp/native/temp_buffer.h +46 -0
  231. warp/native/tile.h +2584 -0
  232. warp/native/tile_reduce.h +264 -0
  233. warp/native/vec.h +1426 -0
  234. warp/native/volume.cpp +501 -0
  235. warp/native/volume.cu +67 -0
  236. warp/native/volume.h +969 -0
  237. warp/native/volume_builder.cu +477 -0
  238. warp/native/volume_builder.h +52 -0
  239. warp/native/volume_impl.h +70 -0
  240. warp/native/warp.cpp +1082 -0
  241. warp/native/warp.cu +3636 -0
  242. warp/native/warp.h +381 -0
  243. warp/optim/__init__.py +17 -0
  244. warp/optim/adam.py +163 -0
  245. warp/optim/linear.py +1137 -0
  246. warp/optim/sgd.py +112 -0
  247. warp/paddle.py +407 -0
  248. warp/render/__init__.py +18 -0
  249. warp/render/render_opengl.py +3518 -0
  250. warp/render/render_usd.py +784 -0
  251. warp/render/utils.py +160 -0
  252. warp/sim/__init__.py +65 -0
  253. warp/sim/articulation.py +793 -0
  254. warp/sim/collide.py +2395 -0
  255. warp/sim/graph_coloring.py +300 -0
  256. warp/sim/import_mjcf.py +790 -0
  257. warp/sim/import_snu.py +227 -0
  258. warp/sim/import_urdf.py +579 -0
  259. warp/sim/import_usd.py +894 -0
  260. warp/sim/inertia.py +324 -0
  261. warp/sim/integrator.py +242 -0
  262. warp/sim/integrator_euler.py +1997 -0
  263. warp/sim/integrator_featherstone.py +2101 -0
  264. warp/sim/integrator_vbd.py +2048 -0
  265. warp/sim/integrator_xpbd.py +3292 -0
  266. warp/sim/model.py +4791 -0
  267. warp/sim/particles.py +121 -0
  268. warp/sim/render.py +427 -0
  269. warp/sim/utils.py +428 -0
  270. warp/sparse.py +2057 -0
  271. warp/stubs.py +3333 -0
  272. warp/tape.py +1203 -0
  273. warp/tests/__init__.py +1 -0
  274. warp/tests/__main__.py +4 -0
  275. warp/tests/assets/curlnoise_golden.npy +0 -0
  276. warp/tests/assets/mlp_golden.npy +0 -0
  277. warp/tests/assets/pixel.npy +0 -0
  278. warp/tests/assets/pnoise_golden.npy +0 -0
  279. warp/tests/assets/spiky.usd +0 -0
  280. warp/tests/assets/test_grid.nvdb +0 -0
  281. warp/tests/assets/test_index_grid.nvdb +0 -0
  282. warp/tests/assets/test_int32_grid.nvdb +0 -0
  283. warp/tests/assets/test_vec_grid.nvdb +0 -0
  284. warp/tests/assets/torus.nvdb +0 -0
  285. warp/tests/assets/torus.usda +105 -0
  286. warp/tests/aux_test_class_kernel.py +34 -0
  287. warp/tests/aux_test_compile_consts_dummy.py +18 -0
  288. warp/tests/aux_test_conditional_unequal_types_kernels.py +29 -0
  289. warp/tests/aux_test_dependent.py +29 -0
  290. warp/tests/aux_test_grad_customs.py +29 -0
  291. warp/tests/aux_test_instancing_gc.py +26 -0
  292. warp/tests/aux_test_module_unload.py +23 -0
  293. warp/tests/aux_test_name_clash1.py +40 -0
  294. warp/tests/aux_test_name_clash2.py +40 -0
  295. warp/tests/aux_test_reference.py +9 -0
  296. warp/tests/aux_test_reference_reference.py +8 -0
  297. warp/tests/aux_test_square.py +16 -0
  298. warp/tests/aux_test_unresolved_func.py +22 -0
  299. warp/tests/aux_test_unresolved_symbol.py +22 -0
  300. warp/tests/cuda/__init__.py +0 -0
  301. warp/tests/cuda/test_async.py +676 -0
  302. warp/tests/cuda/test_ipc.py +124 -0
  303. warp/tests/cuda/test_mempool.py +233 -0
  304. warp/tests/cuda/test_multigpu.py +169 -0
  305. warp/tests/cuda/test_peer.py +139 -0
  306. warp/tests/cuda/test_pinned.py +84 -0
  307. warp/tests/cuda/test_streams.py +634 -0
  308. warp/tests/geometry/__init__.py +0 -0
  309. warp/tests/geometry/test_bvh.py +200 -0
  310. warp/tests/geometry/test_hash_grid.py +221 -0
  311. warp/tests/geometry/test_marching_cubes.py +74 -0
  312. warp/tests/geometry/test_mesh.py +316 -0
  313. warp/tests/geometry/test_mesh_query_aabb.py +399 -0
  314. warp/tests/geometry/test_mesh_query_point.py +932 -0
  315. warp/tests/geometry/test_mesh_query_ray.py +311 -0
  316. warp/tests/geometry/test_volume.py +1103 -0
  317. warp/tests/geometry/test_volume_write.py +346 -0
  318. warp/tests/interop/__init__.py +0 -0
  319. warp/tests/interop/test_dlpack.py +729 -0
  320. warp/tests/interop/test_jax.py +371 -0
  321. warp/tests/interop/test_paddle.py +800 -0
  322. warp/tests/interop/test_torch.py +1001 -0
  323. warp/tests/run_coverage_serial.py +39 -0
  324. warp/tests/sim/__init__.py +0 -0
  325. warp/tests/sim/disabled_kinematics.py +244 -0
  326. warp/tests/sim/flaky_test_sim_grad.py +290 -0
  327. warp/tests/sim/test_collision.py +604 -0
  328. warp/tests/sim/test_coloring.py +258 -0
  329. warp/tests/sim/test_model.py +224 -0
  330. warp/tests/sim/test_sim_grad_bounce_linear.py +212 -0
  331. warp/tests/sim/test_sim_kinematics.py +98 -0
  332. warp/tests/sim/test_vbd.py +597 -0
  333. warp/tests/test_adam.py +163 -0
  334. warp/tests/test_arithmetic.py +1096 -0
  335. warp/tests/test_array.py +2972 -0
  336. warp/tests/test_array_reduce.py +156 -0
  337. warp/tests/test_assert.py +250 -0
  338. warp/tests/test_atomic.py +153 -0
  339. warp/tests/test_bool.py +220 -0
  340. warp/tests/test_builtins_resolution.py +1298 -0
  341. warp/tests/test_closest_point_edge_edge.py +327 -0
  342. warp/tests/test_codegen.py +810 -0
  343. warp/tests/test_codegen_instancing.py +1495 -0
  344. warp/tests/test_compile_consts.py +215 -0
  345. warp/tests/test_conditional.py +252 -0
  346. warp/tests/test_context.py +42 -0
  347. warp/tests/test_copy.py +238 -0
  348. warp/tests/test_ctypes.py +638 -0
  349. warp/tests/test_dense.py +73 -0
  350. warp/tests/test_devices.py +97 -0
  351. warp/tests/test_examples.py +482 -0
  352. warp/tests/test_fabricarray.py +996 -0
  353. warp/tests/test_fast_math.py +74 -0
  354. warp/tests/test_fem.py +2003 -0
  355. warp/tests/test_fp16.py +136 -0
  356. warp/tests/test_func.py +454 -0
  357. warp/tests/test_future_annotations.py +98 -0
  358. warp/tests/test_generics.py +656 -0
  359. warp/tests/test_grad.py +893 -0
  360. warp/tests/test_grad_customs.py +339 -0
  361. warp/tests/test_grad_debug.py +341 -0
  362. warp/tests/test_implicit_init.py +411 -0
  363. warp/tests/test_import.py +45 -0
  364. warp/tests/test_indexedarray.py +1140 -0
  365. warp/tests/test_intersect.py +73 -0
  366. warp/tests/test_iter.py +76 -0
  367. warp/tests/test_large.py +177 -0
  368. warp/tests/test_launch.py +411 -0
  369. warp/tests/test_lerp.py +151 -0
  370. warp/tests/test_linear_solvers.py +193 -0
  371. warp/tests/test_lvalue.py +427 -0
  372. warp/tests/test_mat.py +2089 -0
  373. warp/tests/test_mat_lite.py +122 -0
  374. warp/tests/test_mat_scalar_ops.py +2913 -0
  375. warp/tests/test_math.py +178 -0
  376. warp/tests/test_mlp.py +282 -0
  377. warp/tests/test_module_hashing.py +258 -0
  378. warp/tests/test_modules_lite.py +44 -0
  379. warp/tests/test_noise.py +252 -0
  380. warp/tests/test_operators.py +299 -0
  381. warp/tests/test_options.py +129 -0
  382. warp/tests/test_overwrite.py +551 -0
  383. warp/tests/test_print.py +339 -0
  384. warp/tests/test_quat.py +2315 -0
  385. warp/tests/test_rand.py +339 -0
  386. warp/tests/test_reload.py +302 -0
  387. warp/tests/test_rounding.py +185 -0
  388. warp/tests/test_runlength_encode.py +196 -0
  389. warp/tests/test_scalar_ops.py +105 -0
  390. warp/tests/test_smoothstep.py +108 -0
  391. warp/tests/test_snippet.py +318 -0
  392. warp/tests/test_sparse.py +582 -0
  393. warp/tests/test_spatial.py +2229 -0
  394. warp/tests/test_special_values.py +361 -0
  395. warp/tests/test_static.py +592 -0
  396. warp/tests/test_struct.py +734 -0
  397. warp/tests/test_tape.py +204 -0
  398. warp/tests/test_transient_module.py +93 -0
  399. warp/tests/test_triangle_closest_point.py +145 -0
  400. warp/tests/test_types.py +562 -0
  401. warp/tests/test_utils.py +588 -0
  402. warp/tests/test_vec.py +1487 -0
  403. warp/tests/test_vec_lite.py +80 -0
  404. warp/tests/test_vec_scalar_ops.py +2327 -0
  405. warp/tests/test_verify_fp.py +100 -0
  406. warp/tests/tile/__init__.py +0 -0
  407. warp/tests/tile/test_tile.py +780 -0
  408. warp/tests/tile/test_tile_load.py +407 -0
  409. warp/tests/tile/test_tile_mathdx.py +208 -0
  410. warp/tests/tile/test_tile_mlp.py +402 -0
  411. warp/tests/tile/test_tile_reduce.py +447 -0
  412. warp/tests/tile/test_tile_shared_memory.py +247 -0
  413. warp/tests/tile/test_tile_view.py +173 -0
  414. warp/tests/unittest_serial.py +47 -0
  415. warp/tests/unittest_suites.py +427 -0
  416. warp/tests/unittest_utils.py +468 -0
  417. warp/tests/walkthrough_debug.py +93 -0
  418. warp/thirdparty/__init__.py +0 -0
  419. warp/thirdparty/appdirs.py +598 -0
  420. warp/thirdparty/dlpack.py +145 -0
  421. warp/thirdparty/unittest_parallel.py +570 -0
  422. warp/torch.py +391 -0
  423. warp/types.py +5230 -0
  424. warp/utils.py +1137 -0
  425. warp_lang-1.7.0.dist-info/METADATA +516 -0
  426. warp_lang-1.7.0.dist-info/RECORD +429 -0
  427. warp_lang-1.7.0.dist-info/WHEEL +5 -0
  428. warp_lang-1.7.0.dist-info/licenses/LICENSE.md +202 -0
  429. warp_lang-1.7.0.dist-info/top_level.txt +1 -0
warp/native/vec.h ADDED
@@ -0,0 +1,1426 @@
1
+ /*
2
+ * SPDX-FileCopyrightText: Copyright (c) 2022 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+
18
+ #pragma once
19
+
20
+ #include "initializer_array.h"
21
+
22
+ namespace wp
23
+ {
24
+
25
+ template<unsigned Length, typename Type>
26
+ struct vec_t
27
+ {
28
+ Type c[Length];
29
+
30
+ inline CUDA_CALLABLE vec_t()
31
+ : c()
32
+ {}
33
+
34
+ inline CUDA_CALLABLE vec_t(Type s)
35
+ {
36
+ for( unsigned i=0; i < Length; ++i )
37
+ {
38
+ c[i] = s;
39
+ }
40
+ }
41
+
42
+ template <typename OtherType>
43
+ inline explicit CUDA_CALLABLE vec_t(const vec_t<Length, OtherType>& other)
44
+ {
45
+ for( unsigned i=0; i < Length; ++i )
46
+ {
47
+ c[i] = static_cast<Type>(other[i]);
48
+ }
49
+ }
50
+
51
+ inline CUDA_CALLABLE vec_t(Type x, Type y)
52
+ {
53
+ assert(Length == 2);
54
+ c[0]=x;
55
+ c[1]=y;
56
+ }
57
+
58
+ inline CUDA_CALLABLE vec_t(Type x, Type y, Type z)
59
+ {
60
+ assert(Length == 3);
61
+ c[0]=x;
62
+ c[1]=y;
63
+ c[2]=z;
64
+ }
65
+
66
+
67
+ inline CUDA_CALLABLE vec_t(Type x, Type y, Type z, Type w)
68
+ {
69
+ assert(Length == 4);
70
+ c[0]=x;
71
+ c[1]=y;
72
+ c[2]=z;
73
+ c[3]=w;
74
+ }
75
+
76
+ inline CUDA_CALLABLE vec_t(const initializer_array<Length, Type> &l)
77
+ {
78
+ for( unsigned i=0; i < Length; ++i )
79
+ {
80
+ c[i] = l[i];
81
+ }
82
+ }
83
+
84
+ // special screw vector constructor for spatial_vectors:
85
+ inline CUDA_CALLABLE vec_t(vec_t<3,Type> w, vec_t<3,Type> v)
86
+ {
87
+ c[0] = w[0];
88
+ c[1] = w[1];
89
+ c[2] = w[2];
90
+ c[3] = v[0];
91
+ c[4] = v[1];
92
+ c[5] = v[2];
93
+ }
94
+
95
+ inline CUDA_CALLABLE Type operator[](int index) const
96
+ {
97
+ assert(index < Length);
98
+ return c[index];
99
+ }
100
+
101
+ inline CUDA_CALLABLE Type& operator[](int index)
102
+ {
103
+ assert(index < Length);
104
+ return c[index];
105
+ }
106
+ };
107
+
108
+ using vec2b = vec_t<2,int8>;
109
+ using vec3b = vec_t<3,int8>;
110
+ using vec4b = vec_t<4,int8>;
111
+ using vec2ub = vec_t<2,uint8>;
112
+ using vec3ub = vec_t<3,uint8>;
113
+ using vec4ub = vec_t<4,uint8>;
114
+
115
+ using vec2s = vec_t<2,int16>;
116
+ using vec3s = vec_t<3,int16>;
117
+ using vec4s = vec_t<4,int16>;
118
+ using vec2us = vec_t<2,uint16>;
119
+ using vec3us = vec_t<3,uint16>;
120
+ using vec4us = vec_t<4,uint16>;
121
+
122
+ using vec2i = vec_t<2,int32>;
123
+ using vec3i = vec_t<3,int32>;
124
+ using vec4i = vec_t<4,int32>;
125
+ using vec2ui = vec_t<2,uint32>;
126
+ using vec3ui = vec_t<3,uint32>;
127
+ using vec4ui = vec_t<4,uint32>;
128
+
129
+ using vec2l = vec_t<2,int64>;
130
+ using vec3l = vec_t<3,int64>;
131
+ using vec4l = vec_t<4,int64>;
132
+ using vec2ul = vec_t<2,uint64>;
133
+ using vec3ul = vec_t<3,uint64>;
134
+ using vec4ul = vec_t<4,uint64>;
135
+
136
+ using vec2h = vec_t<2,half>;
137
+ using vec3h = vec_t<3,half>;
138
+ using vec4h = vec_t<4,half>;
139
+
140
+ using vec2 = vec_t<2,float>;
141
+ using vec3 = vec_t<3,float>;
142
+ using vec4 = vec_t<4,float>;
143
+
144
+ using vec2f = vec_t<2,float>;
145
+ using vec3f = vec_t<3,float>;
146
+ using vec4f = vec_t<4,float>;
147
+
148
+ using vec2d = vec_t<2,double>;
149
+ using vec3d = vec_t<3,double>;
150
+ using vec4d = vec_t<4,double>;
151
+
152
+ //--------------
153
+ // vec<Length, Type> methods
154
+
155
+ // Should these accept const references as arguments? It's all
156
+ // inlined so maybe it doesn't matter? Even if it does, it
157
+ // probably depends on the Length of the vector...
158
+
159
+ // negation:
160
+ template<unsigned Length, typename Type>
161
+ inline CUDA_CALLABLE vec_t<Length, Type> operator - (vec_t<Length, Type> a)
162
+ {
163
+ // NB: this constructor will initialize all ret's components to 0, which is
164
+ // unnecessary...
165
+ vec_t<Length, Type> ret;
166
+ for( unsigned i=0; i < Length; ++i )
167
+ {
168
+ ret[i] = -a[i];
169
+ }
170
+
171
+ // Wonder if this does a load of copying when it returns... hopefully not as it's inlined?
172
+ return ret;
173
+ }
174
+
175
+ template<unsigned Length, typename Type>
176
+ CUDA_CALLABLE inline vec_t<Length, Type> pos(const vec_t<Length, Type>& x)
177
+ {
178
+ return x;
179
+ }
180
+
181
+ template<unsigned Length, typename Type>
182
+ CUDA_CALLABLE inline vec_t<Length, Type> neg(const vec_t<Length, Type>& x)
183
+ {
184
+ return -x;
185
+ }
186
+
187
+ template<typename Type>
188
+ CUDA_CALLABLE inline vec_t<3, Type> neg(const vec_t<3, Type>& x)
189
+ {
190
+ return vec_t<3, Type>(-x.c[0], -x.c[1], -x.c[2]);
191
+ }
192
+
193
+ template<typename Type>
194
+ CUDA_CALLABLE inline vec_t<2, Type> neg(const vec_t<2, Type>& x)
195
+ {
196
+ return vec_t<2, Type>(-x.c[0], -x.c[1]);
197
+ }
198
+
199
+ template<unsigned Length, typename Type>
200
+ CUDA_CALLABLE inline void adj_neg(const vec_t<Length, Type>& x, vec_t<Length, Type>& adj_x, const vec_t<Length, Type>& adj_ret)
201
+ {
202
+ adj_x -= adj_ret;
203
+ }
204
+
205
+ // equality:
206
+ template<unsigned Length, typename Type>
207
+ inline CUDA_CALLABLE bool operator ==(const vec_t<Length, Type>& a, const vec_t<Length, Type>& b)
208
+ {
209
+ for( unsigned i=0; i < Length; ++i )
210
+ {
211
+ if(a[i] != b[i])
212
+ {
213
+ return false;
214
+ }
215
+ }
216
+ return true;
217
+ }
218
+
219
+ // scalar multiplication:
220
+ template<unsigned Length, typename Type>
221
+ inline CUDA_CALLABLE vec_t<Length, Type> mul(vec_t<Length, Type> a, Type s)
222
+ {
223
+ vec_t<Length, Type> ret;
224
+ for( unsigned i=0; i < Length; ++i )
225
+ {
226
+ ret[i] = a[i] * s;
227
+ }
228
+ return ret;
229
+ }
230
+
231
+ template<typename Type>
232
+ inline CUDA_CALLABLE vec_t<3, Type> mul(vec_t<3, Type> a, Type s)
233
+ {
234
+ return vec_t<3, Type>(a.c[0]*s,a.c[1]*s,a.c[2]*s);
235
+ }
236
+
237
+ template<typename Type>
238
+ inline CUDA_CALLABLE vec_t<2, Type> mul(vec_t<2, Type> a, Type s)
239
+ {
240
+ return vec_t<2, Type>(a.c[0]*s,a.c[1]*s);
241
+ }
242
+
243
+ template<unsigned Length, typename Type>
244
+ inline CUDA_CALLABLE vec_t<Length, Type> mul(Type s, vec_t<Length, Type> a)
245
+ {
246
+ return mul(a, s);
247
+ }
248
+
249
+ template<unsigned Length, typename Type>
250
+ inline CUDA_CALLABLE vec_t<Length, Type> operator*(Type s, vec_t<Length, Type> a)
251
+ {
252
+ return mul(a, s);
253
+ }
254
+
255
+ template<unsigned Length, typename Type>
256
+ inline CUDA_CALLABLE vec_t<Length, Type> operator*(vec_t<Length, Type> a, Type s)
257
+ {
258
+ return mul(a, s);
259
+ }
260
+
261
+
262
+ // component wise multiplication:
263
+ template<unsigned Length, typename Type>
264
+ inline CUDA_CALLABLE vec_t<Length, Type> cw_mul(vec_t<Length, Type> a, vec_t<Length, Type> b)
265
+ {
266
+ vec_t<Length, Type> ret;
267
+ for( unsigned i=0; i < Length; ++i )
268
+ {
269
+ ret[i] = a[i] * b[i];
270
+ }
271
+ return ret;
272
+ }
273
+
274
+ // division
275
+ template<unsigned Length, typename Type>
276
+ inline CUDA_CALLABLE vec_t<Length, Type> div(vec_t<Length, Type> a, Type s)
277
+ {
278
+ vec_t<Length, Type> ret;
279
+ for( unsigned i=0; i < Length; ++i )
280
+ {
281
+ ret[i] = a[i] / s;
282
+ }
283
+ return ret;
284
+ }
285
+
286
+ template<typename Type>
287
+ inline CUDA_CALLABLE vec_t<3, Type> div(vec_t<3, Type> a, Type s)
288
+ {
289
+ return vec_t<3, Type>(a.c[0]/s,a.c[1]/s,a.c[2]/s);
290
+ }
291
+
292
+ template<typename Type>
293
+ inline CUDA_CALLABLE vec_t<2, Type> div(vec_t<2, Type> a, Type s)
294
+ {
295
+ return vec_t<2, Type>(a.c[0]/s,a.c[1]/s);
296
+ }
297
+
298
+ template<unsigned Length, typename Type>
299
+ inline CUDA_CALLABLE vec_t<Length, Type> div(Type s, vec_t<Length, Type> a)
300
+ {
301
+ vec_t<Length, Type> ret;
302
+ for (unsigned i=0; i < Length; ++i)
303
+ {
304
+ ret[i] = s / a[i];
305
+ }
306
+ return ret;
307
+ }
308
+
309
+ template<typename Type>
310
+ inline CUDA_CALLABLE vec_t<3, Type> div(Type s, vec_t<3, Type> a)
311
+ {
312
+ return vec_t<3, Type>(s/a.c[0],s/a.c[1],s/a.c[2]);
313
+ }
314
+
315
+ template<typename Type>
316
+ inline CUDA_CALLABLE vec_t<2, Type> div(Type s, vec_t<2, Type> a)
317
+ {
318
+ return vec_t<2, Type>(s/a.c[0],s/a.c[1]);
319
+ }
320
+
321
+ template<unsigned Length, typename Type>
322
+ inline CUDA_CALLABLE vec_t<Length, Type> operator / (vec_t<Length, Type> a, Type s)
323
+ {
324
+ return div(a,s);
325
+ }
326
+
327
+ template<unsigned Length, typename Type>
328
+ inline CUDA_CALLABLE vec_t<Length, Type> operator / (Type s, vec_t<Length, Type> a)
329
+ {
330
+ return div(s, a);
331
+ }
332
+
333
+ // component wise division
334
+ template<unsigned Length, typename Type>
335
+ inline CUDA_CALLABLE vec_t<Length, Type> cw_div(vec_t<Length, Type> a, vec_t<Length, Type> b)
336
+ {
337
+ vec_t<Length, Type> ret;
338
+ for( unsigned i=0; i < Length; ++i )
339
+ {
340
+ ret[i] = a[i] / b[i];
341
+ }
342
+ return ret;
343
+ }
344
+
345
+ // addition
346
+ template<unsigned Length, typename Type>
347
+ inline CUDA_CALLABLE vec_t<Length, Type> add(vec_t<Length, Type> a, vec_t<Length, Type> b)
348
+ {
349
+ vec_t<Length, Type> ret;
350
+ for( unsigned i=0; i < Length; ++i )
351
+ {
352
+ ret[i] = a[i] + b[i];
353
+ }
354
+ return ret;
355
+ }
356
+
357
+ template<typename Type>
358
+ inline CUDA_CALLABLE vec_t<2, Type> add(vec_t<2, Type> a, vec_t<2, Type> b)
359
+ {
360
+ return vec_t<2, Type>( a.c[0] + b.c[0], a.c[1] + b.c[1]);
361
+ }
362
+
363
+ template<typename Type>
364
+ inline CUDA_CALLABLE vec_t<3, Type> add(vec_t<3, Type> a, vec_t<3, Type> b)
365
+ {
366
+ return vec_t<3, Type>( a.c[0] + b.c[0], a.c[1] + b.c[1], a.c[2] + b.c[2]);
367
+ }
368
+
369
+ // subtraction
370
+ template<unsigned Length, typename Type>
371
+ inline CUDA_CALLABLE vec_t<Length, Type> sub(vec_t<Length, Type> a, vec_t<Length, Type> b)
372
+ {
373
+ vec_t<Length, Type> ret;
374
+ for( unsigned i=0; i < Length; ++i )
375
+ {
376
+ ret[i] = Type(a[i] - b[i]);
377
+ }
378
+ return ret;
379
+ }
380
+
381
+ template<typename Type>
382
+ inline CUDA_CALLABLE vec_t<2, Type> sub(vec_t<2, Type> a, vec_t<2, Type> b)
383
+ {
384
+ return vec_t<2, Type>( a.c[0] - b.c[0], a.c[1] - b.c[1]);
385
+ }
386
+
387
+ template<typename Type>
388
+ inline CUDA_CALLABLE vec_t<3, Type> sub(vec_t<3, Type> a, vec_t<3, Type> b)
389
+ {
390
+ return vec_t<3, Type>( a.c[0] - b.c[0], a.c[1] - b.c[1], a.c[2] - b.c[2]);
391
+ }
392
+
393
+ // modulo
394
+ template<unsigned Length, typename Type>
395
+ inline CUDA_CALLABLE vec_t<Length, Type> mod(vec_t<Length, Type> a, vec_t<Length, Type> b)
396
+ {
397
+ vec_t<Length, Type> ret;
398
+ for (unsigned i=0; i < Length; ++i)
399
+ {
400
+ ret[i] = mod(a[i], b[i]);
401
+ }
402
+
403
+ return ret;
404
+ }
405
+
406
+ template<typename Type>
407
+ inline CUDA_CALLABLE vec_t<2, Type> mod(vec_t<2, Type> a, vec_t<2, Type> b)
408
+ {
409
+ return vec_t<2, Type>(mod(a.c[0], b.c[0]), mod(a.c[1], b.c[1]));
410
+ }
411
+
412
+ template<typename Type>
413
+ inline CUDA_CALLABLE vec_t<3, Type> mod(vec_t<3, Type> a, vec_t<3, Type> b)
414
+ {
415
+ return vec_t<3, Type>(mod(a.c[0], b.c[0]), mod(a.c[1], b.c[1]), mod(a.c[2], b.c[2]));
416
+ }
417
+
418
+ // dot product:
419
+ template<unsigned Length, typename Type>
420
+ inline CUDA_CALLABLE Type dot(vec_t<Length, Type> a, vec_t<Length, Type> b)
421
+ {
422
+ Type ret(0);
423
+ for( unsigned i=0; i < Length; ++i )
424
+ {
425
+ ret += a[i] * b[i];
426
+ }
427
+ return ret;
428
+ }
429
+
430
+ template<typename Type>
431
+ inline CUDA_CALLABLE Type dot(vec_t<2, Type> a, vec_t<2, Type> b)
432
+ {
433
+ return a.c[0] * b.c[0] + a.c[1] * b.c[1];
434
+ }
435
+
436
+ template<typename Type>
437
+ inline CUDA_CALLABLE Type dot(vec_t<3, Type> a, vec_t<3, Type> b)
438
+ {
439
+ return a.c[0] * b.c[0] + a.c[1] * b.c[1] + a.c[2] * b.c[2];
440
+ }
441
+
442
+ template<unsigned Length, typename Type>
443
+ inline CUDA_CALLABLE Type tensordot(vec_t<Length, Type> a, vec_t<Length, Type> b)
444
+ {
445
+ // corresponds to `np.tensordot()` with all axes being contracted
446
+ return dot(a, b);
447
+ }
448
+
449
+
450
+ template<unsigned Length, typename Type>
451
+ inline CUDA_CALLABLE Type extract(const vec_t<Length, Type> & a, int idx)
452
+ {
453
+ #ifndef NDEBUG
454
+ if (idx < 0 || idx >= Length)
455
+ {
456
+ printf("vec index %d out of bounds at %s %d\n", idx, __FILE__, __LINE__);
457
+ assert(0);
458
+ }
459
+ #endif
460
+
461
+ return a[idx];
462
+ }
463
+
464
+ template<unsigned Length, typename Type>
465
+ inline CUDA_CALLABLE Type* index(vec_t<Length, Type>& v, int idx)
466
+ {
467
+ #ifndef NDEBUG
468
+ if (idx < 0 || idx >= Length)
469
+ {
470
+ printf("vec index %d out of bounds at %s %d\n", idx, __FILE__, __LINE__);
471
+ assert(0);
472
+ }
473
+ #endif
474
+
475
+ return &v[idx];
476
+ }
477
+
478
+ template<unsigned Length, typename Type>
479
+ inline CUDA_CALLABLE Type* indexref(vec_t<Length, Type>* v, int idx)
480
+ {
481
+ #ifndef NDEBUG
482
+ if (idx < 0 || idx >= Length)
483
+ {
484
+ printf("vec store %d out of bounds at %s %d\n", idx, __FILE__, __LINE__);
485
+ assert(0);
486
+ }
487
+ #endif
488
+
489
+ return &((*v)[idx]);
490
+ }
491
+
492
+ template<unsigned Length, typename Type>
493
+ inline CUDA_CALLABLE void adj_index(vec_t<Length, Type>& v, int idx,
494
+ vec_t<Length, Type>& adj_v, int adj_idx, const Type& adj_value)
495
+ {
496
+ // nop
497
+ }
498
+
499
+
500
+ template<unsigned Length, typename Type>
501
+ inline CUDA_CALLABLE void adj_indexref(vec_t<Length, Type>* v, int idx,
502
+ vec_t<Length, Type>& adj_v, int adj_idx, const Type& adj_value)
503
+ {
504
+ // nop
505
+ }
506
+
507
+
508
+ template<unsigned Length, typename Type>
509
+ inline CUDA_CALLABLE void add_inplace(vec_t<Length, Type>& v, int idx, Type value)
510
+ {
511
+ #ifndef NDEBUG
512
+ if (idx < 0 || idx >= Length)
513
+ {
514
+ printf("vec index %d out of bounds at %s %d\n", idx, __FILE__, __LINE__);
515
+ assert(0);
516
+ }
517
+ #endif
518
+
519
+ v[idx] += value;
520
+ }
521
+
522
+
523
+ template<unsigned Length, typename Type>
524
+ inline CUDA_CALLABLE void adj_add_inplace(vec_t<Length, Type>& v, int idx, Type value,
525
+ vec_t<Length, Type>& adj_v, int adj_idx, Type& adj_value)
526
+ {
527
+ #ifndef NDEBUG
528
+ if (idx < 0 || idx >= Length)
529
+ {
530
+ printf("vec index %d out of bounds at %s %d\n", idx, __FILE__, __LINE__);
531
+ assert(0);
532
+ }
533
+ #endif
534
+
535
+ adj_value += adj_v[idx];
536
+ }
537
+
538
+
539
+ template<unsigned Length, typename Type>
540
+ inline CUDA_CALLABLE void sub_inplace(vec_t<Length, Type>& v, int idx, Type value)
541
+ {
542
+ #ifndef NDEBUG
543
+ if (idx < 0 || idx >= Length)
544
+ {
545
+ printf("vec index %d out of bounds at %s %d\n", idx, __FILE__, __LINE__);
546
+ assert(0);
547
+ }
548
+ #endif
549
+
550
+ v[idx] -= value;
551
+ }
552
+
553
+
554
+ template<unsigned Length, typename Type>
555
+ inline CUDA_CALLABLE void adj_sub_inplace(vec_t<Length, Type>& v, int idx, Type value,
556
+ vec_t<Length, Type>& adj_v, int adj_idx, Type& adj_value)
557
+ {
558
+ #ifndef NDEBUG
559
+ if (idx < 0 || idx >= Length)
560
+ {
561
+ printf("vec index %d out of bounds at %s %d\n", idx, __FILE__, __LINE__);
562
+ assert(0);
563
+ }
564
+ #endif
565
+
566
+ adj_value -= adj_v[idx];
567
+ }
568
+
569
+
570
+ template<unsigned Length, typename Type>
571
+ inline CUDA_CALLABLE void assign_inplace(vec_t<Length, Type>& v, int idx, Type value)
572
+ {
573
+ #ifndef NDEBUG
574
+ if (idx < 0 || idx >= Length)
575
+ {
576
+ printf("vec index %d out of bounds at %s %d\n", idx, __FILE__, __LINE__);
577
+ assert(0);
578
+ }
579
+ #endif
580
+
581
+ v[idx] = value;
582
+ }
583
+
584
+ template<unsigned Length, typename Type>
585
+ inline CUDA_CALLABLE void adj_assign_inplace(vec_t<Length, Type>& v, int idx, Type value, vec_t<Length, Type>& adj_v, int& adj_idx, Type& adj_value)
586
+ {
587
+ #ifndef NDEBUG
588
+ if (idx < 0 || idx >= Length)
589
+ {
590
+ printf("vec index %d out of bounds at %s %d\n", idx, __FILE__, __LINE__);
591
+ assert(0);
592
+ }
593
+ #endif
594
+
595
+ adj_value += adj_v[idx];
596
+ }
597
+
598
+
599
+ template<unsigned Length, typename Type>
600
+ inline CUDA_CALLABLE vec_t<Length, Type> assign_copy(vec_t<Length, Type>& v, int idx, Type value)
601
+ {
602
+ #ifndef NDEBUG
603
+ if (idx < 0 || idx >= Length)
604
+ {
605
+ printf("vec index %d out of bounds at %s %d\n", idx, __FILE__, __LINE__);
606
+ assert(0);
607
+ }
608
+ #endif
609
+
610
+ vec_t<Length, Type> ret(v);
611
+ ret[idx] = value;
612
+ return ret;
613
+ }
614
+
615
+ template<unsigned Length, typename Type>
616
+ inline CUDA_CALLABLE void adj_assign_copy(vec_t<Length, Type>& v, int idx, Type value, vec_t<Length, Type>& adj_v, int& adj_idx, Type& adj_value, const vec_t<Length, Type>& adj_ret)
617
+ {
618
+ #ifndef NDEBUG
619
+ if (idx < 0 || idx >= Length)
620
+ {
621
+ printf("vec index %d out of bounds at %s %d\n", idx, __FILE__, __LINE__);
622
+ assert(0);
623
+ }
624
+ #endif
625
+
626
+ adj_value += adj_ret[idx];
627
+ for(unsigned i=0; i < Length; ++i)
628
+ {
629
+ if (i != idx)
630
+ adj_v[i] += adj_ret[i];
631
+ }
632
+ }
633
+
634
+ template<unsigned Length, typename Type>
635
+ inline CUDA_CALLABLE Type length(vec_t<Length, Type> a)
636
+ {
637
+ return sqrt(dot(a, a));
638
+ }
639
+
640
+ template<unsigned Length, typename Type>
641
+ inline CUDA_CALLABLE Type length_sq(vec_t<Length, Type> a)
642
+ {
643
+ return dot(a, a);
644
+ }
645
+
646
+
647
+ template<typename Type>
648
+ inline CUDA_CALLABLE Type length(vec_t<2, Type> a)
649
+ {
650
+ return sqrt(a.c[0] * a.c[0] + a.c[1] * a.c[1]);
651
+ }
652
+
653
+ template<typename Type>
654
+ inline CUDA_CALLABLE Type length(vec_t<3, Type> a)
655
+ {
656
+ return sqrt(a.c[0] * a.c[0] + a.c[1] * a.c[1] + a.c[2] * a.c[2]);
657
+ }
658
+
659
+ template<unsigned Length, typename Type>
660
+ inline CUDA_CALLABLE vec_t<Length, Type> normalize(vec_t<Length, Type> a)
661
+ {
662
+ Type l = length(a);
663
+ if (l > Type(kEps))
664
+ return div(a,l);
665
+ else
666
+ return vec_t<Length, Type>();
667
+ }
668
+
669
+ template<typename Type>
670
+ inline CUDA_CALLABLE vec_t<2, Type> normalize(vec_t<2, Type> a)
671
+ {
672
+ Type l = sqrt(a.c[0] * a.c[0] + a.c[1] * a.c[1]);
673
+ if (l > Type(kEps))
674
+ return vec_t<2, Type>(a.c[0]/l,a.c[1]/l);
675
+ else
676
+ return vec_t<2, Type>();
677
+ }
678
+
679
+ template<typename Type>
680
+ inline CUDA_CALLABLE vec_t<3, Type> normalize(vec_t<3, Type> a)
681
+ {
682
+ Type l = sqrt(a.c[0] * a.c[0] + a.c[1] * a.c[1] + a.c[2] * a.c[2]);
683
+ if (l > Type(kEps))
684
+ return vec_t<3, Type>(a.c[0]/l,a.c[1]/l,a.c[2]/l);
685
+ else
686
+ return vec_t<3, Type>();
687
+ }
688
+
689
+
690
+ template<typename Type>
691
+ inline CUDA_CALLABLE vec_t<3,Type> cross(vec_t<3,Type> a, vec_t<3,Type> b)
692
+ {
693
+ return {
694
+ Type(a[1]*b[2] - a[2]*b[1]),
695
+ Type(a[2]*b[0] - a[0]*b[2]),
696
+ Type(a[0]*b[1] - a[1]*b[0])
697
+ };
698
+ }
699
+
700
+
701
+ template<unsigned Length, typename Type>
702
+ inline bool CUDA_CALLABLE isfinite(vec_t<Length, Type> x)
703
+ {
704
+ for( unsigned i=0; i < Length; ++i )
705
+ {
706
+ if(!isfinite(x[i]))
707
+ {
708
+ return false;
709
+ }
710
+ }
711
+ return true;
712
+ }
713
+
714
+ template<unsigned Length, typename Type>
715
+ inline bool CUDA_CALLABLE isnan(vec_t<Length, Type> x)
716
+ {
717
+ for( unsigned i=0; i < Length; ++i )
718
+ {
719
+ if(isnan(x[i]))
720
+ {
721
+ return true;
722
+ }
723
+ }
724
+ return false;
725
+ }
726
+
727
+ template<unsigned Length, typename Type>
728
+ inline bool CUDA_CALLABLE isinf(vec_t<Length, Type> x)
729
+ {
730
+ for( unsigned i=0; i < Length; ++i )
731
+ {
732
+ if(isinf(x[i]))
733
+ {
734
+ return true;
735
+ }
736
+ }
737
+ return false;
738
+ }
739
+
740
+ // These two functions seem to compile very slowly
741
+ template<unsigned Length, typename Type>
742
+ inline CUDA_CALLABLE vec_t<Length,Type> min(vec_t<Length,Type> a, vec_t<Length,Type> b)
743
+ {
744
+ vec_t<Length,Type> ret;
745
+ for( unsigned i=0; i < Length; ++i )
746
+ {
747
+ ret[i] = a[i] < b[i] ? a[i] : b[i];
748
+ }
749
+ return ret;
750
+ }
751
+
752
+ template<unsigned Length, typename Type>
753
+ inline CUDA_CALLABLE vec_t<Length,Type> max(vec_t<Length,Type> a, vec_t<Length,Type> b)
754
+ {
755
+ vec_t<Length,Type> ret;
756
+ for( unsigned i=0; i < Length; ++i )
757
+ {
758
+ ret[i] = a[i] > b[i] ? a[i] : b[i];
759
+ }
760
+ return ret;
761
+ }
762
+
763
+ template<unsigned Length, typename Type>
764
+ inline CUDA_CALLABLE Type min(vec_t<Length,Type> v)
765
+ {
766
+ Type ret = v[0];
767
+ for( unsigned i=1; i < Length; ++i )
768
+ {
769
+ if (v[i] < ret)
770
+ ret = v[i];
771
+ }
772
+ return ret;
773
+ }
774
+
775
+ template<unsigned Length, typename Type>
776
+ inline CUDA_CALLABLE Type max(vec_t<Length,Type> v)
777
+ {
778
+ Type ret = v[0];
779
+ for( unsigned i=1; i < Length; ++i )
780
+ {
781
+ if (v[i] > ret)
782
+ ret = v[i];
783
+ }
784
+ return ret;
785
+ }
786
+
787
+ template<unsigned Length, typename Type>
788
+ inline CUDA_CALLABLE unsigned argmin(vec_t<Length,Type> v)
789
+ {
790
+ unsigned ret = 0;
791
+ for( unsigned i=1; i < Length; ++i )
792
+ {
793
+ if (v[i] < v[ret])
794
+ ret = i;
795
+ }
796
+ return ret;
797
+ }
798
+
799
+ template<unsigned Length, typename Type>
800
+ inline CUDA_CALLABLE unsigned argmax(vec_t<Length,Type> v)
801
+ {
802
+ unsigned ret = 0;
803
+ for( unsigned i=1; i < Length; ++i )
804
+ {
805
+ if (v[i] > v[ret])
806
+ ret = i;
807
+ }
808
+ return ret;
809
+ }
810
+
811
+ template<unsigned Length, typename Type>
812
+ inline CUDA_CALLABLE vec_t<Length,Type> abs(vec_t<Length,Type> v)
813
+ {
814
+ vec_t<Length,Type> ret;
815
+ for (unsigned i=0; i < Length; ++i)
816
+ {
817
+ ret[i] = abs(v[i]);
818
+ }
819
+
820
+ return ret;
821
+ }
822
+
823
+ template<unsigned Length, typename Type>
824
+ inline CUDA_CALLABLE vec_t<Length,Type> sign(vec_t<Length,Type> v)
825
+ {
826
+ vec_t<Length,Type> ret;
827
+ for (unsigned i=0; i < Length; ++i)
828
+ {
829
+ ret[i] = v[i] < Type(0) ? Type(-1) : Type(1);
830
+ }
831
+
832
+ return ret;
833
+ }
834
+
835
+ template<unsigned Length, typename Type>
836
+ inline CUDA_CALLABLE void expect_near(const vec_t<Length, Type>& actual, const vec_t<Length, Type>& expected, const Type& tolerance)
837
+ {
838
+ Type diff(0);
839
+ for(size_t i=0; i<Length; ++i)
840
+ {
841
+ diff = max(diff,abs(actual[i] - expected[i]));
842
+ }
843
+ if (diff > tolerance)
844
+ {
845
+ printf("Error, expect_near() failed with tolerance "); print(tolerance);
846
+ printf("\t Expected: "); print(expected);
847
+ printf("\t Actual: "); print(actual);
848
+ }
849
+ }
850
+
851
+ template<unsigned Length, typename Type>
852
+ inline CUDA_CALLABLE void adj_expect_near(const vec_t<Length, Type>& actual, const vec_t<Length, Type>& expected, Type tolerance, vec_t<Length, Type>& adj_actual, vec_t<Length, Type>& adj_expected, Type adj_tolerance)
853
+ {
854
+ // nop
855
+ }
856
+
857
+ // adjoint for the initializer_array constructor:
858
+ template<unsigned Length, typename Type>
859
+ inline CUDA_CALLABLE void adj_vec_t(const initializer_array<Length, Type> &cmps, const initializer_array<Length, Type*> &adj_cmps, const vec_t<Length, Type>& adj_ret)
860
+ {
861
+ for(unsigned i=0; i < Length; ++i)
862
+ {
863
+ *(adj_cmps[i]) += adj_ret[i];
864
+ }
865
+ }
866
+
867
+
868
+ // adjoint for the component constructors:
869
+ template<typename Type>
870
+ inline CUDA_CALLABLE void adj_vec_t(Type cmpx, Type cmpy, Type &adj_cmpx, Type &adj_cmpy, const vec_t<2, Type>& adj_ret)
871
+ {
872
+ adj_cmpx += adj_ret.c[0];
873
+ adj_cmpy += adj_ret.c[1];
874
+ }
875
+
876
+ template<typename Type>
877
+ inline CUDA_CALLABLE void adj_vec_t(Type cmpx, Type cmpy, Type cmpz, Type &adj_cmpx, Type &adj_cmpy, Type &adj_cmpz, const vec_t<3, Type>& adj_ret)
878
+ {
879
+ adj_cmpx += adj_ret.c[0];
880
+ adj_cmpy += adj_ret.c[1];
881
+ adj_cmpz += adj_ret.c[2];
882
+ }
883
+
884
+ template<typename Type>
885
+ inline CUDA_CALLABLE void adj_vec_t(Type cmpx, Type cmpy, Type cmpz, Type cmpw, Type &adj_cmpx, Type &adj_cmpy, Type &adj_cmpz, Type &adj_cmpw, const vec_t<4, Type>& adj_ret)
886
+ {
887
+ adj_cmpx += adj_ret.c[0];
888
+ adj_cmpy += adj_ret.c[1];
889
+ adj_cmpz += adj_ret.c[2];
890
+ adj_cmpw += adj_ret.c[3];
891
+ }
892
+
893
+ // adjoint for the constant constructor:
894
+ template<unsigned Length, typename Type>
895
+ inline CUDA_CALLABLE void adj_vec_t(Type s, Type& adj_s, const vec_t<Length, Type>& adj_ret)
896
+ {
897
+ for( unsigned i=0; i < Length; ++i )
898
+ {
899
+ adj_s += adj_ret[i];
900
+ }
901
+ }
902
+
903
+ // adjoint for the casting constructor
904
+ template<unsigned Length, typename Type, typename OtherType>
905
+ inline CUDA_CALLABLE void adj_vec_t(const vec_t<Length, OtherType>& other, vec_t<Length, OtherType>& adj_other, const vec_t<Length, Type>& adj_ret)
906
+ {
907
+ for( unsigned i=0; i < Length; ++i )
908
+ {
909
+ adj_other[i] += static_cast<OtherType>(adj_ret[i]);
910
+ }
911
+ }
912
+
913
+ template<typename Type>
914
+ CUDA_CALLABLE inline void adj_vec_t(const vec_t<3,Type>& w, const vec_t<3,Type>& v, vec_t<3,Type>& adj_w, vec_t<3,Type>& adj_v, const vec_t<6,Type>& adj_ret)
915
+ {
916
+ adj_w[0] += adj_ret[0];
917
+ adj_w[1] += adj_ret[1];
918
+ adj_w[2] += adj_ret[2];
919
+ adj_v[0] += adj_ret[3];
920
+ adj_v[1] += adj_ret[4];
921
+ adj_v[2] += adj_ret[5];
922
+ }
923
+
924
+ template<unsigned Length, typename Type>
925
+ inline CUDA_CALLABLE void adj_mul(vec_t<Length, Type> a, Type s, vec_t<Length, Type>& adj_a, Type& adj_s, const vec_t<Length, Type>& adj_ret)
926
+ {
927
+ for( unsigned i=0; i < Length; ++i )
928
+ {
929
+ adj_a[i] += s*adj_ret[i];
930
+ }
931
+
932
+ adj_s += dot(a, adj_ret);
933
+
934
+ #if FP_CHECK
935
+ if (!isfinite(a) || !isfinite(s) || !isfinite(adj_a) || !isfinite(adj_s) || !isfinite(adj_ret))
936
+ {
937
+ // \TODO: How shall we implement this error message?
938
+ //printf("adj_mul((%f %f %f %f), %f, (%f %f %f %f), %f, (%f %f %f %f)\n", a.x, a.y, a.z, a.w, s, adj_a.x, adj_a.y, adj_a.z, adj_a.w, adj_s, adj_ret.x, adj_ret.y, adj_ret.z, adj_ret.w);
939
+ assert(0);
940
+ }
941
+ #endif
942
+ }
943
+
944
+ template<unsigned Length, typename Type>
945
+ inline CUDA_CALLABLE void adj_mul(Type s, vec_t<Length, Type> a, Type& adj_s, vec_t<Length, Type>& adj_a, const vec_t<Length, Type>& adj_ret)
946
+ {
947
+ adj_mul(a, s, adj_a, adj_s, adj_ret);
948
+ }
949
+
950
+ template<unsigned Length, typename Type>
951
+ inline CUDA_CALLABLE void adj_cw_mul(vec_t<Length, Type> a, vec_t<Length, Type> b, vec_t<Length, Type>& adj_a, vec_t<Length, Type>& adj_b, const vec_t<Length, Type>& adj_ret)
952
+ {
953
+ adj_a += cw_mul(b, adj_ret);
954
+ adj_b += cw_mul(a, adj_ret);
955
+ }
956
+
957
+ template<unsigned Length, typename Type>
958
+ inline CUDA_CALLABLE void adj_div(vec_t<Length, Type> a, Type s, vec_t<Length, Type>& adj_a, Type& adj_s, const vec_t<Length, Type>& adj_ret)
959
+ {
960
+
961
+ adj_s -= dot(a , adj_ret)/ (s * s); // - a / s^2
962
+
963
+ for( unsigned i=0; i < Length; ++i )
964
+ {
965
+ adj_a[i] += adj_ret[i] / s;
966
+ }
967
+
968
+ #if FP_CHECK
969
+ if (!isfinite(a) || !isfinite(s) || !isfinite(adj_a) || !isfinite(adj_s) || !isfinite(adj_ret))
970
+ {
971
+ // \TODO: How shall we implement this error message?
972
+ // printf("adj_div((%f %f %f %f), %f, (%f %f %f %f), %f, (%f %f %f %f)\n", a.x, a.y, a.z, a.w, s, adj_a.x, adj_a.y, adj_a.z, adj_a.w, adj_s, adj_ret.x, adj_ret.y, adj_ret.z, adj_ret.w);
973
+ assert(0);
974
+ }
975
+ #endif
976
+ }
977
+
978
+ template<unsigned Length, typename Type>
979
+ inline CUDA_CALLABLE void adj_div(Type s, vec_t<Length, Type> a, Type& adj_s, vec_t<Length, Type>& adj_a, const vec_t<Length, Type>& adj_ret)
980
+ {
981
+
982
+ adj_s -= dot(a , adj_ret)/ (s * s); // - a / s^2
983
+
984
+ for( unsigned i=0; i < Length; ++i )
985
+ {
986
+ adj_a[i] += s / adj_ret[i];
987
+ }
988
+
989
+ #if FP_CHECK
990
+ if (!isfinite(a) || !isfinite(s) || !isfinite(adj_a) || !isfinite(adj_s) || !isfinite(adj_ret))
991
+ {
992
+ // \TODO: How shall we implement this error message?
993
+ // printf("adj_div((%f %f %f %f), %f, (%f %f %f %f), %f, (%f %f %f %f)\n", a.x, a.y, a.z, a.w, s, adj_a.x, adj_a.y, adj_a.z, adj_a.w, adj_s, adj_ret.x, adj_ret.y, adj_ret.z, adj_ret.w);
994
+ assert(0);
995
+ }
996
+ #endif
997
+ }
998
+
999
+ template<unsigned Length, typename Type>
1000
+ inline CUDA_CALLABLE void adj_cw_div(vec_t<Length, Type> a, vec_t<Length, Type> b, vec_t<Length, Type>& ret, vec_t<Length, Type>& adj_a, vec_t<Length, Type>& adj_b, const vec_t<Length, Type>& adj_ret) {
1001
+ adj_a += cw_div(adj_ret, b);
1002
+ adj_b -= cw_mul(adj_ret, cw_div(ret, b));
1003
+ }
1004
+
1005
+ template<unsigned Length, typename Type>
1006
+ inline CUDA_CALLABLE void adj_add(vec_t<Length, Type> a, vec_t<Length, Type> b, vec_t<Length, Type>& adj_a, vec_t<Length, Type>& adj_b, const vec_t<Length, Type>& adj_ret)
1007
+ {
1008
+ adj_a += adj_ret;
1009
+ adj_b += adj_ret;
1010
+ }
1011
+
1012
+ template<typename Type>
1013
+ inline CUDA_CALLABLE void adj_add(vec_t<2, Type> a, vec_t<2, Type> b, vec_t<2, Type>& adj_a, vec_t<2, Type>& adj_b, const vec_t<2, Type>& adj_ret)
1014
+ {
1015
+ adj_a.c[0] += adj_ret.c[0];
1016
+ adj_a.c[1] += adj_ret.c[1];
1017
+ adj_b.c[0] += adj_ret.c[0];
1018
+ adj_b.c[1] += adj_ret.c[1];
1019
+ }
1020
+
1021
+ template<typename Type>
1022
+ inline CUDA_CALLABLE void adj_add(vec_t<3, Type> a, vec_t<3, Type> b, vec_t<3, Type>& adj_a, vec_t<3, Type>& adj_b, const vec_t<3, Type>& adj_ret)
1023
+ {
1024
+ adj_a.c[0] += adj_ret.c[0];
1025
+ adj_a.c[1] += adj_ret.c[1];
1026
+ adj_a.c[2] += adj_ret.c[2];
1027
+ adj_b.c[0] += adj_ret.c[0];
1028
+ adj_b.c[1] += adj_ret.c[1];
1029
+ adj_b.c[2] += adj_ret.c[2];
1030
+ }
1031
+
1032
+ template<unsigned Length, typename Type>
1033
+ inline CUDA_CALLABLE void adj_sub(vec_t<Length, Type> a, vec_t<Length, Type> b, vec_t<Length, Type>& adj_a, vec_t<Length, Type>& adj_b, const vec_t<Length, Type>& adj_ret)
1034
+ {
1035
+ adj_a += adj_ret;
1036
+ adj_b -= adj_ret;
1037
+ }
1038
+
1039
+ template<typename Type>
1040
+ inline CUDA_CALLABLE void adj_sub(vec_t<2, Type> a, vec_t<2, Type> b, vec_t<2, Type>& adj_a, vec_t<2, Type>& adj_b, const vec_t<2, Type>& adj_ret)
1041
+ {
1042
+ adj_a.c[0] += adj_ret.c[0];
1043
+ adj_a.c[1] += adj_ret.c[1];
1044
+ adj_b.c[0] -= adj_ret.c[0];
1045
+ adj_b.c[1] -= adj_ret.c[1];
1046
+ }
1047
+
1048
+ template<typename Type>
1049
+ inline CUDA_CALLABLE void adj_sub(vec_t<3, Type> a, vec_t<3, Type> b, vec_t<3, Type>& adj_a, vec_t<3, Type>& adj_b, const vec_t<3, Type>& adj_ret)
1050
+ {
1051
+ adj_a.c[0] += adj_ret.c[0];
1052
+ adj_a.c[1] += adj_ret.c[1];
1053
+ adj_a.c[2] += adj_ret.c[2];
1054
+ adj_b.c[0] -= adj_ret.c[0];
1055
+ adj_b.c[1] -= adj_ret.c[1];
1056
+ adj_b.c[2] -= adj_ret.c[2];
1057
+ }
1058
+
1059
+ template<unsigned Length, typename Type>
1060
+ inline CUDA_CALLABLE void adj_mod(vec_t<Length, Type> a, vec_t<Length, Type> b, vec_t<Length, Type>& adj_a, vec_t<Length, Type>& adj_b, const vec_t<Length, Type>& adj_ret)
1061
+ {
1062
+ }
1063
+
1064
+ template<typename Type>
1065
+ inline CUDA_CALLABLE void adj_mod(vec_t<2, Type> a, vec_t<2, Type> b, vec_t<2, Type>& adj_a, vec_t<2, Type>& adj_b, const vec_t<2, Type>& adj_ret)
1066
+ {
1067
+ }
1068
+
1069
+ template<typename Type>
1070
+ inline CUDA_CALLABLE void adj_mod(vec_t<3, Type> a, vec_t<3, Type> b, vec_t<3, Type>& adj_a, vec_t<3, Type>& adj_b, const vec_t<3, Type>& adj_ret)
1071
+ {
1072
+ }
1073
+
1074
+ template<unsigned Length, typename Type>
1075
+ inline CUDA_CALLABLE void adj_dot(vec_t<Length, Type> a, vec_t<Length, Type> b, vec_t<Length, Type>& adj_a, vec_t<Length, Type>& adj_b, const Type adj_ret)
1076
+ {
1077
+ adj_a += b*adj_ret;
1078
+ adj_b += a*adj_ret;
1079
+
1080
+ #if FP_CHECK
1081
+ if (!isfinite(a) || !isfinite(b) || !isfinite(adj_a) || !isfinite(adj_b) || !isfinite(adj_ret))
1082
+ {
1083
+ // \TODO: How shall we implement this error message?
1084
+ //printf("adj_dot((%f %f %f %f), (%f %f %f %f), (%f %f %f %f), (%f %f %f %f), %f)\n", a.x, a.y, a.z, a.w, b.x, b.y, b.z, b.w, adj_a.x, adj_a.y, adj_a.z, adj_a.w, adj_b.x, adj_b.y, adj_b.z, adj_b.w, adj_ret);
1085
+ assert(0);
1086
+ }
1087
+ #endif
1088
+ }
1089
+
1090
+
1091
+
1092
+ template<typename Type>
1093
+ inline CUDA_CALLABLE void adj_dot(vec_t<2, Type> a, vec_t<2, Type> b, vec_t<2, Type>& adj_a, vec_t<2, Type>& adj_b, const Type adj_ret)
1094
+ {
1095
+ adj_a.c[0] += b.c[0]*adj_ret;
1096
+ adj_a.c[1] += b.c[1]*adj_ret;
1097
+
1098
+ adj_b.c[0] += a.c[0]*adj_ret;
1099
+ adj_b.c[1] += a.c[1]*adj_ret;
1100
+ }
1101
+
1102
+ template<typename Type>
1103
+ inline CUDA_CALLABLE void adj_dot(vec_t<3, Type> a, vec_t<3, Type> b, vec_t<3, Type>& adj_a, vec_t<3, Type>& adj_b, const Type adj_ret)
1104
+ {
1105
+ adj_a.c[0] += b.c[0]*adj_ret;
1106
+ adj_a.c[1] += b.c[1]*adj_ret;
1107
+ adj_a.c[2] += b.c[2]*adj_ret;
1108
+
1109
+ adj_b.c[0] += a.c[0]*adj_ret;
1110
+ adj_b.c[1] += a.c[1]*adj_ret;
1111
+ adj_b.c[2] += a.c[2]*adj_ret;
1112
+ }
1113
+
1114
+
1115
+ template<unsigned Length, typename Type>
1116
+ inline CUDA_CALLABLE void adj_extract(const vec_t<Length, Type> & a, int idx, vec_t<Length, Type> & adj_a, int & adj_idx, Type & adj_ret)
1117
+ {
1118
+ #ifndef NDEBUG
1119
+ if (idx < 0 || idx > Length)
1120
+ {
1121
+ printf("Tvec2<Scalar> index %d out of bounds at %s %d\n", idx, __FILE__, __LINE__);
1122
+ assert(0);
1123
+ }
1124
+ #endif
1125
+
1126
+ adj_a[idx] += adj_ret;
1127
+ }
1128
+
1129
+ template<unsigned Length, typename Type>
1130
+ inline CUDA_CALLABLE void adj_length(vec_t<Length, Type> a, Type ret, vec_t<Length, Type>& adj_a, const Type adj_ret)
1131
+ {
1132
+ if (ret > Type(kEps))
1133
+ {
1134
+ adj_a += div(a, ret) * adj_ret;
1135
+ }
1136
+
1137
+ #if FP_CHECK
1138
+ if (!isfinite(adj_a))
1139
+ {
1140
+ // \TODO: How shall we implement this error message?
1141
+ //printf("%s:%d - adj_length((%f %f %f %f), (%f %f %f %f), (%f))\n", __FILE__, __LINE__, a.x, a.y, a.z, a.w, adj_a.x, adj_a.y, adj_a.z, adj_a.w, adj_ret);
1142
+ assert(0);
1143
+ }
1144
+ #endif
1145
+ }
1146
+
1147
+ template<unsigned Length, typename Type>
1148
+ inline CUDA_CALLABLE void adj_length_sq(vec_t<Length, Type> a, vec_t<Length, Type>& adj_a, const Type adj_ret)
1149
+ {
1150
+ adj_a += Type(2.0)*a*adj_ret;
1151
+
1152
+ #if FP_CHECK
1153
+ if (!isfinite(adj_a))
1154
+ {
1155
+ // \TODO: How shall we implement this error message?
1156
+ //printf("%s:%d - adj_length((%f %f %f %f), (%f %f %f %f), (%f))\n", __FILE__, __LINE__, a.x, a.y, a.z, a.w, adj_a.x, adj_a.y, adj_a.z, adj_a.w, adj_ret);
1157
+ assert(0);
1158
+ }
1159
+ #endif
1160
+ }
1161
+
1162
+ template<unsigned Length, typename Type>
1163
+ inline CUDA_CALLABLE void adj_normalize(vec_t<Length, Type> a, vec_t<Length, Type>& ret, vec_t<Length, Type>& adj_a, const vec_t<Length, Type>& adj_ret)
1164
+ {
1165
+ Type d = length(a);
1166
+
1167
+ if (d > Type(kEps))
1168
+ {
1169
+ Type invd = Type(1.0f)/d;
1170
+
1171
+ adj_a += (adj_ret*invd - ret*(dot(ret, adj_ret))*invd);
1172
+
1173
+ #if FP_CHECK
1174
+ if (!isfinite(adj_a))
1175
+ {
1176
+ // \TODO: How shall we implement this error message?
1177
+ //printf("%s:%d - adj_normalize((%f %f %f %f), (%f %f %f %f), (%f, %f, %f, %f))\n", __FILE__, __LINE__, a.x, a.y, a.z, a.w, adj_a.x, adj_a.y, adj_a.z, adj_a.w, adj_ret.x, adj_ret.y, adj_ret.z, adj_ret.w);
1178
+ assert(0);
1179
+ }
1180
+ #endif
1181
+ }
1182
+ }
1183
+
1184
+ template<typename Type>
1185
+ inline CUDA_CALLABLE void adj_cross(vec_t<3,Type> a, vec_t<3,Type> b, vec_t<3,Type>& adj_a, vec_t<3,Type>& adj_b, const vec_t<3,Type>& adj_ret)
1186
+ {
1187
+ // todo: sign check
1188
+ adj_a += cross(b, adj_ret);
1189
+ adj_b -= cross(a, adj_ret);
1190
+ }
1191
+
1192
+ template<unsigned Length, typename Type>
1193
+ inline CUDA_CALLABLE void adj_isfinite(const vec_t<Length, Type> &x, vec_t<Length,Type>& adj_x, const bool &adj_ret)
1194
+ {
1195
+
1196
+ }
1197
+
1198
+ template<unsigned Length, typename Type>
1199
+ inline CUDA_CALLABLE void adj_isnan(const vec_t<Length, Type> &x, vec_t<Length,Type>& adj_x, const bool &adj_ret)
1200
+ {
1201
+
1202
+ }
1203
+
1204
+ template<unsigned Length, typename Type>
1205
+ inline CUDA_CALLABLE void adj_isinf(const vec_t<Length, Type> &x, vec_t<Length,Type>& adj_x, const bool &adj_ret)
1206
+ {
1207
+
1208
+ }
1209
+
1210
+ template<unsigned Length, typename Type>
1211
+ inline CUDA_CALLABLE void adj_min(const vec_t<Length,Type> &a, const vec_t<Length,Type> &b, vec_t<Length,Type>& adj_a, vec_t<Length,Type>& adj_b, const vec_t<Length,Type> &adj_ret)
1212
+ {
1213
+ for( unsigned i=0; i < Length; ++i )
1214
+ {
1215
+ if (a[i] < b[i])
1216
+ adj_a[i] += adj_ret[i];
1217
+ else
1218
+ adj_b[i] += adj_ret[i];
1219
+ }
1220
+ }
1221
+
1222
+ template<unsigned Length, typename Type>
1223
+ inline CUDA_CALLABLE void adj_max(const vec_t<Length,Type> &a, const vec_t<Length,Type> &b, vec_t<Length,Type>& adj_a, vec_t<Length,Type>& adj_b, const vec_t<Length,Type> &adj_ret)
1224
+ {
1225
+ for( unsigned i=0; i < Length; ++i )
1226
+ {
1227
+ if (a[i] > b[i])
1228
+ adj_a[i] += adj_ret[i];
1229
+ else
1230
+ adj_b[i] += adj_ret[i];
1231
+ }
1232
+ }
1233
+
1234
+ template<unsigned Length, typename Type>
1235
+ inline CUDA_CALLABLE void adj_min(const vec_t<Length,Type> &v, vec_t<Length,Type>& adj_v, const Type &adj_ret)
1236
+ {
1237
+ unsigned i = argmin(v);
1238
+ adj_v[i] += adj_ret;
1239
+ }
1240
+
1241
+ template<unsigned Length, typename Type>
1242
+ inline CUDA_CALLABLE void adj_max(const vec_t<Length,Type> &v, vec_t<Length,Type>& adj_v, const Type &adj_ret)
1243
+ {
1244
+ unsigned i = argmax(v);
1245
+ adj_v[i] += adj_ret;
1246
+ }
1247
+
1248
+ template<unsigned Length, typename Type>
1249
+ inline CUDA_CALLABLE void adj_abs(
1250
+ const vec_t<Length,Type>& v,
1251
+ vec_t<Length,Type>& adj_v,
1252
+ const vec_t<Length,Type>& adj_ret
1253
+ )
1254
+ {
1255
+ for (unsigned i=0; i < Length; ++i)
1256
+ {
1257
+ if (v[i] < Type(0))
1258
+ {
1259
+ adj_v[i] -= adj_ret[i];
1260
+ }
1261
+ else
1262
+ {
1263
+ adj_v[i] += adj_ret[i];
1264
+ }
1265
+ }
1266
+ }
1267
+
1268
+ template<unsigned Length, typename Type>
1269
+ inline CUDA_CALLABLE void adj_sign(
1270
+ const vec_t<Length,Type>& v,
1271
+ vec_t<Length,Type>& adj_v,
1272
+ const vec_t<Length,Type>& adj_ret
1273
+ )
1274
+ {
1275
+ for (unsigned i=0; i < Length; ++i)
1276
+ {
1277
+ if (v[i] < Type(0))
1278
+ {
1279
+ adj_v[i] -= adj_ret[i];
1280
+ }
1281
+ else
1282
+ {
1283
+ adj_v[i] += adj_ret[i];
1284
+ }
1285
+ }
1286
+ }
1287
+
1288
+ // Do I need to specialize these for different lengths?
1289
+ template<unsigned Length, typename Type>
1290
+ inline CUDA_CALLABLE vec_t<Length, Type> atomic_add(vec_t<Length, Type> * addr, vec_t<Length, Type> value)
1291
+ {
1292
+ vec_t<Length, Type> ret;
1293
+ for( unsigned i=0; i < Length; ++i )
1294
+ {
1295
+ ret[i] = atomic_add(&(addr -> c[i]), value[i]);
1296
+ }
1297
+
1298
+ return ret;
1299
+ }
1300
+
1301
+ template<unsigned Length, typename Type>
1302
+ inline CUDA_CALLABLE vec_t<Length, Type> atomic_min(vec_t<Length, Type> * addr, vec_t<Length, Type> value)
1303
+ {
1304
+ vec_t<Length, Type> ret;
1305
+ for( unsigned i=0; i < Length; ++i )
1306
+ {
1307
+ ret[i] = atomic_min(&(addr -> c[i]), value[i]);
1308
+ }
1309
+
1310
+ return ret;
1311
+ }
1312
+
1313
+ template<unsigned Length, typename Type>
1314
+ inline CUDA_CALLABLE vec_t<Length, Type> atomic_max(vec_t<Length, Type> * addr, vec_t<Length, Type> value)
1315
+ {
1316
+ vec_t<Length, Type> ret;
1317
+ for( unsigned i=0; i < Length; ++i )
1318
+ {
1319
+ ret[i] = atomic_max(&(addr -> c[i]), value[i]);
1320
+ }
1321
+
1322
+ return ret;
1323
+ }
1324
+
1325
+ template<unsigned Length, typename Type>
1326
+ inline CUDA_CALLABLE void adj_atomic_minmax(
1327
+ vec_t<Length,Type> *addr,
1328
+ vec_t<Length,Type> *adj_addr,
1329
+ const vec_t<Length,Type> &value,
1330
+ vec_t<Length,Type> &adj_value)
1331
+ {
1332
+ for (unsigned i=0; i < Length; ++i)
1333
+ adj_atomic_minmax(&(addr->c[i]), &(adj_addr->c[i]), value[i], adj_value[i]);
1334
+ }
1335
+
1336
+ // ok, the original implementation of this didn't take the absolute values.
1337
+ // I wouldn't consider this expected behavior. It looks like it's only
1338
+ // being used for bounding boxes at the moment, where this doesn't matter,
1339
+ // but you often use it for ray tracing where it does. Not sure if the
1340
+ // fabs() incurs a performance hit...
1341
+ template<unsigned Length, typename Type>
1342
+ CUDA_CALLABLE inline int longest_axis(const vec_t<Length, Type>& v)
1343
+ {
1344
+ Type lmax = abs(v[0]);
1345
+ int ret(0);
1346
+ for( unsigned i=1; i < Length; ++i )
1347
+ {
1348
+ Type l = abs(v[i]);
1349
+ if( l > lmax )
1350
+ {
1351
+ ret = i;
1352
+ lmax = l;
1353
+ }
1354
+ }
1355
+ return ret;
1356
+ }
1357
+
1358
+ template<unsigned Length, typename Type>
1359
+ CUDA_CALLABLE inline vec_t<Length,Type> lerp(const vec_t<Length,Type>& a, const vec_t<Length,Type>& b, Type t)
1360
+ {
1361
+ return a*(Type(1)-t) + b*t;
1362
+ }
1363
+
1364
+ template<unsigned Length, typename Type>
1365
+ CUDA_CALLABLE inline void adj_lerp(const vec_t<Length,Type>& a, const vec_t<Length,Type>& b, Type t, vec_t<Length,Type>& adj_a, vec_t<Length,Type>& adj_b, Type& adj_t, const vec_t<Length,Type>& adj_ret)
1366
+ {
1367
+ adj_a += adj_ret*(Type(1)-t);
1368
+ adj_b += adj_ret*t;
1369
+ adj_t += tensordot(b, adj_ret) - tensordot(a, adj_ret);
1370
+ }
1371
+
1372
+ // for integral types we do not accumulate gradients
1373
+ template<unsigned Length> CUDA_CALLABLE inline void adj_atomic_add(vec_t<Length, int8>* buf, const vec_t<Length, int8> &value) { }
1374
+ template<unsigned Length> CUDA_CALLABLE inline void adj_atomic_add(vec_t<Length, uint8>* buf, const vec_t<Length, uint8> &value) { }
1375
+ template<unsigned Length> CUDA_CALLABLE inline void adj_atomic_add(vec_t<Length, int16>* buf, const vec_t<Length, int16> &value) { }
1376
+ template<unsigned Length> CUDA_CALLABLE inline void adj_atomic_add(vec_t<Length, uint16>* buf, const vec_t<Length, uint16> &value) { }
1377
+ template<unsigned Length> CUDA_CALLABLE inline void adj_atomic_add(vec_t<Length, int32>* buf, const vec_t<Length, int32> &value) { }
1378
+ template<unsigned Length> CUDA_CALLABLE inline void adj_atomic_add(vec_t<Length, uint32>* buf, const vec_t<Length, uint32> &value) { }
1379
+ template<unsigned Length> CUDA_CALLABLE inline void adj_atomic_add(vec_t<Length, int64>* buf, const vec_t<Length, int64> &value) { }
1380
+ template<unsigned Length> CUDA_CALLABLE inline void adj_atomic_add(vec_t<Length, uint64>* buf, const vec_t<Length, uint64> &value) { }
1381
+
1382
+
1383
+ // adjoints for some of the constructors, used in intersect.h
1384
+ inline CUDA_CALLABLE void adj_vec2(float x, float y, float& adj_x, float& adj_y, const vec2& adj_ret)
1385
+ {
1386
+ adj_x += adj_ret[0];
1387
+ adj_y += adj_ret[1];
1388
+ }
1389
+
1390
+ inline CUDA_CALLABLE void adj_vec3(float x, float y, float z, float& adj_x, float& adj_y, float& adj_z, const vec3& adj_ret)
1391
+ {
1392
+ adj_x += adj_ret[0];
1393
+ adj_y += adj_ret[1];
1394
+ adj_z += adj_ret[2];
1395
+ }
1396
+
1397
+ inline CUDA_CALLABLE void adj_vec4(float x, float y, float z, float w, float& adj_x, float& adj_y, float& adj_z, float& adj_w, const vec4& adj_ret)
1398
+ {
1399
+ adj_x += adj_ret[0];
1400
+ adj_y += adj_ret[1];
1401
+ adj_z += adj_ret[2];
1402
+ adj_w += adj_ret[3];
1403
+ }
1404
+
1405
+ inline CUDA_CALLABLE void adj_vec3(float s, float& adj_s, const vec3& adj_ret)
1406
+ {
1407
+ adj_vec_t(s, adj_s, adj_ret);
1408
+ }
1409
+
1410
+ inline CUDA_CALLABLE void adj_vec4(float s, float& adj_s, const vec4& adj_ret)
1411
+ {
1412
+ adj_vec_t(s, adj_s, adj_ret);
1413
+ }
1414
+
1415
+ template<unsigned Length, typename Type>
1416
+ CUDA_CALLABLE inline int len(const vec_t<Length, Type>& x)
1417
+ {
1418
+ return Length;
1419
+ }
1420
+
1421
+ template<unsigned Length, typename Type>
1422
+ CUDA_CALLABLE inline void adj_len(const vec_t<Length, Type>& x, vec_t<Length, Type>& adj_x, const int& adj_ret)
1423
+ {
1424
+ }
1425
+
1426
+ } // namespace wp