From 86ae8e96a2ff74b9959f85d2f5dc6f3434ec0b2e Mon Sep 17 00:00:00 2001 From: Sam Reeve <6740307+streeve@users.noreply.github.com> Date: Wed, 18 Dec 2024 18:05:09 -0500 Subject: [PATCH] fixup: cuda constructor lambda capture --- src/CabanaPD_Particles.hpp | 75 +++++++++++++++++++++----------------- 1 file changed, 41 insertions(+), 34 deletions(-) diff --git a/src/CabanaPD_Particles.hpp b/src/CabanaPD_Particles.hpp index 5af58a1..8127cea 100644 --- a/src/CabanaPD_Particles.hpp +++ b/src/CabanaPD_Particles.hpp @@ -162,8 +162,8 @@ class Particles // Constructor with existing particle data. template - Particles( const ExecSpace& exec_space, PositionType x, VolumeType vol, - std::array low_corner, + Particles( const ExecSpace& exec_space, const PositionType& x, + const VolumeType& vol, std::array low_corner, std::array high_corner, const std::array num_cells, const int max_halo_width ) : halo_width( max_halo_width ) @@ -173,38 +173,7 @@ class Particles createDomain( low_corner, high_corner, num_cells ); _init_timer.start(); - resize( vol.size(), 0 ); - auto p_x = sliceReferencePosition(); - auto p_vol = sliceVolume(); - auto v = sliceVelocity(); - auto f = sliceForce(); - auto type = sliceType(); - auto rho = sliceDensity(); - auto u = sliceDisplacement(); - auto nofail = sliceNoFail(); - - static_assert( - Cabana::is_accessible_from< - memory_space, typename PositionType::execution_space>::value ); - - Kokkos::parallel_for( - "copy_to_particles", Kokkos::RangePolicy( exec_space, 0, n_local ), - KOKKOS_LAMBDA( const int pid ) { - // Set the particle position and volume. - // Set everything else to zero. - p_vol( pid ) = vol( pid ); - for ( int d = 0; d < 3; d++ ) - { - p_x( pid, d ) = x( pid, d ); - u( pid, d ) = 0.0; - v( pid, d ) = 0.0; - f( pid, d ) = 0.0; - } - type( pid ) = 0; - nofail( pid ) = 0; - rho( pid ) = 1.0; - } ); - + implInitCustomParticles( exec_space, x, vol ); _init_timer.stop(); } @@ -517,6 +486,44 @@ class Particles friend class Comm; protected: + // This is necessary to avoid Cuda lambda capture issues in the constructor. + template + void implInitCustomParticles( const ExecSpace& exec_space, + const PositionType& x, const VolumeType& vol ) + { + resize( vol.size(), 0 ); + auto p_x = sliceReferencePosition(); + auto p_vol = sliceVolume(); + auto v = sliceVelocity(); + auto f = sliceForce(); + auto type = sliceType(); + auto rho = sliceDensity(); + auto u = sliceDisplacement(); + auto nofail = sliceNoFail(); + + static_assert( + Cabana::is_accessible_from< + memory_space, typename PositionType::execution_space>::value ); + + Kokkos::parallel_for( + "copy_to_particles", Kokkos::RangePolicy( exec_space, 0, n_local ), + KOKKOS_LAMBDA( const int pid ) { + // Set the particle position and volume. + // Set everything else to zero. + p_vol( pid ) = vol( pid ); + for ( int d = 0; d < 3; d++ ) + { + p_x( pid, d ) = x( pid, d ); + u( pid, d ) = 0.0; + v( pid, d ) = 0.0; + f( pid, d ) = 0.0; + } + type( pid ) = 0; + nofail( pid ) = 0; + rho( pid ) = 1.0; + } ); + } + aosoa_u_type _aosoa_u; aosoa_y_type _aosoa_y; aosoa_vol_type _aosoa_vol;