Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
1236119f4107dc003ed7fe36020bc09095cf5ce7
[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 "mc/mc.h"
14 #include "src/kernel/activity/CommImpl.hpp"
15 #include "src/kernel/activity/ConditionVariableImpl.hpp"
16 #include "src/kernel/activity/MutexImpl.hpp"
17 #include "src/kernel/activity/SemaphoreImpl.hpp"
18 #include "src/kernel/actor/SimcallObserver.hpp"
19 #include "src/mc/mc_replay.hpp"
20 #include "xbt/random.hpp"
21
22 #include "popping_bodies.cpp"
23
24 #include <boost/core/demangle.hpp>
25 #include <string>
26 #include <typeinfo>
27
28 /**
29  * @ingroup simix_comm_management
30  */
31 void simcall_comm_send(smx_actor_t sender, smx_mailbox_t mbox, double task_size, double rate, void* src_buff,
32                        size_t src_buff_size, bool (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
33                        void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t), void* data,
34                        double timeout)
35 {
36   /* checking for infinite values */
37   xbt_assert(std::isfinite(task_size), "task_size is not finite!");
38   xbt_assert(std::isfinite(rate), "rate is not finite!");
39   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
40
41   xbt_assert(mbox, "No rendez-vous point defined for send");
42
43   if (MC_is_active() || MC_record_replay_is_active()) {
44     /* the model-checker wants two separate simcalls */
45     simgrid::kernel::activity::ActivityImplPtr comm =
46         nullptr; /* MC needs the comm to be set to nullptr during the simcall */
47     comm = simcall_comm_isend(sender, mbox, task_size, rate, src_buff, src_buff_size, match_fun, nullptr, copy_data_fun,
48                               data, false);
49     simcall_comm_wait(comm.get(), timeout);
50     comm = nullptr;
51   }
52   else {
53     simcall_BODY_comm_send(sender, mbox, task_size, rate, static_cast<unsigned char*>(src_buff), src_buff_size,
54                            match_fun, copy_data_fun, data, timeout);
55   }
56 }
57
58 /**
59  * @ingroup simix_comm_management
60  */
61 simgrid::kernel::activity::ActivityImplPtr
62 simcall_comm_isend(smx_actor_t sender, smx_mailbox_t mbox, double task_size, double rate, void* src_buff,
63                    size_t src_buff_size, bool (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
64                    void (*clean_fun)(void*), void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t),
65                    void* data, bool detached)
66 {
67   /* checking for infinite values */
68   xbt_assert(std::isfinite(task_size), "task_size is not finite!");
69   xbt_assert(std::isfinite(rate), "rate is not finite!");
70
71   xbt_assert(mbox, "No rendez-vous point defined for isend");
72
73   return simcall_BODY_comm_isend(sender, mbox, task_size, rate, static_cast<unsigned char*>(src_buff), src_buff_size,
74                                  match_fun, clean_fun, copy_data_fun, data, detached);
75 }
76
77 /**
78  * @ingroup simix_comm_management
79  */
80 void simcall_comm_recv(smx_actor_t receiver, smx_mailbox_t mbox, void* dst_buff, size_t* dst_buff_size,
81                        bool (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
82                        void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t), void* data,
83                        double timeout, double rate)
84 {
85   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
86   xbt_assert(mbox, "No rendez-vous point defined for recv");
87
88   if (MC_is_active() || MC_record_replay_is_active()) {
89     /* the model-checker wants two separate simcalls */
90     simgrid::kernel::activity::ActivityImplPtr comm =
91         nullptr; /* MC needs the comm to be set to nullptr during the simcall */
92     comm = simcall_comm_irecv(receiver, mbox, dst_buff, dst_buff_size,
93                               match_fun, copy_data_fun, data, rate);
94     simcall_comm_wait(comm.get(), timeout);
95     comm = nullptr;
96   }
97   else {
98     simcall_BODY_comm_recv(receiver, mbox, static_cast<unsigned char*>(dst_buff), dst_buff_size, match_fun,
99                            copy_data_fun, data, timeout, rate);
100   }
101 }
102 /**
103  * @ingroup simix_comm_management
104  */
105 simgrid::kernel::activity::ActivityImplPtr
106 simcall_comm_irecv(smx_actor_t receiver, smx_mailbox_t mbox, void* dst_buff, size_t* dst_buff_size,
107                    bool (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
108                    void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t), void* data, double rate)
109 {
110   xbt_assert(mbox, "No rendez-vous point defined for irecv");
111
112   return simcall_BODY_comm_irecv(receiver, mbox, static_cast<unsigned char*>(dst_buff), dst_buff_size, match_fun,
113                                  copy_data_fun, data, rate);
114 }
115
116 /**
117  * @ingroup simix_comm_management
118  */
119 ssize_t simcall_comm_waitany(simgrid::kernel::activity::CommImpl* comms[], size_t count, double timeout)
120 {
121   return simcall_BODY_comm_waitany(comms, count, timeout);
122 }
123
124 /**
125  * @ingroup simix_comm_management
126  */
127 ssize_t simcall_comm_testany(simgrid::kernel::activity::CommImpl* comms[], size_t count)
128 {
129   if (count == 0)
130     return -1;
131   return simcall_BODY_comm_testany(comms, count);
132 }
133
134 /**
135  * @ingroup simix_comm_management
136  */
137 void simcall_comm_wait(simgrid::kernel::activity::ActivityImpl* comm, double timeout)
138 {
139   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
140   simcall_BODY_comm_wait(static_cast<simgrid::kernel::activity::CommImpl*>(comm), timeout);
141 }
142
143 /**
144  * @ingroup simix_comm_management
145  *
146  */
147 bool simcall_comm_test(simgrid::kernel::activity::ActivityImpl* comm)
148 {
149   return simcall_BODY_comm_test(static_cast<simgrid::kernel::activity::CommImpl*>(comm));
150 }
151
152 void simcall_run_kernel(std::function<void()> const& code, simgrid::kernel::actor::SimcallObserver* observer)
153 {
154   simgrid::kernel::actor::ActorImpl::self()->simcall_.observer_ = observer;
155   simcall_BODY_run_kernel(&code);
156   simgrid::kernel::actor::ActorImpl::self()->simcall_.observer_ = nullptr;
157 }
158
159 void simcall_run_blocking(std::function<void()> const& code, simgrid::kernel::actor::SimcallObserver* observer)
160 {
161   simgrid::kernel::actor::ActorImpl::self()->simcall_.observer_ = observer;
162   simcall_BODY_run_blocking(&code);
163   simgrid::kernel::actor::ActorImpl::self()->simcall_.observer_ = nullptr;
164 }
165
166 /* ************************************************************************** */
167
168 /** @brief returns a printable string representing a simcall */
169 const char* SIMIX_simcall_name(const s_smx_simcall& simcall)
170 {
171   if (simcall.observer_ != nullptr) {
172     static std::string name;
173     name              = boost::core::demangle(typeid(*simcall.observer_).name());
174     const char* cname = name.c_str();
175     if (name.rfind("simgrid::kernel::", 0) == 0)
176       cname += 17; // strip prefix "simgrid::kernel::"
177     return cname;
178   } else {
179     return simcall_names[static_cast<int>(simcall.call_)];
180   }
181 }
182
183 namespace simgrid {
184 namespace simix {
185
186 void unblock(smx_actor_t actor)
187 {
188   xbt_assert(s4u::Actor::is_maestro());
189   actor->simcall_answer();
190 }
191 } // namespace simix
192 } // namespace simgrid