Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 13 additions & 27 deletions include/picongpu/plugins/openPMD/restart/LoadSpecies.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,13 @@ namespace picongpu
template<typename T_Identifier>
struct RedistributeFilteredParticles
{
template<typename FrameType, typename FilterType, typename RemapType>
template<typename FrameType, typename FilterType>
HINLINE void operator()(
FrameType& frame,
FilterType const& filter,
RemapType const& remap,
uint64_t const numParticlesCurrentBatch,
// becomes numParticlesAfterFiltering after this function call
MemIdxType& particleIndexTarget,
char const filterRemove)
{
using Identifier = T_Identifier;
Expand All @@ -65,13 +66,14 @@ namespace picongpu

ValueType* dataPtr = frame.getIdentifier(Identifier()).getPointer();

for(size_t particleIndex = 0; particleIndex < numParticlesCurrentBatch; ++particleIndex)
particleIndexTarget = 0;
for(MemIdxType particleIndexSrc = 0; particleIndexSrc < numParticlesCurrentBatch; ++particleIndexSrc)
{
if(filter[particleIndex] == filterRemove)
if(filter[particleIndexSrc] == filterRemove)
{
continue;
}
dataPtr[remap[particleIndex]] = dataPtr[particleIndex];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like magic to me and a find the new version much clearer.

Copy link
Contributor Author

@franzpoeschel franzpoeschel Jan 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The major change is that the previous version (unnecessarily) precomputed the target indices. Could have been made a bit clearer by:

auto previouslyComputedNewIndex = remap[particleIndex];
dataPtr[previouslyComputedNewIndex] = dataPtr[particleIndex];

dataPtr[particleIndexTarget++] = dataPtr[particleIndexSrc];
}
}
};
Expand Down Expand Up @@ -291,17 +293,13 @@ namespace picongpu
manager::Device<HostDevice>::get().current(),
manager::Device<ComputeDevice>::get().getPlatform(),
MemSpace<DIM1>(maxChunkSize).toAlpakaMemVec());
auto remap = alpaka::allocMappedBuf<MemIdxType, MemIdxType>(
manager::Device<HostDevice>::get().current(),
manager::Device<ComputeDevice>::get().getPlatform(),
MemSpace<DIM1>(maxChunkSize).toAlpakaMemVec());

loadMatchesGeneric(
partialMatches,
threadParams,
totalNumParticles,
maxChunkSize, /* forEachPatch = */
[this, threadParams, &filter, &remap, &patchTotalOffset, &patchExtent, &patchUpperCorner](
[this, threadParams, &filter, &patchTotalOffset, &patchExtent, &patchUpperCorner](
uint64_t loadRound,
uint64_t numParticlesCurrentBatch,
FrameType& mappedFrame,
Expand Down Expand Up @@ -330,39 +328,27 @@ namespace picongpu
alpaka::getPtrNative(filter));
eventSystem::getTransactionEvent().waitForFinished();

// For simplicity, do the remapping on the CPU again
MemIdxType remapCurrent = 0;
for(size_t particleIndex = 0; particleIndex < numParticlesCurrentBatch; ++particleIndex)
{
if(filter[particleIndex] == filterKeep)
{
remap[particleIndex] = remapCurrent++;
}
else
{
remap[particleIndex] = std::numeric_limits<MemIdxType>::max();
}
}

MemIdxType numParticlesAfterFiltering = 0;
meta::ForEach<
typename NewParticleDescription::ValueTypeSeq,
RedistributeFilteredParticles<boost::mpl::_1>>
redistributeFilteredParticles;
redistributeFilteredParticles(
mappedFrame,
filter,
remap,
numParticlesCurrentBatch,
numParticlesAfterFiltering,
filterRemove);

log<picLog::INPUT_OUTPUT>(
"openPMD: Keeping %1% of the current batch's %2% particles after filtering.")
% remapCurrent % numParticlesCurrentBatch;
% numParticlesAfterFiltering % numParticlesCurrentBatch;

pmacc::particles::operations::splitIntoListOfFrames(
*speciesTmp,
mappedFrame,
remapCurrent, // !! not numParticlesCurrentBatch, filtered vs. unfiltered number
// @attention not numParticlesCurrentBatch, filtered vs. unfiltered number
numParticlesAfterFiltering,
cellOffsetToTotalDomain,
totalCellIdx_,
*threadParams->cellDescription,
Expand Down