Logo AND Algorithmique Numérique Distribuée

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