1 /* Copyright (c) 2007-2017. The SimGrid Team. All rights reserved. */
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. */
6 #include "simgrid/s4u/Engine.hpp"
7 #include "simgrid/s4u/Host.hpp"
8 #include "src/smpi/private.h"
9 #include "src/smpi/smpi_comm.hpp"
10 #include "src/smpi/smpi_coll.hpp"
11 #include "src/smpi/smpi_datatype_derived.hpp"
12 #include "src/smpi/smpi_op.hpp"
13 #include "src/smpi/smpi_process.hpp"
14 #include "src/smpi/smpi_request.hpp"
15 #include "src/smpi/smpi_status.hpp"
16 #include "src/smpi/smpi_win.hpp"
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_pmpi, smpi, "Logging specific to SMPI (pmpi)");
20 //this function need to be here because of the calls to smpi_bench
21 void TRACE_smpi_set_category(const char *category)
23 //need to end bench otherwise categories for execution tasks are wrong
25 TRACE_internal_smpi_set_category (category);
26 //begin bench after changing process's category
30 /* PMPI User level calls */
31 extern "C" { // Obviously, the C MPI interface should use the C linkage
33 int PMPI_Init(int *argc, char ***argv)
35 xbt_assert(simgrid::s4u::Engine::isInitialized(),
36 "Your MPI program was not properly initialized. The easiest is to use smpirun to start it.");
37 // PMPI_Init is called only once per SMPI process
39 MPI_Initialized(&already_init);
40 if(already_init == 0){
41 simgrid::smpi::Process::init(argc, argv);
42 smpi_process()->mark_as_initialized();
43 int rank = smpi_process()->index();
44 TRACE_smpi_init(rank);
45 TRACE_smpi_computing_init(rank);
46 instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
47 extra->type = TRACING_INIT;
48 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, extra);
49 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
61 int rank = smpi_process()->index();
62 instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
63 extra->type = TRACING_FINALIZE;
64 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, extra);
66 smpi_process()->finalize();
68 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
69 TRACE_smpi_finalize(smpi_process()->index());
73 int PMPI_Finalized(int* flag)
75 *flag=smpi_process()!=nullptr ? smpi_process()->finalized() : 0;
79 int PMPI_Get_version (int *version,int *subversion){
80 *version = MPI_VERSION;
81 *subversion= MPI_SUBVERSION;
85 int PMPI_Get_library_version (char *version,int *len){
87 snprintf(version, MPI_MAX_LIBRARY_VERSION_STRING, "SMPI Version %d.%d. Copyright The Simgrid Team 2007-2017",
88 SIMGRID_VERSION_MAJOR, SIMGRID_VERSION_MINOR);
89 *len = strlen(version) > MPI_MAX_LIBRARY_VERSION_STRING ? MPI_MAX_LIBRARY_VERSION_STRING : strlen(version);
94 int PMPI_Init_thread(int *argc, char ***argv, int required, int *provided)
96 if (provided != nullptr) {
97 *provided = MPI_THREAD_SINGLE;
99 return MPI_Init(argc, argv);
102 int PMPI_Query_thread(int *provided)
104 if (provided == nullptr) {
107 *provided = MPI_THREAD_SINGLE;
112 int PMPI_Is_thread_main(int *flag)
114 if (flag == nullptr) {
117 *flag = smpi_process()->index() == 0;
122 int PMPI_Abort(MPI_Comm comm, int errorcode)
125 // FIXME: should kill all processes in comm instead
126 simcall_process_kill(SIMIX_process_self());
132 return smpi_mpi_wtime();
135 extern double sg_maxmin_precision;
138 return sg_maxmin_precision;
141 int PMPI_Address(void *location, MPI_Aint * address)
143 if (address==nullptr) {
146 *address = reinterpret_cast<MPI_Aint>(location);
151 int PMPI_Get_address(void *location, MPI_Aint * address)
153 return PMPI_Address(location, address);
156 int PMPI_Type_free(MPI_Datatype * datatype)
158 /* Free a predefined datatype is an error according to the standard, and should be checked for */
159 if (*datatype == MPI_DATATYPE_NULL) {
162 simgrid::smpi::Datatype::unref(*datatype);
167 int PMPI_Type_size(MPI_Datatype datatype, int *size)
169 if (datatype == MPI_DATATYPE_NULL) {
171 } else if (size == nullptr) {
174 *size = static_cast<int>(datatype->size());
179 int PMPI_Type_size_x(MPI_Datatype datatype, MPI_Count *size)
181 if (datatype == MPI_DATATYPE_NULL) {
183 } else if (size == nullptr) {
186 *size = static_cast<MPI_Count>(datatype->size());
191 int PMPI_Type_get_extent(MPI_Datatype datatype, MPI_Aint * lb, MPI_Aint * extent)
193 if (datatype == MPI_DATATYPE_NULL) {
195 } else if (lb == nullptr || extent == nullptr) {
198 return datatype->extent(lb, extent);
202 int PMPI_Type_get_true_extent(MPI_Datatype datatype, MPI_Aint * lb, MPI_Aint * extent)
204 return PMPI_Type_get_extent(datatype, lb, extent);
207 int PMPI_Type_extent(MPI_Datatype datatype, MPI_Aint * extent)
209 if (datatype == MPI_DATATYPE_NULL) {
211 } else if (extent == nullptr) {
214 *extent = datatype->get_extent();
219 int PMPI_Type_lb(MPI_Datatype datatype, MPI_Aint * disp)
221 if (datatype == MPI_DATATYPE_NULL) {
223 } else if (disp == nullptr) {
226 *disp = datatype->lb();
231 int PMPI_Type_ub(MPI_Datatype datatype, MPI_Aint * disp)
233 if (datatype == MPI_DATATYPE_NULL) {
235 } else if (disp == nullptr) {
238 *disp = datatype->ub();
243 int PMPI_Type_dup(MPI_Datatype datatype, MPI_Datatype *newtype){
244 int retval = MPI_SUCCESS;
245 if (datatype == MPI_DATATYPE_NULL) {
248 *newtype = new simgrid::smpi::Datatype(datatype, &retval);
249 //error when duplicating, free the new datatype
250 if(retval!=MPI_SUCCESS){
251 simgrid::smpi::Datatype::unref(*newtype);
252 *newtype = MPI_DATATYPE_NULL;
258 int PMPI_Op_create(MPI_User_function * function, int commute, MPI_Op * op)
260 if (function == nullptr || op == nullptr) {
263 *op = new simgrid::smpi::Op(function, (commute!=0));
268 int PMPI_Op_free(MPI_Op * op)
272 } else if (*op == MPI_OP_NULL) {
281 int PMPI_Op_commutative(MPI_Op op, int* commute){
282 if (op == MPI_OP_NULL) {
284 } else if (commute==nullptr){
287 *commute = op->is_commutative();
292 int PMPI_Group_free(MPI_Group * group)
294 if (group == nullptr) {
297 if(*group != MPI_COMM_WORLD->group() && *group != MPI_GROUP_EMPTY)
298 simgrid::smpi::Group::unref(*group);
299 *group = MPI_GROUP_NULL;
304 int PMPI_Group_size(MPI_Group group, int *size)
306 if (group == MPI_GROUP_NULL) {
307 return MPI_ERR_GROUP;
308 } else if (size == nullptr) {
311 *size = group->size();
316 int PMPI_Group_rank(MPI_Group group, int *rank)
318 if (group == MPI_GROUP_NULL) {
319 return MPI_ERR_GROUP;
320 } else if (rank == nullptr) {
323 *rank = group->rank(smpi_process()->index());
328 int PMPI_Group_translate_ranks(MPI_Group group1, int n, int *ranks1, MPI_Group group2, int *ranks2)
330 if (group1 == MPI_GROUP_NULL || group2 == MPI_GROUP_NULL) {
331 return MPI_ERR_GROUP;
333 for (int i = 0; i < n; i++) {
334 if(ranks1[i]==MPI_PROC_NULL){
335 ranks2[i]=MPI_PROC_NULL;
337 int index = group1->index(ranks1[i]);
338 ranks2[i] = group2->rank(index);
345 int PMPI_Group_compare(MPI_Group group1, MPI_Group group2, int *result)
347 if (group1 == MPI_GROUP_NULL || group2 == MPI_GROUP_NULL) {
348 return MPI_ERR_GROUP;
349 } else if (result == nullptr) {
352 *result = group1->compare(group2);
357 int PMPI_Group_union(MPI_Group group1, MPI_Group group2, MPI_Group * newgroup)
360 if (group1 == MPI_GROUP_NULL || group2 == MPI_GROUP_NULL) {
361 return MPI_ERR_GROUP;
362 } else if (newgroup == nullptr) {
365 return group1->group_union(group2, newgroup);
369 int PMPI_Group_intersection(MPI_Group group1, MPI_Group group2, MPI_Group * newgroup)
372 if (group1 == MPI_GROUP_NULL || group2 == MPI_GROUP_NULL) {
373 return MPI_ERR_GROUP;
374 } else if (newgroup == nullptr) {
377 return group1->intersection(group2,newgroup);
381 int PMPI_Group_difference(MPI_Group group1, MPI_Group group2, MPI_Group * newgroup)
383 if (group1 == MPI_GROUP_NULL || group2 == MPI_GROUP_NULL) {
384 return MPI_ERR_GROUP;
385 } else if (newgroup == nullptr) {
388 return group1->difference(group2,newgroup);
392 int PMPI_Group_incl(MPI_Group group, int n, int *ranks, MPI_Group * newgroup)
394 if (group == MPI_GROUP_NULL) {
395 return MPI_ERR_GROUP;
396 } else if (newgroup == nullptr) {
399 return group->incl(n, ranks, newgroup);
403 int PMPI_Group_excl(MPI_Group group, int n, int *ranks, MPI_Group * newgroup)
405 if (group == MPI_GROUP_NULL) {
406 return MPI_ERR_GROUP;
407 } else if (newgroup == nullptr) {
412 if (group != MPI_COMM_WORLD->group()
413 && group != MPI_COMM_SELF->group() && group != MPI_GROUP_EMPTY)
416 } else if (n == group->size()) {
417 *newgroup = MPI_GROUP_EMPTY;
420 return group->excl(n,ranks,newgroup);
425 int PMPI_Group_range_incl(MPI_Group group, int n, int ranges[][3], MPI_Group * newgroup)
427 if (group == MPI_GROUP_NULL) {
428 return MPI_ERR_GROUP;
429 } else if (newgroup == nullptr) {
433 *newgroup = MPI_GROUP_EMPTY;
436 return group->range_incl(n,ranges,newgroup);
441 int PMPI_Group_range_excl(MPI_Group group, int n, int ranges[][3], MPI_Group * newgroup)
443 if (group == MPI_GROUP_NULL) {
444 return MPI_ERR_GROUP;
445 } else if (newgroup == nullptr) {
450 if (group != MPI_COMM_WORLD->group() && group != MPI_COMM_SELF->group() &&
451 group != MPI_GROUP_EMPTY)
455 return group->range_excl(n,ranges,newgroup);
460 int PMPI_Comm_rank(MPI_Comm comm, int *rank)
462 if (comm == MPI_COMM_NULL) {
464 } else if (rank == nullptr) {
467 *rank = comm->rank();
472 int PMPI_Comm_size(MPI_Comm comm, int *size)
474 if (comm == MPI_COMM_NULL) {
476 } else if (size == nullptr) {
479 *size = comm->size();
484 int PMPI_Comm_get_name (MPI_Comm comm, char* name, int* len)
486 if (comm == MPI_COMM_NULL) {
488 } else if (name == nullptr || len == nullptr) {
491 comm->get_name(name, len);
496 int PMPI_Comm_group(MPI_Comm comm, MPI_Group * group)
498 if (comm == MPI_COMM_NULL) {
500 } else if (group == nullptr) {
503 *group = comm->group();
504 if (*group != MPI_COMM_WORLD->group() && *group != MPI_GROUP_NULL && *group != MPI_GROUP_EMPTY)
510 int PMPI_Comm_compare(MPI_Comm comm1, MPI_Comm comm2, int *result)
512 if (comm1 == MPI_COMM_NULL || comm2 == MPI_COMM_NULL) {
514 } else if (result == nullptr) {
517 if (comm1 == comm2) { /* Same communicators means same groups */
520 *result = comm1->group()->compare(comm2->group());
521 if (*result == MPI_IDENT) {
522 *result = MPI_CONGRUENT;
529 int PMPI_Comm_dup(MPI_Comm comm, MPI_Comm * newcomm)
531 if (comm == MPI_COMM_NULL) {
533 } else if (newcomm == nullptr) {
536 return comm->dup(newcomm);
540 int PMPI_Comm_create(MPI_Comm comm, MPI_Group group, MPI_Comm * newcomm)
542 if (comm == MPI_COMM_NULL) {
544 } else if (group == MPI_GROUP_NULL) {
545 return MPI_ERR_GROUP;
546 } else if (newcomm == nullptr) {
548 } else if(group->rank(smpi_process()->index())==MPI_UNDEFINED){
549 *newcomm= MPI_COMM_NULL;
553 *newcomm = new simgrid::smpi::Comm(group, nullptr);
558 int PMPI_Comm_free(MPI_Comm * comm)
560 if (comm == nullptr) {
562 } else if (*comm == MPI_COMM_NULL) {
565 simgrid::smpi::Comm::destroy(*comm);
566 *comm = MPI_COMM_NULL;
571 int PMPI_Comm_disconnect(MPI_Comm * comm)
573 /* TODO: wait until all communication in comm are done */
574 if (comm == nullptr) {
576 } else if (*comm == MPI_COMM_NULL) {
579 simgrid::smpi::Comm::destroy(*comm);
580 *comm = MPI_COMM_NULL;
585 int PMPI_Comm_split(MPI_Comm comm, int color, int key, MPI_Comm* comm_out)
590 if (comm_out == nullptr) {
591 retval = MPI_ERR_ARG;
592 } else if (comm == MPI_COMM_NULL) {
593 retval = MPI_ERR_COMM;
595 *comm_out = comm->split(color, key);
596 retval = MPI_SUCCESS;
603 int PMPI_Comm_create_group(MPI_Comm comm, MPI_Group group, int, MPI_Comm* comm_out)
608 if (comm_out == nullptr) {
609 retval = MPI_ERR_ARG;
610 } else if (comm == MPI_COMM_NULL) {
611 retval = MPI_ERR_COMM;
613 retval = MPI_Comm_create(comm, group, comm_out);
620 int PMPI_Send_init(void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm, MPI_Request * request)
625 if (request == nullptr) {
626 retval = MPI_ERR_ARG;
627 } else if (comm == MPI_COMM_NULL) {
628 retval = MPI_ERR_COMM;
629 } else if (not datatype->is_valid()) {
630 retval = MPI_ERR_TYPE;
631 } else if (dst == MPI_PROC_NULL) {
632 retval = MPI_SUCCESS;
634 *request = simgrid::smpi::Request::send_init(buf, count, datatype, dst, tag, comm);
635 retval = MPI_SUCCESS;
638 if (retval != MPI_SUCCESS && request != nullptr)
639 *request = MPI_REQUEST_NULL;
643 int PMPI_Recv_init(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm, MPI_Request * request)
648 if (request == nullptr) {
649 retval = MPI_ERR_ARG;
650 } else if (comm == MPI_COMM_NULL) {
651 retval = MPI_ERR_COMM;
652 } else if (not datatype->is_valid()) {
653 retval = MPI_ERR_TYPE;
654 } else if (src == MPI_PROC_NULL) {
655 retval = MPI_SUCCESS;
657 *request = simgrid::smpi::Request::recv_init(buf, count, datatype, src, tag, comm);
658 retval = MPI_SUCCESS;
661 if (retval != MPI_SUCCESS && request != nullptr)
662 *request = MPI_REQUEST_NULL;
666 int PMPI_Ssend_init(void* buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm, MPI_Request* request)
671 if (request == nullptr) {
672 retval = MPI_ERR_ARG;
673 } else if (comm == MPI_COMM_NULL) {
674 retval = MPI_ERR_COMM;
675 } else if (not datatype->is_valid()) {
676 retval = MPI_ERR_TYPE;
677 } else if (dst == MPI_PROC_NULL) {
678 retval = MPI_SUCCESS;
680 *request = simgrid::smpi::Request::ssend_init(buf, count, datatype, dst, tag, comm);
681 retval = MPI_SUCCESS;
684 if (retval != MPI_SUCCESS && request != nullptr)
685 *request = MPI_REQUEST_NULL;
689 int PMPI_Start(MPI_Request * request)
694 if (request == nullptr || *request == MPI_REQUEST_NULL) {
695 retval = MPI_ERR_REQUEST;
698 retval = MPI_SUCCESS;
704 int PMPI_Startall(int count, MPI_Request * requests)
708 if (requests == nullptr) {
709 retval = MPI_ERR_ARG;
711 retval = MPI_SUCCESS;
712 for (int i = 0; i < count; i++) {
713 if(requests[i] == MPI_REQUEST_NULL) {
714 retval = MPI_ERR_REQUEST;
717 if(retval != MPI_ERR_REQUEST) {
718 simgrid::smpi::Request::startall(count, requests);
725 int PMPI_Request_free(MPI_Request * request)
730 if (*request == MPI_REQUEST_NULL) {
731 retval = MPI_ERR_ARG;
733 simgrid::smpi::Request::unref(request);
734 retval = MPI_SUCCESS;
740 int PMPI_Irecv(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm, MPI_Request * request)
746 if (request == nullptr) {
747 retval = MPI_ERR_ARG;
748 } else if (comm == MPI_COMM_NULL) {
749 retval = MPI_ERR_COMM;
750 } else if (src == MPI_PROC_NULL) {
751 *request = MPI_REQUEST_NULL;
752 retval = MPI_SUCCESS;
753 } else if (src!=MPI_ANY_SOURCE && (src >= comm->group()->size() || src <0)){
754 retval = MPI_ERR_RANK;
755 } else if ((count < 0) || (buf==nullptr && count > 0)) {
756 retval = MPI_ERR_COUNT;
757 } else if (not datatype->is_valid()) {
758 retval = MPI_ERR_TYPE;
759 } else if(tag<0 && tag != MPI_ANY_TAG){
760 retval = MPI_ERR_TAG;
763 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
764 int src_traced = comm->group()->index(src);
766 instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
767 extra->type = TRACING_IRECV;
768 extra->src = src_traced;
771 extra->datatype1 = encode_datatype(datatype, &known);
772 int dt_size_send = 1;
774 dt_size_send = datatype->size();
775 extra->send_size = count*dt_size_send;
776 TRACE_smpi_ptp_in(rank, src_traced, rank, __FUNCTION__, extra);
778 *request = simgrid::smpi::Request::irecv(buf, count, datatype, src, tag, comm);
779 retval = MPI_SUCCESS;
781 TRACE_smpi_ptp_out(rank, src_traced, rank, __FUNCTION__);
785 if (retval != MPI_SUCCESS && request != nullptr)
786 *request = MPI_REQUEST_NULL;
791 int PMPI_Isend(void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm, MPI_Request * request)
796 if (request == nullptr) {
797 retval = MPI_ERR_ARG;
798 } else if (comm == MPI_COMM_NULL) {
799 retval = MPI_ERR_COMM;
800 } else if (dst == MPI_PROC_NULL) {
801 *request = MPI_REQUEST_NULL;
802 retval = MPI_SUCCESS;
803 } else if (dst >= comm->group()->size() || dst <0){
804 retval = MPI_ERR_RANK;
805 } else if ((count < 0) || (buf==nullptr && count > 0)) {
806 retval = MPI_ERR_COUNT;
807 } else if (not datatype->is_valid()) {
808 retval = MPI_ERR_TYPE;
809 } else if(tag<0 && tag != MPI_ANY_TAG){
810 retval = MPI_ERR_TAG;
812 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
813 int dst_traced = comm->group()->index(dst);
814 instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
815 extra->type = TRACING_ISEND;
817 extra->dst = dst_traced;
819 extra->datatype1 = encode_datatype(datatype, &known);
820 int dt_size_send = 1;
822 dt_size_send = datatype->size();
823 extra->send_size = count*dt_size_send;
824 TRACE_smpi_ptp_in(rank, rank, dst_traced, __FUNCTION__, extra);
825 TRACE_smpi_send(rank, rank, dst_traced, tag, count*datatype->size());
827 *request = simgrid::smpi::Request::isend(buf, count, datatype, dst, tag, comm);
828 retval = MPI_SUCCESS;
830 TRACE_smpi_ptp_out(rank, rank, dst_traced, __FUNCTION__);
834 if (retval != MPI_SUCCESS && request!=nullptr)
835 *request = MPI_REQUEST_NULL;
839 int PMPI_Issend(void* buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm, MPI_Request* request)
844 if (request == nullptr) {
845 retval = MPI_ERR_ARG;
846 } else if (comm == MPI_COMM_NULL) {
847 retval = MPI_ERR_COMM;
848 } else if (dst == MPI_PROC_NULL) {
849 *request = MPI_REQUEST_NULL;
850 retval = MPI_SUCCESS;
851 } else if (dst >= comm->group()->size() || dst <0){
852 retval = MPI_ERR_RANK;
853 } else if ((count < 0)|| (buf==nullptr && count > 0)) {
854 retval = MPI_ERR_COUNT;
855 } else if (not datatype->is_valid()) {
856 retval = MPI_ERR_TYPE;
857 } else if(tag<0 && tag != MPI_ANY_TAG){
858 retval = MPI_ERR_TAG;
860 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
861 int dst_traced = comm->group()->index(dst);
862 instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
863 extra->type = TRACING_ISSEND;
865 extra->dst = dst_traced;
867 extra->datatype1 = encode_datatype(datatype, &known);
868 int dt_size_send = 1;
870 dt_size_send = datatype->size();
871 extra->send_size = count*dt_size_send;
872 TRACE_smpi_ptp_in(rank, rank, dst_traced, __FUNCTION__, extra);
873 TRACE_smpi_send(rank, rank, dst_traced, tag, count*datatype->size());
875 *request = simgrid::smpi::Request::issend(buf, count, datatype, dst, tag, comm);
876 retval = MPI_SUCCESS;
878 TRACE_smpi_ptp_out(rank, rank, dst_traced, __FUNCTION__);
882 if (retval != MPI_SUCCESS && request!=nullptr)
883 *request = MPI_REQUEST_NULL;
887 int PMPI_Recv(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm, MPI_Status * status)
892 if (comm == MPI_COMM_NULL) {
893 retval = MPI_ERR_COMM;
894 } else if (src == MPI_PROC_NULL) {
895 simgrid::smpi::Status::empty(status);
896 status->MPI_SOURCE = MPI_PROC_NULL;
897 retval = MPI_SUCCESS;
898 } else if (src!=MPI_ANY_SOURCE && (src >= comm->group()->size() || src <0)){
899 retval = MPI_ERR_RANK;
900 } else if ((count < 0) || (buf==nullptr && count > 0)) {
901 retval = MPI_ERR_COUNT;
902 } else if (not datatype->is_valid()) {
903 retval = MPI_ERR_TYPE;
904 } else if(tag<0 && tag != MPI_ANY_TAG){
905 retval = MPI_ERR_TAG;
907 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
908 int src_traced = comm->group()->index(src);
909 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
910 extra->type = TRACING_RECV;
911 extra->src = src_traced;
914 extra->datatype1 = encode_datatype(datatype, &known);
915 int dt_size_send = 1;
917 dt_size_send = datatype->size();
918 extra->send_size = count * dt_size_send;
919 TRACE_smpi_ptp_in(rank, src_traced, rank, __FUNCTION__, extra);
921 simgrid::smpi::Request::recv(buf, count, datatype, src, tag, comm, status);
922 retval = MPI_SUCCESS;
924 // the src may not have been known at the beginning of the recv (MPI_ANY_SOURCE)
925 if (status != MPI_STATUS_IGNORE) {
926 src_traced = comm->group()->index(status->MPI_SOURCE);
927 if (not TRACE_smpi_view_internals()) {
928 TRACE_smpi_recv(rank, src_traced, rank, tag);
931 TRACE_smpi_ptp_out(rank, src_traced, rank, __FUNCTION__);
938 int PMPI_Send(void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
944 if (comm == MPI_COMM_NULL) {
945 retval = MPI_ERR_COMM;
946 } else if (dst == MPI_PROC_NULL) {
947 retval = MPI_SUCCESS;
948 } else if (dst >= comm->group()->size() || dst <0){
949 retval = MPI_ERR_RANK;
950 } else if ((count < 0) || (buf == nullptr && count > 0)) {
951 retval = MPI_ERR_COUNT;
952 } else if (not datatype->is_valid()) {
953 retval = MPI_ERR_TYPE;
954 } else if(tag < 0 && tag != MPI_ANY_TAG){
955 retval = MPI_ERR_TAG;
957 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
958 int dst_traced = comm->group()->index(dst);
959 instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
960 extra->type = TRACING_SEND;
962 extra->dst = dst_traced;
964 extra->datatype1 = encode_datatype(datatype, &known);
965 int dt_size_send = 1;
967 dt_size_send = datatype->size();
969 extra->send_size = count*dt_size_send;
970 TRACE_smpi_ptp_in(rank, rank, dst_traced, __FUNCTION__, extra);
971 if (not TRACE_smpi_view_internals()) {
972 TRACE_smpi_send(rank, rank, dst_traced, tag,count*datatype->size());
975 simgrid::smpi::Request::send(buf, count, datatype, dst, tag, comm);
976 retval = MPI_SUCCESS;
978 TRACE_smpi_ptp_out(rank, rank, dst_traced, __FUNCTION__);
985 int PMPI_Ssend(void* buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm) {
990 if (comm == MPI_COMM_NULL) {
991 retval = MPI_ERR_COMM;
992 } else if (dst == MPI_PROC_NULL) {
993 retval = MPI_SUCCESS;
994 } else if (dst >= comm->group()->size() || dst <0){
995 retval = MPI_ERR_RANK;
996 } else if ((count < 0) || (buf==nullptr && count > 0)) {
997 retval = MPI_ERR_COUNT;
998 } else if (not datatype->is_valid()) {
999 retval = MPI_ERR_TYPE;
1000 } else if(tag<0 && tag != MPI_ANY_TAG){
1001 retval = MPI_ERR_TAG;
1003 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1004 int dst_traced = comm->group()->index(dst);
1005 instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
1006 extra->type = TRACING_SSEND;
1008 extra->dst = dst_traced;
1010 extra->datatype1 = encode_datatype(datatype, &known);
1011 int dt_size_send = 1;
1013 dt_size_send = datatype->size();
1015 extra->send_size = count*dt_size_send;
1016 TRACE_smpi_ptp_in(rank, rank, dst_traced, __FUNCTION__, extra);
1017 TRACE_smpi_send(rank, rank, dst_traced, tag,count*datatype->size());
1019 simgrid::smpi::Request::ssend(buf, count, datatype, dst, tag, comm);
1020 retval = MPI_SUCCESS;
1022 TRACE_smpi_ptp_out(rank, rank, dst_traced, __FUNCTION__);
1029 int PMPI_Sendrecv(void *sendbuf, int sendcount, MPI_Datatype sendtype, int dst, int sendtag, void *recvbuf,
1030 int recvcount, MPI_Datatype recvtype, int src, int recvtag, MPI_Comm comm, MPI_Status * status)
1036 if (comm == MPI_COMM_NULL) {
1037 retval = MPI_ERR_COMM;
1038 } else if (not sendtype->is_valid() || not recvtype->is_valid()) {
1039 retval = MPI_ERR_TYPE;
1040 } else if (src == MPI_PROC_NULL || dst == MPI_PROC_NULL) {
1041 simgrid::smpi::Status::empty(status);
1042 status->MPI_SOURCE = MPI_PROC_NULL;
1043 retval = MPI_SUCCESS;
1044 }else if (dst >= comm->group()->size() || dst <0 ||
1045 (src!=MPI_ANY_SOURCE && (src >= comm->group()->size() || src <0))){
1046 retval = MPI_ERR_RANK;
1047 } else if ((sendcount < 0 || recvcount<0) ||
1048 (sendbuf==nullptr && sendcount > 0) || (recvbuf==nullptr && recvcount>0)) {
1049 retval = MPI_ERR_COUNT;
1050 } else if((sendtag<0 && sendtag != MPI_ANY_TAG)||(recvtag<0 && recvtag != MPI_ANY_TAG)){
1051 retval = MPI_ERR_TAG;
1054 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1055 int dst_traced = comm->group()->index(dst);
1056 int src_traced = comm->group()->index(src);
1057 instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
1058 extra->type = TRACING_SENDRECV;
1059 extra->src = src_traced;
1060 extra->dst = dst_traced;
1062 extra->datatype1 = encode_datatype(sendtype, &known);
1063 int dt_size_send = 1;
1065 dt_size_send = sendtype->size();
1066 extra->send_size = sendcount*dt_size_send;
1067 extra->datatype2 = encode_datatype(recvtype, &known);
1068 int dt_size_recv = 1;
1070 dt_size_recv = recvtype->size();
1071 extra->recv_size = recvcount*dt_size_recv;
1073 TRACE_smpi_ptp_in(rank, src_traced, dst_traced, __FUNCTION__, extra);
1074 TRACE_smpi_send(rank, rank, dst_traced, sendtag,sendcount*sendtype->size());
1076 simgrid::smpi::Request::sendrecv(sendbuf, sendcount, sendtype, dst, sendtag, recvbuf, recvcount, recvtype, src, recvtag, comm,
1078 retval = MPI_SUCCESS;
1080 TRACE_smpi_ptp_out(rank, src_traced, dst_traced, __FUNCTION__);
1081 TRACE_smpi_recv(rank, src_traced, rank, recvtag);
1088 int PMPI_Sendrecv_replace(void* buf, int count, MPI_Datatype datatype, int dst, int sendtag, int src, int recvtag,
1089 MPI_Comm comm, MPI_Status* status)
1092 if (not datatype->is_valid()) {
1093 return MPI_ERR_TYPE;
1094 } else if (count < 0) {
1095 return MPI_ERR_COUNT;
1097 int size = datatype->get_extent() * count;
1098 void* recvbuf = xbt_new0(char, size);
1099 retval = MPI_Sendrecv(buf, count, datatype, dst, sendtag, recvbuf, count, datatype, src, recvtag, comm, status);
1100 if(retval==MPI_SUCCESS){
1101 simgrid::smpi::Datatype::copy(recvbuf, count, datatype, buf, count, datatype);
1109 int PMPI_Test(MPI_Request * request, int *flag, MPI_Status * status)
1113 if (request == nullptr || flag == nullptr) {
1114 retval = MPI_ERR_ARG;
1115 } else if (*request == MPI_REQUEST_NULL) {
1117 simgrid::smpi::Status::empty(status);
1118 retval = MPI_SUCCESS;
1120 int rank = ((*request)->comm() != MPI_COMM_NULL) ? smpi_process()->index() : -1;
1122 instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
1123 extra->type = TRACING_TEST;
1124 TRACE_smpi_testing_in(rank, extra);
1126 *flag = simgrid::smpi::Request::test(request,status);
1128 TRACE_smpi_testing_out(rank);
1129 retval = MPI_SUCCESS;
1135 int PMPI_Testany(int count, MPI_Request requests[], int *index, int *flag, MPI_Status * status)
1140 if (index == nullptr || flag == nullptr) {
1141 retval = MPI_ERR_ARG;
1143 *flag = simgrid::smpi::Request::testany(count, requests, index, status);
1144 retval = MPI_SUCCESS;
1150 int PMPI_Testall(int count, MPI_Request* requests, int* flag, MPI_Status* statuses)
1155 if (flag == nullptr) {
1156 retval = MPI_ERR_ARG;
1158 *flag = simgrid::smpi::Request::testall(count, requests, statuses);
1159 retval = MPI_SUCCESS;
1165 int PMPI_Probe(int source, int tag, MPI_Comm comm, MPI_Status* status) {
1169 if (status == nullptr) {
1170 retval = MPI_ERR_ARG;
1171 } else if (comm == MPI_COMM_NULL) {
1172 retval = MPI_ERR_COMM;
1173 } else if (source == MPI_PROC_NULL) {
1174 simgrid::smpi::Status::empty(status);
1175 status->MPI_SOURCE = MPI_PROC_NULL;
1176 retval = MPI_SUCCESS;
1178 simgrid::smpi::Request::probe(source, tag, comm, status);
1179 retval = MPI_SUCCESS;
1185 int PMPI_Iprobe(int source, int tag, MPI_Comm comm, int* flag, MPI_Status* status) {
1189 if (flag == nullptr) {
1190 retval = MPI_ERR_ARG;
1191 } else if (comm == MPI_COMM_NULL) {
1192 retval = MPI_ERR_COMM;
1193 } else if (source == MPI_PROC_NULL) {
1195 simgrid::smpi::Status::empty(status);
1196 status->MPI_SOURCE = MPI_PROC_NULL;
1197 retval = MPI_SUCCESS;
1199 simgrid::smpi::Request::iprobe(source, tag, comm, flag, status);
1200 retval = MPI_SUCCESS;
1206 int PMPI_Wait(MPI_Request * request, MPI_Status * status)
1212 simgrid::smpi::Status::empty(status);
1214 if (request == nullptr) {
1215 retval = MPI_ERR_ARG;
1216 } else if (*request == MPI_REQUEST_NULL) {
1217 retval = MPI_SUCCESS;
1220 int rank = (request!=nullptr && (*request)->comm() != MPI_COMM_NULL) ? smpi_process()->index() : -1;
1222 int src_traced = (*request)->src();
1223 int dst_traced = (*request)->dst();
1224 int tag_traced= (*request)->tag();
1225 MPI_Comm comm = (*request)->comm();
1226 int is_wait_for_receive = ((*request)->flags() & RECV);
1227 instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
1228 extra->type = TRACING_WAIT;
1229 TRACE_smpi_ptp_in(rank, src_traced, dst_traced, __FUNCTION__, extra);
1231 simgrid::smpi::Request::wait(request, status);
1232 retval = MPI_SUCCESS;
1234 //the src may not have been known at the beginning of the recv (MPI_ANY_SOURCE)
1235 TRACE_smpi_ptp_out(rank, src_traced, dst_traced, __FUNCTION__);
1236 if (is_wait_for_receive) {
1237 if(src_traced==MPI_ANY_SOURCE)
1238 src_traced = (status!=MPI_STATUS_IGNORE) ?
1239 comm->group()->rank(status->MPI_SOURCE) :
1241 TRACE_smpi_recv(rank, src_traced, dst_traced, tag_traced);
1249 int PMPI_Waitany(int count, MPI_Request requests[], int *index, MPI_Status * status)
1251 if (index == nullptr)
1255 //save requests information for tracing
1263 savedvalstype* savedvals=nullptr;
1265 savedvals = xbt_new0(savedvalstype, count);
1267 for (int i = 0; i < count; i++) {
1268 MPI_Request req = requests[i]; //already received requests are no longer valid
1270 savedvals[i]=(savedvalstype){req->src(), req->dst(), (req->flags() & RECV), req->tag(), req->comm()};
1273 int rank_traced = smpi_process()->index();
1274 instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
1275 extra->type = TRACING_WAITANY;
1276 extra->send_size=count;
1277 TRACE_smpi_ptp_in(rank_traced, -1, -1, __FUNCTION__,extra);
1279 *index = simgrid::smpi::Request::waitany(count, requests, status);
1281 if(*index!=MPI_UNDEFINED){
1282 int src_traced = savedvals[*index].src;
1283 //the src may not have been known at the beginning of the recv (MPI_ANY_SOURCE)
1284 int dst_traced = savedvals[*index].dst;
1285 int is_wait_for_receive = savedvals[*index].recv;
1286 if (is_wait_for_receive) {
1287 if(savedvals[*index].src==MPI_ANY_SOURCE)
1288 src_traced = (status != MPI_STATUSES_IGNORE)
1289 ? savedvals[*index].comm->group()->rank(status->MPI_SOURCE)
1290 : savedvals[*index].src;
1291 TRACE_smpi_recv(rank_traced, src_traced, dst_traced, savedvals[*index].tag);
1293 TRACE_smpi_ptp_out(rank_traced, src_traced, dst_traced, __FUNCTION__);
1295 xbt_free(savedvals);
1301 int PMPI_Waitall(int count, MPI_Request requests[], MPI_Status status[])
1304 //save information from requests
1313 savedvalstype* savedvals=xbt_new0(savedvalstype, count);
1315 for (int i = 0; i < count; i++) {
1316 MPI_Request req = requests[i];
1317 if(req!=MPI_REQUEST_NULL){
1318 savedvals[i]=(savedvalstype){req->src(), req->dst(), (req->flags() & RECV), req->tag(), 1, req->comm()};
1320 savedvals[i].valid=0;
1323 int rank_traced = smpi_process()->index();
1324 instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
1325 extra->type = TRACING_WAITALL;
1326 extra->send_size=count;
1327 TRACE_smpi_ptp_in(rank_traced, -1, -1, __FUNCTION__,extra);
1329 int retval = simgrid::smpi::Request::waitall(count, requests, status);
1331 for (int i = 0; i < count; i++) {
1332 if(savedvals[i].valid){
1333 //the src may not have been known at the beginning of the recv (MPI_ANY_SOURCE)
1334 int src_traced = savedvals[i].src;
1335 int dst_traced = savedvals[i].dst;
1336 int is_wait_for_receive = savedvals[i].recv;
1337 if (is_wait_for_receive) {
1338 if(src_traced==MPI_ANY_SOURCE)
1339 src_traced = (status!=MPI_STATUSES_IGNORE) ?
1340 savedvals[i].comm->group()->rank(status[i].MPI_SOURCE) : savedvals[i].src;
1341 TRACE_smpi_recv(rank_traced, src_traced, dst_traced,savedvals[i].tag);
1345 TRACE_smpi_ptp_out(rank_traced, -1, -1, __FUNCTION__);
1346 xbt_free(savedvals);
1352 int PMPI_Waitsome(int incount, MPI_Request requests[], int *outcount, int *indices, MPI_Status status[])
1357 if (outcount == nullptr) {
1358 retval = MPI_ERR_ARG;
1360 *outcount = simgrid::smpi::Request::waitsome(incount, requests, indices, status);
1361 retval = MPI_SUCCESS;
1367 int PMPI_Testsome(int incount, MPI_Request requests[], int* outcount, int* indices, MPI_Status status[])
1372 if (outcount == nullptr) {
1373 retval = MPI_ERR_ARG;
1375 *outcount = simgrid::smpi::Request::testsome(incount, requests, indices, status);
1376 retval = MPI_SUCCESS;
1383 int PMPI_Bcast(void *buf, int count, MPI_Datatype datatype, int root, MPI_Comm comm)
1389 if (comm == MPI_COMM_NULL) {
1390 retval = MPI_ERR_COMM;
1391 } else if (not datatype->is_valid()) {
1392 retval = MPI_ERR_ARG;
1394 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1395 int root_traced = comm->group()->index(root);
1397 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
1398 extra->type = TRACING_BCAST;
1399 extra->root = root_traced;
1401 extra->datatype1 = encode_datatype(datatype, &known);
1402 int dt_size_send = 1;
1404 dt_size_send = datatype->size();
1405 extra->send_size = count * dt_size_send;
1406 TRACE_smpi_collective_in(rank, root_traced, __FUNCTION__, extra);
1407 if (comm->size() > 1)
1408 simgrid::smpi::Colls::bcast(buf, count, datatype, root, comm);
1409 retval = MPI_SUCCESS;
1411 TRACE_smpi_collective_out(rank, root_traced, __FUNCTION__);
1417 int PMPI_Barrier(MPI_Comm comm)
1423 if (comm == MPI_COMM_NULL) {
1424 retval = MPI_ERR_COMM;
1426 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1427 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
1428 extra->type = TRACING_BARRIER;
1429 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, extra);
1431 simgrid::smpi::Colls::barrier(comm);
1433 //Barrier can be used to synchronize RMA calls. Finish all requests from comm before.
1434 comm->finish_rma_calls();
1436 retval = MPI_SUCCESS;
1438 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
1445 int PMPI_Gather(void *sendbuf, int sendcount, MPI_Datatype sendtype,void *recvbuf, int recvcount, MPI_Datatype recvtype,
1446 int root, MPI_Comm comm)
1452 if (comm == MPI_COMM_NULL) {
1453 retval = MPI_ERR_COMM;
1454 } else if ((( sendbuf != MPI_IN_PLACE) && (sendtype == MPI_DATATYPE_NULL)) ||
1455 ((comm->rank() == root) && (recvtype == MPI_DATATYPE_NULL))){
1456 retval = MPI_ERR_TYPE;
1457 } else if ((( sendbuf != MPI_IN_PLACE) && (sendcount <0)) || ((comm->rank() == root) && (recvcount <0))){
1458 retval = MPI_ERR_COUNT;
1461 char* sendtmpbuf = static_cast<char*>(sendbuf);
1462 int sendtmpcount = sendcount;
1463 MPI_Datatype sendtmptype = sendtype;
1464 if( (comm->rank() == root) && (sendbuf == MPI_IN_PLACE )) {
1466 sendtmptype=recvtype;
1468 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1469 int root_traced = comm->group()->index(root);
1470 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
1471 extra->type = TRACING_GATHER;
1472 extra->root = root_traced;
1474 extra->datatype1 = encode_datatype(sendtmptype, &known);
1475 int dt_size_send = 1;
1477 dt_size_send = sendtmptype->size();
1478 extra->send_size = sendtmpcount * dt_size_send;
1479 extra->datatype2 = encode_datatype(recvtype, &known);
1480 int dt_size_recv = 1;
1481 if ((comm->rank() == root) && known == 0)
1482 dt_size_recv = recvtype->size();
1483 extra->recv_size = recvcount * dt_size_recv;
1485 TRACE_smpi_collective_in(rank, root_traced, __FUNCTION__, extra);
1487 simgrid::smpi::Colls::gather(sendtmpbuf, sendtmpcount, sendtmptype, recvbuf, recvcount, recvtype, root, comm);
1489 retval = MPI_SUCCESS;
1490 TRACE_smpi_collective_out(rank, root_traced, __FUNCTION__);
1497 int PMPI_Gatherv(void *sendbuf, int sendcount, MPI_Datatype sendtype, void *recvbuf, int *recvcounts, int *displs,
1498 MPI_Datatype recvtype, int root, MPI_Comm comm)
1504 if (comm == MPI_COMM_NULL) {
1505 retval = MPI_ERR_COMM;
1506 } else if ((( sendbuf != MPI_IN_PLACE) && (sendtype == MPI_DATATYPE_NULL)) ||
1507 ((comm->rank() == root) && (recvtype == MPI_DATATYPE_NULL))){
1508 retval = MPI_ERR_TYPE;
1509 } else if (( sendbuf != MPI_IN_PLACE) && (sendcount <0)){
1510 retval = MPI_ERR_COUNT;
1511 } else if (recvcounts == nullptr || displs == nullptr) {
1512 retval = MPI_ERR_ARG;
1514 char* sendtmpbuf = static_cast<char*>(sendbuf);
1515 int sendtmpcount = sendcount;
1516 MPI_Datatype sendtmptype = sendtype;
1517 if( (comm->rank() == root) && (sendbuf == MPI_IN_PLACE )) {
1519 sendtmptype=recvtype;
1522 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1523 int root_traced = comm->group()->index(root);
1524 int size = comm->size();
1525 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
1526 extra->type = TRACING_GATHERV;
1527 extra->num_processes = size;
1528 extra->root = root_traced;
1530 extra->datatype1 = encode_datatype(sendtmptype, &known);
1531 int dt_size_send = 1;
1533 dt_size_send = sendtype->size();
1534 extra->send_size = sendtmpcount * dt_size_send;
1535 extra->datatype2 = encode_datatype(recvtype, &known);
1536 int dt_size_recv = 1;
1538 dt_size_recv = recvtype->size();
1539 if (comm->rank() == root) {
1540 extra->recvcounts = xbt_new(int, size);
1541 for (int i = 0; i < size; i++) // copy data to avoid bad free
1542 extra->recvcounts[i] = recvcounts[i] * dt_size_recv;
1544 TRACE_smpi_collective_in(rank, root_traced, __FUNCTION__, extra);
1546 retval = simgrid::smpi::Colls::gatherv(sendtmpbuf, sendtmpcount, sendtmptype, recvbuf, recvcounts, displs, recvtype, root, comm);
1547 TRACE_smpi_collective_out(rank, root_traced, __FUNCTION__);
1554 int PMPI_Allgather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
1555 void *recvbuf, int recvcount, MPI_Datatype recvtype, MPI_Comm comm)
1561 if (comm == MPI_COMM_NULL) {
1562 retval = MPI_ERR_COMM;
1563 } else if ((( sendbuf != MPI_IN_PLACE) && (sendtype == MPI_DATATYPE_NULL)) ||
1564 (recvtype == MPI_DATATYPE_NULL)){
1565 retval = MPI_ERR_TYPE;
1566 } else if ((( sendbuf != MPI_IN_PLACE) && (sendcount <0)) ||
1568 retval = MPI_ERR_COUNT;
1570 if(sendbuf == MPI_IN_PLACE) {
1571 sendbuf=static_cast<char*>(recvbuf)+recvtype->get_extent()*recvcount*comm->rank();
1572 sendcount=recvcount;
1575 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1576 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
1577 extra->type = TRACING_ALLGATHER;
1579 extra->datatype1 = encode_datatype(sendtype, &known);
1580 int dt_size_send = 1;
1582 dt_size_send = sendtype->size();
1583 extra->send_size = sendcount * dt_size_send;
1584 extra->datatype2 = encode_datatype(recvtype, &known);
1585 int dt_size_recv = 1;
1587 dt_size_recv = recvtype->size();
1588 extra->recv_size = recvcount * dt_size_recv;
1590 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, extra);
1592 simgrid::smpi::Colls::allgather(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, comm);
1593 retval = MPI_SUCCESS;
1594 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
1600 int PMPI_Allgatherv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
1601 void *recvbuf, int *recvcounts, int *displs, MPI_Datatype recvtype, MPI_Comm comm)
1607 if (comm == MPI_COMM_NULL) {
1608 retval = MPI_ERR_COMM;
1609 } else if (((sendbuf != MPI_IN_PLACE) && (sendtype == MPI_DATATYPE_NULL)) || (recvtype == MPI_DATATYPE_NULL)) {
1610 retval = MPI_ERR_TYPE;
1611 } else if (( sendbuf != MPI_IN_PLACE) && (sendcount <0)){
1612 retval = MPI_ERR_COUNT;
1613 } else if (recvcounts == nullptr || displs == nullptr) {
1614 retval = MPI_ERR_ARG;
1617 if(sendbuf == MPI_IN_PLACE) {
1618 sendbuf=static_cast<char*>(recvbuf)+recvtype->get_extent()*displs[comm->rank()];
1619 sendcount=recvcounts[comm->rank()];
1622 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1624 int size = comm->size();
1625 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
1626 extra->type = TRACING_ALLGATHERV;
1627 extra->num_processes = size;
1629 extra->datatype1 = encode_datatype(sendtype, &known);
1630 int dt_size_send = 1;
1632 dt_size_send = sendtype->size();
1633 extra->send_size = sendcount * dt_size_send;
1634 extra->datatype2 = encode_datatype(recvtype, &known);
1635 int dt_size_recv = 1;
1637 dt_size_recv = recvtype->size();
1638 extra->recvcounts = xbt_new(int, size);
1639 for (i = 0; i < size; i++) // copy data to avoid bad free
1640 extra->recvcounts[i] = recvcounts[i] * dt_size_recv;
1642 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, extra);
1644 simgrid::smpi::Colls::allgatherv(sendbuf, sendcount, sendtype, recvbuf, recvcounts, displs, recvtype, comm);
1645 retval = MPI_SUCCESS;
1646 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
1653 int PMPI_Scatter(void *sendbuf, int sendcount, MPI_Datatype sendtype,
1654 void *recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm)
1660 if (comm == MPI_COMM_NULL) {
1661 retval = MPI_ERR_COMM;
1662 } else if (((comm->rank() == root) && (not sendtype->is_valid())) ||
1663 ((recvbuf != MPI_IN_PLACE) && (not recvtype->is_valid()))) {
1664 retval = MPI_ERR_TYPE;
1665 } else if ((sendbuf == recvbuf) ||
1666 ((comm->rank()==root) && sendcount>0 && (sendbuf == nullptr))){
1667 retval = MPI_ERR_BUFFER;
1670 if (recvbuf == MPI_IN_PLACE) {
1671 recvtype = sendtype;
1672 recvcount = sendcount;
1674 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1675 int root_traced = comm->group()->index(root);
1676 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
1677 extra->type = TRACING_SCATTER;
1678 extra->root = root_traced;
1680 extra->datatype1 = encode_datatype(sendtype, &known);
1681 int dt_size_send = 1;
1682 if ((comm->rank() == root) && known == 0)
1683 dt_size_send = sendtype->size();
1684 extra->send_size = sendcount * dt_size_send;
1685 extra->datatype2 = encode_datatype(recvtype, &known);
1686 int dt_size_recv = 1;
1688 dt_size_recv = recvtype->size();
1689 extra->recv_size = recvcount * dt_size_recv;
1690 TRACE_smpi_collective_in(rank, root_traced, __FUNCTION__, extra);
1692 simgrid::smpi::Colls::scatter(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, root, comm);
1693 retval = MPI_SUCCESS;
1694 TRACE_smpi_collective_out(rank, root_traced, __FUNCTION__);
1701 int PMPI_Scatterv(void *sendbuf, int *sendcounts, int *displs,
1702 MPI_Datatype sendtype, void *recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm)
1708 if (comm == MPI_COMM_NULL) {
1709 retval = MPI_ERR_COMM;
1710 } else if (sendcounts == nullptr || displs == nullptr) {
1711 retval = MPI_ERR_ARG;
1712 } else if (((comm->rank() == root) && (sendtype == MPI_DATATYPE_NULL)) ||
1713 ((recvbuf != MPI_IN_PLACE) && (recvtype == MPI_DATATYPE_NULL))) {
1714 retval = MPI_ERR_TYPE;
1716 if (recvbuf == MPI_IN_PLACE) {
1717 recvtype = sendtype;
1718 recvcount = sendcounts[comm->rank()];
1720 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1721 int root_traced = comm->group()->index(root);
1722 int size = comm->size();
1723 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
1724 extra->type = TRACING_SCATTERV;
1725 extra->num_processes = size;
1726 extra->root = root_traced;
1728 extra->datatype1 = encode_datatype(sendtype, &known);
1729 int dt_size_send = 1;
1731 dt_size_send = sendtype->size();
1732 if (comm->rank() == root) {
1733 extra->sendcounts = xbt_new(int, size);
1734 for (int i = 0; i < size; i++) // copy data to avoid bad free
1735 extra->sendcounts[i] = sendcounts[i] * dt_size_send;
1737 extra->datatype2 = encode_datatype(recvtype, &known);
1738 int dt_size_recv = 1;
1740 dt_size_recv = recvtype->size();
1741 extra->recv_size = recvcount * dt_size_recv;
1742 TRACE_smpi_collective_in(rank, root_traced, __FUNCTION__, extra);
1744 retval = simgrid::smpi::Colls::scatterv(sendbuf, sendcounts, displs, sendtype, recvbuf, recvcount, recvtype, root, comm);
1746 TRACE_smpi_collective_out(rank, root_traced, __FUNCTION__);
1753 int PMPI_Reduce(void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, int root, MPI_Comm comm)
1759 if (comm == MPI_COMM_NULL) {
1760 retval = MPI_ERR_COMM;
1761 } else if (not datatype->is_valid() || op == MPI_OP_NULL) {
1762 retval = MPI_ERR_ARG;
1764 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1765 int root_traced = comm->group()->index(root);
1766 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
1767 extra->type = TRACING_REDUCE;
1769 extra->datatype1 = encode_datatype(datatype, &known);
1770 int dt_size_send = 1;
1772 dt_size_send = datatype->size();
1773 extra->send_size = count * dt_size_send;
1774 extra->root = root_traced;
1776 TRACE_smpi_collective_in(rank, root_traced, __FUNCTION__, extra);
1778 simgrid::smpi::Colls::reduce(sendbuf, recvbuf, count, datatype, op, root, comm);
1780 retval = MPI_SUCCESS;
1781 TRACE_smpi_collective_out(rank, root_traced, __FUNCTION__);
1788 int PMPI_Reduce_local(void *inbuf, void *inoutbuf, int count, MPI_Datatype datatype, MPI_Op op){
1792 if (not datatype->is_valid() || op == MPI_OP_NULL) {
1793 retval = MPI_ERR_ARG;
1795 op->apply(inbuf, inoutbuf, &count, datatype);
1796 retval = MPI_SUCCESS;
1802 int PMPI_Allreduce(void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
1808 if (comm == MPI_COMM_NULL) {
1809 retval = MPI_ERR_COMM;
1810 } else if (not datatype->is_valid()) {
1811 retval = MPI_ERR_TYPE;
1812 } else if (op == MPI_OP_NULL) {
1813 retval = MPI_ERR_OP;
1816 char* sendtmpbuf = static_cast<char*>(sendbuf);
1817 if( sendbuf == MPI_IN_PLACE ) {
1818 sendtmpbuf = static_cast<char*>(xbt_malloc(count*datatype->get_extent()));
1819 simgrid::smpi::Datatype::copy(recvbuf, count, datatype,sendtmpbuf, count, datatype);
1821 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1822 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
1823 extra->type = TRACING_ALLREDUCE;
1825 extra->datatype1 = encode_datatype(datatype, &known);
1826 int dt_size_send = 1;
1828 dt_size_send = datatype->size();
1829 extra->send_size = count * dt_size_send;
1831 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, extra);
1833 simgrid::smpi::Colls::allreduce(sendtmpbuf, recvbuf, count, datatype, op, comm);
1835 if( sendbuf == MPI_IN_PLACE )
1836 xbt_free(sendtmpbuf);
1838 retval = MPI_SUCCESS;
1839 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
1846 int PMPI_Scan(void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
1852 if (comm == MPI_COMM_NULL) {
1853 retval = MPI_ERR_COMM;
1854 } else if (not datatype->is_valid()) {
1855 retval = MPI_ERR_TYPE;
1856 } else if (op == MPI_OP_NULL) {
1857 retval = MPI_ERR_OP;
1859 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1860 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
1861 extra->type = TRACING_SCAN;
1863 extra->datatype1 = encode_datatype(datatype, &known);
1864 int dt_size_send = 1;
1866 dt_size_send = datatype->size();
1867 extra->send_size = count * dt_size_send;
1869 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, extra);
1871 retval = simgrid::smpi::Colls::scan(sendbuf, recvbuf, count, datatype, op, comm);
1873 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
1880 int PMPI_Exscan(void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm){
1885 if (comm == MPI_COMM_NULL) {
1886 retval = MPI_ERR_COMM;
1887 } else if (not datatype->is_valid()) {
1888 retval = MPI_ERR_TYPE;
1889 } else if (op == MPI_OP_NULL) {
1890 retval = MPI_ERR_OP;
1892 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1893 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
1894 extra->type = TRACING_EXSCAN;
1896 extra->datatype1 = encode_datatype(datatype, &known);
1897 int dt_size_send = 1;
1899 dt_size_send = datatype->size();
1900 extra->send_size = count * dt_size_send;
1901 void* sendtmpbuf = sendbuf;
1902 if (sendbuf == MPI_IN_PLACE) {
1903 sendtmpbuf = static_cast<void*>(xbt_malloc(count * datatype->size()));
1904 memcpy(sendtmpbuf, recvbuf, count * datatype->size());
1906 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, extra);
1908 retval = simgrid::smpi::Colls::exscan(sendtmpbuf, recvbuf, count, datatype, op, comm);
1910 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
1911 if (sendbuf == MPI_IN_PLACE)
1912 xbt_free(sendtmpbuf);
1919 int PMPI_Reduce_scatter(void *sendbuf, void *recvbuf, int *recvcounts, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
1924 if (comm == MPI_COMM_NULL) {
1925 retval = MPI_ERR_COMM;
1926 } else if (not datatype->is_valid()) {
1927 retval = MPI_ERR_TYPE;
1928 } else if (op == MPI_OP_NULL) {
1929 retval = MPI_ERR_OP;
1930 } else if (recvcounts == nullptr) {
1931 retval = MPI_ERR_ARG;
1933 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1935 int size = comm->size();
1936 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
1937 extra->type = TRACING_REDUCE_SCATTER;
1938 extra->num_processes = size;
1940 extra->datatype1 = encode_datatype(datatype, &known);
1941 int dt_size_send = 1;
1943 dt_size_send = datatype->size();
1944 extra->send_size = 0;
1945 extra->recvcounts = xbt_new(int, size);
1947 for (i = 0; i < size; i++) { // copy data to avoid bad free
1948 extra->recvcounts[i] = recvcounts[i] * dt_size_send;
1949 totalcount += recvcounts[i];
1951 void* sendtmpbuf = sendbuf;
1952 if (sendbuf == MPI_IN_PLACE) {
1953 sendtmpbuf = static_cast<void*>(xbt_malloc(totalcount * datatype->size()));
1954 memcpy(sendtmpbuf, recvbuf, totalcount * datatype->size());
1957 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, extra);
1959 simgrid::smpi::Colls::reduce_scatter(sendtmpbuf, recvbuf, recvcounts, datatype, op, comm);
1960 retval = MPI_SUCCESS;
1961 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
1963 if (sendbuf == MPI_IN_PLACE)
1964 xbt_free(sendtmpbuf);
1971 int PMPI_Reduce_scatter_block(void *sendbuf, void *recvbuf, int recvcount,
1972 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
1977 if (comm == MPI_COMM_NULL) {
1978 retval = MPI_ERR_COMM;
1979 } else if (not datatype->is_valid()) {
1980 retval = MPI_ERR_TYPE;
1981 } else if (op == MPI_OP_NULL) {
1982 retval = MPI_ERR_OP;
1983 } else if (recvcount < 0) {
1984 retval = MPI_ERR_ARG;
1986 int count = comm->size();
1988 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1989 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
1990 extra->type = TRACING_REDUCE_SCATTER;
1991 extra->num_processes = count;
1993 extra->datatype1 = encode_datatype(datatype, &known);
1994 int dt_size_send = 1;
1996 dt_size_send = datatype->size();
1997 extra->send_size = 0;
1998 extra->recvcounts = xbt_new(int, count);
1999 for (int i = 0; i < count; i++) // copy data to avoid bad free
2000 extra->recvcounts[i] = recvcount * dt_size_send;
2001 void* sendtmpbuf = sendbuf;
2002 if (sendbuf == MPI_IN_PLACE) {
2003 sendtmpbuf = static_cast<void*>(xbt_malloc(recvcount * count * datatype->size()));
2004 memcpy(sendtmpbuf, recvbuf, recvcount * count * datatype->size());
2007 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, extra);
2009 int* recvcounts = static_cast<int*>(xbt_malloc(count * sizeof(int)));
2010 for (int i = 0; i < count; i++)
2011 recvcounts[i] = recvcount;
2012 simgrid::smpi::Colls::reduce_scatter(sendtmpbuf, recvbuf, recvcounts, datatype, op, comm);
2013 xbt_free(recvcounts);
2014 retval = MPI_SUCCESS;
2016 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
2018 if (sendbuf == MPI_IN_PLACE)
2019 xbt_free(sendtmpbuf);
2026 int PMPI_Alltoall(void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int recvcount,
2027 MPI_Datatype recvtype, MPI_Comm comm)
2032 if (comm == MPI_COMM_NULL) {
2033 retval = MPI_ERR_COMM;
2034 } else if ((sendbuf != MPI_IN_PLACE && sendtype == MPI_DATATYPE_NULL) || recvtype == MPI_DATATYPE_NULL) {
2035 retval = MPI_ERR_TYPE;
2037 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
2038 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
2039 extra->type = TRACING_ALLTOALL;
2041 void* sendtmpbuf = static_cast<char*>(sendbuf);
2042 int sendtmpcount = sendcount;
2043 MPI_Datatype sendtmptype = sendtype;
2044 if (sendbuf == MPI_IN_PLACE) {
2045 sendtmpbuf = static_cast<void*>(xbt_malloc(recvcount * comm->size() * recvtype->size()));
2046 memcpy(sendtmpbuf, recvbuf, recvcount * comm->size() * recvtype->size());
2047 sendtmpcount = recvcount;
2048 sendtmptype = recvtype;
2052 extra->datatype1 = encode_datatype(sendtmptype, &known);
2054 extra->send_size = sendtmpcount * sendtmptype->size();
2056 extra->send_size = sendtmpcount;
2057 extra->datatype2 = encode_datatype(recvtype, &known);
2059 extra->recv_size = recvcount * recvtype->size();
2061 extra->recv_size = recvcount;
2063 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, extra);
2065 retval = simgrid::smpi::Colls::alltoall(sendtmpbuf, sendtmpcount, sendtmptype, recvbuf, recvcount, recvtype, comm);
2067 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
2069 if (sendbuf == MPI_IN_PLACE)
2070 xbt_free(sendtmpbuf);
2077 int PMPI_Alltoallv(void* sendbuf, int* sendcounts, int* senddisps, MPI_Datatype sendtype, void* recvbuf,
2078 int* recvcounts, int* recvdisps, MPI_Datatype recvtype, MPI_Comm comm)
2084 if (comm == MPI_COMM_NULL) {
2085 retval = MPI_ERR_COMM;
2086 } else if (sendtype == MPI_DATATYPE_NULL || recvtype == MPI_DATATYPE_NULL) {
2087 retval = MPI_ERR_TYPE;
2088 } else if ((sendbuf != MPI_IN_PLACE && (sendcounts == nullptr || senddisps == nullptr)) || recvcounts == nullptr ||
2089 recvdisps == nullptr) {
2090 retval = MPI_ERR_ARG;
2092 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
2094 int size = comm->size();
2095 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
2096 extra->type = TRACING_ALLTOALLV;
2097 extra->send_size = 0;
2098 extra->recv_size = 0;
2099 extra->recvcounts = xbt_new(int, size);
2100 extra->sendcounts = xbt_new(int, size);
2102 extra->datatype2 = encode_datatype(recvtype, &known);
2103 int dt_size_recv = recvtype->size();
2105 void* sendtmpbuf = static_cast<char*>(sendbuf);
2106 int* sendtmpcounts = sendcounts;
2107 int* sendtmpdisps = senddisps;
2108 MPI_Datatype sendtmptype = sendtype;
2110 for (i = 0; i < size; i++) { // copy data to avoid bad free
2111 extra->recv_size += recvcounts[i] * dt_size_recv;
2112 extra->recvcounts[i] = recvcounts[i] * dt_size_recv;
2113 if (((recvdisps[i] + recvcounts[i]) * dt_size_recv) > maxsize)
2114 maxsize = (recvdisps[i] + recvcounts[i]) * dt_size_recv;
2117 if (sendbuf == MPI_IN_PLACE) {
2118 sendtmpbuf = static_cast<void*>(xbt_malloc(maxsize));
2119 memcpy(sendtmpbuf, recvbuf, maxsize);
2120 sendtmpcounts = static_cast<int*>(xbt_malloc(size * sizeof(int)));
2121 memcpy(sendtmpcounts, recvcounts, size * sizeof(int));
2122 sendtmpdisps = static_cast<int*>(xbt_malloc(size * sizeof(int)));
2123 memcpy(sendtmpdisps, recvdisps, size * sizeof(int));
2124 sendtmptype = recvtype;
2127 extra->datatype1 = encode_datatype(sendtmptype, &known);
2128 int dt_size_send = 1;
2129 dt_size_send = sendtmptype->size();
2131 for (i = 0; i < size; i++) { // copy data to avoid bad free
2132 extra->send_size += sendtmpcounts[i] * dt_size_send;
2133 extra->sendcounts[i] = sendtmpcounts[i] * dt_size_send;
2135 extra->num_processes = size;
2136 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, extra);
2137 retval = simgrid::smpi::Colls::alltoallv(sendtmpbuf, sendtmpcounts, sendtmpdisps, sendtmptype, recvbuf, recvcounts,
2138 recvdisps, recvtype, comm);
2139 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
2141 if (sendbuf == MPI_IN_PLACE) {
2142 xbt_free(sendtmpbuf);
2143 xbt_free(sendtmpcounts);
2144 xbt_free(sendtmpdisps);
2153 int PMPI_Get_processor_name(char *name, int *resultlen)
2155 strncpy(name, SIMIX_host_self()->cname(), strlen(SIMIX_host_self()->cname()) < MPI_MAX_PROCESSOR_NAME - 1
2156 ? strlen(SIMIX_host_self()->cname()) + 1
2157 : MPI_MAX_PROCESSOR_NAME - 1);
2158 *resultlen = strlen(name) > MPI_MAX_PROCESSOR_NAME ? MPI_MAX_PROCESSOR_NAME : strlen(name);
2163 int PMPI_Get_count(MPI_Status * status, MPI_Datatype datatype, int *count)
2165 if (status == nullptr || count == nullptr) {
2167 } else if (not datatype->is_valid()) {
2168 return MPI_ERR_TYPE;
2170 size_t size = datatype->size();
2174 } else if (status->count % size != 0) {
2175 return MPI_UNDEFINED;
2177 *count = simgrid::smpi::Status::get_count(status, datatype);
2183 int PMPI_Type_contiguous(int count, MPI_Datatype old_type, MPI_Datatype* new_type) {
2184 if (old_type == MPI_DATATYPE_NULL) {
2185 return MPI_ERR_TYPE;
2186 } else if (count<0){
2187 return MPI_ERR_COUNT;
2189 return simgrid::smpi::Datatype::create_contiguous(count, old_type, 0, new_type);
2193 int PMPI_Type_commit(MPI_Datatype* datatype) {
2194 if (datatype == nullptr || *datatype == MPI_DATATYPE_NULL) {
2195 return MPI_ERR_TYPE;
2197 (*datatype)->commit();
2202 int PMPI_Type_vector(int count, int blocklen, int stride, MPI_Datatype old_type, MPI_Datatype* new_type) {
2203 if (old_type == MPI_DATATYPE_NULL) {
2204 return MPI_ERR_TYPE;
2205 } else if (count<0 || blocklen<0){
2206 return MPI_ERR_COUNT;
2208 return simgrid::smpi::Datatype::create_vector(count, blocklen, stride, old_type, new_type);
2212 int PMPI_Type_hvector(int count, int blocklen, MPI_Aint stride, MPI_Datatype old_type, MPI_Datatype* new_type) {
2213 if (old_type == MPI_DATATYPE_NULL) {
2214 return MPI_ERR_TYPE;
2215 } else if (count<0 || blocklen<0){
2216 return MPI_ERR_COUNT;
2218 return simgrid::smpi::Datatype::create_hvector(count, blocklen, stride, old_type, new_type);
2222 int PMPI_Type_create_hvector(int count, int blocklen, MPI_Aint stride, MPI_Datatype old_type, MPI_Datatype* new_type) {
2223 return MPI_Type_hvector(count, blocklen, stride, old_type, new_type);
2226 int PMPI_Type_indexed(int count, int* blocklens, int* indices, MPI_Datatype old_type, MPI_Datatype* new_type) {
2227 if (old_type == MPI_DATATYPE_NULL) {
2228 return MPI_ERR_TYPE;
2229 } else if (count<0){
2230 return MPI_ERR_COUNT;
2232 return simgrid::smpi::Datatype::create_indexed(count, blocklens, indices, old_type, new_type);
2236 int PMPI_Type_create_indexed(int count, int* blocklens, int* indices, MPI_Datatype old_type, MPI_Datatype* new_type) {
2237 if (old_type == MPI_DATATYPE_NULL) {
2238 return MPI_ERR_TYPE;
2239 } else if (count<0){
2240 return MPI_ERR_COUNT;
2242 return simgrid::smpi::Datatype::create_indexed(count, blocklens, indices, old_type, new_type);
2246 int PMPI_Type_create_indexed_block(int count, int blocklength, int* indices, MPI_Datatype old_type,
2247 MPI_Datatype* new_type)
2249 if (old_type == MPI_DATATYPE_NULL) {
2250 return MPI_ERR_TYPE;
2251 } else if (count<0){
2252 return MPI_ERR_COUNT;
2254 int* blocklens=static_cast<int*>(xbt_malloc(blocklength*count*sizeof(int)));
2255 for (int i = 0; i < count; i++)
2256 blocklens[i]=blocklength;
2257 int retval = simgrid::smpi::Datatype::create_indexed(count, blocklens, indices, old_type, new_type);
2258 xbt_free(blocklens);
2263 int PMPI_Type_hindexed(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype old_type, MPI_Datatype* new_type)
2265 if (old_type == MPI_DATATYPE_NULL) {
2266 return MPI_ERR_TYPE;
2267 } else if (count<0){
2268 return MPI_ERR_COUNT;
2270 return simgrid::smpi::Datatype::create_hindexed(count, blocklens, indices, old_type, new_type);
2274 int PMPI_Type_create_hindexed(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype old_type,
2275 MPI_Datatype* new_type) {
2276 return PMPI_Type_hindexed(count, blocklens,indices,old_type,new_type);
2279 int PMPI_Type_create_hindexed_block(int count, int blocklength, MPI_Aint* indices, MPI_Datatype old_type,
2280 MPI_Datatype* new_type) {
2281 if (old_type == MPI_DATATYPE_NULL) {
2282 return MPI_ERR_TYPE;
2283 } else if (count<0){
2284 return MPI_ERR_COUNT;
2286 int* blocklens=(int*)xbt_malloc(blocklength*count*sizeof(int));
2287 for (int i = 0; i < count; i++)
2288 blocklens[i] = blocklength;
2289 int retval = simgrid::smpi::Datatype::create_hindexed(count, blocklens, indices, old_type, new_type);
2290 xbt_free(blocklens);
2295 int PMPI_Type_struct(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype* old_types, MPI_Datatype* new_type) {
2297 return MPI_ERR_COUNT;
2299 return simgrid::smpi::Datatype::create_struct(count, blocklens, indices, old_types, new_type);
2303 int PMPI_Type_create_struct(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype* old_types,
2304 MPI_Datatype* new_type) {
2305 return PMPI_Type_struct(count, blocklens, indices, old_types, new_type);
2308 int PMPI_Error_class(int errorcode, int* errorclass) {
2309 // assume smpi uses only standard mpi error codes
2310 *errorclass=errorcode;
2314 int PMPI_Initialized(int* flag) {
2315 *flag=(smpi_process()!=nullptr && smpi_process()->initialized());
2319 /* The topo part of MPI_COMM_WORLD should always be nullptr. When other topologies will be implemented, not only should we
2320 * check if the topology is nullptr, but we should check if it is the good topology type (so we have to add a
2321 * MPIR_Topo_Type field, and replace the MPI_Topology field by an union)*/
2323 int PMPI_Cart_create(MPI_Comm comm_old, int ndims, int* dims, int* periodic, int reorder, MPI_Comm* comm_cart) {
2324 if (comm_old == MPI_COMM_NULL){
2325 return MPI_ERR_COMM;
2326 } else if (ndims < 0 || (ndims > 0 && (dims == nullptr || periodic == nullptr)) || comm_cart == nullptr) {
2329 simgrid::smpi::Topo_Cart* topo = new simgrid::smpi::Topo_Cart(comm_old, ndims, dims, periodic, reorder, comm_cart);
2330 if(*comm_cart==MPI_COMM_NULL)
2336 int PMPI_Cart_rank(MPI_Comm comm, int* coords, int* rank) {
2337 if(comm == MPI_COMM_NULL || comm->topo() == nullptr) {
2338 return MPI_ERR_TOPOLOGY;
2340 if (coords == nullptr) {
2343 MPIR_Cart_Topology topo = static_cast<MPIR_Cart_Topology>(comm->topo());
2344 if (topo==nullptr) {
2347 return topo->rank(coords, rank);
2350 int PMPI_Cart_shift(MPI_Comm comm, int direction, int displ, int* source, int* dest) {
2351 if(comm == MPI_COMM_NULL || comm->topo() == nullptr) {
2352 return MPI_ERR_TOPOLOGY;
2354 if (source == nullptr || dest == nullptr || direction < 0 ) {
2357 MPIR_Cart_Topology topo = static_cast<MPIR_Cart_Topology>(comm->topo());
2358 if (topo==nullptr) {
2361 return topo->shift(direction, displ, source, dest);
2364 int PMPI_Cart_coords(MPI_Comm comm, int rank, int maxdims, int* coords) {
2365 if(comm == MPI_COMM_NULL || comm->topo() == nullptr) {
2366 return MPI_ERR_TOPOLOGY;
2368 if (rank < 0 || rank >= comm->size()) {
2369 return MPI_ERR_RANK;
2374 if(coords == nullptr) {
2377 MPIR_Cart_Topology topo = static_cast<MPIR_Cart_Topology>(comm->topo());
2378 if (topo==nullptr) {
2381 return topo->coords(rank, maxdims, coords);
2384 int PMPI_Cart_get(MPI_Comm comm, int maxdims, int* dims, int* periods, int* coords) {
2385 if(comm == nullptr || comm->topo() == nullptr) {
2386 return MPI_ERR_TOPOLOGY;
2388 if(maxdims <= 0 || dims == nullptr || periods == nullptr || coords == nullptr) {
2391 MPIR_Cart_Topology topo = static_cast<MPIR_Cart_Topology>(comm->topo());
2392 if (topo==nullptr) {
2395 return topo->get(maxdims, dims, periods, coords);
2398 int PMPI_Cartdim_get(MPI_Comm comm, int* ndims) {
2399 if (comm == MPI_COMM_NULL || comm->topo() == nullptr) {
2400 return MPI_ERR_TOPOLOGY;
2402 if (ndims == nullptr) {
2405 MPIR_Cart_Topology topo = static_cast<MPIR_Cart_Topology>(comm->topo());
2406 if (topo==nullptr) {
2409 return topo->dim_get(ndims);
2412 int PMPI_Dims_create(int nnodes, int ndims, int* dims) {
2413 if(dims == nullptr) {
2416 if (ndims < 1 || nnodes < 1) {
2417 return MPI_ERR_DIMS;
2419 return simgrid::smpi::Topo_Cart::Dims_create(nnodes, ndims, dims);
2422 int PMPI_Cart_sub(MPI_Comm comm, int* remain_dims, MPI_Comm* comm_new) {
2423 if(comm == MPI_COMM_NULL || comm->topo() == nullptr) {
2424 return MPI_ERR_TOPOLOGY;
2426 if (comm_new == nullptr) {
2429 MPIR_Cart_Topology topo = static_cast<MPIR_Cart_Topology>(comm->topo());
2430 if (topo==nullptr) {
2433 MPIR_Cart_Topology cart = topo->sub(remain_dims, comm_new);
2434 if(*comm_new==MPI_COMM_NULL)
2441 int PMPI_Type_create_resized(MPI_Datatype oldtype,MPI_Aint lb, MPI_Aint extent, MPI_Datatype *newtype){
2442 if (oldtype == MPI_DATATYPE_NULL) {
2443 return MPI_ERR_TYPE;
2445 int blocks[3] = {1, 1, 1};
2446 MPI_Aint disps[3] = {lb, 0, lb + extent};
2447 MPI_Datatype types[3] = {MPI_LB, oldtype, MPI_UB};
2449 *newtype = new simgrid::smpi::Type_Struct(oldtype->size(), lb, lb + extent, DT_FLAG_DERIVED, 3, blocks, disps, types);
2451 (*newtype)->addflag(~DT_FLAG_COMMITED);
2455 int PMPI_Win_create( void *base, MPI_Aint size, int disp_unit, MPI_Info info, MPI_Comm comm, MPI_Win *win){
2458 if (comm == MPI_COMM_NULL) {
2459 retval= MPI_ERR_COMM;
2460 }else if ((base == nullptr && size != 0) || disp_unit <= 0 || size < 0 ){
2461 retval= MPI_ERR_OTHER;
2463 *win = new simgrid::smpi::Win( base, size, disp_unit, info, comm);
2464 retval = MPI_SUCCESS;
2470 int PMPI_Win_allocate( MPI_Aint size, int disp_unit, MPI_Info info, MPI_Comm comm, void *base, MPI_Win *win){
2473 if (comm == MPI_COMM_NULL) {
2474 retval= MPI_ERR_COMM;
2475 }else if (disp_unit <= 0 || size < 0 ){
2476 retval= MPI_ERR_OTHER;
2478 void* ptr = xbt_malloc(size);
2480 return MPI_ERR_NO_MEM;
2481 *static_cast<void**>(base) = ptr;
2482 *win = new simgrid::smpi::Win( ptr, size, disp_unit, info, comm,1);
2483 retval = MPI_SUCCESS;
2489 int PMPI_Win_create_dynamic( MPI_Info info, MPI_Comm comm, MPI_Win *win){
2492 if (comm == MPI_COMM_NULL) {
2493 retval= MPI_ERR_COMM;
2495 *win = new simgrid::smpi::Win(info, comm);
2496 retval = MPI_SUCCESS;
2502 int PMPI_Win_attach(MPI_Win win, void *base, MPI_Aint size){
2505 if(win == MPI_WIN_NULL){
2506 retval = MPI_ERR_WIN;
2507 } else if ((base == nullptr && size != 0) || size < 0 ){
2508 retval= MPI_ERR_OTHER;
2510 retval = win->attach(base, size);
2516 int PMPI_Win_detach(MPI_Win win, void *base){
2519 if(win == MPI_WIN_NULL){
2520 retval = MPI_ERR_WIN;
2521 } else if (base == nullptr){
2522 retval= MPI_ERR_OTHER;
2524 retval = win->detach(base);
2531 int PMPI_Win_free( MPI_Win* win){
2534 if (win == nullptr || *win == MPI_WIN_NULL) {
2535 retval = MPI_ERR_WIN;
2544 int PMPI_Win_set_name(MPI_Win win, char * name)
2546 if (win == MPI_WIN_NULL) {
2547 return MPI_ERR_TYPE;
2548 } else if (name == nullptr) {
2551 win->set_name(name);
2556 int PMPI_Win_get_name(MPI_Win win, char * name, int* len)
2558 if (win == MPI_WIN_NULL) {
2560 } else if (name == nullptr) {
2563 win->get_name(name, len);
2568 int PMPI_Win_get_info(MPI_Win win, MPI_Info* info)
2570 if (win == MPI_WIN_NULL) {
2573 *info = win->info();
2578 int PMPI_Win_set_info(MPI_Win win, MPI_Info info)
2580 if (win == MPI_WIN_NULL) {
2581 return MPI_ERR_TYPE;
2583 win->set_info(info);
2588 int PMPI_Win_get_group(MPI_Win win, MPI_Group * group){
2589 if (win == MPI_WIN_NULL) {
2592 win->get_group(group);
2598 int PMPI_Win_fence( int assert, MPI_Win win){
2601 if (win == MPI_WIN_NULL) {
2602 retval = MPI_ERR_WIN;
2604 int rank = smpi_process()->index();
2605 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, nullptr);
2606 retval = win->fence(assert);
2607 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
2613 int PMPI_Get( void *origin_addr, int origin_count, MPI_Datatype origin_datatype, int target_rank,
2614 MPI_Aint target_disp, int target_count, MPI_Datatype target_datatype, MPI_Win win){
2617 if (win == MPI_WIN_NULL) {
2618 retval = MPI_ERR_WIN;
2619 } else if (target_rank == MPI_PROC_NULL) {
2620 retval = MPI_SUCCESS;
2621 } else if (target_rank <0){
2622 retval = MPI_ERR_RANK;
2623 } else if (win->dynamic()==0 && target_disp <0){
2624 //in case of dynamic window, target_disp can be mistakenly seen as negative, as it is an address
2625 retval = MPI_ERR_ARG;
2626 } else if ((origin_count < 0 || target_count < 0) ||
2627 (origin_addr==nullptr && origin_count > 0)){
2628 retval = MPI_ERR_COUNT;
2629 } else if ((not origin_datatype->is_valid()) || (not target_datatype->is_valid())) {
2630 retval = MPI_ERR_TYPE;
2632 int rank = smpi_process()->index();
2634 win->get_group(&group);
2635 int src_traced = group->index(target_rank);
2636 TRACE_smpi_ptp_in(rank, src_traced, rank, __FUNCTION__, nullptr);
2638 retval = win->get( origin_addr, origin_count, origin_datatype, target_rank, target_disp, target_count,
2641 TRACE_smpi_ptp_out(rank, src_traced, rank, __FUNCTION__);
2647 int PMPI_Rget( void *origin_addr, int origin_count, MPI_Datatype origin_datatype, int target_rank,
2648 MPI_Aint target_disp, int target_count, MPI_Datatype target_datatype, MPI_Win win, MPI_Request* request){
2651 if (win == MPI_WIN_NULL) {
2652 retval = MPI_ERR_WIN;
2653 } else if (target_rank == MPI_PROC_NULL) {
2654 *request = MPI_REQUEST_NULL;
2655 retval = MPI_SUCCESS;
2656 } else if (target_rank <0){
2657 retval = MPI_ERR_RANK;
2658 } else if (win->dynamic()==0 && target_disp <0){
2659 //in case of dynamic window, target_disp can be mistakenly seen as negative, as it is an address
2660 retval = MPI_ERR_ARG;
2661 } else if ((origin_count < 0 || target_count < 0) ||
2662 (origin_addr==nullptr && origin_count > 0)){
2663 retval = MPI_ERR_COUNT;
2664 } else if ((not origin_datatype->is_valid()) || (not target_datatype->is_valid())) {
2665 retval = MPI_ERR_TYPE;
2666 } else if(request == nullptr){
2667 retval = MPI_ERR_REQUEST;
2669 int rank = smpi_process()->index();
2671 win->get_group(&group);
2672 int src_traced = group->index(target_rank);
2673 TRACE_smpi_ptp_in(rank, src_traced, rank, __FUNCTION__, nullptr);
2675 retval = win->get( origin_addr, origin_count, origin_datatype, target_rank, target_disp, target_count,
2676 target_datatype, request);
2678 TRACE_smpi_ptp_out(rank, src_traced, rank, __FUNCTION__);
2684 int PMPI_Put( void *origin_addr, int origin_count, MPI_Datatype origin_datatype, int target_rank,
2685 MPI_Aint target_disp, int target_count, MPI_Datatype target_datatype, MPI_Win win){
2688 if (win == MPI_WIN_NULL) {
2689 retval = MPI_ERR_WIN;
2690 } else if (target_rank == MPI_PROC_NULL) {
2691 retval = MPI_SUCCESS;
2692 } else if (target_rank <0){
2693 retval = MPI_ERR_RANK;
2694 } else if (win->dynamic()==0 && target_disp <0){
2695 //in case of dynamic window, target_disp can be mistakenly seen as negative, as it is an address
2696 retval = MPI_ERR_ARG;
2697 } else if ((origin_count < 0 || target_count < 0) ||
2698 (origin_addr==nullptr && origin_count > 0)){
2699 retval = MPI_ERR_COUNT;
2700 } else if ((not origin_datatype->is_valid()) || (not target_datatype->is_valid())) {
2701 retval = MPI_ERR_TYPE;
2703 int rank = smpi_process()->index();
2705 win->get_group(&group);
2706 int dst_traced = group->index(target_rank);
2707 TRACE_smpi_ptp_in(rank, rank, dst_traced, __FUNCTION__, nullptr);
2708 TRACE_smpi_send(rank, rank, dst_traced, SMPI_RMA_TAG, origin_count*origin_datatype->size());
2710 retval = win->put( origin_addr, origin_count, origin_datatype, target_rank, target_disp, target_count,
2713 TRACE_smpi_ptp_out(rank, rank, dst_traced, __FUNCTION__);
2719 int PMPI_Rput( void *origin_addr, int origin_count, MPI_Datatype origin_datatype, int target_rank,
2720 MPI_Aint target_disp, int target_count, MPI_Datatype target_datatype, MPI_Win win, MPI_Request* request){
2723 if (win == MPI_WIN_NULL) {
2724 retval = MPI_ERR_WIN;
2725 } else if (target_rank == MPI_PROC_NULL) {
2726 *request = MPI_REQUEST_NULL;
2727 retval = MPI_SUCCESS;
2728 } else if (target_rank <0){
2729 retval = MPI_ERR_RANK;
2730 } else if (win->dynamic()==0 && target_disp <0){
2731 //in case of dynamic window, target_disp can be mistakenly seen as negative, as it is an address
2732 retval = MPI_ERR_ARG;
2733 } else if ((origin_count < 0 || target_count < 0) ||
2734 (origin_addr==nullptr && origin_count > 0)){
2735 retval = MPI_ERR_COUNT;
2736 } else if ((not origin_datatype->is_valid()) || (not target_datatype->is_valid())) {
2737 retval = MPI_ERR_TYPE;
2738 } else if(request == nullptr){
2739 retval = MPI_ERR_REQUEST;
2741 int rank = smpi_process()->index();
2743 win->get_group(&group);
2744 int dst_traced = group->index(target_rank);
2745 TRACE_smpi_ptp_in(rank, rank, dst_traced, __FUNCTION__, nullptr);
2746 TRACE_smpi_send(rank, rank, dst_traced, SMPI_RMA_TAG, origin_count*origin_datatype->size());
2748 retval = win->put( origin_addr, origin_count, origin_datatype, target_rank, target_disp, target_count,
2749 target_datatype, request);
2751 TRACE_smpi_ptp_out(rank, rank, dst_traced, __FUNCTION__);
2757 int PMPI_Accumulate( void *origin_addr, int origin_count, MPI_Datatype origin_datatype, int target_rank,
2758 MPI_Aint target_disp, int target_count, MPI_Datatype target_datatype, MPI_Op op, MPI_Win win){
2761 if (win == MPI_WIN_NULL) {
2762 retval = MPI_ERR_WIN;
2763 } else if (target_rank == MPI_PROC_NULL) {
2764 retval = MPI_SUCCESS;
2765 } else if (target_rank <0){
2766 retval = MPI_ERR_RANK;
2767 } else if (win->dynamic()==0 && target_disp <0){
2768 //in case of dynamic window, target_disp can be mistakenly seen as negative, as it is an address
2769 retval = MPI_ERR_ARG;
2770 } else if ((origin_count < 0 || target_count < 0) ||
2771 (origin_addr==nullptr && origin_count > 0)){
2772 retval = MPI_ERR_COUNT;
2773 } else if ((not origin_datatype->is_valid()) || (not target_datatype->is_valid())) {
2774 retval = MPI_ERR_TYPE;
2775 } else if (op == MPI_OP_NULL) {
2776 retval = MPI_ERR_OP;
2778 int rank = smpi_process()->index();
2780 win->get_group(&group);
2781 int src_traced = group->index(target_rank);
2782 TRACE_smpi_ptp_in(rank, src_traced, rank, __FUNCTION__, nullptr);
2784 retval = win->accumulate( origin_addr, origin_count, origin_datatype, target_rank, target_disp, target_count,
2785 target_datatype, op);
2787 TRACE_smpi_ptp_out(rank, src_traced, rank, __FUNCTION__);
2793 int PMPI_Raccumulate( void *origin_addr, int origin_count, MPI_Datatype origin_datatype, int target_rank,
2794 MPI_Aint target_disp, int target_count, MPI_Datatype target_datatype, MPI_Op op, MPI_Win win, MPI_Request* request){
2797 if (win == MPI_WIN_NULL) {
2798 retval = MPI_ERR_WIN;
2799 } else if (target_rank == MPI_PROC_NULL) {
2800 *request = MPI_REQUEST_NULL;
2801 retval = MPI_SUCCESS;
2802 } else if (target_rank <0){
2803 retval = MPI_ERR_RANK;
2804 } else if (win->dynamic()==0 && target_disp <0){
2805 //in case of dynamic window, target_disp can be mistakenly seen as negative, as it is an address
2806 retval = MPI_ERR_ARG;
2807 } else if ((origin_count < 0 || target_count < 0) ||
2808 (origin_addr==nullptr && origin_count > 0)){
2809 retval = MPI_ERR_COUNT;
2810 } else if ((not origin_datatype->is_valid()) || (not target_datatype->is_valid())) {
2811 retval = MPI_ERR_TYPE;
2812 } else if (op == MPI_OP_NULL) {
2813 retval = MPI_ERR_OP;
2814 } else if(request == nullptr){
2815 retval = MPI_ERR_REQUEST;
2817 int rank = smpi_process()->index();
2819 win->get_group(&group);
2820 int src_traced = group->index(target_rank);
2821 TRACE_smpi_ptp_in(rank, src_traced, rank, __FUNCTION__, nullptr);
2823 retval = win->accumulate( origin_addr, origin_count, origin_datatype, target_rank, target_disp, target_count,
2824 target_datatype, op, request);
2826 TRACE_smpi_ptp_out(rank, src_traced, rank, __FUNCTION__);
2832 int PMPI_Get_accumulate(void *origin_addr, int origin_count, MPI_Datatype origin_datatype, void *result_addr,
2833 int result_count, MPI_Datatype result_datatype, int target_rank, MPI_Aint target_disp, int target_count,
2834 MPI_Datatype target_datatype, MPI_Op op, MPI_Win win){
2837 if (win == MPI_WIN_NULL) {
2838 retval = MPI_ERR_WIN;
2839 } else if (target_rank == MPI_PROC_NULL) {
2840 retval = MPI_SUCCESS;
2841 } else if (target_rank <0){
2842 retval = MPI_ERR_RANK;
2843 } else if (win->dynamic()==0 && target_disp <0){
2844 //in case of dynamic window, target_disp can be mistakenly seen as negative, as it is an address
2845 retval = MPI_ERR_ARG;
2846 } else if ((origin_count < 0 || target_count < 0 || result_count <0) ||
2847 (origin_addr==nullptr && origin_count > 0 && op != MPI_NO_OP) ||
2848 (result_addr==nullptr && result_count > 0)){
2849 retval = MPI_ERR_COUNT;
2850 } else if ((origin_datatype != MPI_DATATYPE_NULL && not origin_datatype->is_valid()) ||
2851 (not target_datatype->is_valid()) || (not result_datatype->is_valid())) {
2852 retval = MPI_ERR_TYPE;
2853 } else if (op == MPI_OP_NULL) {
2854 retval = MPI_ERR_OP;
2856 int rank = smpi_process()->index();
2858 win->get_group(&group);
2859 int src_traced = group->index(target_rank);
2860 TRACE_smpi_ptp_in(rank, src_traced, rank, __FUNCTION__, nullptr);
2862 retval = win->get_accumulate( origin_addr, origin_count, origin_datatype, result_addr,
2863 result_count, result_datatype, target_rank, target_disp,
2864 target_count, target_datatype, op);
2866 TRACE_smpi_ptp_out(rank, src_traced, rank, __FUNCTION__);
2873 int PMPI_Rget_accumulate(void *origin_addr, int origin_count, MPI_Datatype origin_datatype, void *result_addr,
2874 int result_count, MPI_Datatype result_datatype, int target_rank, MPI_Aint target_disp, int target_count,
2875 MPI_Datatype target_datatype, MPI_Op op, MPI_Win win, MPI_Request* request){
2878 if (win == MPI_WIN_NULL) {
2879 retval = MPI_ERR_WIN;
2880 } else if (target_rank == MPI_PROC_NULL) {
2881 *request = MPI_REQUEST_NULL;
2882 retval = MPI_SUCCESS;
2883 } else if (target_rank <0){
2884 retval = MPI_ERR_RANK;
2885 } else if (win->dynamic()==0 && target_disp <0){
2886 //in case of dynamic window, target_disp can be mistakenly seen as negative, as it is an address
2887 retval = MPI_ERR_ARG;
2888 } else if ((origin_count < 0 || target_count < 0 || result_count <0) ||
2889 (origin_addr==nullptr && origin_count > 0 && op != MPI_NO_OP) ||
2890 (result_addr==nullptr && result_count > 0)){
2891 retval = MPI_ERR_COUNT;
2892 } else if ((origin_datatype != MPI_DATATYPE_NULL && not origin_datatype->is_valid()) ||
2893 (not target_datatype->is_valid()) || (not result_datatype->is_valid())) {
2894 retval = MPI_ERR_TYPE;
2895 } else if (op == MPI_OP_NULL) {
2896 retval = MPI_ERR_OP;
2897 } else if(request == nullptr){
2898 retval = MPI_ERR_REQUEST;
2900 int rank = smpi_process()->index();
2902 win->get_group(&group);
2903 int src_traced = group->index(target_rank);
2904 TRACE_smpi_ptp_in(rank, src_traced, rank, __FUNCTION__, nullptr);
2906 retval = win->get_accumulate( origin_addr, origin_count, origin_datatype, result_addr,
2907 result_count, result_datatype, target_rank, target_disp,
2908 target_count, target_datatype, op, request);
2910 TRACE_smpi_ptp_out(rank, src_traced, rank, __FUNCTION__);
2916 int PMPI_Fetch_and_op(void *origin_addr, void *result_addr, MPI_Datatype dtype, int target_rank, MPI_Aint target_disp, MPI_Op op, MPI_Win win){
2917 return PMPI_Get_accumulate(origin_addr, origin_addr==nullptr?0:1, dtype, result_addr, 1, dtype, target_rank, target_disp, 1, dtype, op, win);
2920 int PMPI_Compare_and_swap(void *origin_addr, void *compare_addr,
2921 void *result_addr, MPI_Datatype datatype, int target_rank,
2922 MPI_Aint target_disp, MPI_Win win){
2925 if (win == MPI_WIN_NULL) {
2926 retval = MPI_ERR_WIN;
2927 } else if (target_rank == MPI_PROC_NULL) {
2928 retval = MPI_SUCCESS;
2929 } else if (target_rank <0){
2930 retval = MPI_ERR_RANK;
2931 } else if (win->dynamic()==0 && target_disp <0){
2932 //in case of dynamic window, target_disp can be mistakenly seen as negative, as it is an address
2933 retval = MPI_ERR_ARG;
2934 } else if (origin_addr==nullptr || result_addr==nullptr || compare_addr==nullptr){
2935 retval = MPI_ERR_COUNT;
2936 } else if (not datatype->is_valid()) {
2937 retval = MPI_ERR_TYPE;
2939 int rank = smpi_process()->index();
2941 win->get_group(&group);
2942 int src_traced = group->index(target_rank);
2943 TRACE_smpi_ptp_in(rank, src_traced, rank, __FUNCTION__, nullptr);
2945 retval = win->compare_and_swap( origin_addr, compare_addr, result_addr, datatype,
2946 target_rank, target_disp);
2948 TRACE_smpi_ptp_out(rank, src_traced, rank, __FUNCTION__);
2954 int PMPI_Win_post(MPI_Group group, int assert, MPI_Win win){
2957 if (win == MPI_WIN_NULL) {
2958 retval = MPI_ERR_WIN;
2959 } else if (group==MPI_GROUP_NULL){
2960 retval = MPI_ERR_GROUP;
2962 int rank = smpi_process()->index();
2963 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, nullptr);
2964 retval = win->post(group,assert);
2965 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
2971 int PMPI_Win_start(MPI_Group group, int assert, MPI_Win win){
2974 if (win == MPI_WIN_NULL) {
2975 retval = MPI_ERR_WIN;
2976 } else if (group==MPI_GROUP_NULL){
2977 retval = MPI_ERR_GROUP;
2979 int rank = smpi_process()->index();
2980 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, nullptr);
2981 retval = win->start(group,assert);
2982 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
2988 int PMPI_Win_complete(MPI_Win win){
2991 if (win == MPI_WIN_NULL) {
2992 retval = MPI_ERR_WIN;
2994 int rank = smpi_process()->index();
2995 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, nullptr);
2997 retval = win->complete();
2999 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
3005 int PMPI_Win_wait(MPI_Win win){
3008 if (win == MPI_WIN_NULL) {
3009 retval = MPI_ERR_WIN;
3011 int rank = smpi_process()->index();
3012 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, nullptr);
3014 retval = win->wait();
3016 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
3022 int PMPI_Win_lock(int lock_type, int rank, int assert, MPI_Win win){
3025 if (win == MPI_WIN_NULL) {
3026 retval = MPI_ERR_WIN;
3027 } else if (lock_type != MPI_LOCK_EXCLUSIVE &&
3028 lock_type != MPI_LOCK_SHARED) {
3029 retval = MPI_ERR_LOCKTYPE;
3030 } else if (rank == MPI_PROC_NULL){
3031 retval = MPI_SUCCESS;
3033 int myrank = smpi_process()->index();
3034 TRACE_smpi_collective_in(myrank, -1, __FUNCTION__, nullptr);
3035 retval = win->lock(lock_type,rank,assert);
3036 TRACE_smpi_collective_out(myrank, -1, __FUNCTION__);
3042 int PMPI_Win_unlock(int rank, MPI_Win win){
3045 if (win == MPI_WIN_NULL) {
3046 retval = MPI_ERR_WIN;
3047 } else if (rank == MPI_PROC_NULL){
3048 retval = MPI_SUCCESS;
3050 int myrank = smpi_process()->index();
3051 TRACE_smpi_collective_in(myrank, -1, __FUNCTION__, nullptr);
3052 retval = win->unlock(rank);
3053 TRACE_smpi_collective_out(myrank, -1, __FUNCTION__);
3059 int PMPI_Win_lock_all(int assert, MPI_Win win){
3062 if (win == MPI_WIN_NULL) {
3063 retval = MPI_ERR_WIN;
3065 int myrank = smpi_process()->index();
3066 TRACE_smpi_collective_in(myrank, -1, __FUNCTION__, nullptr);
3067 retval = win->lock_all(assert);
3068 TRACE_smpi_collective_out(myrank, -1, __FUNCTION__);
3074 int PMPI_Win_unlock_all(MPI_Win win){
3077 if (win == MPI_WIN_NULL) {
3078 retval = MPI_ERR_WIN;
3080 int myrank = smpi_process()->index();
3081 TRACE_smpi_collective_in(myrank, -1, __FUNCTION__, nullptr);
3082 retval = win->unlock_all();
3083 TRACE_smpi_collective_out(myrank, -1, __FUNCTION__);
3089 int PMPI_Win_flush(int rank, MPI_Win win){
3092 if (win == MPI_WIN_NULL) {
3093 retval = MPI_ERR_WIN;
3094 } else if (rank == MPI_PROC_NULL){
3095 retval = MPI_SUCCESS;
3097 int myrank = smpi_process()->index();
3098 TRACE_smpi_collective_in(myrank, -1, __FUNCTION__, nullptr);
3099 retval = win->flush(rank);
3100 TRACE_smpi_collective_out(myrank, -1, __FUNCTION__);
3106 int PMPI_Win_flush_local(int rank, MPI_Win win){
3109 if (win == MPI_WIN_NULL) {
3110 retval = MPI_ERR_WIN;
3111 } else if (rank == MPI_PROC_NULL){
3112 retval = MPI_SUCCESS;
3114 int myrank = smpi_process()->index();
3115 TRACE_smpi_collective_in(myrank, -1, __FUNCTION__, nullptr);
3116 retval = win->flush_local(rank);
3117 TRACE_smpi_collective_out(myrank, -1, __FUNCTION__);
3123 int PMPI_Win_flush_all(MPI_Win win){
3126 if (win == MPI_WIN_NULL) {
3127 retval = MPI_ERR_WIN;
3129 int myrank = smpi_process()->index();
3130 TRACE_smpi_collective_in(myrank, -1, __FUNCTION__, nullptr);
3131 retval = win->flush_all();
3132 TRACE_smpi_collective_out(myrank, -1, __FUNCTION__);
3138 int PMPI_Win_flush_local_all(MPI_Win win){
3141 if (win == MPI_WIN_NULL) {
3142 retval = MPI_ERR_WIN;
3144 int myrank = smpi_process()->index();
3145 TRACE_smpi_collective_in(myrank, -1, __FUNCTION__, nullptr);
3146 retval = win->flush_local_all();
3147 TRACE_smpi_collective_out(myrank, -1, __FUNCTION__);
3153 int PMPI_Alloc_mem(MPI_Aint size, MPI_Info info, void *baseptr){
3154 void *ptr = xbt_malloc(size);
3156 return MPI_ERR_NO_MEM;
3158 *static_cast<void**>(baseptr) = ptr;
3163 int PMPI_Free_mem(void *baseptr){
3168 int PMPI_Type_set_name(MPI_Datatype datatype, char * name)
3170 if (datatype == MPI_DATATYPE_NULL) {
3171 return MPI_ERR_TYPE;
3172 } else if (name == nullptr) {
3175 datatype->set_name(name);
3180 int PMPI_Type_get_name(MPI_Datatype datatype, char * name, int* len)
3182 if (datatype == MPI_DATATYPE_NULL) {
3183 return MPI_ERR_TYPE;
3184 } else if (name == nullptr) {
3187 datatype->get_name(name, len);
3192 MPI_Datatype PMPI_Type_f2c(MPI_Fint datatype){
3193 return static_cast<MPI_Datatype>(simgrid::smpi::F2C::f2c(datatype));
3196 MPI_Fint PMPI_Type_c2f(MPI_Datatype datatype){
3197 return datatype->c2f();
3200 MPI_Group PMPI_Group_f2c(MPI_Fint group){
3201 return simgrid::smpi::Group::f2c(group);
3204 MPI_Fint PMPI_Group_c2f(MPI_Group group){
3205 return group->c2f();
3208 MPI_Request PMPI_Request_f2c(MPI_Fint request){
3209 return static_cast<MPI_Request>(simgrid::smpi::Request::f2c(request));
3212 MPI_Fint PMPI_Request_c2f(MPI_Request request) {
3213 return request->c2f();
3216 MPI_Win PMPI_Win_f2c(MPI_Fint win){
3217 return static_cast<MPI_Win>(simgrid::smpi::Win::f2c(win));
3220 MPI_Fint PMPI_Win_c2f(MPI_Win win){
3224 MPI_Op PMPI_Op_f2c(MPI_Fint op){
3225 return static_cast<MPI_Op>(simgrid::smpi::Op::f2c(op));
3228 MPI_Fint PMPI_Op_c2f(MPI_Op op){
3232 MPI_Comm PMPI_Comm_f2c(MPI_Fint comm){
3233 return static_cast<MPI_Comm>(simgrid::smpi::Comm::f2c(comm));
3236 MPI_Fint PMPI_Comm_c2f(MPI_Comm comm){
3240 MPI_Info PMPI_Info_f2c(MPI_Fint info){
3241 return static_cast<MPI_Info>(simgrid::smpi::Info::f2c(info));
3244 MPI_Fint PMPI_Info_c2f(MPI_Info info){
3248 int PMPI_Keyval_create(MPI_Copy_function* copy_fn, MPI_Delete_function* delete_fn, int* keyval, void* extra_state) {
3249 smpi_copy_fn _copy_fn={copy_fn,nullptr,nullptr};
3250 smpi_delete_fn _delete_fn={delete_fn,nullptr,nullptr};
3251 return simgrid::smpi::Keyval::keyval_create<simgrid::smpi::Comm>(_copy_fn, _delete_fn, keyval, extra_state);
3254 int PMPI_Keyval_free(int* keyval) {
3255 return simgrid::smpi::Keyval::keyval_free<simgrid::smpi::Comm>(keyval);
3258 int PMPI_Attr_delete(MPI_Comm comm, int keyval) {
3259 if(keyval == MPI_TAG_UB||keyval == MPI_HOST||keyval == MPI_IO ||keyval == MPI_WTIME_IS_GLOBAL||keyval == MPI_APPNUM
3260 ||keyval == MPI_UNIVERSE_SIZE||keyval == MPI_LASTUSEDCODE)
3262 else if (comm==MPI_COMM_NULL)
3263 return MPI_ERR_COMM;
3265 return comm->attr_delete<simgrid::smpi::Comm>(keyval);
3268 int PMPI_Attr_get(MPI_Comm comm, int keyval, void* attr_value, int* flag) {
3270 static int zero = 0;
3271 static int tag_ub = INT_MAX;
3272 static int last_used_code = MPI_ERR_LASTCODE;
3274 if (comm==MPI_COMM_NULL){
3276 return MPI_ERR_COMM;
3284 *static_cast<int**>(attr_value) = &zero;
3286 case MPI_UNIVERSE_SIZE:
3288 *static_cast<int**>(attr_value) = &smpi_universe_size;
3290 case MPI_LASTUSEDCODE:
3292 *static_cast<int**>(attr_value) = &last_used_code;
3296 *static_cast<int**>(attr_value) = &tag_ub;
3298 case MPI_WTIME_IS_GLOBAL:
3300 *static_cast<int**>(attr_value) = &one;
3303 return comm->attr_get<simgrid::smpi::Comm>(keyval, attr_value, flag);
3307 int PMPI_Attr_put(MPI_Comm comm, int keyval, void* attr_value) {
3308 if(keyval == MPI_TAG_UB||keyval == MPI_HOST||keyval == MPI_IO ||keyval == MPI_WTIME_IS_GLOBAL||keyval == MPI_APPNUM
3309 ||keyval == MPI_UNIVERSE_SIZE||keyval == MPI_LASTUSEDCODE)
3311 else if (comm==MPI_COMM_NULL)
3312 return MPI_ERR_COMM;
3314 return comm->attr_put<simgrid::smpi::Comm>(keyval, attr_value);
3317 int PMPI_Comm_get_attr (MPI_Comm comm, int comm_keyval, void *attribute_val, int *flag)
3319 return PMPI_Attr_get(comm, comm_keyval, attribute_val,flag);
3322 int PMPI_Comm_set_attr (MPI_Comm comm, int comm_keyval, void *attribute_val)
3324 return PMPI_Attr_put(comm, comm_keyval, attribute_val);
3327 int PMPI_Comm_delete_attr (MPI_Comm comm, int comm_keyval)
3329 return PMPI_Attr_delete(comm, comm_keyval);
3332 int PMPI_Comm_create_keyval(MPI_Comm_copy_attr_function* copy_fn, MPI_Comm_delete_attr_function* delete_fn, int* keyval,
3335 return PMPI_Keyval_create(copy_fn, delete_fn, keyval, extra_state);
3338 int PMPI_Comm_free_keyval(int* keyval) {
3339 return PMPI_Keyval_free(keyval);
3342 int PMPI_Type_get_attr (MPI_Datatype type, int type_keyval, void *attribute_val, int* flag)
3344 if (type==MPI_DATATYPE_NULL)
3345 return MPI_ERR_TYPE;
3347 return type->attr_get<simgrid::smpi::Datatype>(type_keyval, attribute_val, flag);
3350 int PMPI_Type_set_attr (MPI_Datatype type, int type_keyval, void *attribute_val)
3352 if (type==MPI_DATATYPE_NULL)
3353 return MPI_ERR_TYPE;
3355 return type->attr_put<simgrid::smpi::Datatype>(type_keyval, attribute_val);
3358 int PMPI_Type_delete_attr (MPI_Datatype type, int type_keyval)
3360 if (type==MPI_DATATYPE_NULL)
3361 return MPI_ERR_TYPE;
3363 return type->attr_delete<simgrid::smpi::Datatype>(type_keyval);
3366 int PMPI_Type_create_keyval(MPI_Type_copy_attr_function* copy_fn, MPI_Type_delete_attr_function* delete_fn, int* keyval,
3369 smpi_copy_fn _copy_fn={nullptr,copy_fn,nullptr};
3370 smpi_delete_fn _delete_fn={nullptr,delete_fn,nullptr};
3371 return simgrid::smpi::Keyval::keyval_create<simgrid::smpi::Datatype>(_copy_fn, _delete_fn, keyval, extra_state);
3374 int PMPI_Type_free_keyval(int* keyval) {
3375 return simgrid::smpi::Keyval::keyval_free<simgrid::smpi::Datatype>(keyval);
3378 int PMPI_Win_get_attr (MPI_Win win, int keyval, void *attribute_val, int* flag)
3380 static MPI_Aint size;
3381 static int disp_unit;
3382 if (win==MPI_WIN_NULL)
3383 return MPI_ERR_TYPE;
3387 *static_cast<void**>(attribute_val) = win->base();
3392 *static_cast<MPI_Aint**>(attribute_val) = &size;
3395 case MPI_WIN_DISP_UNIT :
3396 disp_unit=win->disp_unit();
3397 *static_cast<int**>(attribute_val) = &disp_unit;
3401 return win->attr_get<simgrid::smpi::Win>(keyval, attribute_val, flag);
3407 int PMPI_Win_set_attr (MPI_Win win, int type_keyval, void *attribute_val)
3409 if (win==MPI_WIN_NULL)
3410 return MPI_ERR_TYPE;
3412 return win->attr_put<simgrid::smpi::Win>(type_keyval, attribute_val);
3415 int PMPI_Win_delete_attr (MPI_Win win, int type_keyval)
3417 if (win==MPI_WIN_NULL)
3418 return MPI_ERR_TYPE;
3420 return win->attr_delete<simgrid::smpi::Win>(type_keyval);
3423 int PMPI_Win_create_keyval(MPI_Win_copy_attr_function* copy_fn, MPI_Win_delete_attr_function* delete_fn, int* keyval,
3426 smpi_copy_fn _copy_fn={nullptr, nullptr, copy_fn};
3427 smpi_delete_fn _delete_fn={nullptr, nullptr, delete_fn};
3428 return simgrid::smpi::Keyval::keyval_create<simgrid::smpi::Win>(_copy_fn, _delete_fn, keyval, extra_state);
3431 int PMPI_Win_free_keyval(int* keyval) {
3432 return simgrid::smpi::Keyval::keyval_free<simgrid::smpi::Win>(keyval);
3435 int PMPI_Info_create( MPI_Info *info){
3436 if (info == nullptr)
3438 *info = new simgrid::smpi::Info();
3442 int PMPI_Info_set( MPI_Info info, char *key, char *value){
3443 if (info == nullptr || key == nullptr || value == nullptr)
3445 info->set(key, value);
3449 int PMPI_Info_free( MPI_Info *info){
3450 if (info == nullptr || *info==nullptr)
3452 simgrid::smpi::Info::unref(*info);
3453 *info=MPI_INFO_NULL;
3457 int PMPI_Info_get(MPI_Info info,char *key,int valuelen, char *value, int *flag){
3459 if (info == nullptr || key == nullptr || valuelen <0)
3461 if (value == nullptr)
3462 return MPI_ERR_INFO_VALUE;
3463 return info->get(key, valuelen, value, flag);
3466 int PMPI_Info_dup(MPI_Info info, MPI_Info *newinfo){
3467 if (info == nullptr || newinfo==nullptr)
3469 *newinfo = new simgrid::smpi::Info(info);
3473 int PMPI_Info_delete(MPI_Info info, char *key){
3474 if (info == nullptr || key==nullptr)
3476 return info->remove(key);
3479 int PMPI_Info_get_nkeys( MPI_Info info, int *nkeys){
3480 if (info == nullptr || nkeys==nullptr)
3482 return info->get_nkeys(nkeys);
3485 int PMPI_Info_get_nthkey( MPI_Info info, int n, char *key){
3486 if (info == nullptr || key==nullptr || n<0 || n> MPI_MAX_INFO_KEY)
3488 return info->get_nthkey(n, key);
3491 int PMPI_Info_get_valuelen( MPI_Info info, char *key, int *valuelen, int *flag){
3493 if (info == nullptr || key == nullptr || valuelen==nullptr)
3495 return info->get_valuelen(key, valuelen, flag);
3498 int PMPI_Unpack(void* inbuf, int incount, int* position, void* outbuf, int outcount, MPI_Datatype type, MPI_Comm comm) {
3499 if(incount<0 || outcount < 0 || inbuf==nullptr || outbuf==nullptr)
3501 if (not type->is_valid())
3502 return MPI_ERR_TYPE;
3503 if(comm==MPI_COMM_NULL)
3504 return MPI_ERR_COMM;
3505 return type->unpack(inbuf, incount, position, outbuf,outcount, comm);
3508 int PMPI_Pack(void* inbuf, int incount, MPI_Datatype type, void* outbuf, int outcount, int* position, MPI_Comm comm) {
3509 if(incount<0 || outcount < 0|| inbuf==nullptr || outbuf==nullptr)
3511 if (not type->is_valid())
3512 return MPI_ERR_TYPE;
3513 if(comm==MPI_COMM_NULL)
3514 return MPI_ERR_COMM;
3515 return type->pack(inbuf, incount, outbuf,outcount,position, comm);
3518 int PMPI_Pack_size(int incount, MPI_Datatype datatype, MPI_Comm comm, int* size) {
3521 if (not datatype->is_valid())
3522 return MPI_ERR_TYPE;
3523 if(comm==MPI_COMM_NULL)
3524 return MPI_ERR_COMM;
3526 *size=incount*datatype->size();