XBT_LOG_NEW_DEFAULT_CATEGORY(task_storm, "Messages specific for this s4u example");
-struct Token {
- double data_ = 0;
- Token(double data) : data_(data) {}
-};
-
int main(int argc, char* argv[])
{
simgrid::s4u::Engine e(&argc, argv);
}
std::vector<double> amount = {1e3,1e6,1e9};
comm->set_amount(amount[count % 3]);
- auto token = std::make_shared<Token>(amount[count % 3]);
+ auto token = std::make_shared<simgrid::plugins::Token>();
+ token->set_data(new double(amount[count % 3]));
t->set_token(token);
});
// The token sent by SA is forwarded by both communication tasks
SA_to_B1->on_this_start_cb([&](simgrid::plugins::Task* t) {
- t->set_token(t->get_next_execution_tokens()[SA]);
+ t->set_token(t->get_next_token_from(SA));
});
SA_to_B2->on_this_start_cb([&](simgrid::plugins::Task* t) {
- t->set_token(t->get_next_execution_tokens()[SA]);
+ t->set_token(t->get_next_token_from(SA));
});
/* B1 and B2 read the value of the token received by their predecessors
and use it to adapt their amount of work to do.
*/
B1->on_this_start_cb([&](simgrid::plugins::Task* t) {
- auto tokens_map = t->get_next_execution_tokens();
- Token* tok = (Token*)(tokens_map[SA_to_B1].get());
- t->set_amount(tok->data_ * 10);
+ auto data = t->get_next_token_from(SA_to_B1)->get_data<double>();
+ t->set_amount(*data * 10);
+ delete data;
});
B2->on_this_start_cb([&](simgrid::plugins::Task* t) {
- auto tokens_map = t->get_next_execution_tokens();
- Token* tok = (Token*)(tokens_map[SA_to_B2].get());
- t->set_amount(tok->data_ * 10);
+ auto data = t->get_next_token_from(SA_to_B2)->get_data<double>();
+ t->set_amount(*data * 10);
+ delete data;
});
// Enqueue executions for tasks without predecessors
Task* task_;
};
+class XBT_PUBLIC Token : public xbt::Extendable<Token> {};
+
class Task {
std::set<Task*> successors_ = {};
std::map<Task*, unsigned int> predecessors_ = {};
protected:
std::string name_;
double amount_;
- std::shared_ptr<void> token_ = NULL;
- std::deque<std::map<TaskPtr, std::shared_ptr<void>>> tokens_received_;
+ std::shared_ptr<Token> token_ = nullptr;
+ std::deque<std::map<TaskPtr, std::shared_ptr<Token>>> tokens_received_;
int queued_execs_ = 0;
int count_ = 0;
bool working_ = false;
void enqueue_execs(int n);
void set_amount(double amount);
double get_amount() const { return amount_; }
- void set_token(std::shared_ptr<void> token);
- std::map<TaskPtr, std::shared_ptr<void>> get_next_execution_tokens() const;
+ void set_token(std::shared_ptr<Token> token);
+ std::shared_ptr<Token> get_next_token_from(TaskPtr t);
void add_successor(TaskPtr t);
void remove_successor(TaskPtr t);
void remove_all_successors();
+#include <memory>
#include <simgrid/Exception.hpp>
#include <simgrid/plugins/task.hpp>
#include <simgrid/s4u/Comm.hpp>
* @brief Set the token to send to successors.
* @note The token is passed to each successor after the task end, i.e., after the on_end callback.
*/
-void Task::set_token(std::shared_ptr<void> token)
+void Task::set_token(std::shared_ptr<Token> token)
{
simgrid::kernel::actor::simcall_answered([this, token] { token_ = token; });
}
* @return Map of tokens received for the next execution.
* @note If there is no queued execution for this task the map might not exist or be partially empty.
*/
-std::map<TaskPtr, std::shared_ptr<void>> Task::get_next_execution_tokens() const
+std::shared_ptr<Token> Task::get_next_token_from(TaskPtr t)
{
- return tokens_received_.front();
+ return tokens_received_.front()[t];
}
/** @ingroup plugin_task