warp-lang 1.7.0__py3-none-manylinux_2_28_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 (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/mat.h ADDED
@@ -0,0 +1,2220 @@
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
+ //----------------------------------------------------------
26
+ // mat
27
+ template<typename T>
28
+ struct quat_t;
29
+
30
+ template<unsigned Rows, unsigned Cols, typename Type>
31
+ struct mat_t
32
+ {
33
+ inline CUDA_CALLABLE mat_t()
34
+ : data()
35
+ {}
36
+
37
+ inline CUDA_CALLABLE mat_t(Type s)
38
+ {
39
+ for (unsigned i=0; i < Rows; ++i)
40
+ for (unsigned j=0; j < Cols; ++j)
41
+ data[i][j] = s;
42
+ }
43
+
44
+ template <typename OtherType>
45
+ inline explicit CUDA_CALLABLE mat_t(const mat_t<Rows, Cols, OtherType>& other)
46
+ {
47
+ for (unsigned i=0; i < Rows; ++i)
48
+ for (unsigned j=0; j < Cols; ++j)
49
+ data[i][j] = other.data[i][j];
50
+ }
51
+
52
+ inline CUDA_CALLABLE mat_t(vec_t<2,Type> c0, vec_t<2,Type> c1)
53
+ {
54
+ data[0][0] = c0[0];
55
+ data[1][0] = c0[1];
56
+
57
+ data[0][1] = c1[0];
58
+ data[1][1] = c1[1];
59
+ }
60
+
61
+ inline CUDA_CALLABLE mat_t(vec_t<3,Type> c0, vec_t<3,Type> c1, vec_t<3,Type> c2)
62
+ {
63
+ data[0][0] = c0[0];
64
+ data[1][0] = c0[1];
65
+ data[2][0] = c0[2];
66
+
67
+ data[0][1] = c1[0];
68
+ data[1][1] = c1[1];
69
+ data[2][1] = c1[2];
70
+
71
+ data[0][2] = c2[0];
72
+ data[1][2] = c2[1];
73
+ data[2][2] = c2[2];
74
+ }
75
+
76
+ inline CUDA_CALLABLE mat_t(vec_t<4,Type> c0, vec_t<4,Type> c1, vec_t<4,Type> c2, vec_t<4,Type> c3)
77
+ {
78
+ data[0][0] = c0[0];
79
+ data[1][0] = c0[1];
80
+ data[2][0] = c0[2];
81
+ data[3][0] = c0[3];
82
+
83
+ data[0][1] = c1[0];
84
+ data[1][1] = c1[1];
85
+ data[2][1] = c1[2];
86
+ data[3][1] = c1[3];
87
+
88
+ data[0][2] = c2[0];
89
+ data[1][2] = c2[1];
90
+ data[2][2] = c2[2];
91
+ data[3][2] = c2[3];
92
+
93
+ data[0][3] = c3[0];
94
+ data[1][3] = c3[1];
95
+ data[2][3] = c3[2];
96
+ data[3][3] = c3[3];
97
+ }
98
+
99
+ inline CUDA_CALLABLE mat_t(Type m00, Type m01, Type m10, Type m11)
100
+ {
101
+ data[0][0] = m00;
102
+ data[1][0] = m10;
103
+ data[0][1] = m01;
104
+ data[1][1] = m11;
105
+ }
106
+
107
+ inline CUDA_CALLABLE mat_t(
108
+ Type m00, Type m01, Type m02,
109
+ Type m10, Type m11, Type m12,
110
+ Type m20, Type m21, Type m22)
111
+ {
112
+ data[0][0] = m00;
113
+ data[1][0] = m10;
114
+ data[2][0] = m20;
115
+
116
+ data[0][1] = m01;
117
+ data[1][1] = m11;
118
+ data[2][1] = m21;
119
+
120
+ data[0][2] = m02;
121
+ data[1][2] = m12;
122
+ data[2][2] = m22;
123
+ }
124
+
125
+ inline CUDA_CALLABLE mat_t(
126
+ Type m00, Type m01, Type m02, Type m03,
127
+ Type m10, Type m11, Type m12, Type m13,
128
+ Type m20, Type m21, Type m22, Type m23,
129
+ Type m30, Type m31, Type m32, Type m33)
130
+ {
131
+ data[0][0] = m00;
132
+ data[1][0] = m10;
133
+ data[2][0] = m20;
134
+ data[3][0] = m30;
135
+
136
+ data[0][1] = m01;
137
+ data[1][1] = m11;
138
+ data[2][1] = m21;
139
+ data[3][1] = m31;
140
+
141
+ data[0][2] = m02;
142
+ data[1][2] = m12;
143
+ data[2][2] = m22;
144
+ data[3][2] = m32;
145
+
146
+ data[0][3] = m03;
147
+ data[1][3] = m13;
148
+ data[2][3] = m23;
149
+ data[3][3] = m33;
150
+ }
151
+
152
+ // implemented in quat.h
153
+ inline CUDA_CALLABLE mat_t(const vec_t<3,Type>& pos, const quat_t<Type>& rot, const vec_t<3,Type>& scale);
154
+
155
+
156
+ inline CUDA_CALLABLE mat_t(const initializer_array<Rows * Cols, Type> &l)
157
+ {
158
+ for (unsigned i=0; i < Rows; ++i)
159
+ {
160
+ for (unsigned j=0; j < Cols; ++j)
161
+ {
162
+ data[i][j] = l[i * Cols + j];
163
+ }
164
+ }
165
+ }
166
+
167
+ inline CUDA_CALLABLE mat_t(const initializer_array<Cols, vec_t<Rows,Type> > &l)
168
+ {
169
+ for (unsigned j=0; j < Cols; ++j)
170
+ {
171
+ for (unsigned i=0; i < Rows; ++i)
172
+ {
173
+ data[i][j] = l[j][i];
174
+ }
175
+ }
176
+ }
177
+
178
+ CUDA_CALLABLE vec_t<Cols,Type> get_row(int index) const
179
+ {
180
+ return (vec_t<Cols,Type>&)data[index];
181
+ }
182
+
183
+ CUDA_CALLABLE void set_row(int index, const vec_t<Cols,Type>& v)
184
+ {
185
+ (vec_t<Cols,Type>&)data[index] = v;
186
+ }
187
+
188
+ CUDA_CALLABLE vec_t<Rows,Type> get_col(int index) const
189
+ {
190
+ vec_t<Rows,Type> ret;
191
+ for( unsigned i=0;i < Rows; ++i )
192
+ {
193
+ ret[i] = data[i][index];
194
+ }
195
+ return ret;
196
+ }
197
+
198
+ CUDA_CALLABLE void set_col(int index, const vec_t<Rows,Type>& v)
199
+ {
200
+ for( unsigned i=0;i < Rows; ++i )
201
+ {
202
+ data[i][index] = v[i];
203
+ }
204
+ }
205
+
206
+ // row major storage assumed to be compatible with PyTorch
207
+ Type data[Rows][Cols];
208
+ };
209
+
210
+ template<typename Type>
211
+ inline CUDA_CALLABLE mat_t<2, 2, Type> matrix_from_cols(vec_t<2, Type> c0, vec_t<2, Type> c1)
212
+ {
213
+ mat_t<2, 2, Type> m;
214
+
215
+ m.data[0][0] = c0[0];
216
+ m.data[1][0] = c0[1];
217
+
218
+ m.data[0][1] = c1[0];
219
+ m.data[1][1] = c1[1];
220
+
221
+ return m;
222
+ }
223
+
224
+ template<typename Type>
225
+ inline CUDA_CALLABLE mat_t<3, 3, Type> matrix_from_cols(vec_t<3, Type> c0, vec_t<3, Type> c1, vec_t<3, Type> c2)
226
+ {
227
+ mat_t<3, 3, Type> m;
228
+
229
+ m.data[0][0] = c0[0];
230
+ m.data[1][0] = c0[1];
231
+ m.data[2][0] = c0[2];
232
+
233
+ m.data[0][1] = c1[0];
234
+ m.data[1][1] = c1[1];
235
+ m.data[2][1] = c1[2];
236
+
237
+ m.data[0][2] = c2[0];
238
+ m.data[1][2] = c2[1];
239
+ m.data[2][2] = c2[2];
240
+
241
+ return m;
242
+ }
243
+
244
+ template<typename Type>
245
+ inline CUDA_CALLABLE mat_t<4, 4, Type> matrix_from_cols(vec_t<4, Type> c0, vec_t<4, Type> c1, vec_t<4, Type> c2, vec_t<4, Type> c3)
246
+ {
247
+ mat_t<4, 4, Type> m;
248
+
249
+ m.data[0][0] = c0[0];
250
+ m.data[1][0] = c0[1];
251
+ m.data[2][0] = c0[2];
252
+ m.data[3][0] = c0[3];
253
+
254
+ m.data[0][1] = c1[0];
255
+ m.data[1][1] = c1[1];
256
+ m.data[2][1] = c1[2];
257
+ m.data[3][1] = c1[3];
258
+
259
+ m.data[0][2] = c2[0];
260
+ m.data[1][2] = c2[1];
261
+ m.data[2][2] = c2[2];
262
+ m.data[3][2] = c2[3];
263
+
264
+ m.data[0][3] = c3[0];
265
+ m.data[1][3] = c3[1];
266
+ m.data[2][3] = c3[2];
267
+ m.data[3][3] = c3[3];
268
+
269
+ return m;
270
+ }
271
+
272
+ template<unsigned Rows, unsigned Cols, typename Type>
273
+ inline CUDA_CALLABLE mat_t<Rows, Cols, Type> matrix_from_cols(const initializer_array<Cols, vec_t<Rows, Type> >& l)
274
+ {
275
+ mat_t<Rows, Cols, Type> m;
276
+ for (unsigned j=0; j < Cols; ++j)
277
+ {
278
+ for (unsigned i=0; i < Rows; ++i)
279
+ {
280
+ m.data[i][j] = l[j][i];
281
+ }
282
+ }
283
+
284
+ return m;
285
+ }
286
+
287
+ template<typename Type>
288
+ inline CUDA_CALLABLE mat_t<2, 2, Type> matrix_from_rows(vec_t<2, Type> r0, vec_t<2, Type> r1)
289
+ {
290
+ mat_t<2, 2, Type> m;
291
+
292
+ m.data[0][0] = r0[0];
293
+ m.data[0][1] = r0[1];
294
+
295
+ m.data[1][0] = r1[0];
296
+ m.data[1][1] = r1[1];
297
+
298
+ return m;
299
+ }
300
+
301
+ template<typename Type>
302
+ inline CUDA_CALLABLE mat_t<3, 3, Type> matrix_from_rows(vec_t<3, Type> r0, vec_t<3, Type> r1, vec_t<3, Type> r2)
303
+ {
304
+ mat_t<3, 3, Type> m;
305
+
306
+ m.data[0][0] = r0[0];
307
+ m.data[0][1] = r0[1];
308
+ m.data[0][2] = r0[2];
309
+
310
+ m.data[1][0] = r1[0];
311
+ m.data[1][1] = r1[1];
312
+ m.data[1][2] = r1[2];
313
+
314
+ m.data[2][0] = r2[0];
315
+ m.data[2][1] = r2[1];
316
+ m.data[2][2] = r2[2];
317
+
318
+ return m;
319
+ }
320
+
321
+ template<typename Type>
322
+ inline CUDA_CALLABLE mat_t<4, 4, Type> matrix_from_rows(vec_t<4, Type> r0, vec_t<4, Type> r1, vec_t<4, Type> r2, vec_t<4, Type> r3)
323
+ {
324
+ mat_t<4, 4, Type> m;
325
+
326
+ m.data[0][0] = r0[0];
327
+ m.data[0][1] = r0[1];
328
+ m.data[0][2] = r0[2];
329
+ m.data[0][3] = r0[3];
330
+
331
+ m.data[1][0] = r1[0];
332
+ m.data[1][1] = r1[1];
333
+ m.data[1][2] = r1[2];
334
+ m.data[1][3] = r1[3];
335
+
336
+ m.data[2][0] = r2[0];
337
+ m.data[2][1] = r2[1];
338
+ m.data[2][2] = r2[2];
339
+ m.data[2][3] = r2[3];
340
+
341
+ m.data[3][0] = r3[0];
342
+ m.data[3][1] = r3[1];
343
+ m.data[3][2] = r3[2];
344
+ m.data[3][3] = r3[3];
345
+
346
+ return m;
347
+ }
348
+
349
+ template<unsigned Rows, unsigned Cols, typename Type>
350
+ inline CUDA_CALLABLE mat_t<Rows, Cols, Type> matrix_from_rows(const initializer_array<Rows, vec_t<Cols, Type> >& l)
351
+ {
352
+ mat_t<Rows, Cols, Type> m;
353
+ for (unsigned i=0; i < Rows; ++i)
354
+ {
355
+ for (unsigned j=0; j < Cols; ++j)
356
+ {
357
+ m.data[i][j] = l[i][j];
358
+ }
359
+ }
360
+
361
+ return m;
362
+ }
363
+
364
+ template<unsigned Rows, typename Type>
365
+ inline CUDA_CALLABLE mat_t<Rows, Rows, Type> identity()
366
+ {
367
+ mat_t<Rows, Rows, Type> m;
368
+ for( unsigned i=0; i < Rows; ++i )
369
+ {
370
+ m.data[i][i] = Type(1);
371
+ }
372
+ return m;
373
+ }
374
+
375
+ template<unsigned Rows, typename Type>
376
+ inline CUDA_CALLABLE void adj_identity(const mat_t<Rows, Rows, Type>& adj_ret)
377
+ {
378
+ // nop
379
+ }
380
+
381
+ template<unsigned Rows, unsigned Cols, typename Type>
382
+ inline CUDA_CALLABLE bool operator==(const mat_t<Rows,Cols,Type>& a, const mat_t<Rows,Cols,Type>& b)
383
+ {
384
+ for (unsigned i=0; i < Rows; ++i)
385
+ for (unsigned j=0; j < Cols; ++j)
386
+ if (a.data[i][j] != b.data[i][j])
387
+ return false;
388
+
389
+ return true;
390
+ }
391
+
392
+
393
+ // negation:
394
+ template<unsigned Rows, unsigned Cols, typename Type>
395
+ inline CUDA_CALLABLE mat_t<Rows,Cols,Type> operator - (mat_t<Rows,Cols,Type> a)
396
+ {
397
+ // NB: this constructor will initialize all ret's components to 0, which is
398
+ // unnecessary...
399
+ mat_t<Rows,Cols,Type> ret;
400
+ for (unsigned i=0; i < Rows; ++i)
401
+ for (unsigned j=0; j < Cols; ++j)
402
+ ret.data[i][j] = -a.data[i][j];
403
+
404
+ // Wonder if this does a load of copying when it returns... hopefully not as it's inlined?
405
+ return ret;
406
+ }
407
+
408
+
409
+ template<unsigned Rows, unsigned Cols, typename Type>
410
+ CUDA_CALLABLE inline mat_t<Rows,Cols,Type> pos(const mat_t<Rows,Cols,Type>& x)
411
+ {
412
+ return x;
413
+ }
414
+
415
+ template<unsigned Rows, unsigned Cols, typename Type>
416
+ CUDA_CALLABLE inline void adj_pos(const mat_t<Rows,Cols,Type>& x, mat_t<Rows,Cols,Type>& adj_x, const mat_t<Rows,Cols,Type>& adj_ret)
417
+ {
418
+ adj_x += adj_ret;
419
+ }
420
+
421
+ template<unsigned Rows, unsigned Cols, typename Type>
422
+ CUDA_CALLABLE inline mat_t<Rows,Cols,Type> neg(const mat_t<Rows,Cols,Type>& x)
423
+ {
424
+ return -x;
425
+ }
426
+
427
+ template<unsigned Rows, unsigned Cols, typename Type>
428
+ CUDA_CALLABLE inline void adj_neg(const mat_t<Rows,Cols,Type>& x, mat_t<Rows,Cols,Type>& adj_x, const mat_t<Rows,Cols,Type>& adj_ret)
429
+ {
430
+ adj_x -= adj_ret;
431
+ }
432
+
433
+
434
+ template<unsigned Rows, unsigned Cols, typename Type>
435
+ inline CUDA_CALLABLE mat_t<Rows,Cols,Type> atomic_add(mat_t<Rows,Cols,Type> * addr, mat_t<Rows,Cols,Type> value)
436
+ {
437
+ mat_t<Rows,Cols,Type> m;
438
+
439
+ for (unsigned i=0; i < Rows; ++i)
440
+ for (unsigned j=0; j < Cols; ++j)
441
+ m.data[i][j] = atomic_add(&addr->data[i][j], value.data[i][j]);
442
+
443
+ return m;
444
+ }
445
+
446
+ template<unsigned Rows, unsigned Cols, typename Type>
447
+ inline CUDA_CALLABLE mat_t<Rows,Cols,Type> atomic_min(mat_t<Rows,Cols,Type> * addr, mat_t<Rows,Cols,Type> value)
448
+ {
449
+ mat_t<Rows,Cols,Type> m;
450
+
451
+ for (unsigned i=0; i < Rows; ++i)
452
+ for (unsigned j=0; j < Cols; ++j)
453
+ m.data[i][j] = atomic_min(&addr->data[i][j], value.data[i][j]);
454
+
455
+ return m;
456
+ }
457
+
458
+ template<unsigned Rows, unsigned Cols, typename Type>
459
+ inline CUDA_CALLABLE mat_t<Rows,Cols,Type> atomic_max(mat_t<Rows,Cols,Type> * addr, mat_t<Rows,Cols,Type> value)
460
+ {
461
+ mat_t<Rows,Cols,Type> m;
462
+
463
+ for (unsigned i=0; i < Rows; ++i)
464
+ for (unsigned j=0; j < Cols; ++j)
465
+ m.data[i][j] = atomic_max(&addr->data[i][j], value.data[i][j]);
466
+
467
+ return m;
468
+ }
469
+
470
+ template<unsigned Rows, unsigned Cols, typename Type>
471
+ inline CUDA_CALLABLE void adj_atomic_minmax(
472
+ mat_t<Rows,Cols,Type> *addr,
473
+ mat_t<Rows,Cols,Type> *adj_addr,
474
+ const mat_t<Rows,Cols,Type> &value,
475
+ mat_t<Rows,Cols,Type> &adj_value)
476
+ {
477
+ for (unsigned i=0; i < Rows; ++i)
478
+ for (unsigned j=0; j < Cols; ++j)
479
+ adj_atomic_minmax(&addr->data[i][j], &adj_addr->data[i][j], value.data[i][j], adj_value.data[i][j]);
480
+ }
481
+
482
+ template<unsigned Rows, unsigned Cols, typename Type>
483
+ inline CUDA_CALLABLE vec_t<Cols,Type> extract(const mat_t<Rows,Cols,Type>& m, int row)
484
+ {
485
+ vec_t<Cols,Type> ret;
486
+ for(unsigned i=0; i < Cols; ++i)
487
+ {
488
+ ret.c[i] = m.data[row][i];
489
+ }
490
+ return ret;
491
+ }
492
+
493
+ template<unsigned Rows, unsigned Cols, typename Type>
494
+ inline CUDA_CALLABLE Type extract(const mat_t<Rows,Cols,Type>& m, int row, int col)
495
+ {
496
+ #ifndef NDEBUG
497
+ if (row < 0 || row >= Rows)
498
+ {
499
+ printf("mat row index %d out of bounds at %s %d\n", row, __FILE__, __LINE__);
500
+ assert(0);
501
+ }
502
+ if (col < 0 || col >= Cols)
503
+ {
504
+ printf("mat col index %d out of bounds at %s %d\n", col, __FILE__, __LINE__);
505
+ assert(0);
506
+ }
507
+ #endif
508
+ return m.data[row][col];
509
+ }
510
+
511
+ template<unsigned Rows, unsigned Cols, typename Type>
512
+ inline CUDA_CALLABLE vec_t<Cols, Type>* index(mat_t<Rows,Cols,Type>& m, int row)
513
+ {
514
+ #ifndef NDEBUG
515
+ if (row < 0 || row >= Rows)
516
+ {
517
+ printf("mat row index %d out of bounds at %s %d\n", row, __FILE__, __LINE__);
518
+ assert(0);
519
+ }
520
+ #endif
521
+
522
+ return reinterpret_cast<vec_t<Cols, Type>*>(&m.data[row]);
523
+ }
524
+
525
+ template<unsigned Rows, unsigned Cols, typename Type>
526
+ inline CUDA_CALLABLE Type* index(mat_t<Rows,Cols,Type>& m, int row, int col)
527
+ {
528
+ #ifndef NDEBUG
529
+ if (row < 0 || row >= Rows)
530
+ {
531
+ printf("mat row index %d out of bounds at %s %d\n", row, __FILE__, __LINE__);
532
+ assert(0);
533
+ }
534
+ if (col < 0 || col >= Cols)
535
+ {
536
+ printf("mat col index %d out of bounds at %s %d\n", col, __FILE__, __LINE__);
537
+ assert(0);
538
+ }
539
+ #endif
540
+
541
+ return &m.data[row][col];
542
+ }
543
+
544
+ template<unsigned Rows, unsigned Cols, typename Type>
545
+ inline CUDA_CALLABLE void adj_index(const mat_t<Rows,Cols,Type>& m, int row,
546
+ const mat_t<Rows,Cols,Type>& adj_m, int adj_row, const vec_t<Cols, Type>& adj_value)
547
+ {
548
+ // nop
549
+ }
550
+
551
+ template<unsigned Rows, unsigned Cols, typename Type>
552
+ inline CUDA_CALLABLE void adj_index(const mat_t<Rows,Cols,Type>& m, int row, int col,
553
+ const mat_t<Rows,Cols,Type>& adj_m, int adj_row, int adj_col, Type adj_value)
554
+ {
555
+ // nop
556
+ }
557
+
558
+
559
+ template<unsigned Rows, unsigned Cols, typename Type>
560
+ inline CUDA_CALLABLE void add_inplace(mat_t<Rows,Cols,Type>& m, int row, int col, Type value)
561
+ {
562
+ #ifndef NDEBUG
563
+ if (row < 0 || row >= Rows)
564
+ {
565
+ printf("mat row index %d out of bounds at %s %d\n", row, __FILE__, __LINE__);
566
+ assert(0);
567
+ }
568
+ if (col < 0 || col >= Cols)
569
+ {
570
+ printf("mat col index %d out of bounds at %s %d\n", col, __FILE__, __LINE__);
571
+ assert(0);
572
+ }
573
+ #endif
574
+
575
+ m.data[row][col] += value;
576
+ }
577
+
578
+
579
+ template<unsigned Rows, unsigned Cols, typename Type>
580
+ inline CUDA_CALLABLE void add_inplace(mat_t<Rows,Cols,Type>& m, int row, vec_t<Cols,Type>& value)
581
+ {
582
+ #ifndef NDEBUG
583
+ if (row < 0 || row >= Rows)
584
+ {
585
+ printf("mat row index %d out of bounds at %s %d\n", row, __FILE__, __LINE__);
586
+ assert(0);
587
+ }
588
+ #endif
589
+
590
+ for(unsigned i=0; i < Cols; ++i)
591
+ {
592
+ m.data[row][i] += value[i];
593
+ }
594
+ }
595
+
596
+
597
+ template<unsigned Rows, unsigned Cols, typename Type>
598
+ inline CUDA_CALLABLE void adj_add_inplace(mat_t<Rows,Cols,Type>& m, int row, int col, Type value,
599
+ mat_t<Rows,Cols,Type>& adj_m, int adj_row, int adj_col, Type& adj_value)
600
+ {
601
+ #ifndef NDEBUG
602
+ if (row < 0 || row >= Rows)
603
+ {
604
+ printf("mat row index %d out of bounds at %s %d\n", row, __FILE__, __LINE__);
605
+ assert(0);
606
+ }
607
+ if (col < 0 || col >= Cols)
608
+ {
609
+ printf("mat col index %d out of bounds at %s %d\n", col, __FILE__, __LINE__);
610
+ assert(0);
611
+ }
612
+ #endif
613
+
614
+ adj_value += adj_m.data[row][col];
615
+ }
616
+
617
+
618
+ template<unsigned Rows, unsigned Cols, typename Type>
619
+ inline CUDA_CALLABLE void adj_add_inplace(mat_t<Rows,Cols,Type>& m, int row, vec_t<Cols,Type>& value,
620
+ mat_t<Rows,Cols,Type>& adj_m, int adj_row, vec_t<Cols,Type>& adj_value)
621
+ {
622
+ #ifndef NDEBUG
623
+ if (row < 0 || row >= Rows)
624
+ {
625
+ printf("mat row index %d out of bounds at %s %d\n", row, __FILE__, __LINE__);
626
+ assert(0);
627
+ }
628
+ #endif
629
+
630
+ for(unsigned i=0; i < Cols; ++i)
631
+ {
632
+ adj_value[i] += adj_m.data[row][i];
633
+ }
634
+ }
635
+
636
+
637
+ template<unsigned Rows, unsigned Cols, typename Type>
638
+ inline CUDA_CALLABLE void sub_inplace(mat_t<Rows,Cols,Type>& m, int row, int col, Type value)
639
+ {
640
+ #ifndef NDEBUG
641
+ if (row < 0 || row >= Rows)
642
+ {
643
+ printf("mat row index %d out of bounds at %s %d\n", row, __FILE__, __LINE__);
644
+ assert(0);
645
+ }
646
+ if (col < 0 || col >= Cols)
647
+ {
648
+ printf("mat col index %d out of bounds at %s %d\n", col, __FILE__, __LINE__);
649
+ assert(0);
650
+ }
651
+ #endif
652
+
653
+ m.data[row][col] -= value;
654
+ }
655
+
656
+
657
+ template<unsigned Rows, unsigned Cols, typename Type>
658
+ inline CUDA_CALLABLE void sub_inplace(mat_t<Rows,Cols,Type>& m, int row, vec_t<Cols,Type>& value)
659
+ {
660
+ #ifndef NDEBUG
661
+ if (row < 0 || row >= Rows)
662
+ {
663
+ printf("mat row index %d out of bounds at %s %d\n", row, __FILE__, __LINE__);
664
+ assert(0);
665
+ }
666
+ #endif
667
+
668
+ for(unsigned i=0; i < Cols; ++i)
669
+ {
670
+ m.data[row][i] -= value[i];
671
+ }
672
+ }
673
+
674
+
675
+ template<unsigned Rows, unsigned Cols, typename Type>
676
+ inline CUDA_CALLABLE void adj_sub_inplace(mat_t<Rows,Cols,Type>& m, int row, int col, Type value,
677
+ mat_t<Rows,Cols,Type>& adj_m, int adj_row, int adj_col, Type& adj_value)
678
+ {
679
+ #ifndef NDEBUG
680
+ if (row < 0 || row >= Rows)
681
+ {
682
+ printf("mat row index %d out of bounds at %s %d\n", row, __FILE__, __LINE__);
683
+ assert(0);
684
+ }
685
+ if (col < 0 || col >= Cols)
686
+ {
687
+ printf("mat col index %d out of bounds at %s %d\n", col, __FILE__, __LINE__);
688
+ assert(0);
689
+ }
690
+ #endif
691
+
692
+ adj_value -= adj_m.data[row][col];
693
+ }
694
+
695
+
696
+ template<unsigned Rows, unsigned Cols, typename Type>
697
+ inline CUDA_CALLABLE void adj_sub_inplace(mat_t<Rows,Cols,Type>& m, int row, vec_t<Cols,Type>& value,
698
+ mat_t<Rows,Cols,Type>& adj_m, int adj_row, vec_t<Cols,Type>& adj_value)
699
+ {
700
+ #ifndef NDEBUG
701
+ if (row < 0 || row >= Rows)
702
+ {
703
+ printf("mat row index %d out of bounds at %s %d\n", row, __FILE__, __LINE__);
704
+ assert(0);
705
+ }
706
+ #endif
707
+
708
+ for(unsigned i=0; i < Cols; ++i)
709
+ {
710
+ adj_value[i] -= adj_m.data[row][i];
711
+ }
712
+ }
713
+
714
+
715
+ template<unsigned Rows, unsigned Cols, typename Type>
716
+ inline CUDA_CALLABLE void assign_inplace(mat_t<Rows,Cols,Type>& m, int row, int col, Type value)
717
+ {
718
+ #ifndef NDEBUG
719
+ if (row < 0 || row >= Rows)
720
+ {
721
+ printf("mat row index %d out of bounds at %s %d\n", row, __FILE__, __LINE__);
722
+ assert(0);
723
+ }
724
+ if (col < 0 || col >= Cols)
725
+ {
726
+ printf("mat col index %d out of bounds at %s %d\n", col, __FILE__, __LINE__);
727
+ assert(0);
728
+ }
729
+ #endif
730
+
731
+ m.data[row][col] = value;
732
+ }
733
+
734
+
735
+ template<unsigned Rows, unsigned Cols, typename Type>
736
+ inline CUDA_CALLABLE void assign_inplace(mat_t<Rows,Cols,Type>& m, int row, vec_t<Cols,Type>& value)
737
+ {
738
+ #ifndef NDEBUG
739
+ if (row < 0 || row >= Rows)
740
+ {
741
+ printf("mat row index %d out of bounds at %s %d\n", row, __FILE__, __LINE__);
742
+ assert(0);
743
+ }
744
+ #endif
745
+
746
+ for(unsigned i=0; i < Cols; ++i)
747
+ {
748
+ m.data[row][i] = value[i];
749
+ }
750
+ }
751
+
752
+
753
+ template<unsigned Rows, unsigned Cols, typename Type>
754
+ inline CUDA_CALLABLE void adj_assign_inplace(mat_t<Rows,Cols,Type>& m, int row, int col, Type value,
755
+ mat_t<Rows,Cols,Type>& adj_m, int& adj_row, int& adj_col, Type& adj_value)
756
+ {
757
+ #ifndef NDEBUG
758
+ if (row < 0 || row >= Rows)
759
+ {
760
+ printf("mat row index %d out of bounds at %s %d\n", row, __FILE__, __LINE__);
761
+ assert(0);
762
+ }
763
+ if (col < 0 || col >= Cols)
764
+ {
765
+ printf("mat col index %d out of bounds at %s %d\n", col, __FILE__, __LINE__);
766
+ assert(0);
767
+ }
768
+ #endif
769
+
770
+ adj_value += adj_m.data[row][col];
771
+ }
772
+
773
+
774
+ template<unsigned Rows, unsigned Cols, typename Type>
775
+ inline CUDA_CALLABLE void adj_assign_inplace(mat_t<Rows,Cols,Type>& m, int row, vec_t<Cols,Type>& value,
776
+ mat_t<Rows,Cols,Type>& adj_m, int& adj_row, vec_t<Cols,Type>& adj_value)
777
+ {
778
+ #ifndef NDEBUG
779
+ if (row < 0 || row >= Rows)
780
+ {
781
+ printf("mat row index %d out of bounds at %s %d\n", row, __FILE__, __LINE__);
782
+ assert(0);
783
+ }
784
+ #endif
785
+
786
+ for(unsigned i=0; i < Cols; ++i)
787
+ {
788
+ adj_value[i] += adj_m.data[row][i];
789
+ }
790
+ }
791
+
792
+
793
+ template<unsigned Rows, unsigned Cols, typename Type>
794
+ inline CUDA_CALLABLE mat_t<Rows,Cols,Type> assign_copy(mat_t<Rows,Cols,Type>& m, int row, int col, Type value)
795
+ {
796
+ #ifndef NDEBUG
797
+ if (row < 0 || row >= Rows)
798
+ {
799
+ printf("mat row index %d out of bounds at %s %d\n", row, __FILE__, __LINE__);
800
+ assert(0);
801
+ }
802
+ if (col < 0 || col >= Cols)
803
+ {
804
+ printf("mat col index %d out of bounds at %s %d\n", col, __FILE__, __LINE__);
805
+ assert(0);
806
+ }
807
+ #endif
808
+
809
+ mat_t<Rows,Cols,Type> ret(m);
810
+ ret.data[row][col] = value;
811
+ return ret;
812
+ }
813
+
814
+
815
+ template<unsigned Rows, unsigned Cols, typename Type>
816
+ inline CUDA_CALLABLE mat_t<Rows,Cols,Type> assign_copy(mat_t<Rows,Cols,Type>& m, int row, vec_t<Cols,Type>& value)
817
+ {
818
+ #ifndef NDEBUG
819
+ if (row < 0 || row >= Rows)
820
+ {
821
+ printf("mat row index %d out of bounds at %s %d\n", row, __FILE__, __LINE__);
822
+ assert(0);
823
+ }
824
+ #endif
825
+
826
+ mat_t<Rows,Cols,Type> ret(m);
827
+ for(unsigned i=0; i < Cols; ++i)
828
+ {
829
+ ret.data[row][i] = value[i];
830
+ }
831
+ return ret;
832
+ }
833
+
834
+
835
+ template<unsigned Rows, unsigned Cols, typename Type>
836
+ inline CUDA_CALLABLE void adj_assign_copy(mat_t<Rows,Cols,Type>& m, int row, int col, Type value,
837
+ mat_t<Rows,Cols,Type>& adj_m, int& adj_row, int& adj_col, Type& adj_value, const mat_t<Rows,Cols,Type>& adj_ret)
838
+ {
839
+ #ifndef NDEBUG
840
+ if (row < 0 || row >= Rows)
841
+ {
842
+ printf("mat row index %d out of bounds at %s %d\n", row, __FILE__, __LINE__);
843
+ assert(0);
844
+ }
845
+ if (col < 0 || col >= Cols)
846
+ {
847
+ printf("mat col index %d out of bounds at %s %d\n", col, __FILE__, __LINE__);
848
+ assert(0);
849
+ }
850
+ #endif
851
+
852
+ adj_value += adj_ret.data[row][col];
853
+ for(unsigned i=0; i < Rows; ++i)
854
+ {
855
+ for(unsigned j=0; j < Cols; ++j)
856
+ {
857
+ if(i != row || j != col)
858
+ adj_m.data[i][j] += adj_ret.data[i][j];
859
+ }
860
+ }
861
+ }
862
+
863
+
864
+ template<unsigned Rows, unsigned Cols, typename Type>
865
+ inline CUDA_CALLABLE void adj_assign_copy(mat_t<Rows,Cols,Type>& m, int row, vec_t<Cols,Type>& value,
866
+ mat_t<Rows,Cols,Type>& adj_m, int& adj_row, vec_t<Cols,Type>& adj_value, const mat_t<Rows,Cols,Type>& adj_ret)
867
+ {
868
+ #ifndef NDEBUG
869
+ if (row < 0 || row >= Rows)
870
+ {
871
+ printf("mat row index %d out of bounds at %s %d\n", row, __FILE__, __LINE__);
872
+ assert(0);
873
+ }
874
+ #endif
875
+
876
+ for(unsigned i=0; i < Rows; ++i)
877
+ {
878
+ for(unsigned j=0; j < Cols; ++j)
879
+ {
880
+ if (i==row)
881
+ adj_value[j] += adj_ret.data[i][j];
882
+ else
883
+ adj_m.data[i][j] += adj_ret.data[i][j];
884
+ }
885
+ }
886
+ }
887
+
888
+
889
+ template<unsigned Rows, unsigned Cols, typename Type>
890
+ inline bool CUDA_CALLABLE isfinite(const mat_t<Rows,Cols,Type>& m)
891
+ {
892
+ for (unsigned i=0; i < Rows; ++i)
893
+ for (unsigned j=0; j < Cols; ++j)
894
+ if (!isfinite(m.data[i][j]))
895
+ return false;
896
+ return true;
897
+ }
898
+
899
+ template<unsigned Rows, unsigned Cols, typename Type>
900
+ inline void CUDA_CALLABLE adj_isfinite(const mat_t<Rows,Cols,Type>& m, mat_t<Rows,Cols,Type>& adj_m, const bool &adj_ret)
901
+ {
902
+ }
903
+
904
+ template<unsigned Rows, unsigned Cols, typename Type>
905
+ inline bool CUDA_CALLABLE isnan(const mat_t<Rows,Cols,Type>& m)
906
+ {
907
+ for (unsigned i=0; i < Rows; ++i)
908
+ for (unsigned j=0; j < Cols; ++j)
909
+ if (isnan(m.data[i][j]))
910
+ return true;
911
+ return false;
912
+ }
913
+
914
+ template<unsigned Rows, unsigned Cols, typename Type>
915
+ inline void CUDA_CALLABLE adj_isnan(const mat_t<Rows,Cols,Type>& m, mat_t<Rows,Cols,Type>& adj_m, const bool &adj_ret)
916
+ {
917
+ }
918
+
919
+ template<unsigned Rows, unsigned Cols, typename Type>
920
+ inline bool CUDA_CALLABLE isinf(const mat_t<Rows,Cols,Type>& m)
921
+ {
922
+ for (unsigned i=0; i < Rows; ++i)
923
+ for (unsigned j=0; j < Cols; ++j)
924
+ if (isinf(m.data[i][j]))
925
+ return true;
926
+ return false;
927
+ }
928
+
929
+ template<unsigned Rows, unsigned Cols, typename Type>
930
+ inline void CUDA_CALLABLE adj_isinf(const mat_t<Rows,Cols,Type>& m, mat_t<Rows,Cols,Type>& adj_m, const bool &adj_ret)
931
+ {
932
+ }
933
+
934
+ template<unsigned Rows, unsigned Cols, typename Type>
935
+ inline CUDA_CALLABLE mat_t<Rows,Cols,Type> add(const mat_t<Rows,Cols,Type>& a, const mat_t<Rows,Cols,Type>& b)
936
+ {
937
+ mat_t<Rows,Cols,Type> t;
938
+ for (unsigned i=0; i < Rows; ++i)
939
+ {
940
+ for (unsigned j=0; j < Cols; ++j)
941
+ {
942
+ t.data[i][j] = a.data[i][j] + b.data[i][j];
943
+ }
944
+ }
945
+
946
+ return t;
947
+ }
948
+
949
+ template<unsigned Rows, unsigned Cols, typename Type>
950
+ inline CUDA_CALLABLE mat_t<Rows,Cols,Type> sub(const mat_t<Rows,Cols,Type>& a, const mat_t<Rows,Cols,Type>& b)
951
+ {
952
+ mat_t<Rows,Cols,Type> t;
953
+ for (unsigned i=0; i < Rows; ++i)
954
+ {
955
+ for (unsigned j=0; j < Cols; ++j)
956
+ {
957
+ t.data[i][j] = a.data[i][j] - b.data[i][j];
958
+ }
959
+ }
960
+
961
+ return t;
962
+ }
963
+
964
+ template<unsigned Rows, unsigned Cols, typename Type>
965
+ inline CUDA_CALLABLE mat_t<Rows,Cols,Type> div(const mat_t<Rows,Cols,Type>& a, Type b)
966
+ {
967
+ mat_t<Rows,Cols,Type> t;
968
+ for (unsigned i=0; i < Rows; ++i)
969
+ {
970
+ for (unsigned j=0; j < Cols; ++j)
971
+ {
972
+ t.data[i][j] = a.data[i][j]/b;
973
+ }
974
+ }
975
+
976
+ return t;
977
+ }
978
+
979
+ template<unsigned Rows, unsigned Cols, typename Type>
980
+ inline CUDA_CALLABLE mat_t<Rows,Cols,Type> div(Type b, const mat_t<Rows,Cols,Type>& a)
981
+ {
982
+ mat_t<Rows,Cols,Type> t;
983
+ for (unsigned i=0; i < Rows; ++i)
984
+ {
985
+ for (unsigned j=0; j < Cols; ++j)
986
+ {
987
+ t.data[i][j] = b / a.data[i][j];
988
+ }
989
+ }
990
+
991
+ return t;
992
+ }
993
+
994
+ template<unsigned Rows, unsigned Cols, typename Type>
995
+ inline CUDA_CALLABLE mat_t<Rows,Cols,Type> mul(const mat_t<Rows,Cols,Type>& a, Type b)
996
+ {
997
+ mat_t<Rows,Cols,Type> t;
998
+ for (unsigned i=0; i < Rows; ++i)
999
+ {
1000
+ for (unsigned j=0; j < Cols; ++j)
1001
+ {
1002
+ t.data[i][j] = a.data[i][j]*b;
1003
+ }
1004
+ }
1005
+
1006
+ return t;
1007
+ }
1008
+
1009
+ template<unsigned Rows, unsigned Cols, typename Type>
1010
+ inline CUDA_CALLABLE mat_t<Rows,Cols,Type> mul(Type b, const mat_t<Rows,Cols,Type>& a)
1011
+ {
1012
+ return mul(a,b);
1013
+ }
1014
+
1015
+
1016
+ template<unsigned Rows, unsigned Cols, typename Type>
1017
+ inline CUDA_CALLABLE mat_t<Rows,Cols,Type> operator*(Type b, const mat_t<Rows,Cols,Type>& a)
1018
+ {
1019
+ return mul(a,b);
1020
+ }
1021
+
1022
+ template<unsigned Rows, unsigned Cols, typename Type>
1023
+ inline CUDA_CALLABLE mat_t<Rows,Cols,Type> operator*( const mat_t<Rows,Cols,Type>& a, Type b)
1024
+ {
1025
+ return mul(a,b);
1026
+ }
1027
+
1028
+ template<unsigned Rows, unsigned Cols, typename Type>
1029
+ inline CUDA_CALLABLE vec_t<Rows,Type> mul(const mat_t<Rows,Cols,Type>& a, const vec_t<Cols,Type>& b)
1030
+ {
1031
+ vec_t<Rows,Type> r = a.get_col(0)*b[0];
1032
+ for( unsigned i=1; i < Cols; ++i )
1033
+ {
1034
+ r += a.get_col(i)*b[i];
1035
+ }
1036
+ return r;
1037
+ }
1038
+
1039
+ template<unsigned Rows, unsigned Cols, typename Type>
1040
+ inline CUDA_CALLABLE vec_t<Cols,Type> mul(const vec_t<Rows,Type>& b, const mat_t<Rows,Cols,Type>& a)
1041
+ {
1042
+ vec_t<Cols,Type> r = a.get_row(0)*b[0];
1043
+ for( unsigned i=1; i < Rows; ++i )
1044
+ {
1045
+ r += a.get_row(i)*b[i];
1046
+ }
1047
+ return r;
1048
+ }
1049
+
1050
+ template<typename T>
1051
+ inline CUDA_CALLABLE T muladd(T a, T b, T c) {
1052
+ return c + a*b;
1053
+ }
1054
+ template<>
1055
+ inline CUDA_CALLABLE float muladd(float a, float b, float c) {
1056
+ return fmaf(a, b, c);
1057
+ }
1058
+ template<>
1059
+ inline CUDA_CALLABLE double muladd(double a, double b, double c) {
1060
+ return fma(a, b, c);
1061
+ }
1062
+
1063
+
1064
+ template<unsigned Rows, unsigned Cols, unsigned ColsOut, typename Type>
1065
+ inline CUDA_CALLABLE mat_t<Rows,ColsOut,Type> mul(const mat_t<Rows,Cols,Type>& a, const mat_t<Cols,ColsOut,Type>& b)
1066
+ {
1067
+ mat_t<Rows,ColsOut,Type> t(0);
1068
+ for (unsigned i=0; i < Rows; ++i)
1069
+ {
1070
+ for (unsigned j=0; j < ColsOut; ++j)
1071
+ {
1072
+ Type sum(0.0);
1073
+
1074
+ for (unsigned k=0; k < Cols; ++k)
1075
+ {
1076
+ sum = muladd<Type>(a.data[i][k], b.data[k][j], sum);
1077
+ }
1078
+
1079
+ t.data[i][j] = sum;
1080
+ }
1081
+ }
1082
+
1083
+ return t;
1084
+ }
1085
+
1086
+ template<unsigned Rows, unsigned Cols, typename Type>
1087
+ inline CUDA_CALLABLE Type ddot(const mat_t<Rows,Cols,Type>& a, const mat_t<Rows,Cols,Type>& b)
1088
+ {
1089
+ // double dot product between a and b:
1090
+ Type r(0);
1091
+ for (unsigned i=0; i < Rows; ++i)
1092
+ {
1093
+ for (unsigned j=0; j < Cols; ++j)
1094
+ {
1095
+ r = muladd<Type>(a.data[i][j], b.data[i][j], r);
1096
+ }
1097
+ }
1098
+ return r;
1099
+ }
1100
+
1101
+ template<unsigned Rows, unsigned Cols, typename Type>
1102
+ inline CUDA_CALLABLE Type tensordot(const mat_t<Rows,Cols,Type>& a, const mat_t<Rows,Cols,Type>& b)
1103
+ {
1104
+ // corresponds to `np.tensordot()` with all axes being contracted
1105
+ return ddot(a, b);
1106
+ }
1107
+
1108
+ template<unsigned Rows, unsigned Cols, typename Type>
1109
+ inline CUDA_CALLABLE mat_t<Cols,Rows,Type> transpose(const mat_t<Rows,Cols,Type>& a)
1110
+ {
1111
+ mat_t<Cols,Rows,Type> t;
1112
+ for (unsigned i=0; i < Cols; ++i)
1113
+ {
1114
+ for (unsigned j=0; j < Rows; ++j)
1115
+ {
1116
+ t.data[i][j] = a.data[j][i];
1117
+ }
1118
+ }
1119
+
1120
+ return t;
1121
+ }
1122
+
1123
+ // Only implementing determinants for 2x2, 3x3 and 4x4 matrices for now...
1124
+ template<typename Type>
1125
+ inline CUDA_CALLABLE Type determinant(const mat_t<2,2,Type>& m)
1126
+ {
1127
+ return m.data[0][0]*m.data[1][1] - m.data[1][0]*m.data[0][1];
1128
+ }
1129
+
1130
+ template<typename Type>
1131
+ inline CUDA_CALLABLE Type determinant(const mat_t<3,3,Type>& m)
1132
+ {
1133
+ return dot(
1134
+ vec_t<3,Type>(m.data[0][0],m.data[0][1],m.data[0][2]),
1135
+ cross(
1136
+ vec_t<3,Type>(m.data[1][0],m.data[1][1],m.data[1][2]),
1137
+ vec_t<3,Type>(m.data[2][0],m.data[2][1],m.data[2][2])
1138
+ )
1139
+ );
1140
+ }
1141
+
1142
+ template<typename Type>
1143
+ inline CUDA_CALLABLE Type determinant(const mat_t<4,4,Type>& m)
1144
+ {
1145
+ // adapted from USD GfMatrix4f::Inverse()
1146
+ Type x00, x01, x02, x03;
1147
+ Type x10, x11, x12, x13;
1148
+ Type x20, x21, x22, x23;
1149
+ Type x30, x31, x32, x33;
1150
+ double y01, y02, y03, y12, y13, y23;
1151
+ Type z00, z10, z20, z30;
1152
+
1153
+ // Pickle 1st two columns of matrix into registers
1154
+ x00 = m.data[0][0];
1155
+ x01 = m.data[0][1];
1156
+ x10 = m.data[1][0];
1157
+ x11 = m.data[1][1];
1158
+ x20 = m.data[2][0];
1159
+ x21 = m.data[2][1];
1160
+ x30 = m.data[3][0];
1161
+ x31 = m.data[3][1];
1162
+
1163
+ // Compute all six 2x2 determinants of 1st two columns
1164
+ y01 = x00*x11 - x10*x01;
1165
+ y02 = x00*x21 - x20*x01;
1166
+ y03 = x00*x31 - x30*x01;
1167
+ y12 = x10*x21 - x20*x11;
1168
+ y13 = x10*x31 - x30*x11;
1169
+ y23 = x20*x31 - x30*x21;
1170
+
1171
+ // Pickle 2nd two columns of matrix into registers
1172
+ x02 = m.data[0][2];
1173
+ x03 = m.data[0][3];
1174
+ x12 = m.data[1][2];
1175
+ x13 = m.data[1][3];
1176
+ x22 = m.data[2][2];
1177
+ x23 = m.data[2][3];
1178
+ x32 = m.data[3][2];
1179
+ x33 = m.data[3][3];
1180
+
1181
+ // Compute all six 2x2 determinants of 2nd two columns
1182
+ y01 = x02*x13 - x12*x03;
1183
+ y02 = x02*x23 - x22*x03;
1184
+ y03 = x02*x33 - x32*x03;
1185
+ y12 = x12*x23 - x22*x13;
1186
+ y13 = x12*x33 - x32*x13;
1187
+ y23 = x22*x33 - x32*x23;
1188
+
1189
+ // Compute all 3x3 cofactors for 1st two columns
1190
+ z30 = x11*y02 - x21*y01 - x01*y12;
1191
+ z20 = x01*y13 - x11*y03 + x31*y01;
1192
+ z10 = x21*y03 - x31*y02 - x01*y23;
1193
+ z00 = x11*y23 - x21*y13 + x31*y12;
1194
+
1195
+ // compute 4x4 determinant & its reciprocal
1196
+ double det = x30*z30 + x20*z20 + x10*z10 + x00*z00;
1197
+ return det;
1198
+ }
1199
+
1200
+ template<unsigned Rows, typename Type>
1201
+ inline CUDA_CALLABLE Type trace(const mat_t<Rows,Rows,Type>& m)
1202
+ {
1203
+ Type ret = m.data[0][0];
1204
+ for( unsigned i=1; i < Rows; ++i )
1205
+ {
1206
+ ret += m.data[i][i];
1207
+ }
1208
+ return ret;
1209
+ }
1210
+
1211
+ template<unsigned Rows, typename Type>
1212
+ inline CUDA_CALLABLE vec_t<Rows, Type> get_diag(const mat_t<Rows,Rows,Type>& m)
1213
+ {
1214
+ vec_t<Rows, Type> ret;
1215
+ for( unsigned i=0; i < Rows; ++i )
1216
+ {
1217
+ ret[i] = m.data[i][i];
1218
+ }
1219
+ return ret;
1220
+ }
1221
+
1222
+ // Only implementing inverses for 2x2, 3x3 and 4x4 matrices for now...
1223
+ template<typename Type>
1224
+ inline CUDA_CALLABLE mat_t<2,2,Type> inverse(const mat_t<2,2,Type>& m)
1225
+ {
1226
+ Type det = determinant(m);
1227
+ if (det > Type(kEps) || det < -Type(kEps))
1228
+ {
1229
+ return mat_t<2,2,Type>( m.data[1][1], -m.data[0][1],
1230
+ -m.data[1][0], m.data[0][0])*(Type(1.0f)/det);
1231
+ }
1232
+ else
1233
+ {
1234
+ return mat_t<2,2,Type>();
1235
+ }
1236
+ }
1237
+
1238
+ template<typename Type>
1239
+ inline CUDA_CALLABLE mat_t<3,3,Type> inverse(const mat_t<3,3,Type>& m)
1240
+ {
1241
+ Type det = determinant(m);
1242
+
1243
+ if (det != Type(0.0f))
1244
+ {
1245
+ mat_t<3,3,Type> b;
1246
+
1247
+ b.data[0][0] = m.data[1][1]*m.data[2][2] - m.data[1][2]*m.data[2][1];
1248
+ b.data[1][0] = m.data[1][2]*m.data[2][0] - m.data[1][0]*m.data[2][2];
1249
+ b.data[2][0] = m.data[1][0]*m.data[2][1] - m.data[1][1]*m.data[2][0];
1250
+
1251
+ b.data[0][1] = m.data[0][2]*m.data[2][1] - m.data[0][1]*m.data[2][2];
1252
+ b.data[1][1] = m.data[0][0]*m.data[2][2] - m.data[0][2]*m.data[2][0];
1253
+ b.data[2][1] = m.data[0][1]*m.data[2][0] - m.data[0][0]*m.data[2][1];
1254
+
1255
+ b.data[0][2] = m.data[0][1]*m.data[1][2] - m.data[0][2]*m.data[1][1];
1256
+ b.data[1][2] = m.data[0][2]*m.data[1][0] - m.data[0][0]*m.data[1][2];
1257
+ b.data[2][2] = m.data[0][0]*m.data[1][1] - m.data[0][1]*m.data[1][0];
1258
+
1259
+ return b*(Type(1.0f)/det);
1260
+ }
1261
+ else
1262
+ {
1263
+ return mat_t<3,3,Type>();
1264
+ }
1265
+ }
1266
+
1267
+ template<typename Type>
1268
+ inline CUDA_CALLABLE mat_t<4,4,Type> inverse(const mat_t<4,4,Type>& m)
1269
+ {
1270
+ // adapted from USD GfMatrix4f::Inverse()
1271
+ Type x00, x01, x02, x03;
1272
+ Type x10, x11, x12, x13;
1273
+ Type x20, x21, x22, x23;
1274
+ Type x30, x31, x32, x33;
1275
+ double y01, y02, y03, y12, y13, y23;
1276
+ Type z00, z10, z20, z30;
1277
+ Type z01, z11, z21, z31;
1278
+ double z02, z03, z12, z13, z22, z23, z32, z33;
1279
+
1280
+ // Pickle 1st two columns of matrix into registers
1281
+ x00 = m.data[0][0];
1282
+ x01 = m.data[0][1];
1283
+ x10 = m.data[1][0];
1284
+ x11 = m.data[1][1];
1285
+ x20 = m.data[2][0];
1286
+ x21 = m.data[2][1];
1287
+ x30 = m.data[3][0];
1288
+ x31 = m.data[3][1];
1289
+
1290
+ // Compute all six 2x2 determinants of 1st two columns
1291
+ y01 = x00*x11 - x10*x01;
1292
+ y02 = x00*x21 - x20*x01;
1293
+ y03 = x00*x31 - x30*x01;
1294
+ y12 = x10*x21 - x20*x11;
1295
+ y13 = x10*x31 - x30*x11;
1296
+ y23 = x20*x31 - x30*x21;
1297
+
1298
+ // Pickle 2nd two columns of matrix into registers
1299
+ x02 = m.data[0][2];
1300
+ x03 = m.data[0][3];
1301
+ x12 = m.data[1][2];
1302
+ x13 = m.data[1][3];
1303
+ x22 = m.data[2][2];
1304
+ x23 = m.data[2][3];
1305
+ x32 = m.data[3][2];
1306
+ x33 = m.data[3][3];
1307
+
1308
+ // Compute all 3x3 cofactors for 2nd two columns */
1309
+ z33 = x02*y12 - x12*y02 + x22*y01;
1310
+ z23 = x12*y03 - x32*y01 - x02*y13;
1311
+ z13 = x02*y23 - x22*y03 + x32*y02;
1312
+ z03 = x22*y13 - x32*y12 - x12*y23;
1313
+ z32 = x13*y02 - x23*y01 - x03*y12;
1314
+ z22 = x03*y13 - x13*y03 + x33*y01;
1315
+ z12 = x23*y03 - x33*y02 - x03*y23;
1316
+ z02 = x13*y23 - x23*y13 + x33*y12;
1317
+
1318
+ // Compute all six 2x2 determinants of 2nd two columns
1319
+ y01 = x02*x13 - x12*x03;
1320
+ y02 = x02*x23 - x22*x03;
1321
+ y03 = x02*x33 - x32*x03;
1322
+ y12 = x12*x23 - x22*x13;
1323
+ y13 = x12*x33 - x32*x13;
1324
+ y23 = x22*x33 - x32*x23;
1325
+
1326
+ // Compute all 3x3 cofactors for 1st two columns
1327
+ z30 = x11*y02 - x21*y01 - x01*y12;
1328
+ z20 = x01*y13 - x11*y03 + x31*y01;
1329
+ z10 = x21*y03 - x31*y02 - x01*y23;
1330
+ z00 = x11*y23 - x21*y13 + x31*y12;
1331
+ z31 = x00*y12 - x10*y02 + x20*y01;
1332
+ z21 = x10*y03 - x30*y01 - x00*y13;
1333
+ z11 = x00*y23 - x20*y03 + x30*y02;
1334
+ z01 = x20*y13 - x30*y12 - x10*y23;
1335
+
1336
+ // compute 4x4 determinant & its reciprocal
1337
+ double det = x30*z30 + x20*z20 + x10*z10 + x00*z00;
1338
+
1339
+ if(fabs(det) > kEps)
1340
+ {
1341
+ mat_t<4,4,Type> invm;
1342
+
1343
+ double rcp = 1.0 / det;
1344
+
1345
+ // Multiply all 3x3 cofactors by reciprocal & transpose
1346
+ invm.data[0][0] = Type(z00*rcp);
1347
+ invm.data[0][1] = Type(z10*rcp);
1348
+ invm.data[1][0] = Type(z01*rcp);
1349
+ invm.data[0][2] = Type(z20*rcp);
1350
+ invm.data[2][0] = Type(z02*rcp);
1351
+ invm.data[0][3] = Type(z30*rcp);
1352
+ invm.data[3][0] = Type(z03*rcp);
1353
+ invm.data[1][1] = Type(z11*rcp);
1354
+ invm.data[1][2] = Type(z21*rcp);
1355
+ invm.data[2][1] = Type(z12*rcp);
1356
+ invm.data[1][3] = Type(z31*rcp);
1357
+ invm.data[3][1] = Type(z13*rcp);
1358
+ invm.data[2][2] = Type(z22*rcp);
1359
+ invm.data[2][3] = Type(z32*rcp);
1360
+ invm.data[3][2] = Type(z23*rcp);
1361
+ invm.data[3][3] = Type(z33*rcp);
1362
+
1363
+ return invm;
1364
+ }
1365
+ else
1366
+ {
1367
+ return mat_t<4,4,Type>();
1368
+ }
1369
+ }
1370
+
1371
+ template<unsigned Rows,typename Type>
1372
+ inline CUDA_CALLABLE mat_t<Rows,Rows,Type> diag(const vec_t<Rows,Type>& d)
1373
+ {
1374
+ mat_t<Rows,Rows,Type> ret(Type(0));
1375
+ for (unsigned i=0; i < Rows; ++i)
1376
+ {
1377
+ ret.data[i][i] = d[i];
1378
+ }
1379
+ return ret;
1380
+ }
1381
+
1382
+ template<unsigned Rows,unsigned Cols,typename Type>
1383
+ inline CUDA_CALLABLE mat_t<Rows,Cols,Type> outer(const vec_t<Rows,Type>& a, const vec_t<Cols,Type>& b)
1384
+ {
1385
+ // col 0 = a * b[0] etc...
1386
+ mat_t<Rows,Cols,Type> ret;
1387
+ for (unsigned row=0; row < Rows; ++row)
1388
+ {
1389
+ for (unsigned col=0; col < Cols; ++col) // columns
1390
+ {
1391
+ ret.data[row][col] = a[row] * b[col];
1392
+ }
1393
+ }
1394
+ return ret;
1395
+ }
1396
+
1397
+ template<unsigned Cols,typename Type>
1398
+ inline CUDA_CALLABLE vec_t<Cols,Type> outer(Type a, const vec_t<Cols,Type>& b)
1399
+ {
1400
+ return mul(a, b);
1401
+ }
1402
+
1403
+ template<unsigned Rows,typename Type>
1404
+ inline CUDA_CALLABLE vec_t<Rows,Type> outer(const vec_t<Rows,Type>& a, Type b)
1405
+ {
1406
+ return mul(a, b);
1407
+ }
1408
+
1409
+ template<typename Type>
1410
+ inline CUDA_CALLABLE mat_t<3,3,Type> skew(const vec_t<3,Type>& a)
1411
+ {
1412
+ mat_t<3,3,Type> out(
1413
+ Type(0), -a[2], a[1],
1414
+ a[2], Type(0), -a[0],
1415
+ -a[1], a[0], Type(0)
1416
+ );
1417
+
1418
+ return out;
1419
+ }
1420
+
1421
+
1422
+ template<unsigned Rows, unsigned Cols, typename Type>
1423
+ inline CUDA_CALLABLE mat_t<Rows,Cols,Type> cw_mul(const mat_t<Rows,Cols,Type>& a, const mat_t<Rows,Cols,Type>& b)
1424
+ {
1425
+ mat_t<Rows,Cols,Type> t;
1426
+ for (unsigned i=0; i < Rows; ++i)
1427
+ {
1428
+ for (unsigned j=0; j < Cols; ++j)
1429
+ {
1430
+ t.data[i][j] = a.data[i][j] * b.data[i][j];
1431
+ }
1432
+ }
1433
+
1434
+ return t;
1435
+ }
1436
+
1437
+
1438
+ template<unsigned Rows, unsigned Cols, typename Type>
1439
+ inline CUDA_CALLABLE mat_t<Rows,Cols,Type> cw_div(const mat_t<Rows,Cols,Type>& a, const mat_t<Rows,Cols,Type>& b)
1440
+ {
1441
+ mat_t<Rows,Cols,Type> t;
1442
+ for (unsigned i=0; i < Rows; ++i)
1443
+ {
1444
+ for (unsigned j=0; j < Cols; ++j)
1445
+ {
1446
+ t.data[i][j] = a.data[i][j] / b.data[i][j];
1447
+ }
1448
+ }
1449
+
1450
+ return t;
1451
+ }
1452
+
1453
+ template<typename Type>
1454
+ inline CUDA_CALLABLE vec_t<3,Type> transform_point(const mat_t<4,4,Type>& m, const vec_t<3,Type>& v)
1455
+ {
1456
+ vec_t<4,Type> out = mul(m, vec_t<4,Type>(v[0], v[1], v[2], Type(1)));
1457
+ return vec_t<3,Type>(out[0], out[1], out[2]);
1458
+ }
1459
+
1460
+ template<typename Type>
1461
+ inline CUDA_CALLABLE vec_t<3,Type> transform_vector(const mat_t<4,4,Type>& m, const vec_t<3,Type>& v)
1462
+ {
1463
+ vec_t<4,Type> out = mul(m, vec_t<4,Type>(v[0], v[1], v[2], 0.f));
1464
+ return vec_t<3,Type>(out[0], out[1], out[2]);
1465
+ }
1466
+
1467
+ template<unsigned Rows, unsigned Cols, typename Type>
1468
+ inline CUDA_CALLABLE void adj_extract(const mat_t<Rows,Cols,Type>& m, int row, mat_t<Rows,Cols,Type>& adj_m, int& adj_row, const vec_t<Cols,Type>& adj_ret)
1469
+ {
1470
+ for( unsigned col=0; col < Cols; ++col )
1471
+ adj_m.data[row][col] += adj_ret[col];
1472
+ }
1473
+
1474
+ template<unsigned Rows, unsigned Cols, typename Type>
1475
+ inline void CUDA_CALLABLE adj_extract(const mat_t<Rows,Cols,Type>& m, int row, int col, mat_t<Rows,Cols,Type>& adj_m, int& adj_row, int& adj_col, Type adj_ret)
1476
+ {
1477
+ #ifndef NDEBUG
1478
+ if (row < 0 || row > Rows)
1479
+ {
1480
+ printf("mat row index %d out of bounds at %s %d\n", row, __FILE__, __LINE__);
1481
+ assert(0);
1482
+ }
1483
+ if (col < 0 || col > Cols)
1484
+ {
1485
+ printf("mat col index %d out of bounds at %s %d\n", col, __FILE__, __LINE__);
1486
+ assert(0);
1487
+ }
1488
+ #endif
1489
+ adj_m.data[row][col] += adj_ret;
1490
+ }
1491
+
1492
+ template<unsigned Rows, unsigned Cols, typename Type>
1493
+ inline CUDA_CALLABLE void adj_outer(const vec_t<Rows,Type>& a, const vec_t<Cols,Type>& b, vec_t<Rows,Type>& adj_a, vec_t<Cols,Type>& adj_b, const mat_t<Rows,Cols,Type>& adj_ret)
1494
+ {
1495
+ adj_a += mul(adj_ret, b);
1496
+ adj_b += mul(transpose(adj_ret), a);
1497
+ }
1498
+
1499
+ template<unsigned Rows, unsigned Cols, typename Type>
1500
+ inline CUDA_CALLABLE void adj_add(const mat_t<Rows,Cols,Type>& a, const mat_t<Rows,Cols,Type>& b, mat_t<Rows,Cols,Type>& adj_a, mat_t<Rows,Cols,Type>& adj_b, const mat_t<Rows,Cols,Type>& adj_ret)
1501
+ {
1502
+ for (unsigned i=0; i < Rows; ++i)
1503
+ {
1504
+ for (unsigned j=0; j < Cols; ++j)
1505
+ {
1506
+ adj_a.data[i][j] += adj_ret.data[i][j];
1507
+ adj_b.data[i][j] += adj_ret.data[i][j];
1508
+ }
1509
+ }
1510
+ }
1511
+
1512
+ template<unsigned Rows, unsigned Cols, typename Type>
1513
+ inline CUDA_CALLABLE void adj_sub(const mat_t<Rows,Cols,Type>& a, const mat_t<Rows,Cols,Type>& b, mat_t<Rows,Cols,Type>& adj_a, mat_t<Rows,Cols,Type>& adj_b, const mat_t<Rows,Cols,Type>& adj_ret)
1514
+ {
1515
+ for (unsigned i=0; i < Rows; ++i)
1516
+ {
1517
+ for (unsigned j=0; j < Cols; ++j)
1518
+ {
1519
+ adj_a.data[i][j] += adj_ret.data[i][j];
1520
+ adj_b.data[i][j] -= adj_ret.data[i][j];
1521
+ }
1522
+ }
1523
+ }
1524
+
1525
+ template<unsigned Rows, unsigned Cols, typename Type>
1526
+ inline CUDA_CALLABLE void adj_div(const mat_t<Rows,Cols,Type>& a, Type s, mat_t<Rows,Cols,Type>& adj_a, Type& adj_s, const mat_t<Rows,Cols,Type>& adj_ret)
1527
+ {
1528
+ adj_s -= tensordot(a , adj_ret)/ (s * s); // - a / s^2
1529
+
1530
+ for (unsigned i=0; i < Rows; ++i)
1531
+ {
1532
+ for (unsigned j=0; j < Cols; ++j)
1533
+ {
1534
+ adj_a.data[i][j] += adj_ret.data[i][j] / s;
1535
+ }
1536
+ }
1537
+ }
1538
+
1539
+ template<unsigned Rows, unsigned Cols, typename Type>
1540
+ inline CUDA_CALLABLE void adj_div(Type s, const mat_t<Rows,Cols,Type>& a, Type& adj_s, mat_t<Rows,Cols,Type>& adj_a, const mat_t<Rows,Cols,Type>& adj_ret)
1541
+ {
1542
+ adj_s -= tensordot(a , adj_ret)/ (s * s); // - a / s^2
1543
+
1544
+ for (unsigned i=0; i < Rows; ++i)
1545
+ {
1546
+ for (unsigned j=0; j < Cols; ++j)
1547
+ {
1548
+ adj_a.data[i][j] += s / adj_ret.data[i][j];
1549
+ }
1550
+ }
1551
+ }
1552
+
1553
+ template<unsigned Rows, unsigned Cols, typename Type>
1554
+ inline CUDA_CALLABLE void adj_mul(const mat_t<Rows,Cols,Type>& a, Type b, mat_t<Rows,Cols,Type>& adj_a, Type& adj_b, const mat_t<Rows,Cols,Type>& adj_ret)
1555
+ {
1556
+ for (unsigned i=0; i < Rows; ++i)
1557
+ {
1558
+ for (unsigned j=0; j < Cols; ++j)
1559
+ {
1560
+ adj_a.data[i][j] += b*adj_ret.data[i][j];
1561
+ adj_b += a.data[i][j]*adj_ret.data[i][j];
1562
+ }
1563
+ }
1564
+ }
1565
+
1566
+ template<unsigned Rows, unsigned Cols, typename Type>
1567
+ inline CUDA_CALLABLE void adj_mul(Type b, const mat_t<Rows,Cols,Type>& a, Type& adj_b, mat_t<Rows,Cols,Type>& adj_a, const mat_t<Rows,Cols,Type>& adj_ret)
1568
+ {
1569
+ adj_mul(a, b, adj_a, adj_b, adj_ret);
1570
+ }
1571
+
1572
+ template<unsigned Rows, unsigned Cols, typename Type>
1573
+ inline CUDA_CALLABLE void adj_ddot(mat_t<Rows,Cols,Type> a, mat_t<Rows,Cols,Type> b, mat_t<Rows,Cols,Type>& adj_a, mat_t<Rows,Cols,Type>& adj_b, const Type adj_ret)
1574
+ {
1575
+ adj_a += b*adj_ret;
1576
+ adj_b += a*adj_ret;
1577
+ }
1578
+
1579
+ template<unsigned Rows, unsigned Cols, typename Type>
1580
+ inline CUDA_CALLABLE void adj_mul(const mat_t<Rows,Cols,Type>& a, const vec_t<Cols,Type>& b, mat_t<Rows,Cols,Type>& adj_a, vec_t<Cols,Type>& adj_b, const vec_t<Rows,Type>& adj_ret)
1581
+ {
1582
+ adj_a += outer(adj_ret, b);
1583
+ adj_b += mul(transpose(a), adj_ret);
1584
+ }
1585
+
1586
+ template<unsigned Rows, unsigned Cols, typename Type>
1587
+ inline CUDA_CALLABLE void adj_mul(const vec_t<Rows,Type>& b, const mat_t<Rows,Cols,Type>& a, vec_t<Rows,Type>& adj_b, mat_t<Rows,Cols,Type>& adj_a, const vec_t<Cols,Type>& adj_ret)
1588
+ {
1589
+ adj_a += outer(b, adj_ret);
1590
+ adj_b += mul(adj_ret, transpose(a));
1591
+ }
1592
+
1593
+ template<unsigned Rows, unsigned Cols, unsigned ColsOut, typename Type>
1594
+ inline CUDA_CALLABLE void adj_mul(const mat_t<Rows,Cols,Type>& a, const mat_t<Cols,ColsOut,Type>& b, mat_t<Rows,Cols,Type>& adj_a, mat_t<Cols,ColsOut,Type>& adj_b, const mat_t<Rows,ColsOut,Type>& adj_ret)
1595
+ {
1596
+ adj_a += mul(adj_ret, transpose(b));
1597
+ adj_b += mul(transpose(a), adj_ret);
1598
+ }
1599
+
1600
+ template<unsigned Rows, unsigned Cols, typename Type>
1601
+ inline CUDA_CALLABLE void adj_transpose(const mat_t<Rows,Cols,Type>& a, mat_t<Rows,Cols,Type>& adj_a, const mat_t<Cols,Rows,Type>& adj_ret)
1602
+ {
1603
+ adj_a += transpose(adj_ret);
1604
+ }
1605
+
1606
+ template<unsigned Rows, typename Type>
1607
+ inline CUDA_CALLABLE void adj_trace(const mat_t<Rows,Rows,Type>& m, mat_t<Rows,Rows,Type>& adj_m, Type adj_ret)
1608
+ {
1609
+ for (unsigned i=0; i < Rows; ++i)
1610
+ adj_m.data[i][i] += adj_ret;
1611
+ }
1612
+
1613
+ template<unsigned Rows, typename Type>
1614
+ inline CUDA_CALLABLE void adj_diag(const vec_t<Rows,Type>& d, vec_t<Rows,Type>& adj_d, const mat_t<Rows,Rows,Type>& adj_ret)
1615
+ {
1616
+ for (unsigned i=0; i < Rows; ++i)
1617
+ adj_d[i] += adj_ret.data[i][i];
1618
+ }
1619
+
1620
+ template<unsigned Rows, typename Type>
1621
+ inline CUDA_CALLABLE void adj_get_diag(const mat_t<Rows,Rows,Type>& m, mat_t<Rows,Rows,Type>& adj_m, const vec_t<Rows,Type>& adj_ret)
1622
+ {
1623
+ for (unsigned i=0; i < Rows; ++i)
1624
+ adj_m.data[i][i] += adj_ret[i];
1625
+ }
1626
+
1627
+ template<typename Type>
1628
+ inline CUDA_CALLABLE void adj_determinant(const mat_t<2,2,Type>& m, mat_t<2,2,Type>& adj_m, Type adj_ret)
1629
+ {
1630
+ adj_m.data[0][0] += m.data[1][1]*adj_ret;
1631
+ adj_m.data[1][1] += m.data[0][0]*adj_ret;
1632
+ adj_m.data[0][1] -= m.data[1][0]*adj_ret;
1633
+ adj_m.data[1][0] -= m.data[0][1]*adj_ret;
1634
+ }
1635
+
1636
+ template<typename Type>
1637
+ inline CUDA_CALLABLE void adj_determinant(const mat_t<3,3,Type>& m, mat_t<3,3,Type>& adj_m, Type adj_ret)
1638
+ {
1639
+ (vec_t<3,Type>&)adj_m.data[0] += cross(m.get_row(1), m.get_row(2))*adj_ret;
1640
+ (vec_t<3,Type>&)adj_m.data[1] += cross(m.get_row(2), m.get_row(0))*adj_ret;
1641
+ (vec_t<3,Type>&)adj_m.data[2] += cross(m.get_row(0), m.get_row(1))*adj_ret;
1642
+ }
1643
+
1644
+ template<typename Type>
1645
+ inline CUDA_CALLABLE void adj_determinant(const mat_t<4,4,Type>& m, mat_t<4,4,Type>& adj_m, Type adj_ret)
1646
+ {
1647
+ // adapted from USD GfMatrix4f::Inverse()
1648
+ Type x00, x01, x02, x03;
1649
+ Type x10, x11, x12, x13;
1650
+ Type x20, x21, x22, x23;
1651
+ Type x30, x31, x32, x33;
1652
+ double y01, y02, y03, y12, y13, y23;
1653
+ Type z00, z10, z20, z30;
1654
+ Type z01, z11, z21, z31;
1655
+ double z02, z03, z12, z13, z22, z23, z32, z33;
1656
+
1657
+ // Pickle 1st two columns of matrix into registers
1658
+ x00 = m.data[0][0];
1659
+ x01 = m.data[0][1];
1660
+ x10 = m.data[1][0];
1661
+ x11 = m.data[1][1];
1662
+ x20 = m.data[2][0];
1663
+ x21 = m.data[2][1];
1664
+ x30 = m.data[3][0];
1665
+ x31 = m.data[3][1];
1666
+
1667
+ // Compute all six 2x2 determinants of 1st two columns
1668
+ y01 = x00*x11 - x10*x01;
1669
+ y02 = x00*x21 - x20*x01;
1670
+ y03 = x00*x31 - x30*x01;
1671
+ y12 = x10*x21 - x20*x11;
1672
+ y13 = x10*x31 - x30*x11;
1673
+ y23 = x20*x31 - x30*x21;
1674
+
1675
+ // Pickle 2nd two columns of matrix into registers
1676
+ x02 = m.data[0][2];
1677
+ x03 = m.data[0][3];
1678
+ x12 = m.data[1][2];
1679
+ x13 = m.data[1][3];
1680
+ x22 = m.data[2][2];
1681
+ x23 = m.data[2][3];
1682
+ x32 = m.data[3][2];
1683
+ x33 = m.data[3][3];
1684
+
1685
+ // Compute all 3x3 cofactors for 2nd two columns */
1686
+ z33 = x02*y12 - x12*y02 + x22*y01;
1687
+ z23 = x12*y03 - x32*y01 - x02*y13;
1688
+ z13 = x02*y23 - x22*y03 + x32*y02;
1689
+ z03 = x22*y13 - x32*y12 - x12*y23;
1690
+ z32 = x13*y02 - x23*y01 - x03*y12;
1691
+ z22 = x03*y13 - x13*y03 + x33*y01;
1692
+ z12 = x23*y03 - x33*y02 - x03*y23;
1693
+ z02 = x13*y23 - x23*y13 + x33*y12;
1694
+
1695
+ // Compute all six 2x2 determinants of 2nd two columns
1696
+ y01 = x02*x13 - x12*x03;
1697
+ y02 = x02*x23 - x22*x03;
1698
+ y03 = x02*x33 - x32*x03;
1699
+ y12 = x12*x23 - x22*x13;
1700
+ y13 = x12*x33 - x32*x13;
1701
+ y23 = x22*x33 - x32*x23;
1702
+
1703
+ // Compute all 3x3 cofactors for 1st two columns
1704
+ z30 = x11*y02 - x21*y01 - x01*y12;
1705
+ z20 = x01*y13 - x11*y03 + x31*y01;
1706
+ z10 = x21*y03 - x31*y02 - x01*y23;
1707
+ z00 = x11*y23 - x21*y13 + x31*y12;
1708
+ z31 = x00*y12 - x10*y02 + x20*y01;
1709
+ z21 = x10*y03 - x30*y01 - x00*y13;
1710
+ z11 = x00*y23 - x20*y03 + x30*y02;
1711
+ z01 = x20*y13 - x30*y12 - x10*y23;
1712
+
1713
+ // Multiply all 3x3 cofactors by adjoint & transpose
1714
+ adj_m.data[0][0] += Type(z00*adj_ret);
1715
+ adj_m.data[1][0] += Type(z10*adj_ret);
1716
+ adj_m.data[0][1] += Type(z01*adj_ret);
1717
+ adj_m.data[2][0] += Type(z20*adj_ret);
1718
+ adj_m.data[0][2] += Type(z02*adj_ret);
1719
+ adj_m.data[3][0] += Type(z30*adj_ret);
1720
+ adj_m.data[0][3] += Type(z03*adj_ret);
1721
+ adj_m.data[1][1] += Type(z11*adj_ret);
1722
+ adj_m.data[2][1] += Type(z21*adj_ret);
1723
+ adj_m.data[1][2] += Type(z12*adj_ret);
1724
+ adj_m.data[3][1] += Type(z31*adj_ret);
1725
+ adj_m.data[1][3] += Type(z13*adj_ret);
1726
+ adj_m.data[2][2] += Type(z22*adj_ret);
1727
+ adj_m.data[3][2] += Type(z32*adj_ret);
1728
+ adj_m.data[2][3] += Type(z23*adj_ret);
1729
+ adj_m.data[3][3] += Type(z33*adj_ret);
1730
+ }
1731
+
1732
+ template<unsigned Rows, typename Type>
1733
+ inline CUDA_CALLABLE void adj_inverse(const mat_t<Rows,Rows,Type>& m, mat_t<Rows,Rows,Type>& ret, mat_t<Rows,Rows,Type>& adj_m, const mat_t<Rows,Rows,Type>& adj_ret)
1734
+ {
1735
+ // todo: how to cache this from the forward pass?
1736
+ mat_t<Rows,Rows,Type> invt = transpose(ret);
1737
+
1738
+ // see https://people.maths.ox.ac.uk/gilesm/files/NA-08-01.pdf 2.2.3
1739
+ adj_m -= mul(mul(invt, adj_ret), invt);
1740
+ }
1741
+
1742
+ template<typename Type>
1743
+ inline CUDA_CALLABLE void adj_transform_point(const mat_t<4,4,Type>& m, const vec_t<3,Type>& v, mat_t<4,4,Type>& adj_m, vec_t<3,Type>& adj_v, const vec_t<3,Type>& adj_ret)
1744
+ {
1745
+ vec_t<4,Type> out = vec_t<4,Type>(v[0], v[1], v[2], 1.f);
1746
+ adj_m = add(adj_m, transpose(mat_t<4,4,Type>(adj_ret[0] * out, adj_ret[1] * out, adj_ret[2] * out, vec_t<4,Type>())));
1747
+ adj_v[0] += dot(vec_t<3,Type>(m.data[0][0], m.data[1][0], m.data[2][0]), adj_ret);
1748
+ adj_v[1] += dot(vec_t<3,Type>(m.data[0][1], m.data[1][1], m.data[2][1]), adj_ret);
1749
+ adj_v[2] += dot(vec_t<3,Type>(m.data[0][2], m.data[1][2], m.data[2][2]), adj_ret);
1750
+ }
1751
+
1752
+ template<typename Type>
1753
+ inline CUDA_CALLABLE void adj_transform_vector(const mat_t<4,4,Type>& m, const vec_t<3,Type>& v, mat_t<4,4,Type>& adj_m, vec_t<3,Type>& adj_v, const vec_t<3,Type>& adj_ret)
1754
+ {
1755
+ vec_t<4,Type> out = vec_t<4,Type>(v[0], v[1], v[2], 0.f);
1756
+ adj_m = add(adj_m, transpose(mat_t<4,4,Type>(adj_ret[0] * out, adj_ret[1] * out, adj_ret[2] * out, vec_t<4,Type>())));
1757
+ adj_v[0] += dot(vec_t<3,Type>(m.data[0][0], m.data[1][0], m.data[2][0]), adj_ret);
1758
+ adj_v[1] += dot(vec_t<3,Type>(m.data[0][1], m.data[1][1], m.data[2][1]), adj_ret);
1759
+ adj_v[2] += dot(vec_t<3,Type>(m.data[0][2], m.data[1][2], m.data[2][2]), adj_ret);
1760
+ }
1761
+
1762
+ template<typename Type>
1763
+ inline CUDA_CALLABLE void adj_skew(const vec_t<3,Type>& a, vec_t<3,Type>& adj_a, const mat_t<3,3,Type>& adj_ret)
1764
+ {
1765
+ adj_a[0] += adj_ret.data[2][1] - adj_ret.data[1][2];
1766
+ adj_a[1] += adj_ret.data[0][2] - adj_ret.data[2][0];
1767
+ adj_a[2] += adj_ret.data[1][0] - adj_ret.data[0][1];
1768
+ }
1769
+
1770
+ template<unsigned Rows, unsigned Cols, typename Type>
1771
+ inline CUDA_CALLABLE void adj_cw_mul(const mat_t<Rows,Cols,Type>& a, const mat_t<Rows,Cols,Type>& b, mat_t<Rows,Cols,Type>& adj_a, mat_t<Rows,Cols,Type>& adj_b, const mat_t<Rows,Cols,Type>& adj_ret)
1772
+ {
1773
+ adj_a += cw_mul(b, adj_ret);
1774
+ adj_b += cw_mul(a, adj_ret);
1775
+ }
1776
+
1777
+ template<unsigned Rows, unsigned Cols, typename Type>
1778
+ inline CUDA_CALLABLE void adj_cw_div(const mat_t<Rows,Cols,Type>& a, const mat_t<Rows,Cols,Type>& b, mat_t<Rows,Cols,Type>& ret, mat_t<Rows,Cols,Type>& adj_a, mat_t<Rows,Cols,Type>& adj_b, const mat_t<Rows,Cols,Type>& adj_ret)
1779
+ {
1780
+ adj_a += cw_div(adj_ret, b);
1781
+ adj_b -= cw_mul(adj_ret, cw_div(ret, b));
1782
+ }
1783
+
1784
+ // adjoint for the constant constructor:
1785
+ template<unsigned Rows, unsigned Cols, typename Type>
1786
+ inline CUDA_CALLABLE void adj_mat_t(Type s, Type& adj_s, const mat_t<Rows, Cols, Type>& adj_ret)
1787
+ {
1788
+ for (unsigned i=0; i < Rows; ++i)
1789
+ {
1790
+ for (unsigned j=0; j < Cols; ++j)
1791
+ {
1792
+ adj_s += adj_ret.data[i][j];
1793
+ }
1794
+ }
1795
+ }
1796
+
1797
+ // adjoint for the casting constructor:
1798
+ template<unsigned Rows, unsigned Cols, typename Type, typename OtherType>
1799
+ inline CUDA_CALLABLE void adj_mat_t(const mat_t<Rows, Cols, OtherType>& other, mat_t<Rows, Cols, OtherType>& adj_other, const mat_t<Rows, Cols, Type>& adj_ret)
1800
+ {
1801
+ for (unsigned i=0; i < Rows; ++i)
1802
+ {
1803
+ for (unsigned j=0; j < Cols; ++j)
1804
+ {
1805
+ adj_other.data[i][j] += adj_ret.data[i][j];
1806
+ }
1807
+ }
1808
+ }
1809
+
1810
+ // adjoint for the initializer_array scalar constructor:
1811
+ template<unsigned Rows, unsigned Cols, typename Type>
1812
+ inline CUDA_CALLABLE void adj_mat_t(const initializer_array<Rows * Cols, Type> &cmps, const initializer_array<Rows * Cols, Type*> &adj_cmps, const mat_t<Rows, Cols, Type>& adj_ret)
1813
+ {
1814
+ for (unsigned i=0; i < Rows; ++i)
1815
+ {
1816
+ for (unsigned j=0; j < Cols; ++j)
1817
+ {
1818
+ *adj_cmps[i * Cols + j] += adj_ret.data[i][j];
1819
+ }
1820
+ }
1821
+ }
1822
+
1823
+ template<typename Type>
1824
+ inline CUDA_CALLABLE void adj_mat_t(Type m00, Type m01, Type m10, Type m11, Type& adj_m00, Type& adj_m01, Type& adj_m10, Type& adj_m11, const mat_t<2, 2, Type>& adj_ret)
1825
+ {
1826
+ adj_m00 += adj_ret.data[0][0];
1827
+ adj_m01 += adj_ret.data[0][1];
1828
+ adj_m10 += adj_ret.data[1][0];
1829
+ adj_m11 += adj_ret.data[1][1];
1830
+ }
1831
+
1832
+ template<typename Type>
1833
+ inline CUDA_CALLABLE void adj_mat_t(Type m00, Type m01, Type m02,
1834
+ Type m10, Type m11, Type m12,
1835
+ Type m20, Type m21, Type m22,
1836
+ Type& a00, Type& a01, Type& a02,
1837
+ Type& a10, Type& a11, Type& a12,
1838
+ Type& a20, Type& a21, Type& a22,
1839
+ const mat_t<3, 3, Type>& adj_ret)
1840
+ {
1841
+ a00 += adj_ret.data[0][0];
1842
+ a01 += adj_ret.data[0][1];
1843
+ a02 += adj_ret.data[0][2];
1844
+ a10 += adj_ret.data[1][0];
1845
+ a11 += adj_ret.data[1][1];
1846
+ a12 += adj_ret.data[1][2];
1847
+ a20 += adj_ret.data[2][0];
1848
+ a21 += adj_ret.data[2][1];
1849
+ a22 += adj_ret.data[2][2];
1850
+ }
1851
+
1852
+
1853
+ template<typename Type>
1854
+ inline CUDA_CALLABLE void adj_mat_t(Type m00, Type m01, Type m02, Type m03,
1855
+ Type m10, Type m11, Type m12, Type m13,
1856
+ Type m20, Type m21, Type m22, Type m23,
1857
+ Type m30, Type m31, Type m32, Type m33,
1858
+ Type& a00, Type& a01, Type& a02, Type& a03,
1859
+ Type& a10, Type& a11, Type& a12, Type& a13,
1860
+ Type& a20, Type& a21, Type& a22, Type& a23,
1861
+ Type& a30, Type& a31, Type& a32, Type& a33,
1862
+ const mat_t<4, 4, Type>& adj_ret)
1863
+ {
1864
+ a00 += adj_ret.data[0][0];
1865
+ a01 += adj_ret.data[0][1];
1866
+ a02 += adj_ret.data[0][2];
1867
+ a03 += adj_ret.data[0][3];
1868
+
1869
+ a10 += adj_ret.data[1][0];
1870
+ a11 += adj_ret.data[1][1];
1871
+ a12 += adj_ret.data[1][2];
1872
+ a13 += adj_ret.data[1][3];
1873
+
1874
+ a20 += adj_ret.data[2][0];
1875
+ a21 += adj_ret.data[2][1];
1876
+ a22 += adj_ret.data[2][2];
1877
+ a23 += adj_ret.data[2][3];
1878
+
1879
+ a30 += adj_ret.data[3][0];
1880
+ a31 += adj_ret.data[3][1];
1881
+ a32 += adj_ret.data[3][2];
1882
+ a33 += adj_ret.data[3][3];
1883
+ }
1884
+
1885
+
1886
+
1887
+ // adjoint for the initializer_array vector constructor:
1888
+ template<unsigned Rows, unsigned Cols, typename Type>
1889
+ inline CUDA_CALLABLE void adj_mat_t(const initializer_array<Cols, vec_t<Rows,Type> > &cmps, const initializer_array<Cols, vec_t<Rows,Type>* > &adj_cmps, const mat_t<Rows, Cols, Type>& adj_ret)
1890
+ {
1891
+ for (unsigned j=0; j < Cols; ++j)
1892
+ {
1893
+ for (unsigned i=0; i < Rows; ++i)
1894
+ {
1895
+ (*adj_cmps[j])[i] += adj_ret.data[i][j];
1896
+ }
1897
+ }
1898
+ }
1899
+
1900
+ template<typename Type>
1901
+ inline CUDA_CALLABLE void adj_mat_t(const vec_t<2,Type> &cmps0, const vec_t<2,Type> &cmps1, vec_t<2,Type> &adj_cmps0, vec_t<2,Type> &adj_cmps1, const mat_t<2, 2, Type>& adj_ret)
1902
+ {
1903
+ for (unsigned i=0; i < 2; ++i)
1904
+ {
1905
+ adj_cmps0[i] += adj_ret.data[i][0];
1906
+ adj_cmps1[i] += adj_ret.data[i][1];
1907
+ }
1908
+ }
1909
+
1910
+ template<typename Type>
1911
+ inline CUDA_CALLABLE void adj_mat_t(const vec_t<3,Type> &cmps0, const vec_t<3,Type> &cmps1, const vec_t<3,Type> &cmps2, vec_t<3,Type> &adj_cmps0, vec_t<3,Type> &adj_cmps1, vec_t<3,Type> &adj_cmps2, const mat_t<3, 3, Type>& adj_ret)
1912
+ {
1913
+ for (unsigned i=0; i < 3; ++i)
1914
+ {
1915
+ adj_cmps0[i] += adj_ret.data[i][0];
1916
+ adj_cmps1[i] += adj_ret.data[i][1];
1917
+ adj_cmps2[i] += adj_ret.data[i][2];
1918
+ }
1919
+ }
1920
+
1921
+ template<typename Type>
1922
+ inline CUDA_CALLABLE void adj_mat_t(const vec_t<4,Type> &cmps0, const vec_t<4,Type> &cmps1, const vec_t<4,Type> &cmps2, const vec_t<4,Type> &cmps3, vec_t<4,Type> &adj_cmps0, vec_t<4,Type> &adj_cmps1, vec_t<4,Type> &adj_cmps2, vec_t<4,Type> &adj_cmps3, const mat_t<4, 4, Type>& adj_ret)
1923
+ {
1924
+ for (unsigned i=0; i < 4; ++i)
1925
+ {
1926
+ adj_cmps0[i] += adj_ret.data[i][0];
1927
+ adj_cmps1[i] += adj_ret.data[i][1];
1928
+ adj_cmps2[i] += adj_ret.data[i][2];
1929
+ adj_cmps3[i] += adj_ret.data[i][3];
1930
+ }
1931
+ }
1932
+
1933
+ template<typename Type>
1934
+ inline CUDA_CALLABLE void adj_matrix_from_cols(
1935
+ const vec_t<2, Type>& c0, const vec_t<2, Type>& c1,
1936
+ vec_t<2, Type>& adj_c0, vec_t<2, Type>& adj_c1,
1937
+ const mat_t<2, 2, Type>& adj_ret
1938
+ )
1939
+ {
1940
+ for (unsigned i=0; i < 2; ++i)
1941
+ {
1942
+ adj_c0[i] += adj_ret.data[i][0];
1943
+ adj_c1[i] += adj_ret.data[i][1];
1944
+ }
1945
+ }
1946
+
1947
+ template<typename Type>
1948
+ inline CUDA_CALLABLE void adj_matrix_from_cols(
1949
+ const vec_t<3, Type>& c0, const vec_t<3, Type>& c1, const vec_t<3, Type>& c2,
1950
+ vec_t<3, Type>& adj_c0, vec_t<3, Type>& adj_c1, vec_t<3, Type>& adj_c2,
1951
+ const mat_t<3, 3, Type>& adj_ret
1952
+ )
1953
+ {
1954
+ for (unsigned i=0; i < 3; ++i)
1955
+ {
1956
+ adj_c0[i] += adj_ret.data[i][0];
1957
+ adj_c1[i] += adj_ret.data[i][1];
1958
+ adj_c2[i] += adj_ret.data[i][2];
1959
+ }
1960
+ }
1961
+
1962
+ template<typename Type>
1963
+ inline CUDA_CALLABLE void adj_matrix_from_cols(
1964
+ const vec_t<4, Type>& c0, const vec_t<4, Type>& c1, const vec_t<4, Type>& c2, const vec_t<4, Type>& c3,
1965
+ vec_t<4, Type>& adj_c0, vec_t<4, Type>& adj_c1, vec_t<4, Type>& adj_c2, vec_t<4, Type>& adj_c3,
1966
+ const mat_t<4, 4, Type>& adj_ret
1967
+ )
1968
+ {
1969
+ for (unsigned i=0; i < 4; ++i)
1970
+ {
1971
+ adj_c0[i] += adj_ret.data[i][0];
1972
+ adj_c1[i] += adj_ret.data[i][1];
1973
+ adj_c2[i] += adj_ret.data[i][2];
1974
+ adj_c3[i] += adj_ret.data[i][3];
1975
+ }
1976
+ }
1977
+
1978
+ template<unsigned Rows, unsigned Cols, typename Type>
1979
+ inline CUDA_CALLABLE void adj_matrix_from_cols(
1980
+ const initializer_array<Cols, vec_t<Rows, Type> >& l,
1981
+ const initializer_array<Cols, vec_t<Rows, Type>* >& adj_l,
1982
+ const mat_t<Rows, Cols, Type>& adj_ret
1983
+ )
1984
+ {
1985
+ for (unsigned j=0; j < Cols; ++j)
1986
+ {
1987
+ for (unsigned i=0; i < Rows; ++i)
1988
+ {
1989
+ (*adj_l[j])[i] += adj_ret.data[i][j];
1990
+ }
1991
+ }
1992
+ }
1993
+
1994
+ template<typename Type>
1995
+ inline CUDA_CALLABLE void adj_matrix_from_rows(
1996
+ const vec_t<2, Type>& r0, const vec_t<2, Type>& r1,
1997
+ vec_t<2, Type>& adj_r0, vec_t<2, Type>& adj_r1,
1998
+ const mat_t<2, 2, Type>& adj_ret
1999
+ )
2000
+ {
2001
+ for (unsigned j=0; j < 2; ++j)
2002
+ {
2003
+ adj_r0[j] += adj_ret.data[0][j];
2004
+ adj_r1[j] += adj_ret.data[1][j];
2005
+ }
2006
+ }
2007
+
2008
+ template<typename Type>
2009
+ inline CUDA_CALLABLE void adj_matrix_from_rows(
2010
+ const vec_t<3, Type>& c0, const vec_t<3, Type>& c1, const vec_t<3, Type>& c2,
2011
+ vec_t<3, Type>& adj_c0, vec_t<3, Type>& adj_c1, vec_t<3, Type>& adj_c2,
2012
+ const mat_t<3, 3, Type>& adj_ret
2013
+ )
2014
+ {
2015
+ for (unsigned j=0; j < 3; ++j)
2016
+ {
2017
+ adj_c0[j] += adj_ret.data[0][j];
2018
+ adj_c1[j] += adj_ret.data[1][j];
2019
+ adj_c2[j] += adj_ret.data[2][j];
2020
+ }
2021
+ }
2022
+
2023
+ template<typename Type>
2024
+ inline CUDA_CALLABLE void adj_matrix_from_rows(
2025
+ const vec_t<4, Type>& c0, const vec_t<4, Type>& c1, const vec_t<4, Type>& c2, const vec_t<4, Type>& c3,
2026
+ vec_t<4, Type>& adj_c0, vec_t<4, Type>& adj_c1, vec_t<4, Type>& adj_c2, vec_t<4, Type>& adj_c3,
2027
+ const mat_t<4, 4, Type>& adj_ret
2028
+ )
2029
+ {
2030
+ for (unsigned j=0; j < 4; ++j)
2031
+ {
2032
+ adj_c0[j] += adj_ret.data[0][j];
2033
+ adj_c1[j] += adj_ret.data[1][j];
2034
+ adj_c2[j] += adj_ret.data[2][j];
2035
+ adj_c3[j] += adj_ret.data[3][j];
2036
+ }
2037
+ }
2038
+
2039
+ template<unsigned Rows, unsigned Cols, typename Type>
2040
+ inline CUDA_CALLABLE void adj_matrix_from_rows(
2041
+ const initializer_array<Rows, vec_t<Cols, Type> >& l,
2042
+ const initializer_array<Rows, vec_t<Cols, Type>* >& adj_l,
2043
+ const mat_t<Rows, Cols, Type>& adj_ret
2044
+ )
2045
+ {
2046
+ for (unsigned i=0; i < Rows; ++i)
2047
+ {
2048
+ for (unsigned j=0; j < Cols; ++j)
2049
+ {
2050
+ (*adj_l[i])[j] += adj_ret.data[i][j];
2051
+ }
2052
+ }
2053
+ }
2054
+
2055
+ template<unsigned Rows, unsigned Cols, typename Type>
2056
+ CUDA_CALLABLE inline mat_t<Rows, Cols, Type> lerp(const mat_t<Rows, Cols, Type>& a, const mat_t<Rows, Cols, Type>& b, Type t)
2057
+ {
2058
+ return a*(Type(1)-t) + b*t;
2059
+ }
2060
+
2061
+ template<unsigned Rows, unsigned Cols, typename Type>
2062
+ CUDA_CALLABLE inline void adj_lerp(const mat_t<Rows, Cols, Type>& a, const mat_t<Rows, Cols, Type>& b, Type t, mat_t<Rows, Cols, Type>& adj_a, mat_t<Rows, Cols, Type>& adj_b, Type& adj_t, const mat_t<Rows, Cols, Type>& adj_ret)
2063
+ {
2064
+ adj_a += adj_ret*(Type(1)-t);
2065
+ adj_b += adj_ret*t;
2066
+ adj_t += tensordot(b, adj_ret) - tensordot(a, adj_ret);
2067
+ }
2068
+
2069
+ // for integral types we do not accumulate gradients
2070
+ template<unsigned Rows, unsigned Cols> CUDA_CALLABLE inline void adj_atomic_add(mat_t<Rows, Cols, int8>* buf, const mat_t<Rows, Cols, int8> &value) { }
2071
+ template<unsigned Rows, unsigned Cols> CUDA_CALLABLE inline void adj_atomic_add(mat_t<Rows, Cols, uint8>* buf, const mat_t<Rows, Cols, uint8> &value) { }
2072
+ template<unsigned Rows, unsigned Cols> CUDA_CALLABLE inline void adj_atomic_add(mat_t<Rows, Cols, int16>* buf, const mat_t<Rows, Cols, int16> &value) { }
2073
+ template<unsigned Rows, unsigned Cols> CUDA_CALLABLE inline void adj_atomic_add(mat_t<Rows, Cols, uint16>* buf, const mat_t<Rows, Cols, uint16> &value) { }
2074
+ template<unsigned Rows, unsigned Cols> CUDA_CALLABLE inline void adj_atomic_add(mat_t<Rows, Cols, int32>* buf, const mat_t<Rows, Cols, int32> &value) { }
2075
+ template<unsigned Rows, unsigned Cols> CUDA_CALLABLE inline void adj_atomic_add(mat_t<Rows, Cols, uint32>* buf, const mat_t<Rows, Cols, uint32> &value) { }
2076
+ template<unsigned Rows, unsigned Cols> CUDA_CALLABLE inline void adj_atomic_add(mat_t<Rows, Cols, int64>* buf, const mat_t<Rows, Cols, int64> &value) { }
2077
+ template<unsigned Rows, unsigned Cols> CUDA_CALLABLE inline void adj_atomic_add(mat_t<Rows, Cols, uint64>* buf, const mat_t<Rows, Cols, uint64> &value) { }
2078
+
2079
+ using mat22h = mat_t<2,2,half>;
2080
+ using mat33h = mat_t<3,3,half>;
2081
+ using mat44h = mat_t<4,4,half>;
2082
+
2083
+ using mat22 = mat_t<2,2,float>;
2084
+ using mat33 = mat_t<3,3,float>;
2085
+ using mat44 = mat_t<4,4,float>;
2086
+
2087
+ using mat22f = mat_t<2,2,float>;
2088
+ using mat33f = mat_t<3,3,float>;
2089
+ using mat44f = mat_t<4,4,float>;
2090
+
2091
+ using mat22d = mat_t<2,2,double>;
2092
+ using mat33d = mat_t<3,3,double>;
2093
+ using mat44d = mat_t<4,4,double>;
2094
+
2095
+ inline CUDA_CALLABLE void adj_mat22(vec2 c0, vec2 c1,
2096
+ vec2& a0, vec2& a1,
2097
+ const mat22& adj_ret)
2098
+ {
2099
+ a0 += adj_ret.get_col(0);
2100
+ a1 += adj_ret.get_col(1);
2101
+ }
2102
+
2103
+ inline CUDA_CALLABLE void adj_mat22(float m00, float m01, float m10, float m11, float& adj_m00, float& adj_m01, float& adj_m10, float& adj_m11, const mat22& adj_ret)
2104
+ {
2105
+ adj_m00 += adj_ret.data[0][0];
2106
+ adj_m01 += adj_ret.data[0][1];
2107
+ adj_m10 += adj_ret.data[1][0];
2108
+ adj_m11 += adj_ret.data[1][1];
2109
+ }
2110
+
2111
+ inline CUDA_CALLABLE void adj_mat33(vec3 c0, vec3 c1, vec3 c2,
2112
+ vec3& a0, vec3& a1, vec3& a2,
2113
+ const mat33& adj_ret)
2114
+ {
2115
+ // column constructor
2116
+ a0 += adj_ret.get_col(0);
2117
+ a1 += adj_ret.get_col(1);
2118
+ a2 += adj_ret.get_col(2);
2119
+
2120
+ }
2121
+
2122
+ inline CUDA_CALLABLE void adj_mat33(float m00, float m01, float m02,
2123
+ float m10, float m11, float m12,
2124
+ float m20, float m21, float m22,
2125
+ float& a00, float& a01, float& a02,
2126
+ float& a10, float& a11, float& a12,
2127
+ float& a20, float& a21, float& a22,
2128
+ const mat33& adj_ret)
2129
+ {
2130
+ a00 += adj_ret.data[0][0];
2131
+ a01 += adj_ret.data[0][1];
2132
+ a02 += adj_ret.data[0][2];
2133
+ a10 += adj_ret.data[1][0];
2134
+ a11 += adj_ret.data[1][1];
2135
+ a12 += adj_ret.data[1][2];
2136
+ a20 += adj_ret.data[2][0];
2137
+ a21 += adj_ret.data[2][1];
2138
+ a22 += adj_ret.data[2][2];
2139
+ }
2140
+
2141
+ inline CUDA_CALLABLE void adj_mat44(
2142
+ vec4 c0, vec4 c1, vec4 c2, vec4 c3,
2143
+ vec4& a0, vec4& a1, vec4& a2, vec4& a3,
2144
+ const mat44& adj_ret)
2145
+ {
2146
+ // column constructor
2147
+ a0 += adj_ret.get_col(0);
2148
+ a1 += adj_ret.get_col(1);
2149
+ a2 += adj_ret.get_col(2);
2150
+ a3 += adj_ret.get_col(3);
2151
+ }
2152
+
2153
+ inline CUDA_CALLABLE void adj_mat44(float m00, float m01, float m02, float m03,
2154
+ float m10, float m11, float m12, float m13,
2155
+ float m20, float m21, float m22, float m23,
2156
+ float m30, float m31, float m32, float m33,
2157
+ float& a00, float& a01, float& a02, float& a03,
2158
+ float& a10, float& a11, float& a12, float& a13,
2159
+ float& a20, float& a21, float& a22, float& a23,
2160
+ float& a30, float& a31, float& a32, float& a33,
2161
+ const mat44& adj_ret)
2162
+ {
2163
+ a00 += adj_ret.data[0][0];
2164
+ a01 += adj_ret.data[0][1];
2165
+ a02 += adj_ret.data[0][2];
2166
+ a03 += adj_ret.data[0][3];
2167
+
2168
+ a10 += adj_ret.data[1][0];
2169
+ a11 += adj_ret.data[1][1];
2170
+ a12 += adj_ret.data[1][2];
2171
+ a13 += adj_ret.data[1][3];
2172
+
2173
+ a20 += adj_ret.data[2][0];
2174
+ a21 += adj_ret.data[2][1];
2175
+ a22 += adj_ret.data[2][2];
2176
+ a23 += adj_ret.data[2][3];
2177
+
2178
+ a30 += adj_ret.data[3][0];
2179
+ a31 += adj_ret.data[3][1];
2180
+ a32 += adj_ret.data[3][2];
2181
+ a33 += adj_ret.data[3][3];
2182
+ }
2183
+
2184
+ template<unsigned Rows, unsigned Cols, typename Type>
2185
+ CUDA_CALLABLE inline int len(const mat_t<Rows,Cols,Type>& x)
2186
+ {
2187
+ return Rows;
2188
+ }
2189
+
2190
+ template<unsigned Rows, unsigned Cols, typename Type>
2191
+ CUDA_CALLABLE inline void adj_len(const mat_t<Rows,Cols,Type>& x, mat_t<Rows,Cols,Type>& adj_x, const int& adj_ret)
2192
+ {
2193
+ }
2194
+
2195
+ template<unsigned Rows, unsigned Cols, typename Type>
2196
+ inline CUDA_CALLABLE void expect_near(const mat_t<Rows,Cols,Type>& actual, const mat_t<Rows,Cols,Type>& expected, const Type& tolerance)
2197
+ {
2198
+ Type diff(0);
2199
+ for (unsigned i = 0; i < Rows; ++i)
2200
+ {
2201
+ for (unsigned j = 0; j < Cols; ++j)
2202
+ {
2203
+ diff = max(diff, abs(actual.data[i][j] - expected.data[i][j]));
2204
+ }
2205
+ }
2206
+ if (diff > tolerance)
2207
+ {
2208
+ printf("Error, expect_near() failed with tolerance "); print(tolerance);
2209
+ printf("\t Expected: "); print(expected);
2210
+ printf("\t Actual: "); print(actual);
2211
+ }
2212
+ }
2213
+
2214
+ template<unsigned Rows, unsigned Cols, typename Type>
2215
+ inline CUDA_CALLABLE void adj_expect_near(const mat_t<Rows,Cols,Type>& actual, const mat_t<Rows,Cols,Type>& expected, Type tolerance, mat_t<Rows,Cols,Type>& adj_actual, mat_t<Rows,Cols,Type>& adj_expected, Type adj_tolerance)
2216
+ {
2217
+ // nop
2218
+ }
2219
+
2220
+ } // namespace wp