Skip to content

Commit 9bf069e

Browse files
authored
Merge pull request #2270 from pshriwise/vol-lrg-samples
Update volume calc types to mitigate overflow issues
2 parents dc0731c + 93065d1 commit 9bf069e

File tree

3 files changed

+37
-19
lines changed

3 files changed

+37
-19
lines changed

include/openmc/constants.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#define OPENMC_CONSTANTS_H
66

77
#include <cmath>
8+
#include <cstdint>
89
#include <limits>
910

1011
#include "openmc/array.h"
@@ -339,6 +340,11 @@ enum class RunMode {
339340

340341
enum class GeometryType { CSG, DAG };
341342

343+
//==============================================================================
344+
// Volume Calculation Constants
345+
346+
constexpr uint64_t UINT64_T_MAX {std::numeric_limits<uint64_t>::max()};
347+
342348
} // namespace openmc
343349

344350
#endif // OPENMC_CONSTANTS_H

include/openmc/volume_calc.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#ifndef OPENMC_VOLUME_CALC_H
22
#define OPENMC_VOLUME_CALC_H
33

4+
#include <cstdint>
5+
#include <string>
6+
47
#include "openmc/array.h"
58
#include "openmc/position.h"
69
#include "openmc/tallies/trigger.h"
@@ -10,7 +13,6 @@
1013
#include "xtensor/xtensor.hpp"
1114

1215
#include <gsl/gsl-lite.hpp>
13-
#include <string>
1416

1517
namespace openmc {
1618

@@ -69,7 +71,8 @@ class VolumeCalculation {
6971
//! \param[in] i_material Index in global materials vector
7072
//! \param[in,out] indices Vector of material indices
7173
//! \param[in,out] hits Number of hits corresponding to each material
72-
void check_hit(int i_material, vector<int>& indices, vector<int>& hits) const;
74+
void check_hit(
75+
int i_material, vector<uint64_t>& indices, vector<uint64_t>& hits) const;
7376
};
7477

7578
//==============================================================================

src/volume_calc.cpp

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,16 @@ vector<VolumeCalculation::Result> VolumeCalculation::execute() const
9999
{
100100
// Shared data that is collected from all threads
101101
int n = domain_ids_.size();
102-
vector<vector<int>> master_indices(
102+
vector<vector<uint64_t>> master_indices(
103103
n); // List of material indices for each domain
104-
vector<vector<int>> master_hits(
104+
vector<vector<uint64_t>> master_hits(
105105
n); // Number of hits for each material in each domain
106106
int iterations = 0;
107107

108108
// Divide work over MPI processes
109-
size_t min_samples = n_samples_ / mpi::n_procs;
110-
size_t remainder = n_samples_ % mpi::n_procs;
111-
size_t i_start, i_end;
109+
uint64_t min_samples = n_samples_ / mpi::n_procs;
110+
uint64_t remainder = n_samples_ % mpi::n_procs;
111+
uint64_t i_start, i_end;
112112
if (mpi::rank < remainder) {
113113
i_start = (min_samples + 1) * mpi::rank;
114114
i_end = i_start + min_samples + 1;
@@ -123,14 +123,14 @@ vector<VolumeCalculation::Result> VolumeCalculation::execute() const
123123
#pragma omp parallel
124124
{
125125
// Variables that are private to each thread
126-
vector<vector<int>> indices(n);
127-
vector<vector<int>> hits(n);
126+
vector<vector<uint64_t>> indices(n);
127+
vector<vector<uint64_t>> hits(n);
128128
Particle p;
129129

130130
// Sample locations and count hits
131131
#pragma omp for
132132
for (size_t i = i_start; i < i_end; i++) {
133-
int64_t id = iterations * n_samples_ + i;
133+
uint64_t id = iterations * n_samples_ + i;
134134
uint64_t seed = init_seed(id, STREAM_VOLUME);
135135

136136
p.n_coord() = 1;
@@ -223,7 +223,15 @@ vector<VolumeCalculation::Result> VolumeCalculation::execute() const
223223
// bump iteration counter and get total number
224224
// of samples at this point
225225
iterations++;
226-
size_t total_samples = iterations * n_samples_;
226+
uint64_t total_samples = iterations * n_samples_;
227+
228+
// warn user if total sample size is greater than what the size_t type can
229+
// represent
230+
if (total_samples == UINT64_T_MAX) {
231+
warning("The number of samples has exceeded the type used to track hits. "
232+
"Volume "
233+
"results may be inaccurate.");
234+
}
227235

228236
// reset
229237
double trigger_val = -INFTY;
@@ -246,10 +254,11 @@ vector<VolumeCalculation::Result> VolumeCalculation::execute() const
246254
if (mpi::master) {
247255
for (int j = 1; j < mpi::n_procs; j++) {
248256
int q;
257+
// retrieve results
249258
MPI_Recv(
250-
&q, 1, MPI_INTEGER, j, 2 * j, mpi::intracomm, MPI_STATUS_IGNORE);
251-
vector<int> buffer(2 * q);
252-
MPI_Recv(buffer.data(), 2 * q, MPI_INTEGER, j, 2 * j + 1,
259+
&q, 1, MPI_UINT64_T, j, 2 * j, mpi::intracomm, MPI_STATUS_IGNORE);
260+
vector<uint64_t> buffer(2 * q);
261+
MPI_Recv(buffer.data(), 2 * q, MPI_UINT64_T, j, 2 * j + 1,
253262
mpi::intracomm, MPI_STATUS_IGNORE);
254263
for (int k = 0; k < q; ++k) {
255264
bool already_added = false;
@@ -268,20 +277,20 @@ vector<VolumeCalculation::Result> VolumeCalculation::execute() const
268277
}
269278
} else {
270279
int q = master_indices[i_domain].size();
271-
vector<int> buffer(2 * q);
280+
vector<uint64_t> buffer(2 * q);
272281
for (int k = 0; k < q; ++k) {
273282
buffer[2 * k] = master_indices[i_domain][k];
274283
buffer[2 * k + 1] = master_hits[i_domain][k];
275284
}
276285

277-
MPI_Send(&q, 1, MPI_INTEGER, 0, 2 * mpi::rank, mpi::intracomm);
278-
MPI_Send(buffer.data(), 2 * q, MPI_INTEGER, 0, 2 * mpi::rank + 1,
286+
MPI_Send(&q, 1, MPI_UINT64_T, 0, 2 * mpi::rank, mpi::intracomm);
287+
MPI_Send(buffer.data(), 2 * q, MPI_UINT64_T, 0, 2 * mpi::rank + 1,
279288
mpi::intracomm);
280289
}
281290
#endif
282291

283292
if (mpi::master) {
284-
int total_hits = 0;
293+
size_t total_hits = 0;
285294
for (int j = 0; j < master_indices[i_domain].size(); ++j) {
286295
total_hits += master_hits[i_domain][j];
287296
double f =
@@ -464,7 +473,7 @@ void VolumeCalculation::to_hdf5(
464473
}
465474

466475
void VolumeCalculation::check_hit(
467-
int i_material, vector<int>& indices, vector<int>& hits) const
476+
int i_material, vector<uint64_t>& indices, vector<uint64_t>& hits) const
468477
{
469478

470479
// Check if this material was previously hit and if so, increment count

0 commit comments

Comments
 (0)