From 4afd2c1e5cd38e7974d3eaee825e61d89c59f662 Mon Sep 17 00:00:00 2001 From: Kai Germaschewski Date: Sun, 8 Mar 2026 10:55:13 -0700 Subject: [PATCH 01/10] gpu: fix test cuda_mparticles_randomize_sort I don't think there was an actual issue, just the test itself making invalid assumptions. --- src/libpsc/tests/test_collision_cuda.cxx | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/libpsc/tests/test_collision_cuda.cxx b/src/libpsc/tests/test_collision_cuda.cxx index b0e0f60640..620d219dcd 100644 --- a/src/libpsc/tests/test_collision_cuda.cxx +++ b/src/libpsc/tests/test_collision_cuda.cxx @@ -210,13 +210,14 @@ TEST(cuda_mparticles_randomize_sort, sort) d_id, (std::vector{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14})); sort.sort(d_random_idx, d_id); - EXPECT_EQ( - d_id, (std::vector{0, 1, 7, 5, 8, 6, 2, 4, 3, 9, 10, 14, 13, 11, 12})); + // The result here depends on the particular random numbers generated, which + // seem to have changed across CUDA versions, so we just check that the random + // indices end up sorted. EXPECT_EQ( + // d_id, (std::vector{0, 1, 7, 5, 8, 6, 2, 4, 3, 9, 10, 14, 13, 11, + // 12})); - auto last = d_random_idx[0]; for (int i = 1; i < cmprts.size(); i++) { - EXPECT_GE(d_random_idx[i], last); - last = d_random_idx[i]; + EXPECT_GE(d_random_idx[i], d_random_idx[i - 1]); } // std::cout << "rnd idx "; // std::copy(sort.d_random_idx.begin(), sort.d_random_idx.end(), @@ -247,7 +248,7 @@ TEST(cuda_mparticles_randomize_sort, sort) // std::cout << "\n"; EXPECT_EQ(d_off, off); -#if 1 +#if 0 // do over, get different permutation sort.find_indices_ids(cmprts, d_random_idx, d_id); sort.sort(d_random_idx, d_id); From 7010a376da10958add0491baa3838b107b56af8b Mon Sep 17 00:00:00 2001 From: Kai Germaschewski Date: Mon, 9 Mar 2026 04:38:48 -0700 Subject: [PATCH 02/10] gpu: move particle injection back to before pushing particles. The GPU code is expecting this order, though it certainly is not desirable to have two different ways of doing it depending on whether GPU is used or not. --- src/include/psc.hxx | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/include/psc.hxx b/src/include/psc.hxx index 93a8feed78..f5aedb4a78 100644 --- a/src/include/psc.hxx +++ b/src/include/psc.hxx @@ -363,6 +363,15 @@ struct Psc prof_stop(pr_collision); } +#ifdef USE_CUDA + // === particle injection + prof_start(pr_inject_prts); + for (auto injector : injectors_) { + injector->inject(mprts_, mflds_); + } + prof_stop(pr_inject_prts); +#endif + if (checks_.continuity.should_do_check(timestep)) { mpi_printf(comm, "***** Checking continuity (1 of 2)...\n"); prof_start(pr_checks); @@ -376,6 +385,7 @@ struct Psc pushp_.push_mprts(mprts_, mflds_); prof_stop(pr_push_prts); +#ifndef USE_CUDA // === particle injection prof_start(pr_inject_prts); for (auto injector : injectors_) { @@ -383,6 +393,7 @@ struct Psc injector->inject(mprts_, mflds_); } prof_stop(pr_inject_prts); +#endif // === external current prof_start(pr_external_current); From 37abeef894c6f48df1f7f682bc74b012e2333834 Mon Sep 17 00:00:00 2001 From: Kai Germaschewski Date: Wed, 4 Mar 2026 13:48:05 -0800 Subject: [PATCH 03/10] io/adios2: add an assert() to FileAdios2::sizeAttribute() BP5 changes behavior from BP4, and I guess doesn't find attributes outside of a step. So at the very least, we should catch this with an assert(). --- src/kg/include/kg/io/FileAdios2.inl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/kg/include/kg/io/FileAdios2.inl b/src/kg/include/kg/io/FileAdios2.inl index 8b49bdf293..f2bcdbe3fc 100644 --- a/src/kg/include/kg/io/FileAdios2.inl +++ b/src/kg/include/kg/io/FileAdios2.inl @@ -257,6 +257,7 @@ inline size_t FileAdios2::sizeAttribute(const std::string& name) const { auto& io = const_cast(io_); // FIXME auto type = io.AttributeType(name); + assert(!type.empty()); if (0) { } From aef82a3a5a65d2411544c0498103f70ea4c918fe Mon Sep 17 00:00:00 2001 From: Kai Germaschewski Date: Wed, 4 Mar 2026 13:57:08 -0800 Subject: [PATCH 04/10] io/adios2: wrap put/get in steps --- src/libpsc/tests/test_grid_2.cxx | 4 ++++ src/libpsc/tests/test_mfields_io.cxx | 12 ++++++++++++ src/libpsc/tests/test_mparticles.cxx | 4 ++++ 3 files changed, 20 insertions(+) diff --git a/src/libpsc/tests/test_grid_2.cxx b/src/libpsc/tests/test_grid_2.cxx index 93c493665c..55024ff3f9 100644 --- a/src/libpsc/tests/test_grid_2.cxx +++ b/src/libpsc/tests/test_grid_2.cxx @@ -106,7 +106,9 @@ TEST(Grid2, adios2_write) auto grid = Grid_t{domain, bc, kinds, norm, dt, n_patches}; auto writer = io.open("test.bp", kg::io::Mode::Write); + writer.beginStep(kg::io::StepMode::Append); writer.put("grid", grid); + writer.endStep(); writer.close(); } @@ -118,7 +120,9 @@ TEST(Grid2, adios2_write) Grid_t grid; auto reader = io.open("test.bp", kg::io::Mode::Read); + reader.beginStep(kg::io::StepMode::Read); reader.get("grid", grid); + reader.endStep(); reader.close(); EXPECT_EQ(grid.dt, .1); diff --git a/src/libpsc/tests/test_mfields_io.cxx b/src/libpsc/tests/test_mfields_io.cxx index 59e267dd4a..0c08d0464f 100644 --- a/src/libpsc/tests/test_mfields_io.cxx +++ b/src/libpsc/tests/test_mfields_io.cxx @@ -68,14 +68,18 @@ TYPED_TEST(MfieldsTest, WriteRead) auto io = kg::io::IOAdios2{}; { auto writer = io.open("test.bp", kg::io::Mode::Write); + writer.beginStep(kg::io::StepMode::Append); writer.put("mflds", mflds); + writer.endStep(); writer.close(); } auto mflds2 = Mfields{grid, NR_FIELDS, {}}; { auto reader = io.open("test.bp", kg::io::Mode::Read); + reader.beginStep(kg::io::StepMode::Read); reader.get("mflds", mflds2); + reader.endStep(); reader.close(); } @@ -99,14 +103,18 @@ TYPED_TEST(MfieldsTest, WriteWithGhostsRead) { auto writer = io.open("test.bp", kg::io::Mode::Write); + writer.beginStep(kg::io::StepMode::Append); writer.put("mflds", mflds); + writer.endStep(); writer.close(); } auto mflds2 = Mfields{grid, NR_FIELDS, {}}; { auto reader = io.open("test.bp", kg::io::Mode::Read); + reader.beginStep(kg::io::StepMode::Read); reader.get("mflds", mflds2); + reader.endStep(); reader.close(); } @@ -130,14 +138,18 @@ TYPED_TEST(MfieldsTest, WriteReadWithGhosts) { auto writer = io.open("test.bp", kg::io::Mode::Write); + writer.beginStep(kg::io::StepMode::Append); writer.put("mflds", mflds); + writer.endStep(); writer.close(); } auto mflds2 = Mfields{grid, NR_FIELDS, {2, 2, 2}}; { auto reader = io.open("test.bp", kg::io::Mode::Read); + reader.beginStep(kg::io::StepMode::Read); reader.get("mflds", mflds2); + reader.endStep(); reader.close(); } diff --git a/src/libpsc/tests/test_mparticles.cxx b/src/libpsc/tests/test_mparticles.cxx index 770418669f..140996af44 100644 --- a/src/libpsc/tests/test_mparticles.cxx +++ b/src/libpsc/tests/test_mparticles.cxx @@ -411,14 +411,18 @@ TYPED_TEST(MparticlesIOTest, WriteRead) { auto writer = io.open("test.bp", kg::io::Mode::Write); + writer.beginStep(kg::io::StepMode::Append); writer.put("mprts", mprts); + writer.endStep(); writer.close(); } auto mprts2 = this->mk_mprts(); { auto reader = io.open("test.bp", kg::io::Mode::Read); + reader.beginStep(kg::io::StepMode::Read); reader.get("mprts", mprts2); + reader.endStep(); reader.close(); } From 12089a492504d654ffdfc605ea5ade0f71bf014c Mon Sep 17 00:00:00 2001 From: Kai Germaschewski Date: Sun, 8 Mar 2026 10:52:42 -0700 Subject: [PATCH 05/10] build: fix std::iterator deprecation warning --- src/include/range.hxx | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/include/range.hxx b/src/include/range.hxx index 84c33c4d58..e76103d1ce 100644 --- a/src/include/range.hxx +++ b/src/include/range.hxx @@ -6,8 +6,14 @@ template struct Range { - struct iterator : std::iterator + struct iterator { + using iterator_category = std::input_iterator_tag; + using value_type = T; + using difference_type = std::ptrdiff_t; + using pointer = T*; + using reference = T&; + __device__ __host__ explicit iterator(T val) : val_(val) {} __device__ __host__ T operator*() const { return val_; } @@ -51,8 +57,14 @@ private: template struct RangeStrided { - struct iterator : std::iterator + struct iterator { + using iterator_category = std::input_iterator_tag; + using value_type = T; + using difference_type = std::ptrdiff_t; + using pointer = T*; + using reference = T&; + __device__ __host__ explicit iterator(T val, T stride) : val_(val), stride_(stride) {} From e9b15989aaa9a154fcd1091f53bab5277ac37f38 Mon Sep 17 00:00:00 2001 From: Kai Germaschewski Date: Sun, 8 Mar 2026 19:05:47 -0700 Subject: [PATCH 06/10] fix a stray semicolon --- src/libpsc/cuda/cuda_mparticles.cxx | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libpsc/cuda/cuda_mparticles.cxx b/src/libpsc/cuda/cuda_mparticles.cxx index 4a3cae24d2..8ac965ec27 100644 --- a/src/libpsc/cuda/cuda_mparticles.cxx +++ b/src/libpsc/cuda/cuda_mparticles.cxx @@ -354,7 +354,6 @@ void cuda_mparticles::inject(const std::vector& buf, auto bidx = this->blockIndex(prt, p); assert(bidx >= 0 && bidx < this->n_blocks); h_bidx[off + n] = bidx; - ; // h_id[off + n] = this->n_prts + off + n; } off += buf_n_by_patch[p]; From 2676b8d40906f6b366223b739133e752a983930d Mon Sep 17 00:00:00 2001 From: Kai Germaschewski Date: Sun, 8 Mar 2026 19:04:34 -0700 Subject: [PATCH 07/10] TMP: additional debug --- src/libpsc/cuda/cuda_mparticles_checks.cxx | 5 ++++- src/libpsc/cuda/cuda_mparticles_indexer.h | 9 ++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/libpsc/cuda/cuda_mparticles_checks.cxx b/src/libpsc/cuda/cuda_mparticles_checks.cxx index aa8ee0856f..f11bff9caa 100644 --- a/src/libpsc/cuda/cuda_mparticles_checks.cxx +++ b/src/libpsc/cuda/cuda_mparticles_checks.cxx @@ -14,8 +14,11 @@ bool cuda_mparticles::check_in_patch_unordered_slow() for (int p = 0; p < this->n_patches(); p++) { for (int n = 0; n < n_prts_by_patch[p]; n++) { int bidx = this->blockIndex(this->storage[off + n], p); - if (!(bidx >= 0 && bidx <= this->n_blocks)) + if (!(bidx >= 0 && bidx <= this->n_blocks)) { + std::cerr << "check_in_patch_unordered_slow: p " << p << " n+off " << n + off + << " bidx " << bidx << std::endl; return false; + } } off += n_prts_by_patch[p]; } diff --git a/src/libpsc/cuda/cuda_mparticles_indexer.h b/src/libpsc/cuda/cuda_mparticles_indexer.h index c83af0b852..cbdebe7130 100644 --- a/src/libpsc/cuda/cuda_mparticles_indexer.h +++ b/src/libpsc/cuda/cuda_mparticles_indexer.h @@ -62,7 +62,12 @@ struct cuda_mparticles_indexer int blockIndex(const DParticleCuda& prt, int p) const { Int3 bpos = blockPosition(prt.x); - return blockIndex(bpos, p); + int bidx = blockIndex(bpos, p); + if (bidx < 0) { + printf("blockIndex: prt.x %g %g %g bpos %d %d %d p %d bidx %d\n", + prt.x[0], prt.x[1], prt.x[2], bpos[0], bpos[1], bpos[2], p, bidx); + } + return bidx; } void checkInPatchMod(real_t xi[3]) const { pi_.checkInPatchMod(xi); } @@ -73,6 +78,8 @@ struct cuda_mparticles_indexer { if (uint(bpos[0]) >= b_mx_[0] || uint(bpos[1]) >= b_mx_[1] || uint(bpos[2]) >= b_mx_[2]) { + printf("blockIndex: bpos %d %d %d b_mx %d %d %d\n", bpos[0], bpos[1], bpos[2], + b_mx_[0], b_mx_[1], b_mx_[2]); return -1; } From 19673d0f97f11534f0ffc634554d842ae22082fa Mon Sep 17 00:00:00 2001 From: Kai Germaschewski Date: Sun, 8 Mar 2026 19:04:52 -0700 Subject: [PATCH 08/10] Revert "TMP: additional debug" This reverts commit 920b034f1b99b1e0519edde77c82239003c60006. --- src/libpsc/cuda/cuda_mparticles_checks.cxx | 5 +---- src/libpsc/cuda/cuda_mparticles_indexer.h | 9 +-------- 2 files changed, 2 insertions(+), 12 deletions(-) diff --git a/src/libpsc/cuda/cuda_mparticles_checks.cxx b/src/libpsc/cuda/cuda_mparticles_checks.cxx index f11bff9caa..aa8ee0856f 100644 --- a/src/libpsc/cuda/cuda_mparticles_checks.cxx +++ b/src/libpsc/cuda/cuda_mparticles_checks.cxx @@ -14,11 +14,8 @@ bool cuda_mparticles::check_in_patch_unordered_slow() for (int p = 0; p < this->n_patches(); p++) { for (int n = 0; n < n_prts_by_patch[p]; n++) { int bidx = this->blockIndex(this->storage[off + n], p); - if (!(bidx >= 0 && bidx <= this->n_blocks)) { - std::cerr << "check_in_patch_unordered_slow: p " << p << " n+off " << n + off - << " bidx " << bidx << std::endl; + if (!(bidx >= 0 && bidx <= this->n_blocks)) return false; - } } off += n_prts_by_patch[p]; } diff --git a/src/libpsc/cuda/cuda_mparticles_indexer.h b/src/libpsc/cuda/cuda_mparticles_indexer.h index cbdebe7130..c83af0b852 100644 --- a/src/libpsc/cuda/cuda_mparticles_indexer.h +++ b/src/libpsc/cuda/cuda_mparticles_indexer.h @@ -62,12 +62,7 @@ struct cuda_mparticles_indexer int blockIndex(const DParticleCuda& prt, int p) const { Int3 bpos = blockPosition(prt.x); - int bidx = blockIndex(bpos, p); - if (bidx < 0) { - printf("blockIndex: prt.x %g %g %g bpos %d %d %d p %d bidx %d\n", - prt.x[0], prt.x[1], prt.x[2], bpos[0], bpos[1], bpos[2], p, bidx); - } - return bidx; + return blockIndex(bpos, p); } void checkInPatchMod(real_t xi[3]) const { pi_.checkInPatchMod(xi); } @@ -78,8 +73,6 @@ struct cuda_mparticles_indexer { if (uint(bpos[0]) >= b_mx_[0] || uint(bpos[1]) >= b_mx_[1] || uint(bpos[2]) >= b_mx_[2]) { - printf("blockIndex: bpos %d %d %d b_mx %d %d %d\n", bpos[0], bpos[1], bpos[2], - b_mx_[0], b_mx_[1], b_mx_[2]); return -1; } From e515e7779b58ca76731c43d654d93df7a50b18c5 Mon Sep 17 00:00:00 2001 From: Kai Germaschewski Date: Sun, 8 Mar 2026 19:05:18 -0700 Subject: [PATCH 09/10] TMP: smaller test case --- src/psc_2d_shock.cxx | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/psc_2d_shock.cxx b/src/psc_2d_shock.cxx index 8a66e258a1..3ded9060fd 100644 --- a/src/psc_2d_shock.cxx +++ b/src/psc_2d_shock.cxx @@ -286,9 +286,12 @@ Grid_t* setupGrid() Int3 gdims = {1, 160, 3 * 160}; // global number of grid points Int3 np = {1, 5, 3 * 5}; // division into patches #elif CASE == CASE_1D - Grid_t::Real3 LL = {1., 9.6, 60000.0}; // domain size (in d_e) - Int3 gdims = {1, 32, 200000}; // global number of grid points - Int3 np = {1, 1, 6250}; // division into patches + // Grid_t::Real3 LL = {1., 9.6, 60000.0}; // domain size (in d_e) + // Int3 gdims = {1, 32, 200000}; // global number of grid points + // Int3 np = {1, 1, 6250}; // division into patches + Grid_t::Real3 LL = {1., 9.6, 240.0}; // domain size (in d_e) + Int3 gdims = {1, 32, 800}; // global number of grid points + Int3 np = {1, 1, 25}; // division into patches #endif Grid_t::Domain domain{gdims, LL, -.5 * LL, np}; From 2eef87b82701ccd36bd2adbcacaed203b718d9ac Mon Sep 17 00:00:00 2001 From: Kai Germaschewski Date: Sun, 8 Mar 2026 19:05:22 -0700 Subject: [PATCH 10/10] Revert "TMP: smaller test case" This reverts commit 01d1d0d027c8f144b93e21b206137d8a693deaa9. --- src/psc_2d_shock.cxx | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/psc_2d_shock.cxx b/src/psc_2d_shock.cxx index 3ded9060fd..8a66e258a1 100644 --- a/src/psc_2d_shock.cxx +++ b/src/psc_2d_shock.cxx @@ -286,12 +286,9 @@ Grid_t* setupGrid() Int3 gdims = {1, 160, 3 * 160}; // global number of grid points Int3 np = {1, 5, 3 * 5}; // division into patches #elif CASE == CASE_1D - // Grid_t::Real3 LL = {1., 9.6, 60000.0}; // domain size (in d_e) - // Int3 gdims = {1, 32, 200000}; // global number of grid points - // Int3 np = {1, 1, 6250}; // division into patches - Grid_t::Real3 LL = {1., 9.6, 240.0}; // domain size (in d_e) - Int3 gdims = {1, 32, 800}; // global number of grid points - Int3 np = {1, 1, 25}; // division into patches + Grid_t::Real3 LL = {1., 9.6, 60000.0}; // domain size (in d_e) + Int3 gdims = {1, 32, 200000}; // global number of grid points + Int3 np = {1, 1, 6250}; // division into patches #endif Grid_t::Domain domain{gdims, LL, -.5 * LL, np};