@@ -66,18 +66,21 @@ namespace ParticleZoo {
6666
6767 // Calculate starting history for each thread
6868 startingHistorys_.reserve (numThreads);
69- threadStats_.resize (numThreads);
69+ threadStats_.reserve (numThreads);
70+ for (size_t i = 0 ; i < numThreads; ++i) {
71+ threadStats_.emplace_back (std::make_unique<ThreadStatistics>());
72+ }
7073 std::uint64_t historiesPerThread = numberOfRepresentedHistories_ / numThreads;
7174 std::uint64_t remainderHistories = numberOfRepresentedHistories_ % numThreads;
7275 std::uint64_t currentStartingHistory = 0 ;
7376 for (size_t i = 0 ; i < numThreads; ++i) {
7477 startingHistorys_.push_back (currentStartingHistory);
75- threadStats_[i]. historiesRead = currentStartingHistory;
78+ threadStats_[i]-> historiesRead = currentStartingHistory;
7679 // Calculate the correct initial error for this thread based on how many
7780 // represented histories have been "processed" before this thread's starting point
7881 if (hasGapsBetweenHistories_) {
7982 std::uint64_t initialError = numberOfRepresentedHistories_ / 2 ;
80- threadStats_[i]. emptyHistoryError = (initialError + currentStartingHistory * perHistoryErrorContribution_) % numberOfRepresentedHistories_;
83+ threadStats_[i]-> emptyHistoryError = (initialError + currentStartingHistory * perHistoryErrorContribution_) % numberOfRepresentedHistories_;
8184 }
8285 currentStartingHistory += historiesPerThread;
8386 if (i < remainderHistories) {
@@ -122,8 +125,8 @@ namespace ParticleZoo {
122125 }
123126
124127 // check the cache first
125- if (threadStats_[threadIndex]. hasMoreParticlesCache != NEEDS_CHECKING) {
126- return threadStats_[threadIndex]. hasMoreParticlesCache == HAS_MORE_PARTICLES;
128+ if (threadStats_[threadIndex]-> hasMoreParticlesCache != NEEDS_CHECKING) {
129+ return threadStats_[threadIndex]-> hasMoreParticlesCache == HAS_MORE_PARTICLES;
127130 }
128131
129132 // Check if the reader has more particles
@@ -137,7 +140,7 @@ namespace ParticleZoo {
137140 : numberOfRepresentedHistories_;
138141
139142 // Check if we have completed all histories for this thread
140- if (threadStats_[threadIndex]. historiesRead >= targetHistories) {
143+ if (threadStats_[threadIndex]-> historiesRead >= targetHistories) {
141144 // Check if the next particle would start a new history
142145 const Particle nextParticle = readers_[threadIndex]->peekNextParticle ();
143146 // If the next particle starts a new history, we have no more particles for this thread
@@ -146,7 +149,7 @@ namespace ParticleZoo {
146149 }
147150
148151 // Update the cache
149- threadStats_[threadIndex]. hasMoreParticlesCache = hasMoreParticles ? HAS_MORE_PARTICLES : NO_MORE_PARTICLES;
152+ threadStats_[threadIndex]-> hasMoreParticlesCache = hasMoreParticles ? HAS_MORE_PARTICLES : NO_MORE_PARTICLES;
150153
151154 return hasMoreParticles;
152155 }
@@ -161,33 +164,33 @@ namespace ParticleZoo {
161164 }
162165
163166 // reset the cache since we are consuming a particle
164- threadStats_[threadIndex]. hasMoreParticlesCache = NEEDS_CHECKING;
167+ threadStats_[threadIndex]-> hasMoreParticlesCache = NEEDS_CHECKING;
165168
166169 // Get the next particle from the appropriate reader
167170 Particle particle = readers_[threadIndex]->getNextParticle ();
168171
169172 // Update history count if this particle starts a new history
170173 int32_t incrementalHistoryNumber = 0 ;
171174 {
172- std::lock_guard<std::mutex> lock (threadStats_[threadIndex]. mutex );
173- threadStats_[threadIndex]. particlesRead ++;
175+ std::lock_guard<std::mutex> lock (threadStats_[threadIndex]-> mutex );
176+ threadStats_[threadIndex]-> particlesRead ++;
174177
175178 if (particle.isNewHistory ()) {
176179 if (hasGapsBetweenHistories_) {
177- threadStats_[threadIndex]. emptyHistoryError += perHistoryErrorContribution_;
178- if (threadStats_[threadIndex]. emptyHistoryError >= numberOfRepresentedHistories_) {
180+ threadStats_[threadIndex]-> emptyHistoryError += perHistoryErrorContribution_;
181+ if (threadStats_[threadIndex]-> emptyHistoryError >= numberOfRepresentedHistories_) {
179182 incrementalHistoryNumber = 2 + emptyHistoriesBetweenEachHistory_;
180- threadStats_[threadIndex]. emptyHistoryError -= numberOfRepresentedHistories_;
183+ threadStats_[threadIndex]-> emptyHistoryError -= numberOfRepresentedHistories_;
181184 } else {
182185 incrementalHistoryNumber = 1 + emptyHistoriesBetweenEachHistory_;
183186 }
184187 } else {
185188 incrementalHistoryNumber = 1 ;
186189 }
187- threadStats_[threadIndex]. historiesRead ++;
190+ threadStats_[threadIndex]-> historiesRead ++;
188191 }
189192
190- threadStats_[threadIndex]. totalHistoriesRead += incrementalHistoryNumber;
193+ threadStats_[threadIndex]-> totalHistoriesRead += incrementalHistoryNumber;
191194 }
192195
193196 particle.setIntProperty (IntPropertyType::INCREMENTAL_HISTORY_NUMBER, incrementalHistoryNumber);
@@ -213,27 +216,27 @@ namespace ParticleZoo {
213216 if (threadIndex >= readers_.size ()) {
214217 throw std::out_of_range (" Thread index out of range in getHistoriesRead()" );
215218 }
216- return threadStats_[threadIndex]. totalHistoriesRead ;
219+ return threadStats_[threadIndex]-> totalHistoriesRead ;
217220 }
218221
219222 std::uint64_t HistoryBalancedParallelReader::getParticlesRead (size_t threadIndex) const {
220223 if (threadIndex >= readers_.size ()) {
221224 throw std::out_of_range (" Thread index out of range in getParticlesRead()" );
222225 }
223- return threadStats_[threadIndex]. particlesRead ;
226+ return threadStats_[threadIndex]-> particlesRead ;
224227 }
225228
226229 std::uint64_t HistoryBalancedParallelReader::getTotalParticlesRead () const {
227230 std::uint64_t total = 0 ;
228231 for (size_t t = 0 ; t < readers_.size (); t++)
229- total += threadStats_[t]. particlesRead ;
232+ total += threadStats_[t]-> particlesRead ;
230233 return total;
231234 }
232235
233236 std::uint64_t HistoryBalancedParallelReader::getTotalHistoriesRead () const {
234237 std::uint64_t total = 0 ;
235238 for (size_t t = 0 ; t < readers_.size (); t++)
236- total += threadStats_[t]. totalHistoriesRead ;
239+ total += threadStats_[t]-> totalHistoriesRead ;
237240 return total;
238241 }
239242
0 commit comments