forked from celeritas-project/celeritas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ActionSequence.cc
157 lines (140 loc) · 5.3 KB
/
ActionSequence.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
//----------------------------------*-C++-*----------------------------------//
// Copyright 2022-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/global/detail/ActionSequence.cc
//---------------------------------------------------------------------------//
#include "ActionSequence.hh"
#include <algorithm>
#include <tuple>
#include <type_traits>
#include <utility>
#include "corecel/device_runtime_api.h"
#include "corecel/Types.hh"
#include "corecel/cont/EnumArray.hh"
#include "corecel/cont/Range.hh"
#include "corecel/sys/Device.hh"
#include "corecel/sys/ScopedProfiling.hh"
#include "corecel/sys/Stopwatch.hh"
#include "corecel/sys/Stream.hh"
#include "celeritas/global/CoreParams.hh"
#include "../ActionInterface.hh"
#include "../ActionRegistry.hh"
#include "../CoreState.hh"
namespace celeritas
{
namespace detail
{
//---------------------------------------------------------------------------//
/*!
* Construct from an action registry and sequence options.
*/
template<class Params>
ActionSequence<Params>::ActionSequence(ActionRegistry const& reg,
Options options)
: options_(std::move(options))
{
actions_.reserve(reg.num_actions());
// Loop over all action IDs
for (auto aidx : range(reg.num_actions()))
{
// Get abstract action shared pointer and see if it's explicit
auto const& base = reg.action(ActionId{aidx});
using element_type = typename SPConstSpecializedExplicit::element_type;
if (auto expl = std::dynamic_pointer_cast<element_type>(base))
{
// Add explicit action to our array
actions_.push_back(std::move(expl));
}
}
begin_run_.reserve(reg.mutable_actions().size());
// Loop over all mutable actions
for (auto const& base : reg.mutable_actions())
{
if (auto brun = std::dynamic_pointer_cast<BeginRunActionInterface>(base))
{
// Add beginning-of-run to the array
begin_run_.push_back(std::move(brun));
}
}
// Sort actions by increasing order (and secondarily, increasing IDs)
std::sort(actions_.begin(),
actions_.end(),
[](SPConstSpecializedExplicit const& a,
SPConstSpecializedExplicit const& b) {
return std::make_tuple(a->order(), a->action_id())
< std::make_tuple(b->order(), b->action_id());
});
// Initialize timing
accum_time_.resize(actions_.size());
CELER_ENSURE(actions_.size() == accum_time_.size());
}
//---------------------------------------------------------------------------//
/*!
* Initialize actions and states.
*/
template<class Params>
template<MemSpace M>
void ActionSequence<Params>::begin_run(Params const& params, State<M>& state)
{
for (auto const& sp_action : begin_run_)
{
ScopedProfiling profile_this{std::string{sp_action->label()}};
sp_action->begin_run(params, state);
}
}
//---------------------------------------------------------------------------//
/*!
* Call all explicit actions with host or device data.
*/
template<class Params>
template<MemSpace M>
void ActionSequence<Params>::execute(Params const& params, State<M>& state)
{
[[maybe_unused]] Stream::StreamT stream = nullptr;
if (M == MemSpace::device && options_.sync)
{
stream = celeritas::device().stream(state.stream_id()).get();
}
if ((M == MemSpace::host || options_.sync) && !state.warming_up())
{
// Execute all actions and record the time elapsed
for (auto i : range(actions_.size()))
{
ScopedProfiling profile_this{std::string{actions_[i]->label()}};
Stopwatch get_time;
actions_[i]->execute(params, state);
if (M == MemSpace::device)
{
CELER_DEVICE_CALL_PREFIX(StreamSynchronize(stream));
}
accum_time_[i] += get_time();
}
}
else
{
// Just loop over the actions
for (auto const& sp_action : actions_)
{
ScopedProfiling profile_this{std::string{sp_action->label()}};
sp_action->execute(params, state);
}
}
}
//---------------------------------------------------------------------------//
// Explicit template instantiation
//---------------------------------------------------------------------------//
template class ActionSequence<CoreParams>;
template void ActionSequence<CoreParams>::begin_run(CoreParams const&,
State<MemSpace::host>&);
template void ActionSequence<CoreParams>::begin_run(CoreParams const&,
State<MemSpace::device>&);
template void
ActionSequence<CoreParams>::execute(CoreParams const&, State<MemSpace::host>&);
template void ActionSequence<CoreParams>::execute(CoreParams const&,
State<MemSpace::device>&);
// TODO: add explicit template instantiation of execute for optical data
//---------------------------------------------------------------------------//
} // namespace detail
} // namespace celeritas