forked from celeritas-project/celeritas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StepDiagnostic.cc
161 lines (139 loc) · 4.91 KB
/
StepDiagnostic.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
//----------------------------------*-C++-*----------------------------------//
// Copyright 2023-2024 UT-Battelle, LLC, and other Celeritas developers.
// See the top-level COPYRIGHT file for details.
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file celeritas/user/StepDiagnostic.cc
//---------------------------------------------------------------------------//
#include "StepDiagnostic.hh"
#include <type_traits>
#include <utility>
#include "celeritas_config.h"
#include "corecel/cont/Range.hh"
#include "corecel/cont/Span.hh"
#include "corecel/data/CollectionAlgorithms.hh"
#include "corecel/io/JsonPimpl.hh"
#include "celeritas/global/ActionLauncher.hh"
#include "celeritas/global/CoreParams.hh"
#include "celeritas/global/CoreState.hh"
#include "celeritas/global/TrackExecutor.hh"
#include "celeritas/phys/ParticleParams.hh" // IWYU pragma: keep
#include "ParticleTallyData.hh"
#include "detail/StepDiagnosticExecutor.hh"
#if CELERITAS_USE_JSON
# include <nlohmann/json.hpp>
# include "corecel/io/LabelIO.json.hh"
#endif
namespace celeritas
{
//---------------------------------------------------------------------------//
/*!
* Construct with particle data.
*/
StepDiagnostic::StepDiagnostic(ActionId id,
SPConstParticle particle,
size_type max_steps,
size_type num_streams)
: id_(id), num_streams_(num_streams)
{
CELER_EXPECT(id_);
CELER_EXPECT(particle);
CELER_EXPECT(max_steps > 0);
CELER_EXPECT(num_streams_ > 0);
// Add two extra bins for underflow and overflow
HostVal<ParticleTallyParamsData> host_params;
host_params.num_bins = max_steps + 2;
host_params.num_particles = particle->size();
store_ = {std::move(host_params), num_streams_};
CELER_ENSURE(store_);
}
//---------------------------------------------------------------------------//
//! Default destructor
StepDiagnostic::~StepDiagnostic() = default;
//---------------------------------------------------------------------------//
/*!
* Execute action with host data.
*/
void StepDiagnostic::execute(CoreParams const& params,
CoreStateHost& state) const
{
auto execute = make_active_track_executor(
params.ptr<MemSpace::native>(),
state.ptr(),
detail::StepDiagnosticExecutor{
store_.params<MemSpace::native>(),
store_.state<MemSpace::native>(state.stream_id(),
this->state_size())});
return launch_action(*this, params, state, execute);
}
//---------------------------------------------------------------------------//
#if !CELER_USE_DEVICE
void StepDiagnostic::execute(CoreParams const&, CoreStateDevice&) const
{
CELER_NOT_CONFIGURED("CUDA OR HIP");
}
#endif
//---------------------------------------------------------------------------//
/*!
* Get a long description of the action.
*/
std::string_view StepDiagnostic::description() const
{
return "accumulate total step counters";
}
//---------------------------------------------------------------------------//
/*!
* Write output to the given JSON object.
*/
void StepDiagnostic::output(JsonPimpl* j) const
{
#if CELERITAS_USE_JSON
using json = nlohmann::json;
auto obj = json::object();
obj["steps"] = this->calc_steps();
obj["_index"] = {"particle", "num_steps"};
j->obj = std::move(obj);
#else
CELER_DISCARD(j);
#endif
}
//---------------------------------------------------------------------------//
/*!
* Get the diagnostic results accumulated over all streams.
*/
auto StepDiagnostic::calc_steps() const -> VecVecCount
{
// Get the raw data accumulated over all host/device streams
std::vector<size_type> counts(this->state_size(), 0);
accumulate_over_streams(
store_, [](auto& state) { return state.counts; }, &counts);
auto const& params = store_.params<MemSpace::host>();
VecVecCount result(params.num_particles);
for (auto i : range(result.size()))
{
auto start = counts.begin() + i * params.num_bins;
CELER_ASSERT(start + params.num_bins <= counts.end());
result[i] = {start, start + params.num_bins};
}
return result;
}
//---------------------------------------------------------------------------//
/*!
* Size of diagnostic state data (number of bins times number of particles)
*/
size_type StepDiagnostic::state_size() const
{
auto const& params = store_.params<MemSpace::host>();
return params.num_bins * params.num_particles;
}
//---------------------------------------------------------------------------//
/*!
* Reset diagnostic results.
*/
void StepDiagnostic::clear()
{
apply_to_all_streams(
store_, [](auto& state) { fill(size_type(0), &state.counts); });
}
//---------------------------------------------------------------------------//
} // namespace celeritas