From bf81e2ef60540dfb53f7437ab54b327c6f078cad Mon Sep 17 00:00:00 2001 From: Andrew Myers Date: Tue, 4 Nov 2025 12:03:02 -0800 Subject: [PATCH] Do not rely on managed memory in EM PIC tutorial --- .../Source/EMParticleContainerInit.cpp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/ExampleCodes/Particles/ElectromagneticPIC/Source/EMParticleContainerInit.cpp b/ExampleCodes/Particles/ElectromagneticPIC/Source/EMParticleContainerInit.cpp index e48bf40a..52cb12f1 100644 --- a/ExampleCodes/Particles/ElectromagneticPIC/Source/EMParticleContainerInit.cpp +++ b/ExampleCodes/Particles/ElectromagneticPIC/Source/EMParticleContainerInit.cpp @@ -70,10 +70,10 @@ InitParticles(const IntVect& a_num_particles_per_cell, const auto lo = amrex::lbound(tile_box); const auto hi = amrex::ubound(tile_box); - Gpu::ManagedVector counts(tile_box.numPts(), 0); + Gpu::DeviceVector counts(tile_box.numPts(), 0); unsigned int* pcount = counts.dataPtr(); - Gpu::ManagedVector offsets(tile_box.numPts()); + Gpu::DeviceVector offsets(tile_box.numPts()); unsigned int* poffset = offsets.dataPtr(); amrex::ParallelFor(tile_box, @@ -109,7 +109,16 @@ InitParticles(const IntVect& a_num_particles_per_cell, Gpu::exclusive_scan(counts.begin(), counts.end(), offsets.begin()); - int num_to_add = offsets[tile_box.numPts()-1] + counts[tile_box.numPts()-1]; + unsigned int last_offset; + unsigned int last_count; +#ifdef AMREX_USE_GPU + Gpu::dtoh_memcpy(&last_offset,offsets.dataPtr()+tile_box.numPts()-1,sizeof(unsigned int)); + Gpu::dtoh_memcpy(&last_count,counts.dataPtr()+tile_box.numPts()-1,sizeof(unsigned int)); +#else + std::memcpy(&last_offset,offsets.dataPtr()+tile_box.numPts()-1,sizeof(unsigned int) + std::memcpy(&last_count,counts.dataPtr()+tile_box.numPts()-1,sizeof(unsigned int) +#endif + int num_to_add = last_offset + last_count; auto& particles = GetParticles(lev); auto& particle_tile = particles[std::make_pair(mfi.index(), mfi.LocalTileIndex())];