Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
implement mpi_isendrecv and mpi_isendrecv_replace
[simgrid.git] / src / smpi / bindings / smpi_pmpi_request.cpp
1 /* Copyright (c) 2007-2023. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "private.hpp"
7 #include "smpi_comm.hpp"
8 #include "smpi_datatype.hpp"
9 #include "smpi_request.hpp"
10 #include "src/smpi/include/smpi_actor.hpp"
11
12 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(smpi_pmpi);
13
14 static aid_t getPid(MPI_Comm comm, int id)
15 {
16   return comm->group()->actor(id);
17 }
18
19 #define CHECK_SEND_INPUTS\
20   SET_BUF1(buf)\
21   CHECK_COUNT(2, count)\
22   CHECK_TYPE(3, datatype)\
23   CHECK_BUFFER(1, buf, count, datatype)\
24   CHECK_COMM(6)\
25   if(dst!= MPI_PROC_NULL)\
26     CHECK_RANK(4, dst, comm)\
27   CHECK_TAG(5, tag)
28
29 #define CHECK_ISEND_INPUTS\
30   CHECK_REQUEST(7)\
31   *request = MPI_REQUEST_NULL;\
32   CHECK_SEND_INPUTS
33
34 #define CHECK_IRECV_INPUTS\
35   SET_BUF1(buf)\
36   CHECK_REQUEST(7)\
37   *request = MPI_REQUEST_NULL;\
38   CHECK_COUNT(2, count)\
39   CHECK_TYPE(3, datatype)\
40   CHECK_BUFFER(1, buf, count, datatype)\
41   CHECK_COMM(6)\
42   if(src!=MPI_ANY_SOURCE && src!=MPI_PROC_NULL)\
43     CHECK_RANK(4, src, comm)\
44   CHECK_TAG(5, tag)
45 /* PMPI User level calls */
46
47 int PMPI_Send_init(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm, MPI_Request * request)
48 {
49   CHECK_ISEND_INPUTS
50
51   const SmpiBenchGuard suspend_bench;
52   *request = simgrid::smpi::Request::send_init(buf, count, datatype, dst, tag, comm);
53
54   return MPI_SUCCESS;
55 }
56
57 int PMPI_Recv_init(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm, MPI_Request * request)
58 {
59   CHECK_IRECV_INPUTS
60
61   const SmpiBenchGuard suspend_bench;
62   *request = simgrid::smpi::Request::recv_init(buf, count, datatype, src, tag, comm);
63   return MPI_SUCCESS;
64 }
65
66 int PMPI_Rsend_init(const void* buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm,
67                     MPI_Request* request)
68 {
69   return PMPI_Send_init(buf, count, datatype, dst, tag, comm, request);
70 }
71
72 int PMPI_Ssend_init(const void* buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm, MPI_Request* request)
73 {
74   CHECK_ISEND_INPUTS
75
76   int retval = 0;
77   const SmpiBenchGuard suspend_bench;
78   *request = simgrid::smpi::Request::ssend_init(buf, count, datatype, dst, tag, comm);
79   retval = MPI_SUCCESS;
80
81   return retval;
82 }
83
84 /*
85  * This function starts a request returned by init functions such as
86  * MPI_Send_init(), MPI_Ssend_init (see above), and friends.
87  * They should already have performed sanity checks.
88  */
89 int PMPI_Start(MPI_Request * request)
90 {
91   int retval = 0;
92
93   const SmpiBenchGuard suspend_bench;
94   CHECK_REQUEST_VALID(1)
95   if ( *request == MPI_REQUEST_NULL) {
96     retval = MPI_ERR_REQUEST;
97   } else {
98     MPI_Request req = *request;
99     aid_t my_proc_id = (req->comm() != MPI_COMM_NULL) ? simgrid::s4u::this_actor::get_pid() : -1;
100     TRACE_smpi_comm_in(my_proc_id, __func__,
101                        new simgrid::instr::Pt2PtTIData("Start", MPI_COMM_WORLD->group()->rank(req->dst()), req->size(), req->tag(),
102                                                        simgrid::smpi::Datatype::encode(req->type())));
103     if (not TRACE_smpi_view_internals() && req->flags() & MPI_REQ_SEND)
104       TRACE_smpi_send(my_proc_id, my_proc_id, getPid(req->comm(), req->dst()), req->tag(), req->size());
105
106     req->start();
107
108     if (not TRACE_smpi_view_internals() && req->flags() & MPI_REQ_RECV)
109       TRACE_smpi_recv(getPid(req->comm(), req->src()), my_proc_id, req->tag());
110     retval = MPI_SUCCESS;
111     TRACE_smpi_comm_out(my_proc_id);
112   }
113   return retval;
114 }
115
116 int PMPI_Startall(int count, MPI_Request * requests)
117 {
118   int retval;
119   const SmpiBenchGuard suspend_bench;
120   if (requests == nullptr) {
121     retval = MPI_ERR_ARG;
122   } else {
123     retval = MPI_SUCCESS;
124     for (int i = 0; i < count; i++) {
125       if(requests[i] == MPI_REQUEST_NULL) {
126         retval = MPI_ERR_REQUEST;
127       }
128     }
129     if(retval != MPI_ERR_REQUEST) {
130       aid_t my_proc_id = simgrid::s4u::this_actor::get_pid();
131       TRACE_smpi_comm_in(my_proc_id, __func__, new simgrid::instr::NoOpTIData("Startall"));
132       if (not TRACE_smpi_view_internals())
133         for (int i = 0; i < count; i++) {
134           const simgrid::smpi::Request* req = requests[i];
135           if (req->flags() & MPI_REQ_SEND)
136             TRACE_smpi_send(my_proc_id, my_proc_id, getPid(req->comm(), req->dst()), req->tag(), req->size());
137         }
138
139       simgrid::smpi::Request::startall(count, requests);
140
141       if (not TRACE_smpi_view_internals())
142         for (int i = 0; i < count; i++) {
143           const simgrid::smpi::Request* req = requests[i];
144           if (req->flags() & MPI_REQ_RECV)
145             TRACE_smpi_recv(getPid(req->comm(), req->src()), my_proc_id, req->tag());
146         }
147       TRACE_smpi_comm_out(my_proc_id);
148     }
149   }
150   return retval;
151 }
152
153 int PMPI_Request_free(MPI_Request * request)
154 {
155   int retval = 0;
156
157   const SmpiBenchGuard suspend_bench;
158   if (*request != MPI_REQUEST_NULL) {
159     (*request)->mark_as_deleted();
160     simgrid::smpi::Request::unref(request);
161     *request = MPI_REQUEST_NULL;
162     retval = MPI_SUCCESS;
163   }
164   return retval;
165 }
166
167 int PMPI_Irecv(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm, MPI_Request * request)
168 {
169   CHECK_IRECV_INPUTS
170
171   const SmpiBenchGuard suspend_bench;
172   aid_t my_proc_id = simgrid::s4u::this_actor::get_pid();
173   TRACE_smpi_comm_in(my_proc_id, __func__,
174                      new simgrid::instr::Pt2PtTIData("irecv", MPI_COMM_WORLD->group()->rank(getPid(comm, src)),
175                                                      count,
176                                                      tag, simgrid::smpi::Datatype::encode(datatype)));
177   *request = simgrid::smpi::Request::irecv(buf, count, datatype, src, tag, comm);
178   TRACE_smpi_comm_out(my_proc_id);
179   return MPI_SUCCESS;
180 }
181
182
183 int PMPI_Isend(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm, MPI_Request * request)
184 {
185   CHECK_ISEND_INPUTS
186
187   const SmpiBenchGuard suspend_bench;
188   int retval = 0;
189   aid_t my_proc_id = simgrid::s4u::this_actor::get_pid();
190   aid_t trace_dst  = getPid(comm, dst);
191   TRACE_smpi_comm_in(my_proc_id, __func__,
192                      new simgrid::instr::Pt2PtTIData("isend", MPI_COMM_WORLD->group()->rank(trace_dst),
193                                                      count,
194                                                      tag, simgrid::smpi::Datatype::encode(datatype)));
195   TRACE_smpi_send(my_proc_id, my_proc_id, trace_dst, tag, count * datatype->size());
196   *request = simgrid::smpi::Request::isend(buf, count, datatype, dst, tag, comm);
197   retval = MPI_SUCCESS;
198   TRACE_smpi_comm_out(my_proc_id);
199
200   return retval;
201 }
202
203 int PMPI_Irsend(const void* buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm,
204                 MPI_Request* request)
205 {
206   return PMPI_Isend(buf, count, datatype, dst, tag, comm, request);
207 }
208
209 int PMPI_Issend(const void* buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm, MPI_Request* request)
210 {
211   CHECK_ISEND_INPUTS
212
213   const SmpiBenchGuard suspend_bench;
214   aid_t my_proc_id = simgrid::s4u::this_actor::get_pid();
215   aid_t trace_dst  = getPid(comm, dst);
216   TRACE_smpi_comm_in(my_proc_id, __func__,
217                      new simgrid::instr::Pt2PtTIData("ISsend", MPI_COMM_WORLD->group()->rank(trace_dst),
218                                                      count,
219                                                      tag, simgrid::smpi::Datatype::encode(datatype)));
220   TRACE_smpi_send(my_proc_id, my_proc_id, trace_dst, tag, count * datatype->size());
221   *request = simgrid::smpi::Request::issend(buf, count, datatype, dst, tag, comm);
222   TRACE_smpi_comm_out(my_proc_id);
223   return MPI_SUCCESS;
224 }
225
226 int PMPI_Recv(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm, MPI_Status * status)
227 {
228   int retval = 0;
229   SET_BUF1(buf)
230   CHECK_COUNT(2, count)
231   CHECK_TYPE(3, datatype)
232   CHECK_BUFFER(1, buf, count, datatype)
233   CHECK_TAG(5, tag)
234   CHECK_COMM(6)
235
236   const SmpiBenchGuard suspend_bench;
237   if (src == MPI_PROC_NULL) {
238     if(status != MPI_STATUS_IGNORE){
239       simgrid::smpi::Status::empty(status);
240       status->MPI_SOURCE = MPI_PROC_NULL;
241     }
242     retval = MPI_SUCCESS;
243   } else if (src!=MPI_ANY_SOURCE && (src >= comm->group()->size() || src <0)){
244     retval = MPI_ERR_RANK;
245   } else {
246     aid_t my_proc_id = simgrid::s4u::this_actor::get_pid();
247     TRACE_smpi_comm_in(my_proc_id, __func__,
248                        new simgrid::instr::Pt2PtTIData("recv", MPI_COMM_WORLD->group()->rank(getPid(comm, src)),
249                                                        count,
250                                                        tag, simgrid::smpi::Datatype::encode(datatype)));
251
252     retval = simgrid::smpi::Request::recv(buf, count, datatype, src, tag, comm, status);
253
254     // the src may not have been known at the beginning of the recv (MPI_ANY_SOURCE)
255     if (not TRACE_smpi_view_internals()) {
256       aid_t src_traced = (status != MPI_STATUS_IGNORE) ? getPid(comm, status->MPI_SOURCE) : getPid(comm, src);
257       TRACE_smpi_recv(src_traced, my_proc_id, tag);
258     }
259
260     TRACE_smpi_comm_out(my_proc_id);
261   }
262
263   return retval;
264 }
265
266 int PMPI_Send(const void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
267 {
268   CHECK_SEND_INPUTS
269
270   const SmpiBenchGuard suspend_bench;
271   aid_t my_proc_id = simgrid::s4u::this_actor::get_pid();
272   aid_t dst_traced = getPid(comm, dst);
273   TRACE_smpi_comm_in(my_proc_id, __func__,
274                      new simgrid::instr::Pt2PtTIData("send", MPI_COMM_WORLD->group()->rank(dst_traced),
275                                                      count,
276                                                      tag, simgrid::smpi::Datatype::encode(datatype)));
277   if (not TRACE_smpi_view_internals()) {
278     TRACE_smpi_send(my_proc_id, my_proc_id, dst_traced, tag, count * datatype->size());
279   }
280   simgrid::smpi::Request::send(buf, count, datatype, dst, tag, comm);
281   TRACE_smpi_comm_out(my_proc_id);
282   return MPI_SUCCESS;
283 }
284
285 int PMPI_Rsend(const void* buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
286 {
287   return PMPI_Send(buf, count, datatype, dst, tag, comm);
288 }
289
290 int PMPI_Bsend(const void* buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
291 {
292   CHECK_SEND_INPUTS
293
294   const SmpiBenchGuard suspend_bench;
295   aid_t my_proc_id   = simgrid::s4u::this_actor::get_pid();
296   aid_t dst_traced   = getPid(comm, dst);
297   int bsend_buf_size = 0;
298   void* bsend_buf = nullptr;
299   smpi_process()->bsend_buffer(&bsend_buf, &bsend_buf_size);
300   if (bsend_buf == nullptr || bsend_buf_size < datatype->get_extent() * count + MPI_BSEND_OVERHEAD)
301     return MPI_ERR_BUFFER;
302   TRACE_smpi_comm_in(my_proc_id, __func__,
303                      new simgrid::instr::Pt2PtTIData("bsend", MPI_COMM_WORLD->group()->rank(dst_traced),
304                                                      count,
305                                                      tag, simgrid::smpi::Datatype::encode(datatype)));
306   if (not TRACE_smpi_view_internals()) {
307     TRACE_smpi_send(my_proc_id, my_proc_id, dst_traced, tag, count * datatype->size());
308   }
309   simgrid::smpi::Request::bsend(buf, count, datatype, dst, tag, comm);
310   TRACE_smpi_comm_out(my_proc_id);
311   return MPI_SUCCESS;
312 }
313
314 int PMPI_Ibsend(const void* buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm, MPI_Request* request)
315 {
316   CHECK_ISEND_INPUTS
317
318   const SmpiBenchGuard suspend_bench;
319   aid_t my_proc_id   = simgrid::s4u::this_actor::get_pid();
320   aid_t trace_dst    = getPid(comm, dst);
321   int bsend_buf_size = 0;
322   void* bsend_buf = nullptr;
323   smpi_process()->bsend_buffer(&bsend_buf, &bsend_buf_size);
324   if (bsend_buf == nullptr || bsend_buf_size < datatype->get_extent() * count + MPI_BSEND_OVERHEAD)
325     return MPI_ERR_BUFFER;
326   TRACE_smpi_comm_in(my_proc_id, __func__,
327                      new simgrid::instr::Pt2PtTIData("ibsend", MPI_COMM_WORLD->group()->rank(trace_dst),
328                                                      count,
329                                                      tag, simgrid::smpi::Datatype::encode(datatype)));
330   TRACE_smpi_send(my_proc_id, my_proc_id, trace_dst, tag, count * datatype->size());
331   *request = simgrid::smpi::Request::ibsend(buf, count, datatype, dst, tag, comm);
332   TRACE_smpi_comm_out(my_proc_id);
333   return MPI_SUCCESS;
334 }
335
336 int PMPI_Bsend_init(const void* buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm, MPI_Request* request)
337 {
338   CHECK_ISEND_INPUTS
339
340   const SmpiBenchGuard suspend_bench;
341   int retval = 0;
342   int bsend_buf_size = 0;
343   void* bsend_buf = nullptr;
344   smpi_process()->bsend_buffer(&bsend_buf, &bsend_buf_size);
345   if( bsend_buf==nullptr || bsend_buf_size < datatype->get_extent() * count + MPI_BSEND_OVERHEAD ) {
346     retval = MPI_ERR_BUFFER;
347   } else {
348     *request = simgrid::smpi::Request::bsend_init(buf, count, datatype, dst, tag, comm);
349     retval   = MPI_SUCCESS;
350   }
351   return retval;
352 }
353
354 int PMPI_Ssend(const void* buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
355 {
356   CHECK_SEND_INPUTS
357
358   const SmpiBenchGuard suspend_bench;
359   aid_t my_proc_id = simgrid::s4u::this_actor::get_pid();
360   aid_t dst_traced = getPid(comm, dst);
361   TRACE_smpi_comm_in(my_proc_id, __func__,
362                      new simgrid::instr::Pt2PtTIData("Ssend", MPI_COMM_WORLD->group()->rank(dst_traced),
363                                                      count,
364                                                      tag, simgrid::smpi::Datatype::encode(datatype)));
365   TRACE_smpi_send(my_proc_id, my_proc_id, dst_traced, tag, count * datatype->size());
366   simgrid::smpi::Request::ssend(buf, count, datatype, dst, tag, comm);
367   TRACE_smpi_comm_out(my_proc_id);
368   return MPI_SUCCESS;
369 }
370
371 int PMPI_Sendrecv(const void* sendbuf, int sendcount, MPI_Datatype sendtype, int dst, int sendtag, void* recvbuf,
372                   int recvcount, MPI_Datatype recvtype, int src, int recvtag, MPI_Comm comm, MPI_Status* status)
373 {
374   int retval = 0;
375   SET_BUF1(sendbuf)
376   SET_BUF2(recvbuf)
377   CHECK_COUNT(2, sendcount)
378   CHECK_TYPE(3, sendtype)
379   CHECK_TAG(5, sendtag)
380   CHECK_COUNT(7, recvcount)
381   CHECK_TYPE(8, recvtype)
382   CHECK_BUFFER(1, sendbuf, sendcount, sendtype)
383   CHECK_BUFFER(6, recvbuf, recvcount, recvtype)
384   CHECK_ARGS(sendbuf == recvbuf && sendcount > 0 && recvcount > 0, MPI_ERR_BUFFER,
385              "%s: Invalid parameters 1 and 6: sendbuf and recvbuf must be disjoint", __func__);
386   CHECK_TAG(10, recvtag)
387   CHECK_COMM(11)
388   const SmpiBenchGuard suspend_bench;
389
390   if (src == MPI_PROC_NULL) {
391     if(status!=MPI_STATUS_IGNORE){
392       simgrid::smpi::Status::empty(status);
393       status->MPI_SOURCE = MPI_PROC_NULL;
394     }
395     if(dst != MPI_PROC_NULL)
396       simgrid::smpi::Request::send(sendbuf, sendcount, sendtype, dst, sendtag, comm);
397     retval = MPI_SUCCESS;
398   } else if (dst == MPI_PROC_NULL){
399     retval = simgrid::smpi::Request::recv(recvbuf, recvcount, recvtype, src, recvtag, comm, status);
400   } else if (dst >= comm->group()->size() || dst <0 ||
401       (src!=MPI_ANY_SOURCE && (src >= comm->group()->size() || src <0))){
402     retval = MPI_ERR_RANK;
403   } else {
404     aid_t my_proc_id = simgrid::s4u::this_actor::get_pid();
405     aid_t dst_traced = MPI_COMM_WORLD->group()->rank(getPid(comm, dst));
406     aid_t src_traced = MPI_COMM_WORLD->group()->rank(getPid(comm, src));
407
408     // FIXME: Hack the way to trace this one
409     auto dst_hack = std::make_shared<std::vector<int>>();
410     auto src_hack = std::make_shared<std::vector<int>>();
411     dst_hack->push_back(dst_traced);
412     src_hack->push_back(src_traced);
413     TRACE_smpi_comm_in(my_proc_id, __func__,
414                        new simgrid::instr::VarCollTIData(
415                            "sendRecv", -1, sendcount,
416                            dst_hack, recvcount, src_hack,
417                            simgrid::smpi::Datatype::encode(sendtype), simgrid::smpi::Datatype::encode(recvtype)));
418
419     TRACE_smpi_send(my_proc_id, my_proc_id, dst_traced, sendtag, sendcount * sendtype->size());
420
421     simgrid::smpi::Request::sendrecv(sendbuf, sendcount, sendtype, dst, sendtag, recvbuf, recvcount, recvtype, src,
422                                      recvtag, comm, status);
423     retval = MPI_SUCCESS;
424
425     TRACE_smpi_recv(src_traced, my_proc_id, recvtag);
426     TRACE_smpi_comm_out(my_proc_id);
427   }
428
429   return retval;
430 }
431
432 int PMPI_Isendrecv(const void* sendbuf, int sendcount, MPI_Datatype sendtype, int dst, int sendtag, void* recvbuf,
433                   int recvcount, MPI_Datatype recvtype, int src, int recvtag, MPI_Comm comm, MPI_Request* request)
434 {
435   int retval = 0;
436   SET_BUF1(sendbuf)
437   SET_BUF2(recvbuf)
438   CHECK_COUNT(2, sendcount)
439   CHECK_TYPE(3, sendtype)
440   CHECK_TAG(5, sendtag)
441   CHECK_COUNT(7, recvcount)
442   CHECK_TYPE(8, recvtype)
443   CHECK_BUFFER(1, sendbuf, sendcount, sendtype)
444   CHECK_BUFFER(6, recvbuf, recvcount, recvtype)
445   CHECK_ARGS(sendbuf == recvbuf && sendcount > 0 && recvcount > 0, MPI_ERR_BUFFER,
446              "%s: Invalid parameters 1 and 6: sendbuf and recvbuf must be disjoint", __func__);
447   CHECK_TAG(10, recvtag)
448   CHECK_COMM(11)
449   CHECK_REQUEST(12)
450   *request = MPI_REQUEST_NULL;
451   const SmpiBenchGuard suspend_bench;
452
453   if (src == MPI_PROC_NULL && dst != MPI_PROC_NULL){
454     *request=simgrid::smpi::Request::isend(sendbuf, sendcount, sendtype, dst, sendtag, comm);
455     retval = MPI_SUCCESS;
456   } else if (dst == MPI_PROC_NULL){
457     *request = simgrid::smpi::Request::irecv(recvbuf, recvcount, recvtype, src, recvtag, comm);
458     retval = MPI_SUCCESS;
459   } else if (dst >= comm->group()->size() || dst <0 ||
460       (src!=MPI_ANY_SOURCE && (src >= comm->group()->size() || src <0))){
461     retval = MPI_ERR_RANK;
462   } else {
463     aid_t my_proc_id = simgrid::s4u::this_actor::get_pid();
464     aid_t dst_traced = MPI_COMM_WORLD->group()->rank(getPid(comm, dst));
465     aid_t src_traced = MPI_COMM_WORLD->group()->rank(getPid(comm, src));
466
467     // FIXME: Hack the way to trace this one
468     auto dst_hack = std::make_shared<std::vector<int>>();
469     auto src_hack = std::make_shared<std::vector<int>>();
470     dst_hack->push_back(dst_traced);
471     src_hack->push_back(src_traced);
472     TRACE_smpi_comm_in(my_proc_id, __func__,
473                        new simgrid::instr::VarCollTIData(
474                            "isendRecv", -1, sendcount,
475                            dst_hack, recvcount, src_hack,
476                            simgrid::smpi::Datatype::encode(sendtype), simgrid::smpi::Datatype::encode(recvtype)));
477
478     TRACE_smpi_send(my_proc_id, my_proc_id, dst_traced, sendtag, sendcount * sendtype->size());
479
480     simgrid::smpi::Request::isendrecv(sendbuf, sendcount, sendtype, dst, sendtag, recvbuf, recvcount, recvtype, src,
481                                      recvtag, comm, request);
482     retval = MPI_SUCCESS;
483
484     TRACE_smpi_recv(src_traced, my_proc_id, recvtag);
485     TRACE_smpi_comm_out(my_proc_id);
486   }
487
488   return retval;
489 }
490
491
492 int PMPI_Sendrecv_replace(void* buf, int count, MPI_Datatype datatype, int dst, int sendtag, int src, int recvtag,
493                           MPI_Comm comm, MPI_Status* status)
494 {
495   int retval = 0;
496   SET_BUF1(buf)
497   CHECK_COUNT(2, count)
498   CHECK_TYPE(3, datatype)
499   CHECK_BUFFER(1, buf, count, datatype)
500
501   int size = datatype->get_extent() * count;
502   if (size == 0)
503     return MPI_SUCCESS;
504   else if (size <0)
505     return MPI_ERR_ARG;
506   std::vector<char> recvbuf(size);
507   retval =
508       MPI_Sendrecv(buf, count, datatype, dst, sendtag, recvbuf.data(), count, datatype, src, recvtag, comm, status);
509   if(retval==MPI_SUCCESS){
510     simgrid::smpi::Datatype::copy(recvbuf.data(), count, datatype, buf, count, datatype);
511   }
512   return retval;
513 }
514
515 int PMPI_Isendrecv_replace(void* buf, int count, MPI_Datatype datatype, int dst, int sendtag, int src, int recvtag,
516                           MPI_Comm comm, MPI_Request* request)
517 {
518   int retval = 0;
519   SET_BUF1(buf)
520   CHECK_COUNT(2, count)
521   CHECK_TYPE(3, datatype)
522   CHECK_BUFFER(1, buf, count, datatype)
523   CHECK_REQUEST(10)
524   *request = MPI_REQUEST_NULL;
525
526   int size = datatype->get_extent() * count;
527   if (size == 0)
528     return MPI_SUCCESS;
529   else if (size <0)
530     return MPI_ERR_ARG;
531   std::vector<char> sendbuf(size);
532   simgrid::smpi::Datatype::copy(buf, count, datatype, sendbuf.data(), count, datatype);
533   retval =
534       MPI_Isendrecv(sendbuf.data(), count, datatype, dst, sendtag, buf, count, datatype, src, recvtag, comm, request);
535   return retval;
536 }
537
538 int PMPI_Test(MPI_Request * request, int *flag, MPI_Status * status)
539 {
540   int retval = 0;
541   const SmpiBenchGuard suspend_bench;
542   if (request == nullptr || flag == nullptr) {
543     retval = MPI_ERR_ARG;
544   } else if (*request == MPI_REQUEST_NULL) {
545     if (status != MPI_STATUS_IGNORE){
546       *flag= true;
547       simgrid::smpi::Status::empty(status);
548     }
549     retval = MPI_SUCCESS;
550   } else {
551     aid_t my_proc_id = ((*request)->comm() != MPI_COMM_NULL) ? simgrid::s4u::this_actor::get_pid() : -1;
552
553     TRACE_smpi_comm_in(my_proc_id, __func__,
554                        new simgrid::instr::WaitTIData("test", MPI_COMM_WORLD->group()->rank((*request)->src()),
555                                                       MPI_COMM_WORLD->group()->rank((*request)->dst()),
556                                                       (*request)->tag()));
557     retval = simgrid::smpi::Request::test(request,status, flag);
558
559     TRACE_smpi_comm_out(my_proc_id);
560   }
561   return retval;
562 }
563
564 int PMPI_Testany(int count, MPI_Request requests[], int *index, int *flag, MPI_Status * status)
565 {
566   int retval = 0;
567   CHECK_COUNT(1, count)
568   const SmpiBenchGuard suspend_bench;
569   if (index == nullptr || flag == nullptr) {
570     retval = MPI_ERR_ARG;
571   } else {
572     aid_t my_proc_id = simgrid::s4u::this_actor::get_pid();
573     TRACE_smpi_comm_in(my_proc_id, __func__, new simgrid::instr::NoOpTIData("testany"));
574     retval = simgrid::smpi::Request::testany(count, requests, index, flag, status);
575     TRACE_smpi_comm_out(my_proc_id);
576   }
577   return retval;
578 }
579
580 int PMPI_Testall(int count, MPI_Request* requests, int* flag, MPI_Status* statuses)
581 {
582   int retval = 0;
583   CHECK_COUNT(1, count)
584   const SmpiBenchGuard suspend_bench;
585   if (flag == nullptr) {
586     retval = MPI_ERR_ARG;
587   } else {
588     aid_t my_proc_id = simgrid::s4u::this_actor::get_pid();
589     TRACE_smpi_comm_in(my_proc_id, __func__, new simgrid::instr::NoOpTIData("testall"));
590     retval = simgrid::smpi::Request::testall(count, requests, flag, statuses);
591     TRACE_smpi_comm_out(my_proc_id);
592   }
593   return retval;
594 }
595
596 int PMPI_Testsome(int incount, MPI_Request requests[], int* outcount, int* indices, MPI_Status status[])
597 {
598   int retval = 0;
599   CHECK_COUNT(1, incount)
600   const SmpiBenchGuard suspend_bench;
601   if (outcount == nullptr) {
602     retval = MPI_ERR_ARG;
603   } else {
604     aid_t my_proc_id = simgrid::s4u::this_actor::get_pid();
605     TRACE_smpi_comm_in(my_proc_id, __func__, new simgrid::instr::NoOpTIData("testsome"));
606     retval = simgrid::smpi::Request::testsome(incount, requests, outcount, indices, status);
607     TRACE_smpi_comm_out(my_proc_id);
608   }
609   return retval;
610 }
611
612 int PMPI_Probe(int source, int tag, MPI_Comm comm, MPI_Status* status) {
613   int retval = 0;
614   const SmpiBenchGuard suspend_bench;
615
616   CHECK_COMM(6)
617   if(source!=MPI_ANY_SOURCE && source!=MPI_PROC_NULL)\
618     CHECK_RANK(1, source, comm)
619   CHECK_TAG(2, tag)
620   if (source == MPI_PROC_NULL) {
621     if (status != MPI_STATUS_IGNORE){
622       simgrid::smpi::Status::empty(status);
623       status->MPI_SOURCE = MPI_PROC_NULL;
624     }
625     retval = MPI_SUCCESS;
626   } else {
627     simgrid::smpi::Request::probe(source, tag, comm, status);
628     retval = MPI_SUCCESS;
629   }
630   return retval;
631 }
632
633 int PMPI_Iprobe(int source, int tag, MPI_Comm comm, int* flag, MPI_Status* status) {
634   int retval = 0;
635   const SmpiBenchGuard suspend_bench;
636   CHECK_COMM(6)
637   if(source!=MPI_ANY_SOURCE && source!=MPI_PROC_NULL)\
638     CHECK_RANK(1, source, comm)
639   CHECK_TAG(2, tag)
640   if (flag == nullptr) {
641     retval = MPI_ERR_ARG;
642   } else if (source == MPI_PROC_NULL) {
643     *flag=true;
644     if (status != MPI_STATUS_IGNORE){
645       simgrid::smpi::Status::empty(status);
646       status->MPI_SOURCE = MPI_PROC_NULL;
647     }
648     retval = MPI_SUCCESS;
649   } else {
650     simgrid::smpi::Request::iprobe(source, tag, comm, flag, status);
651     retval = MPI_SUCCESS;
652   }
653   return retval;
654 }
655
656 static void trace_smpi_wait_recv_helper(MPI_Request* request, MPI_Status* status)
657 {
658   const simgrid::smpi::Request* req = *request;
659   // Requests already received are null. Is this request a wait for RECV?
660   if (req != MPI_REQUEST_NULL && (req->flags() & MPI_REQ_RECV)) {
661     aid_t src_traced = req->src();
662     aid_t dst_traced = req->dst();
663     // the src may not have been known at the beginning of the recv (MPI_ANY_SOURCE)
664     if (src_traced == MPI_ANY_SOURCE && status != MPI_STATUS_IGNORE)
665       src_traced = req->comm()->group()->actor(status->MPI_SOURCE);
666     TRACE_smpi_recv(src_traced, dst_traced, req->tag());
667   }
668 }
669
670 int PMPI_Wait(MPI_Request * request, MPI_Status * status)
671 {
672   int retval = 0;
673
674   const SmpiBenchGuard suspend_bench;
675
676   simgrid::smpi::Status::empty(status);
677
678   CHECK_NULL(1, MPI_ERR_ARG, request)
679   if (*request == MPI_REQUEST_NULL) {
680     retval = MPI_SUCCESS;
681   } else {
682     // for tracing, save the handle which might get overridden before we can use the helper on it
683     MPI_Request savedreq = *request;
684     if (not(savedreq->flags() & (MPI_REQ_FINISHED | MPI_REQ_GENERALIZED | MPI_REQ_NBC)))
685       savedreq->ref();//don't erase the handle in Request::wait, we'll need it later
686     else
687       savedreq = MPI_REQUEST_NULL;
688
689     aid_t my_proc_id = (*request)->comm() != MPI_COMM_NULL ? simgrid::s4u::this_actor::get_pid() : -1;
690     TRACE_smpi_comm_in(my_proc_id, __func__,
691                        new simgrid::instr::WaitTIData("wait", MPI_COMM_WORLD->group()->rank((*request)->src()),
692                                                       MPI_COMM_WORLD->group()->rank((*request)->dst()),
693                                                       (*request)->tag()));
694
695     retval = simgrid::smpi::Request::wait(request, status);
696
697     //the src may not have been known at the beginning of the recv (MPI_ANY_SOURCE)
698     TRACE_smpi_comm_out(my_proc_id);
699     trace_smpi_wait_recv_helper(&savedreq, status);
700     if (savedreq != MPI_REQUEST_NULL)
701       simgrid::smpi::Request::unref(&savedreq);
702   }
703
704   return retval;
705 }
706
707 int PMPI_Waitany(int count, MPI_Request requests[], int *index, MPI_Status * status)
708 {
709   if (index == nullptr)
710     return MPI_ERR_ARG;
711
712   if (count <= 0)
713     return MPI_SUCCESS;
714
715   const SmpiBenchGuard suspend_bench;
716   // for tracing, save the handles which might get overridden before we can use the helper on it
717   std::vector<MPI_Request> savedreqs(requests, requests + count);
718   for (MPI_Request& req : savedreqs) {
719     if (req != MPI_REQUEST_NULL && not(req->flags() & (MPI_REQ_FINISHED | MPI_REQ_NBC)))
720       req->ref();
721     else
722       req = MPI_REQUEST_NULL;
723   }
724
725   aid_t rank_traced = simgrid::s4u::this_actor::get_pid(); // FIXME: In PMPI_Wait, we check if the comm is null?
726   TRACE_smpi_comm_in(rank_traced, __func__, new simgrid::instr::CpuTIData("waitAny", count));
727
728   *index = simgrid::smpi::Request::waitany(count, requests, status);
729
730   if(*index!=MPI_UNDEFINED){
731     trace_smpi_wait_recv_helper(&savedreqs[*index], status);
732     TRACE_smpi_comm_out(rank_traced);
733   }
734
735   for (MPI_Request& req : savedreqs)
736     if (req != MPI_REQUEST_NULL)
737       simgrid::smpi::Request::unref(&req);
738
739   return MPI_SUCCESS;
740 }
741
742 int PMPI_Waitall(int count, MPI_Request requests[], MPI_Status status[])
743 {
744   const SmpiBenchGuard suspend_bench;
745   CHECK_COUNT(1, count)
746   // for tracing, save the handles which might get overridden before we can use the helper on it
747   std::vector<MPI_Request> savedreqs(requests, requests + count);
748   for (MPI_Request& req : savedreqs) {
749     if (req != MPI_REQUEST_NULL && not(req->flags() & (MPI_REQ_FINISHED | MPI_REQ_NBC)))
750       req->ref();
751     else
752       req = MPI_REQUEST_NULL;
753   }
754
755   aid_t rank_traced = simgrid::s4u::this_actor::get_pid(); // FIXME: In PMPI_Wait, we check if the comm is null?
756   TRACE_smpi_comm_in(rank_traced, __func__, new simgrid::instr::CpuTIData("waitall", count));
757
758   int retval = simgrid::smpi::Request::waitall(count, requests, status);
759
760   for (int i = 0; i < count; i++) {
761     trace_smpi_wait_recv_helper(&savedreqs[i], status != MPI_STATUSES_IGNORE ? &status[i] : MPI_STATUS_IGNORE);
762   }
763   TRACE_smpi_comm_out(rank_traced);
764
765   for (MPI_Request& req : savedreqs)
766     if (req != MPI_REQUEST_NULL)
767       simgrid::smpi::Request::unref(&req);
768
769   return retval;
770 }
771
772 int PMPI_Waitsome(int incount, MPI_Request requests[], int *outcount, int *indices, MPI_Status status[])
773 {
774   int retval = 0;
775   CHECK_COUNT(1, incount)
776   const SmpiBenchGuard suspend_bench;
777   if (outcount == nullptr) {
778     retval = MPI_ERR_ARG;
779   } else {
780     *outcount = simgrid::smpi::Request::waitsome(incount, requests, indices, status);
781     retval = MPI_SUCCESS;
782   }
783   return retval;
784 }
785
786 int PMPI_Cancel(MPI_Request* request)
787 {
788   int retval = 0;
789
790   const SmpiBenchGuard suspend_bench;
791   CHECK_REQUEST_VALID(1)
792   if (*request == MPI_REQUEST_NULL) {
793     retval = MPI_ERR_REQUEST;
794   } else {
795     (*request)->cancel();
796     retval = MPI_SUCCESS;
797   }
798   return retval;
799 }
800
801 int PMPI_Test_cancelled(const MPI_Status* status, int* flag){
802   if(status==MPI_STATUS_IGNORE){
803     *flag=0;
804     return MPI_ERR_ARG;
805   }
806   *flag=simgrid::smpi::Status::cancelled(status);
807   return MPI_SUCCESS;
808 }
809
810 int PMPI_Status_set_cancelled(MPI_Status* status, int flag){
811   if(status==MPI_STATUS_IGNORE){
812     return MPI_ERR_ARG;
813   }
814   simgrid::smpi::Status::set_cancelled(status,flag);
815   return MPI_SUCCESS;
816 }
817
818 int PMPI_Status_set_elements(MPI_Status* status, MPI_Datatype datatype, int count){
819   if(status==MPI_STATUS_IGNORE){
820     return MPI_ERR_ARG;
821   }
822   simgrid::smpi::Status::set_elements(status,datatype, count);
823   return MPI_SUCCESS;
824 }
825
826 int PMPI_Status_set_elements_x(MPI_Status* status, MPI_Datatype datatype, MPI_Count count){
827   if(status==MPI_STATUS_IGNORE){
828     return MPI_ERR_ARG;
829   }
830   simgrid::smpi::Status::set_elements(status,datatype, static_cast<int>(count));
831   return MPI_SUCCESS;
832 }
833
834 int PMPI_Grequest_start( MPI_Grequest_query_function *query_fn, MPI_Grequest_free_function *free_fn, MPI_Grequest_cancel_function *cancel_fn, void *extra_state, MPI_Request *request){
835   return simgrid::smpi::Request::grequest_start(query_fn, free_fn,cancel_fn, extra_state, request);
836 }
837
838 int PMPI_Grequest_complete( MPI_Request request){
839   return simgrid::smpi::Request::grequest_complete(request);
840 }
841
842 int PMPI_Request_get_status( MPI_Request request, int *flag, MPI_Status *status){
843   if(request==MPI_REQUEST_NULL){
844     *flag=1;
845     simgrid::smpi::Status::empty(status);
846     return MPI_SUCCESS;
847   } else if (flag == nullptr) {
848     return MPI_ERR_ARG;
849   }
850   return simgrid::smpi::Request::get_status(request,flag,status);
851 }
852
853 MPI_Request PMPI_Request_f2c(MPI_Fint request){
854   if(request==-1)
855     return MPI_REQUEST_NULL;
856   return simgrid::smpi::Request::f2c(request);
857 }
858
859 MPI_Fint PMPI_Request_c2f(MPI_Request request) {
860   if(request==MPI_REQUEST_NULL)
861     return -1;
862   return request->c2f();
863 }