Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ChangeLog [ci-skip]
[simgrid.git] / src / simix / libsmx.cpp
1 /* libsmx.c - public interface to simix                                       */
2 /* --------                                                                   */
3 /* These functions are the only ones that are visible from the higher levels  */
4 /* (most of them simply add some documentation to the generated simcall body) */
5 /*                                                                            */
6 /* This is somehow the "libc" of SimGrid                                      */
7
8 /* Copyright (c) 2010-2022. The SimGrid Team. All rights reserved.          */
9
10 /* This program is free software; you can redistribute it and/or modify it
11  * under the terms of the license (GNU LGPL) which comes with this package. */
12
13 #include "src/kernel/EngineImpl.hpp"
14 #include "src/kernel/activity/CommImpl.hpp"
15 #include "src/kernel/actor/SimcallObserver.hpp"
16 #include <simgrid/s4u/Activity.hpp>
17
18 #define SIMIX_H_NO_DEPRECATED_WARNING // avoid deprecation warning on include (remove with XBT_ATTRIB_DEPRECATED_v335)
19 #include <simgrid/simix.h>
20
21 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(ker_simcall);
22
23 /**
24  * @ingroup simix_comm_management
25  */
26 void simcall_comm_send(simgrid::kernel::actor::ActorImpl* sender, simgrid::kernel::activity::MailboxImpl* mbox,
27                        double task_size, double rate, void* src_buff, size_t src_buff_size,
28                        bool (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
29                        void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t), void* data,
30                        double timeout) // XBT_ATTRIB_DEPRECATED_v335
31 {
32   xbt_assert(mbox, "No rendez-vous point defined for send");
33   simgrid::s4u::Comm::send(sender, mbox->get_iface(), task_size, rate, src_buff, src_buff_size, match_fun,
34                            copy_data_fun, data, timeout);
35 }
36
37 /**
38  * @ingroup simix_comm_management
39  */
40 simgrid::kernel::activity::ActivityImplPtr
41 simcall_comm_isend(simgrid::kernel::actor::ActorImpl* sender, simgrid::kernel::activity::MailboxImpl* mbox,
42                    double task_size, double rate, void* src_buff, size_t src_buff_size,
43                    bool (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*), void (*clean_fun)(void*),
44                    void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t), void* data,
45                    bool detached) // XBT_ATTRIB_DEPRECATED_v335
46 {
47   /* checking for infinite values */
48   xbt_assert(std::isfinite(task_size), "task_size is not finite!");
49   xbt_assert(std::isfinite(rate), "rate is not finite!");
50
51   xbt_assert(mbox, "No rendez-vous point defined for isend");
52
53   simgrid::kernel::actor::CommIsendSimcall observer(sender, mbox, task_size, rate,
54                                                     static_cast<unsigned char*>(src_buff), src_buff_size, match_fun,
55                                                     clean_fun, copy_data_fun, data, detached);
56   return simgrid::kernel::actor::simcall_answered(
57       [&observer] { return simgrid::kernel::activity::CommImpl::isend(&observer); });
58 }
59
60 /**
61  * @ingroup simix_comm_management
62  */
63 void simcall_comm_recv(simgrid::kernel::actor::ActorImpl* receiver, simgrid::kernel::activity::MailboxImpl* mbox,
64                        void* dst_buff, size_t* dst_buff_size,
65                        bool (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
66                        void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t), void* data,
67                        double timeout, double rate) // XBT_ATTRIB_DEPRECATED_v335
68 {
69   xbt_assert(mbox, "No rendez-vous point defined for recv");
70   simgrid::s4u::Comm::recv(receiver, mbox->get_iface(), dst_buff, dst_buff_size, match_fun, copy_data_fun, data,
71                            timeout, rate);
72 }
73
74 /**
75  * @ingroup simix_comm_management
76  */
77 simgrid::kernel::activity::ActivityImplPtr
78 simcall_comm_irecv(simgrid::kernel::actor::ActorImpl* receiver, simgrid::kernel::activity::MailboxImpl* mbox,
79                    void* dst_buff, size_t* dst_buff_size,
80                    bool (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
81                    void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t), void* data,
82                    double rate) // XBT_ATTRIB_DEPRECATED_v335
83 {
84   xbt_assert(mbox, "No rendez-vous point defined for irecv");
85
86   simgrid::kernel::actor::CommIrecvSimcall observer(receiver, mbox, static_cast<unsigned char*>(dst_buff),
87                                                     dst_buff_size, match_fun, copy_data_fun, data, rate);
88   return simgrid::kernel::actor::simcall_answered(
89       [&observer] { return simgrid::kernel::activity::CommImpl::irecv(&observer); });
90 }
91
92 /**
93  * @ingroup simix_comm_management
94  */
95 ssize_t simcall_comm_waitany(simgrid::kernel::activity::CommImpl* comms[], size_t count,
96                              double timeout) // XBT_ATTRIB_DEPRECATED_v335
97 {
98   std::vector<simgrid::kernel::activity::ActivityImpl*> activities;
99   for (size_t i = 0; i < count; i++)
100     activities.push_back(static_cast<simgrid::kernel::activity::ActivityImpl*>(comms[i]));
101   simgrid::kernel::actor::ActorImpl* issuer = simgrid::kernel::actor::ActorImpl::self();
102   simgrid::kernel::actor::ActivityWaitanySimcall observer{issuer, activities, timeout};
103   ssize_t changed_pos = simgrid::kernel::actor::simcall_blocking(
104       [&observer] {
105         simgrid::kernel::activity::ActivityImpl::wait_any_for(observer.get_issuer(), observer.get_activities(),
106                                                               observer.get_timeout());
107       },
108       &observer);
109   if (changed_pos != -1)
110     activities.at(changed_pos)->get_iface()->complete(simgrid::s4u::Activity::State::FINISHED);
111   return changed_pos;
112 }
113
114 /**
115  * @ingroup simix_comm_management
116  */
117 ssize_t simcall_comm_testany(simgrid::kernel::activity::CommImpl* comms[], size_t count) // XBT_ATTRIB_DEPRECATED_v335
118 {
119   if (count == 0)
120     return -1;
121   std::vector<simgrid::kernel::activity::ActivityImpl*> activities;
122   for (size_t i = 0; i < count; i++)
123     activities.push_back(static_cast<simgrid::kernel::activity::ActivityImpl*>(comms[i]));
124
125   simgrid::kernel::actor::ActorImpl* issuer = simgrid::kernel::actor::ActorImpl::self();
126   simgrid::kernel::actor::ActivityTestanySimcall observer{issuer, activities};
127   ssize_t changed_pos = simgrid::kernel::actor::simcall_blocking(
128       [&observer] {
129         simgrid::kernel::activity::ActivityImpl::test_any(observer.get_issuer(), observer.get_activities());
130       },
131       &observer);
132   if (changed_pos != -1)
133     comms[changed_pos]->get_iface()->complete(simgrid::s4u::Activity::State::FINISHED);
134   return changed_pos;
135 }
136
137 /**
138  * @ingroup simix_comm_management
139  */
140 void simcall_comm_wait(simgrid::kernel::activity::ActivityImpl* comm, double timeout) // XBT_ATTRIB_DEPRECATED_v335
141 {
142   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
143   simgrid::kernel::actor::ActorImpl* issuer = simgrid::kernel::actor::ActorImpl::self();
144   simgrid::kernel::actor::simcall_blocking([issuer, comm, timeout] { comm->wait_for(issuer, timeout); });
145 }
146
147 /**
148  * @ingroup simix_comm_management
149  *
150  */
151 bool simcall_comm_test(simgrid::kernel::activity::ActivityImpl* comm) // XBT_ATTRIB_DEPRECATED_v335
152 {
153   simgrid::kernel::actor::ActorImpl* issuer = simgrid::kernel::actor::ActorImpl::self();
154   simgrid::kernel::actor::ActivityTestSimcall observer{issuer, comm};
155   if (simgrid::kernel::actor::simcall_blocking([&observer] { observer.get_activity()->test(observer.get_issuer()); },
156                                                &observer)) {
157     comm->get_iface()->complete(simgrid::s4u::Activity::State::FINISHED);
158     return true;
159   }
160   return false;
161 }
162
163 static void simcall(simgrid::kernel::actor::Simcall::Type call, std::function<void()> const& code,
164                     simgrid::kernel::actor::SimcallObserver* observer)
165 {
166   auto self = simgrid::kernel::actor::ActorImpl::self();
167   self->simcall_.call_ = call;
168   self->simcall_.code_ = &code;
169   self->simcall_.observer_ = observer;
170   if (simgrid::kernel::EngineImpl::get_instance()->is_maestro(self)) {
171     self->simcall_handle(0);
172   } else {
173     XBT_DEBUG("Yield process '%s' on simcall %s", self->get_cname(), self->simcall_.get_cname());
174     self->yield();
175   }
176   self->simcall_.observer_ = nullptr;
177 }
178
179 void simcall_run_answered(std::function<void()> const& code, simgrid::kernel::actor::SimcallObserver* observer)
180 {
181   // The function `code` is called in kernel mode (either because we are already in maestor or after a context switch)
182   // and simcall_answer() is called
183   simcall(simgrid::kernel::actor::Simcall::Type::RUN_ANSWERED, code, observer);
184 }
185
186 void simcall_run_blocking(std::function<void()> const& code, simgrid::kernel::actor::SimcallObserver* observer)
187 {
188   // The function `code` is called in kernel mode (either because we are already in maestor or after a context switch)
189   // BUT simcall_answer IS NOT CALLED
190   simcall(simgrid::kernel::actor::Simcall::Type::RUN_BLOCKING, code, observer);
191 }