forked from celeritas-project/celeritas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Transporter.cc
191 lines (171 loc) · 5.93 KB
/
Transporter.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//----------------------------------*-C++-*----------------------------------//
// Copyright 2021-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 celer-sim/Transporter.cc
//---------------------------------------------------------------------------//
#include "Transporter.hh"
#include <csignal>
#include <memory>
#include <utility>
#include "corecel/Assert.hh"
#include "corecel/cont/Range.hh"
#include "corecel/data/Ref.hh"
#include "corecel/io/Logger.hh"
#include "corecel/io/ScopedTimeLog.hh"
#include "corecel/sys/ScopedSignalHandler.hh"
#include "corecel/sys/Stopwatch.hh"
#include "celeritas/Types.hh"
#include "celeritas/global/CoreParams.hh"
#include "celeritas/global/Stepper.hh"
#include "celeritas/global/detail/ActionSequence.hh"
#include "celeritas/grid/VectorUtils.hh"
#include "celeritas/phys/Model.hh"
namespace celeritas
{
namespace app
{
//---------------------------------------------------------------------------//
//! Default virtual destructor
TransporterBase::~TransporterBase() = default;
//---------------------------------------------------------------------------//
/*!
* Construct from persistent problem data.
*/
template<MemSpace M>
Transporter<M>::Transporter(TransporterInput inp)
: max_steps_(inp.max_steps)
, num_streams_(inp.params->max_streams())
, store_track_counts_(inp.store_track_counts)
, store_step_times_(inp.store_step_times)
{
CELER_EXPECT(inp);
// Create stepper
CELER_LOG_LOCAL(status) << "Creating states";
StepperInput step_input;
step_input.params = inp.params;
step_input.num_track_slots = inp.num_track_slots;
step_input.stream_id = inp.stream_id;
step_input.sync = inp.sync;
stepper_ = std::make_shared<Stepper<M>>(std::move(step_input));
}
//---------------------------------------------------------------------------//
/*!
* Run a single step with no active states to "warm up".
*
* This is to reduce the uncertainty in timing for problems, especially on AMD
* hardware.
*/
template<MemSpace M>
void Transporter<M>::operator()()
{
CELER_LOG(status) << "Warming up";
ScopedTimeLog scoped_time;
StepperResult step_counts = (*stepper_)();
CELER_ENSURE(step_counts.alive == 0);
}
//---------------------------------------------------------------------------//
/*!
* Transport the input primaries and all secondaries produced.
*/
template<MemSpace M>
auto Transporter<M>::operator()(SpanConstPrimary primaries)
-> TransporterResult
{
// Initialize results
TransporterResult result;
auto append_track_counts = [&result](StepperResult const& track_counts) {
result.initializers.push_back(track_counts.queued);
result.active.push_back(track_counts.active);
result.alive.push_back(track_counts.alive);
};
constexpr size_type min_alloc{65536};
result.initializers.reserve(std::min(min_alloc, max_steps_));
result.active.reserve(std::min(min_alloc, max_steps_));
result.alive.reserve(std::min(min_alloc, max_steps_));
if (store_step_times_)
{
result.step_times.reserve(std::min(min_alloc, max_steps_));
}
// Abort cleanly for interrupt and user-defined signals
#ifndef _WIN32
ScopedSignalHandler interrupted{SIGINT, SIGUSR2};
#else
ScopedSignalHandler interrupted{SIGINT};
#endif
CELER_LOG_LOCAL(status)
<< "Transporting " << primaries.size() << " primaries";
Stopwatch get_step_time;
size_type remaining_steps = max_steps_;
auto& step = *stepper_;
// Copy primaries to device and transport the first step
auto track_counts = step(primaries);
if (store_track_counts_)
{
append_track_counts(track_counts);
}
if (store_step_times_)
{
result.step_times.push_back(get_step_time());
}
while (track_counts)
{
if (CELER_UNLIKELY(--remaining_steps == 0))
{
CELER_LOG_LOCAL(error) << "Exceeded step count of " << max_steps_
<< ": aborting transport loop";
break;
}
if (CELER_UNLIKELY(interrupted()))
{
CELER_LOG_LOCAL(error)
<< "Caught interrupt signal: aborting transport "
"loop";
interrupted = {};
break;
}
get_step_time = {};
track_counts = step();
if (store_track_counts_)
{
append_track_counts(track_counts);
}
if (store_step_times_)
{
result.step_times.push_back(get_step_time());
}
}
result.num_track_slots = stepper_->state().size();
return result;
}
//---------------------------------------------------------------------------//
/*!
* Transport the input primaries and all secondaries produced.
*/
template<MemSpace M>
void Transporter<M>::accum_action_times(MapStrDouble* result) const
{
// Get kernel timing if running with a single stream and if either on the
// device with synchronization enabled or on the host
auto const& step = *stepper_;
auto const& action_seq = step.actions();
if (M == MemSpace::host || action_seq.sync())
{
auto const& action_ptrs = action_seq.actions();
auto const& times = action_seq.accum_time();
CELER_ASSERT(action_ptrs.size() == times.size());
for (auto i : range(action_ptrs.size()))
{
(*result)[std::string{action_ptrs[i]->label()}] += times[i];
}
}
}
//---------------------------------------------------------------------------//
// EXPLICIT INSTANTIATION
//---------------------------------------------------------------------------//
template class Transporter<MemSpace::host>;
template class Transporter<MemSpace::device>;
//---------------------------------------------------------------------------//
} // namespace app
} // namespace celeritas