Repository navigation
Expand file tree
/
Copy pathEngine.cpp
More file actions
169 lines (143 loc) · 6.48 KB
/
Copy pathEngine.cpp
File metadata and controls
169 lines (143 loc) · 6.48 KB
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
/* Copyright (c) 2022-2026. The SWAT Team. All rights reserved. */
/* This program is free software; you can redistribute it and/or modify it
* under the terms of the license (GNU LGPL) which comes with this package. */
#include <simgrid/s4u/Actor.hpp>
#include <simgrid/s4u/MessageQueue.hpp>
#include "dtlmod/DTL.hpp"
#include "dtlmod/FileTransport.hpp"
XBT_LOG_NEW_DEFAULT_SUBCATEGORY(dtlmod_engine, dtlmod, "DTL logging about Engines");
namespace sg4 = simgrid::s4u;
namespace dtlmod {
/// \cond EXCLUDE_FROM_DOCUMENTATION
Engine::Engine(const std::string& name, std::shared_ptr<Stream> stream, Type type)
: name_(name), type_(type), stream_(stream), simulate_memory_copy_(stream->should_simulate_memory_copy())
{
}
/// \endcond
////////////////////////////////////////////
///////////// PUBLIC INTERFACE /////////////
////////////////////////////////////////////
/// All put and get operations must take place within a transaction (in a sense close to that used for databases).
/// When multiple actors have opened the same Stream and thus subscribed to the same Engine, only one of them has to
/// do the following when a transaction begins, this function is a no-op for the other subscribers:
/// 1. if no transaction is currently in progress, start one, exit otherwise
/// 2. if this is the first transaction for that Engine, create a synchronization barrier among all the subscribers.
/// 3. Otherwise, wait for the completion of the simulated activities started by the previous transaction.
void Engine::begin_transaction()
{
publishers_.contains(sg4::Actor::self()) ? begin_pub_transaction() : begin_sub_transaction();
}
/// The actual data transport is delegated to the Transport method associated to the Engine.
void Engine::put(const std::shared_ptr<Variable>& var) const
{
size_t size;
if (var->is_reduced()) {
// Perform an Exec activity before putting the variable into the DTL to account for the time needed to reduce it.
sg4::this_actor::execute(var->get_reduction_method()->get_flop_amount_to_reduce_variable(*var));
XBT_DEBUG("Variable %s has been reduced!", var->get_cname());
// Now put the reduced version of the variable into the DTL, i.e., using its reduced local size.
size = var->get_reduction_method()->get_reduced_variable_local_size(*var, get_current_transaction());
XBT_DEBUG("Put this reduced version of %s (initial size = %zu, reduced size = %zu)", var->get_cname(),
var->get_local_size(), size);
} else {
size = var->get_local_size();
}
if (simulate_memory_copy_)
simulate_memory_copy_overhead(size);
transport_->put(var, size);
}
void Engine::put(const std::shared_ptr<Variable>& var, size_t simulated_size_in_bytes) const
{
if (simulate_memory_copy_)
simulate_memory_copy_overhead(simulated_size_in_bytes);
transport_->put(var, simulated_size_in_bytes);
}
/// The actual data transport is delegated to the Transport method associated to the Engine.
void Engine::get(const std::shared_ptr<Variable>& var) const
{
if (var->is_reduced() && var->is_reduced_by_subscriber()) {
var->get_reduction_method()->reduce_variable(*var);
// Perform an Exec activity before getting the variable for the DTL to account for the time needed to reduce it.
sg4::this_actor::execute(var->get_reduction_method()->get_flop_amount_to_reduce_variable(*var));
}
transport_->get(var);
// Decompression cost after receiving compressed data (e.g., publisher-side compression)
if (var->is_reduced()) {
double decompression_flops = var->get_reduction_method()->get_flop_amount_to_decompress_variable(*var);
if (decompression_flops > 0)
sg4::this_actor::execute(decompression_flops);
}
}
/// This function first synchronizes all the subscribers thanks to the internal barrier. When the last subscriber
/// enters the barrier, all the simulated activities registered for the current transaction are started.
///
/// Then it marks the transaction as done.
void Engine::end_transaction()
{
publishers_.contains(sg4::Actor::self()) ? end_pub_transaction() : end_sub_transaction();
}
/// This function is called by all the actors that have opened that Stream. The first subscriber to enter that
/// function waits for the completion of the activities of last transaction on that Engine. Then all the subscribers
/// are synchronized before the Engine is properly closed (and destroyed).
void Engine::close()
{
publishers_.contains(sg4::Actor::self()) ? pub_close() : sub_close();
}
void Engine::cancel_transaction(unsigned int transaction_id)
{
transaction_id = get_transaction_to_cancel(transaction_id);
// No-op if both sides have already moved past the target transaction: cancelling now would
// affect the wrong (subsequent) transaction.
if (get_current_transaction_impl() > transaction_id && get_current_sub_transaction_impl() > transaction_id)
return;
// transaction_id == 0 means no transaction has started yet; treat as T1 so all checks against
// (current + 1 >= 1) still fire correctly.
canceled_transaction_id_.store(transaction_id == 0 ? 1 : transaction_id);
cancel_activities();
}
////////////////////////////////////////////
///////////////// INTERNALS ////////////////
////////////////////////////////////////////
/// \cond EXCLUDE_FROM_DOCUMENTATION
void Engine::cancel_pending_activities(sg4::ActivitySet& activities)
{
std::vector<sg4::ActivityPtr> snapshot;
snapshot.reserve(activities.size());
for (int i = 0; i < activities.size(); i++)
snapshot.push_back(activities.at(i));
for (const auto& activity : snapshot)
if (activity->get_state() == sg4::Activity::State::STARTED)
activity->cancel();
}
void Engine::drain(sg4::ActivitySet& activities)
{
cancel_pending_activities(activities);
activities.clear();
}
void Engine::add_publisher(sg4::ActorPtr actor)
{
pub_ever_present_ = true;
transport_->add_publisher(publishers_.count());
publishers_.add(actor);
}
void Engine::add_subscriber(sg4::ActorPtr actor)
{
transport_->add_subscriber(subscribers_.count());
subscribers_.add(actor);
}
void Engine::close_stream() const
{
if (auto s = stream_.lock())
s->close();
}
/// Model the cost of a local memory copy as a loopback network transfer on the host of the calling actor. The cost
/// is governed by the loopback link's bandwidth/latency, so it is tuned like any other link in the platform.
void Engine::simulate_memory_copy_overhead(size_t bytes) const
{
if (bytes == 0)
return;
auto* host = sg4::Host::current();
sg4::Comm::sendto(host, host, bytes);
}
/// \endcond
} // namespace dtlmod