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. */
8 #include "simgrid/s4u/Engine.hpp"
9 #include "simgrid/s4u/Host.hpp"
10 #include "src/smpi/private.h"
11 #include "src/smpi/smpi_comm.hpp"
12 #include "src/smpi/smpi_coll.hpp"
13 #include "src/smpi/smpi_datatype_derived.hpp"
14 #include "src/smpi/smpi_op.hpp"
15 #include "src/smpi/smpi_process.hpp"
16 #include "src/smpi/smpi_request.hpp"
17 #include "src/smpi/smpi_status.hpp"
18 #include "src/smpi/smpi_win.hpp"
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_pmpi, smpi, "Logging specific to SMPI (pmpi)");
22 //this function need to be here because of the calls to smpi_bench
23 void TRACE_smpi_set_category(const char *category)
25 //need to end bench otherwise categories for execution tasks are wrong
27 TRACE_internal_smpi_set_category (category);
28 //begin bench after changing process's category
32 /* PMPI User level calls */
33 extern "C" { // Obviously, the C MPI interface should use the C linkage
35 int PMPI_Init(int *argc, char ***argv)
37 xbt_assert(simgrid::s4u::Engine::isInitialized(),
38 "Your MPI program was not properly initialized. The easiest is to use smpirun to start it.");
39 // PMPI_Init is called only once per SMPI process
41 MPI_Initialized(&already_init);
42 if(already_init == 0){
43 simgrid::smpi::Process::init(argc, argv);
44 smpi_process()->mark_as_initialized();
45 int rank = smpi_process()->index();
46 TRACE_smpi_init(rank);
47 TRACE_smpi_computing_init(rank);
48 instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
49 extra->type = TRACING_INIT;
50 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, extra);
51 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
63 int rank = smpi_process()->index();
64 instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
65 extra->type = TRACING_FINALIZE;
66 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, extra);
68 smpi_process()->finalize();
70 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
71 TRACE_smpi_finalize(smpi_process()->index());
75 int PMPI_Finalized(int* flag)
77 *flag=smpi_process()!=nullptr ? smpi_process()->finalized() : 0;
81 int PMPI_Get_version (int *version,int *subversion){
82 *version = MPI_VERSION;
83 *subversion= MPI_SUBVERSION;
87 int PMPI_Get_library_version (char *version,int *len){
89 snprintf(version, MPI_MAX_LIBRARY_VERSION_STRING, "SMPI Version %d.%d. Copyright The Simgrid Team 2007-2017",
90 SIMGRID_VERSION_MAJOR, SIMGRID_VERSION_MINOR);
91 *len = strlen(version) > MPI_MAX_LIBRARY_VERSION_STRING ? MPI_MAX_LIBRARY_VERSION_STRING : strlen(version);
96 int PMPI_Init_thread(int *argc, char ***argv, int required, int *provided)
98 if (provided != nullptr) {
99 *provided = MPI_THREAD_SINGLE;
101 return MPI_Init(argc, argv);
104 int PMPI_Query_thread(int *provided)
106 if (provided == nullptr) {
109 *provided = MPI_THREAD_SINGLE;
114 int PMPI_Is_thread_main(int *flag)
116 if (flag == nullptr) {
119 *flag = smpi_process()->index() == 0;
124 int PMPI_Abort(MPI_Comm comm, int errorcode)
127 // FIXME: should kill all processes in comm instead
128 simcall_process_kill(SIMIX_process_self());
134 return smpi_mpi_wtime();
137 extern double sg_maxmin_precision;
140 return sg_maxmin_precision;
143 int PMPI_Address(void *location, MPI_Aint * address)
145 if (address==nullptr) {
148 *address = reinterpret_cast<MPI_Aint>(location);
153 int PMPI_Get_address(void *location, MPI_Aint * address)
155 return PMPI_Address(location, address);
158 int PMPI_Type_free(MPI_Datatype * datatype)
160 /* Free a predefined datatype is an error according to the standard, and should be checked for */
161 if (*datatype == MPI_DATATYPE_NULL) {
164 simgrid::smpi::Datatype::unref(*datatype);
169 int PMPI_Type_size(MPI_Datatype datatype, int *size)
171 if (datatype == MPI_DATATYPE_NULL) {
173 } else if (size == nullptr) {
176 *size = static_cast<int>(datatype->size());
181 int PMPI_Type_size_x(MPI_Datatype datatype, MPI_Count *size)
183 if (datatype == MPI_DATATYPE_NULL) {
185 } else if (size == nullptr) {
188 *size = static_cast<MPI_Count>(datatype->size());
193 int PMPI_Type_get_extent(MPI_Datatype datatype, MPI_Aint * lb, MPI_Aint * extent)
195 if (datatype == MPI_DATATYPE_NULL) {
197 } else if (lb == nullptr || extent == nullptr) {
200 return datatype->extent(lb, extent);
204 int PMPI_Type_get_true_extent(MPI_Datatype datatype, MPI_Aint * lb, MPI_Aint * extent)
206 return PMPI_Type_get_extent(datatype, lb, extent);
209 int PMPI_Type_extent(MPI_Datatype datatype, MPI_Aint * extent)
211 if (datatype == MPI_DATATYPE_NULL) {
213 } else if (extent == nullptr) {
216 *extent = datatype->get_extent();
221 int PMPI_Type_lb(MPI_Datatype datatype, MPI_Aint * disp)
223 if (datatype == MPI_DATATYPE_NULL) {
225 } else if (disp == nullptr) {
228 *disp = datatype->lb();
233 int PMPI_Type_ub(MPI_Datatype datatype, MPI_Aint * disp)
235 if (datatype == MPI_DATATYPE_NULL) {
237 } else if (disp == nullptr) {
240 *disp = datatype->ub();
245 int PMPI_Type_dup(MPI_Datatype datatype, MPI_Datatype *newtype){
246 int retval = MPI_SUCCESS;
247 if (datatype == MPI_DATATYPE_NULL) {
250 *newtype = new simgrid::smpi::Datatype(datatype, &retval);
251 //error when duplicating, free the new datatype
252 if(retval!=MPI_SUCCESS){
253 simgrid::smpi::Datatype::unref(*newtype);
254 *newtype = MPI_DATATYPE_NULL;
260 int PMPI_Op_create(MPI_User_function * function, int commute, MPI_Op * op)
262 if (function == nullptr || op == nullptr) {
265 *op = new simgrid::smpi::Op(function, (commute!=0));
270 int PMPI_Op_free(MPI_Op * op)
274 } else if (*op == MPI_OP_NULL) {
283 int PMPI_Op_commutative(MPI_Op op, int* commute){
284 if (op == MPI_OP_NULL) {
286 } else if (commute==nullptr){
289 *commute = op->is_commutative();
294 int PMPI_Group_free(MPI_Group * group)
296 if (group == nullptr) {
299 if(*group != MPI_COMM_WORLD->group() && *group != MPI_GROUP_EMPTY)
300 simgrid::smpi::Group::unref(*group);
301 *group = MPI_GROUP_NULL;
306 int PMPI_Group_size(MPI_Group group, int *size)
308 if (group == MPI_GROUP_NULL) {
309 return MPI_ERR_GROUP;
310 } else if (size == nullptr) {
313 *size = group->size();
318 int PMPI_Group_rank(MPI_Group group, int *rank)
320 if (group == MPI_GROUP_NULL) {
321 return MPI_ERR_GROUP;
322 } else if (rank == nullptr) {
325 *rank = group->rank(smpi_process()->index());
330 int PMPI_Group_translate_ranks(MPI_Group group1, int n, int *ranks1, MPI_Group group2, int *ranks2)
332 if (group1 == MPI_GROUP_NULL || group2 == MPI_GROUP_NULL) {
333 return MPI_ERR_GROUP;
335 for (int i = 0; i < n; i++) {
336 if(ranks1[i]==MPI_PROC_NULL){
337 ranks2[i]=MPI_PROC_NULL;
339 int index = group1->index(ranks1[i]);
340 ranks2[i] = group2->rank(index);
347 int PMPI_Group_compare(MPI_Group group1, MPI_Group group2, int *result)
349 if (group1 == MPI_GROUP_NULL || group2 == MPI_GROUP_NULL) {
350 return MPI_ERR_GROUP;
351 } else if (result == nullptr) {
354 *result = group1->compare(group2);
359 int PMPI_Group_union(MPI_Group group1, MPI_Group group2, MPI_Group * newgroup)
362 if (group1 == MPI_GROUP_NULL || group2 == MPI_GROUP_NULL) {
363 return MPI_ERR_GROUP;
364 } else if (newgroup == nullptr) {
367 return group1->group_union(group2, newgroup);
371 int PMPI_Group_intersection(MPI_Group group1, MPI_Group group2, MPI_Group * newgroup)
374 if (group1 == MPI_GROUP_NULL || group2 == MPI_GROUP_NULL) {
375 return MPI_ERR_GROUP;
376 } else if (newgroup == nullptr) {
379 return group1->intersection(group2,newgroup);
383 int PMPI_Group_difference(MPI_Group group1, MPI_Group group2, MPI_Group * newgroup)
385 if (group1 == MPI_GROUP_NULL || group2 == MPI_GROUP_NULL) {
386 return MPI_ERR_GROUP;
387 } else if (newgroup == nullptr) {
390 return group1->difference(group2,newgroup);
394 int PMPI_Group_incl(MPI_Group group, int n, int *ranks, MPI_Group * newgroup)
396 if (group == MPI_GROUP_NULL) {
397 return MPI_ERR_GROUP;
398 } else if (newgroup == nullptr) {
401 return group->incl(n, ranks, newgroup);
405 int PMPI_Group_excl(MPI_Group group, int n, int *ranks, MPI_Group * newgroup)
407 if (group == MPI_GROUP_NULL) {
408 return MPI_ERR_GROUP;
409 } else if (newgroup == nullptr) {
414 if (group != MPI_COMM_WORLD->group()
415 && group != MPI_COMM_SELF->group() && group != MPI_GROUP_EMPTY)
418 } else if (n == group->size()) {
419 *newgroup = MPI_GROUP_EMPTY;
422 return group->excl(n,ranks,newgroup);
427 int PMPI_Group_range_incl(MPI_Group group, int n, int ranges[][3], MPI_Group * newgroup)
429 if (group == MPI_GROUP_NULL) {
430 return MPI_ERR_GROUP;
431 } else if (newgroup == nullptr) {
435 *newgroup = MPI_GROUP_EMPTY;
438 return group->range_incl(n,ranges,newgroup);
443 int PMPI_Group_range_excl(MPI_Group group, int n, int ranges[][3], MPI_Group * newgroup)
445 if (group == MPI_GROUP_NULL) {
446 return MPI_ERR_GROUP;
447 } else if (newgroup == nullptr) {
452 if (group != MPI_COMM_WORLD->group() && group != MPI_COMM_SELF->group() &&
453 group != MPI_GROUP_EMPTY)
457 return group->range_excl(n,ranges,newgroup);
462 int PMPI_Comm_rank(MPI_Comm comm, int *rank)
464 if (comm == MPI_COMM_NULL) {
466 } else if (rank == nullptr) {
469 *rank = comm->rank();
474 int PMPI_Comm_size(MPI_Comm comm, int *size)
476 if (comm == MPI_COMM_NULL) {
478 } else if (size == nullptr) {
481 *size = comm->size();
486 int PMPI_Comm_get_name (MPI_Comm comm, char* name, int* len)
488 if (comm == MPI_COMM_NULL) {
490 } else if (name == nullptr || len == nullptr) {
493 comm->get_name(name, len);
498 int PMPI_Comm_group(MPI_Comm comm, MPI_Group * group)
500 if (comm == MPI_COMM_NULL) {
502 } else if (group == nullptr) {
505 *group = comm->group();
506 if (*group != MPI_COMM_WORLD->group() && *group != MPI_GROUP_NULL && *group != MPI_GROUP_EMPTY)
512 int PMPI_Comm_compare(MPI_Comm comm1, MPI_Comm comm2, int *result)
514 if (comm1 == MPI_COMM_NULL || comm2 == MPI_COMM_NULL) {
516 } else if (result == nullptr) {
519 if (comm1 == comm2) { /* Same communicators means same groups */
522 *result = comm1->group()->compare(comm2->group());
523 if (*result == MPI_IDENT) {
524 *result = MPI_CONGRUENT;
531 int PMPI_Comm_dup(MPI_Comm comm, MPI_Comm * newcomm)
533 if (comm == MPI_COMM_NULL) {
535 } else if (newcomm == nullptr) {
538 return comm->dup(newcomm);
542 int PMPI_Comm_create(MPI_Comm comm, MPI_Group group, MPI_Comm * newcomm)
544 if (comm == MPI_COMM_NULL) {
546 } else if (group == MPI_GROUP_NULL) {
547 return MPI_ERR_GROUP;
548 } else if (newcomm == nullptr) {
550 } else if(group->rank(smpi_process()->index())==MPI_UNDEFINED){
551 *newcomm= MPI_COMM_NULL;
555 *newcomm = new simgrid::smpi::Comm(group, nullptr);
560 int PMPI_Comm_free(MPI_Comm * comm)
562 if (comm == nullptr) {
564 } else if (*comm == MPI_COMM_NULL) {
567 simgrid::smpi::Comm::destroy(*comm);
568 *comm = MPI_COMM_NULL;
573 int PMPI_Comm_disconnect(MPI_Comm * comm)
575 /* TODO: wait until all communication in comm are done */
576 if (comm == nullptr) {
578 } else if (*comm == MPI_COMM_NULL) {
581 simgrid::smpi::Comm::destroy(*comm);
582 *comm = MPI_COMM_NULL;
587 int PMPI_Comm_split(MPI_Comm comm, int color, int key, MPI_Comm* comm_out)
592 if (comm_out == nullptr) {
593 retval = MPI_ERR_ARG;
594 } else if (comm == MPI_COMM_NULL) {
595 retval = MPI_ERR_COMM;
597 *comm_out = comm->split(color, key);
598 retval = MPI_SUCCESS;
605 int PMPI_Comm_create_group(MPI_Comm comm, MPI_Group group, int, MPI_Comm* comm_out)
610 if (comm_out == nullptr) {
611 retval = MPI_ERR_ARG;
612 } else if (comm == MPI_COMM_NULL) {
613 retval = MPI_ERR_COMM;
615 retval = MPI_Comm_create(comm, group, comm_out);
622 int PMPI_Send_init(void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm, MPI_Request * request)
627 if (request == nullptr) {
628 retval = MPI_ERR_ARG;
629 } else if (comm == MPI_COMM_NULL) {
630 retval = MPI_ERR_COMM;
631 } else if (not datatype->is_valid()) {
632 retval = MPI_ERR_TYPE;
633 } else if (dst == MPI_PROC_NULL) {
634 retval = MPI_SUCCESS;
636 *request = simgrid::smpi::Request::send_init(buf, count, datatype, dst, tag, comm);
637 retval = MPI_SUCCESS;
640 if (retval != MPI_SUCCESS && request != nullptr)
641 *request = MPI_REQUEST_NULL;
645 int PMPI_Recv_init(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm, MPI_Request * request)
650 if (request == nullptr) {
651 retval = MPI_ERR_ARG;
652 } else if (comm == MPI_COMM_NULL) {
653 retval = MPI_ERR_COMM;
654 } else if (not datatype->is_valid()) {
655 retval = MPI_ERR_TYPE;
656 } else if (src == MPI_PROC_NULL) {
657 retval = MPI_SUCCESS;
659 *request = simgrid::smpi::Request::recv_init(buf, count, datatype, src, tag, comm);
660 retval = MPI_SUCCESS;
663 if (retval != MPI_SUCCESS && request != nullptr)
664 *request = MPI_REQUEST_NULL;
668 int PMPI_Ssend_init(void* buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm, MPI_Request* request)
673 if (request == nullptr) {
674 retval = MPI_ERR_ARG;
675 } else if (comm == MPI_COMM_NULL) {
676 retval = MPI_ERR_COMM;
677 } else if (not datatype->is_valid()) {
678 retval = MPI_ERR_TYPE;
679 } else if (dst == MPI_PROC_NULL) {
680 retval = MPI_SUCCESS;
682 *request = simgrid::smpi::Request::ssend_init(buf, count, datatype, dst, tag, comm);
683 retval = MPI_SUCCESS;
686 if (retval != MPI_SUCCESS && request != nullptr)
687 *request = MPI_REQUEST_NULL;
691 int PMPI_Start(MPI_Request * request)
696 if (request == nullptr || *request == MPI_REQUEST_NULL) {
697 retval = MPI_ERR_REQUEST;
700 retval = MPI_SUCCESS;
706 int PMPI_Startall(int count, MPI_Request * requests)
710 if (requests == nullptr) {
711 retval = MPI_ERR_ARG;
713 retval = MPI_SUCCESS;
714 for (int i = 0; i < count; i++) {
715 if(requests[i] == MPI_REQUEST_NULL) {
716 retval = MPI_ERR_REQUEST;
719 if(retval != MPI_ERR_REQUEST) {
720 simgrid::smpi::Request::startall(count, requests);
727 int PMPI_Request_free(MPI_Request * request)
732 if (*request == MPI_REQUEST_NULL) {
733 retval = MPI_ERR_ARG;
735 simgrid::smpi::Request::unref(request);
736 retval = MPI_SUCCESS;
742 int PMPI_Irecv(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm, MPI_Request * request)
748 if (request == nullptr) {
749 retval = MPI_ERR_ARG;
750 } else if (comm == MPI_COMM_NULL) {
751 retval = MPI_ERR_COMM;
752 } else if (src == MPI_PROC_NULL) {
753 *request = MPI_REQUEST_NULL;
754 retval = MPI_SUCCESS;
755 } else if (src!=MPI_ANY_SOURCE && (src >= comm->group()->size() || src <0)){
756 retval = MPI_ERR_RANK;
757 } else if ((count < 0) || (buf==nullptr && count > 0)) {
758 retval = MPI_ERR_COUNT;
759 } else if (not datatype->is_valid()) {
760 retval = MPI_ERR_TYPE;
761 } else if(tag<0 && tag != MPI_ANY_TAG){
762 retval = MPI_ERR_TAG;
765 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
766 int src_traced = comm->group()->index(src);
768 instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
769 extra->type = TRACING_IRECV;
770 extra->src = src_traced;
773 extra->datatype1 = encode_datatype(datatype, &known);
774 int dt_size_send = 1;
776 dt_size_send = datatype->size();
777 extra->send_size = count*dt_size_send;
778 TRACE_smpi_ptp_in(rank, src_traced, rank, __FUNCTION__, extra);
780 *request = simgrid::smpi::Request::irecv(buf, count, datatype, src, tag, comm);
781 retval = MPI_SUCCESS;
783 TRACE_smpi_ptp_out(rank, src_traced, rank, __FUNCTION__);
787 if (retval != MPI_SUCCESS && request != nullptr)
788 *request = MPI_REQUEST_NULL;
793 int PMPI_Isend(void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm, MPI_Request * request)
798 if (request == nullptr) {
799 retval = MPI_ERR_ARG;
800 } else if (comm == MPI_COMM_NULL) {
801 retval = MPI_ERR_COMM;
802 } else if (dst == MPI_PROC_NULL) {
803 *request = MPI_REQUEST_NULL;
804 retval = MPI_SUCCESS;
805 } else if (dst >= comm->group()->size() || dst <0){
806 retval = MPI_ERR_RANK;
807 } else if ((count < 0) || (buf==nullptr && count > 0)) {
808 retval = MPI_ERR_COUNT;
809 } else if (not datatype->is_valid()) {
810 retval = MPI_ERR_TYPE;
811 } else if(tag<0 && tag != MPI_ANY_TAG){
812 retval = MPI_ERR_TAG;
814 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
815 int dst_traced = comm->group()->index(dst);
816 instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
817 extra->type = TRACING_ISEND;
819 extra->dst = dst_traced;
821 extra->datatype1 = encode_datatype(datatype, &known);
822 int dt_size_send = 1;
824 dt_size_send = datatype->size();
825 extra->send_size = count*dt_size_send;
826 TRACE_smpi_ptp_in(rank, rank, dst_traced, __FUNCTION__, extra);
827 TRACE_smpi_send(rank, rank, dst_traced, tag, count*datatype->size());
829 *request = simgrid::smpi::Request::isend(buf, count, datatype, dst, tag, comm);
830 retval = MPI_SUCCESS;
832 TRACE_smpi_ptp_out(rank, rank, dst_traced, __FUNCTION__);
836 if (retval != MPI_SUCCESS && request!=nullptr)
837 *request = MPI_REQUEST_NULL;
841 int PMPI_Issend(void* buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm, MPI_Request* request)
846 if (request == nullptr) {
847 retval = MPI_ERR_ARG;
848 } else if (comm == MPI_COMM_NULL) {
849 retval = MPI_ERR_COMM;
850 } else if (dst == MPI_PROC_NULL) {
851 *request = MPI_REQUEST_NULL;
852 retval = MPI_SUCCESS;
853 } else if (dst >= comm->group()->size() || dst <0){
854 retval = MPI_ERR_RANK;
855 } else if ((count < 0)|| (buf==nullptr && count > 0)) {
856 retval = MPI_ERR_COUNT;
857 } else if (not datatype->is_valid()) {
858 retval = MPI_ERR_TYPE;
859 } else if(tag<0 && tag != MPI_ANY_TAG){
860 retval = MPI_ERR_TAG;
862 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
863 int dst_traced = comm->group()->index(dst);
864 instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
865 extra->type = TRACING_ISSEND;
867 extra->dst = dst_traced;
869 extra->datatype1 = encode_datatype(datatype, &known);
870 int dt_size_send = 1;
872 dt_size_send = datatype->size();
873 extra->send_size = count*dt_size_send;
874 TRACE_smpi_ptp_in(rank, rank, dst_traced, __FUNCTION__, extra);
875 TRACE_smpi_send(rank, rank, dst_traced, tag, count*datatype->size());
877 *request = simgrid::smpi::Request::issend(buf, count, datatype, dst, tag, comm);
878 retval = MPI_SUCCESS;
880 TRACE_smpi_ptp_out(rank, rank, dst_traced, __FUNCTION__);
884 if (retval != MPI_SUCCESS && request!=nullptr)
885 *request = MPI_REQUEST_NULL;
889 int PMPI_Recv(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm, MPI_Status * status)
894 if (comm == MPI_COMM_NULL) {
895 retval = MPI_ERR_COMM;
896 } else if (src == MPI_PROC_NULL) {
897 simgrid::smpi::Status::empty(status);
898 status->MPI_SOURCE = MPI_PROC_NULL;
899 retval = MPI_SUCCESS;
900 } else if (src!=MPI_ANY_SOURCE && (src >= comm->group()->size() || src <0)){
901 retval = MPI_ERR_RANK;
902 } else if ((count < 0) || (buf==nullptr && count > 0)) {
903 retval = MPI_ERR_COUNT;
904 } else if (not datatype->is_valid()) {
905 retval = MPI_ERR_TYPE;
906 } else if(tag<0 && tag != MPI_ANY_TAG){
907 retval = MPI_ERR_TAG;
909 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
910 int src_traced = comm->group()->index(src);
911 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
912 extra->type = TRACING_RECV;
913 extra->src = src_traced;
916 extra->datatype1 = encode_datatype(datatype, &known);
917 int dt_size_send = 1;
919 dt_size_send = datatype->size();
920 extra->send_size = count * dt_size_send;
921 TRACE_smpi_ptp_in(rank, src_traced, rank, __FUNCTION__, extra);
923 simgrid::smpi::Request::recv(buf, count, datatype, src, tag, comm, status);
924 retval = MPI_SUCCESS;
926 // the src may not have been known at the beginning of the recv (MPI_ANY_SOURCE)
927 if (status != MPI_STATUS_IGNORE) {
928 src_traced = comm->group()->index(status->MPI_SOURCE);
929 if (not TRACE_smpi_view_internals()) {
930 TRACE_smpi_recv(rank, src_traced, rank, tag);
933 TRACE_smpi_ptp_out(rank, src_traced, rank, __FUNCTION__);
940 int PMPI_Send(void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
946 if (comm == MPI_COMM_NULL) {
947 retval = MPI_ERR_COMM;
948 } else if (dst == MPI_PROC_NULL) {
949 retval = MPI_SUCCESS;
950 } else if (dst >= comm->group()->size() || dst <0){
951 retval = MPI_ERR_RANK;
952 } else if ((count < 0) || (buf == nullptr && count > 0)) {
953 retval = MPI_ERR_COUNT;
954 } else if (not datatype->is_valid()) {
955 retval = MPI_ERR_TYPE;
956 } else if(tag < 0 && tag != MPI_ANY_TAG){
957 retval = MPI_ERR_TAG;
959 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
960 int dst_traced = comm->group()->index(dst);
961 instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
962 extra->type = TRACING_SEND;
964 extra->dst = dst_traced;
966 extra->datatype1 = encode_datatype(datatype, &known);
967 int dt_size_send = 1;
969 dt_size_send = datatype->size();
971 extra->send_size = count*dt_size_send;
972 TRACE_smpi_ptp_in(rank, rank, dst_traced, __FUNCTION__, extra);
973 if (not TRACE_smpi_view_internals()) {
974 TRACE_smpi_send(rank, rank, dst_traced, tag,count*datatype->size());
977 simgrid::smpi::Request::send(buf, count, datatype, dst, tag, comm);
978 retval = MPI_SUCCESS;
980 TRACE_smpi_ptp_out(rank, rank, dst_traced, __FUNCTION__);
987 int PMPI_Ssend(void* buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm) {
992 if (comm == MPI_COMM_NULL) {
993 retval = MPI_ERR_COMM;
994 } else if (dst == MPI_PROC_NULL) {
995 retval = MPI_SUCCESS;
996 } else if (dst >= comm->group()->size() || dst <0){
997 retval = MPI_ERR_RANK;
998 } else if ((count < 0) || (buf==nullptr && count > 0)) {
999 retval = MPI_ERR_COUNT;
1000 } else if (not datatype->is_valid()) {
1001 retval = MPI_ERR_TYPE;
1002 } else if(tag<0 && tag != MPI_ANY_TAG){
1003 retval = MPI_ERR_TAG;
1005 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1006 int dst_traced = comm->group()->index(dst);
1007 instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
1008 extra->type = TRACING_SSEND;
1010 extra->dst = dst_traced;
1012 extra->datatype1 = encode_datatype(datatype, &known);
1013 int dt_size_send = 1;
1015 dt_size_send = datatype->size();
1017 extra->send_size = count*dt_size_send;
1018 TRACE_smpi_ptp_in(rank, rank, dst_traced, __FUNCTION__, extra);
1019 TRACE_smpi_send(rank, rank, dst_traced, tag,count*datatype->size());
1021 simgrid::smpi::Request::ssend(buf, count, datatype, dst, tag, comm);
1022 retval = MPI_SUCCESS;
1024 TRACE_smpi_ptp_out(rank, rank, dst_traced, __FUNCTION__);
1031 int PMPI_Sendrecv(void *sendbuf, int sendcount, MPI_Datatype sendtype, int dst, int sendtag, void *recvbuf,
1032 int recvcount, MPI_Datatype recvtype, int src, int recvtag, MPI_Comm comm, MPI_Status * status)
1038 if (comm == MPI_COMM_NULL) {
1039 retval = MPI_ERR_COMM;
1040 } else if (not sendtype->is_valid() || not recvtype->is_valid()) {
1041 retval = MPI_ERR_TYPE;
1042 } else if (src == MPI_PROC_NULL || dst == MPI_PROC_NULL) {
1043 simgrid::smpi::Status::empty(status);
1044 status->MPI_SOURCE = MPI_PROC_NULL;
1045 retval = MPI_SUCCESS;
1046 }else if (dst >= comm->group()->size() || dst <0 ||
1047 (src!=MPI_ANY_SOURCE && (src >= comm->group()->size() || src <0))){
1048 retval = MPI_ERR_RANK;
1049 } else if ((sendcount < 0 || recvcount<0) ||
1050 (sendbuf==nullptr && sendcount > 0) || (recvbuf==nullptr && recvcount>0)) {
1051 retval = MPI_ERR_COUNT;
1052 } else if((sendtag<0 && sendtag != MPI_ANY_TAG)||(recvtag<0 && recvtag != MPI_ANY_TAG)){
1053 retval = MPI_ERR_TAG;
1056 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1057 int dst_traced = comm->group()->index(dst);
1058 int src_traced = comm->group()->index(src);
1059 instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
1060 extra->type = TRACING_SENDRECV;
1061 extra->src = src_traced;
1062 extra->dst = dst_traced;
1064 extra->datatype1 = encode_datatype(sendtype, &known);
1065 int dt_size_send = 1;
1067 dt_size_send = sendtype->size();
1068 extra->send_size = sendcount*dt_size_send;
1069 extra->datatype2 = encode_datatype(recvtype, &known);
1070 int dt_size_recv = 1;
1072 dt_size_recv = recvtype->size();
1073 extra->recv_size = recvcount*dt_size_recv;
1075 TRACE_smpi_ptp_in(rank, src_traced, dst_traced, __FUNCTION__, extra);
1076 TRACE_smpi_send(rank, rank, dst_traced, sendtag,sendcount*sendtype->size());
1078 simgrid::smpi::Request::sendrecv(sendbuf, sendcount, sendtype, dst, sendtag, recvbuf, recvcount, recvtype, src, recvtag, comm,
1080 retval = MPI_SUCCESS;
1082 TRACE_smpi_ptp_out(rank, src_traced, dst_traced, __FUNCTION__);
1083 TRACE_smpi_recv(rank, src_traced, rank, recvtag);
1090 int PMPI_Sendrecv_replace(void* buf, int count, MPI_Datatype datatype, int dst, int sendtag, int src, int recvtag,
1091 MPI_Comm comm, MPI_Status* status)
1094 if (not datatype->is_valid()) {
1095 return MPI_ERR_TYPE;
1096 } else if (count < 0) {
1097 return MPI_ERR_COUNT;
1099 int size = datatype->get_extent() * count;
1100 void* recvbuf = xbt_new0(char, size);
1101 retval = MPI_Sendrecv(buf, count, datatype, dst, sendtag, recvbuf, count, datatype, src, recvtag, comm, status);
1102 if(retval==MPI_SUCCESS){
1103 simgrid::smpi::Datatype::copy(recvbuf, count, datatype, buf, count, datatype);
1111 int PMPI_Test(MPI_Request * request, int *flag, MPI_Status * status)
1115 if (request == nullptr || flag == nullptr) {
1116 retval = MPI_ERR_ARG;
1117 } else if (*request == MPI_REQUEST_NULL) {
1119 simgrid::smpi::Status::empty(status);
1120 retval = MPI_SUCCESS;
1122 int rank = ((*request)->comm() != MPI_COMM_NULL) ? smpi_process()->index() : -1;
1124 instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
1125 extra->type = TRACING_TEST;
1126 TRACE_smpi_testing_in(rank, extra);
1128 *flag = simgrid::smpi::Request::test(request,status);
1130 TRACE_smpi_testing_out(rank);
1131 retval = MPI_SUCCESS;
1137 int PMPI_Testany(int count, MPI_Request requests[], int *index, int *flag, MPI_Status * status)
1142 if (index == nullptr || flag == nullptr) {
1143 retval = MPI_ERR_ARG;
1145 *flag = simgrid::smpi::Request::testany(count, requests, index, status);
1146 retval = MPI_SUCCESS;
1152 int PMPI_Testall(int count, MPI_Request* requests, int* flag, MPI_Status* statuses)
1157 if (flag == nullptr) {
1158 retval = MPI_ERR_ARG;
1160 *flag = simgrid::smpi::Request::testall(count, requests, statuses);
1161 retval = MPI_SUCCESS;
1167 int PMPI_Probe(int source, int tag, MPI_Comm comm, MPI_Status* status) {
1171 if (status == nullptr) {
1172 retval = MPI_ERR_ARG;
1173 } else if (comm == MPI_COMM_NULL) {
1174 retval = MPI_ERR_COMM;
1175 } else if (source == MPI_PROC_NULL) {
1176 simgrid::smpi::Status::empty(status);
1177 status->MPI_SOURCE = MPI_PROC_NULL;
1178 retval = MPI_SUCCESS;
1180 simgrid::smpi::Request::probe(source, tag, comm, status);
1181 retval = MPI_SUCCESS;
1187 int PMPI_Iprobe(int source, int tag, MPI_Comm comm, int* flag, MPI_Status* status) {
1191 if (flag == nullptr) {
1192 retval = MPI_ERR_ARG;
1193 } else if (comm == MPI_COMM_NULL) {
1194 retval = MPI_ERR_COMM;
1195 } else if (source == MPI_PROC_NULL) {
1197 simgrid::smpi::Status::empty(status);
1198 status->MPI_SOURCE = MPI_PROC_NULL;
1199 retval = MPI_SUCCESS;
1201 simgrid::smpi::Request::iprobe(source, tag, comm, flag, status);
1202 retval = MPI_SUCCESS;
1208 int PMPI_Wait(MPI_Request * request, MPI_Status * status)
1214 simgrid::smpi::Status::empty(status);
1216 if (request == nullptr) {
1217 retval = MPI_ERR_ARG;
1218 } else if (*request == MPI_REQUEST_NULL) {
1219 retval = MPI_SUCCESS;
1222 int rank = (request!=nullptr && (*request)->comm() != MPI_COMM_NULL) ? smpi_process()->index() : -1;
1224 int src_traced = (*request)->src();
1225 int dst_traced = (*request)->dst();
1226 int tag_traced= (*request)->tag();
1227 MPI_Comm comm = (*request)->comm();
1228 int is_wait_for_receive = ((*request)->flags() & RECV);
1229 instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
1230 extra->type = TRACING_WAIT;
1231 TRACE_smpi_ptp_in(rank, src_traced, dst_traced, __FUNCTION__, extra);
1233 simgrid::smpi::Request::wait(request, status);
1234 retval = MPI_SUCCESS;
1236 //the src may not have been known at the beginning of the recv (MPI_ANY_SOURCE)
1237 TRACE_smpi_ptp_out(rank, src_traced, dst_traced, __FUNCTION__);
1238 if (is_wait_for_receive) {
1239 if(src_traced==MPI_ANY_SOURCE)
1240 src_traced = (status!=MPI_STATUS_IGNORE) ?
1241 comm->group()->rank(status->MPI_SOURCE) :
1243 TRACE_smpi_recv(rank, src_traced, dst_traced, tag_traced);
1251 int PMPI_Waitany(int count, MPI_Request requests[], int *index, MPI_Status * status)
1253 if (index == nullptr)
1257 //save requests information for tracing
1265 savedvalstype* savedvals=nullptr;
1267 savedvals = xbt_new0(savedvalstype, count);
1269 for (int i = 0; i < count; i++) {
1270 MPI_Request req = requests[i]; //already received requests are no longer valid
1272 savedvals[i]=(savedvalstype){req->src(), req->dst(), (req->flags() & RECV), req->tag(), req->comm()};
1275 int rank_traced = smpi_process()->index();
1276 instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
1277 extra->type = TRACING_WAITANY;
1278 extra->send_size=count;
1279 TRACE_smpi_ptp_in(rank_traced, -1, -1, __FUNCTION__,extra);
1281 *index = simgrid::smpi::Request::waitany(count, requests, status);
1283 if(*index!=MPI_UNDEFINED){
1284 int src_traced = savedvals[*index].src;
1285 //the src may not have been known at the beginning of the recv (MPI_ANY_SOURCE)
1286 int dst_traced = savedvals[*index].dst;
1287 int is_wait_for_receive = savedvals[*index].recv;
1288 if (is_wait_for_receive) {
1289 if(savedvals[*index].src==MPI_ANY_SOURCE)
1290 src_traced = (status != MPI_STATUSES_IGNORE)
1291 ? savedvals[*index].comm->group()->rank(status->MPI_SOURCE)
1292 : savedvals[*index].src;
1293 TRACE_smpi_recv(rank_traced, src_traced, dst_traced, savedvals[*index].tag);
1295 TRACE_smpi_ptp_out(rank_traced, src_traced, dst_traced, __FUNCTION__);
1297 xbt_free(savedvals);
1303 int PMPI_Waitall(int count, MPI_Request requests[], MPI_Status status[])
1306 //save information from requests
1315 savedvalstype* savedvals=xbt_new0(savedvalstype, count);
1317 for (int i = 0; i < count; i++) {
1318 MPI_Request req = requests[i];
1319 if(req!=MPI_REQUEST_NULL){
1320 savedvals[i]=(savedvalstype){req->src(), req->dst(), (req->flags() & RECV), req->tag(), 1, req->comm()};
1322 savedvals[i].valid=0;
1325 int rank_traced = smpi_process()->index();
1326 instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
1327 extra->type = TRACING_WAITALL;
1328 extra->send_size=count;
1329 TRACE_smpi_ptp_in(rank_traced, -1, -1, __FUNCTION__,extra);
1331 int retval = simgrid::smpi::Request::waitall(count, requests, status);
1333 for (int i = 0; i < count; i++) {
1334 if(savedvals[i].valid){
1335 //the src may not have been known at the beginning of the recv (MPI_ANY_SOURCE)
1336 int src_traced = savedvals[i].src;
1337 int dst_traced = savedvals[i].dst;
1338 int is_wait_for_receive = savedvals[i].recv;
1339 if (is_wait_for_receive) {
1340 if(src_traced==MPI_ANY_SOURCE)
1341 src_traced = (status!=MPI_STATUSES_IGNORE) ?
1342 savedvals[i].comm->group()->rank(status[i].MPI_SOURCE) : savedvals[i].src;
1343 TRACE_smpi_recv(rank_traced, src_traced, dst_traced,savedvals[i].tag);
1347 TRACE_smpi_ptp_out(rank_traced, -1, -1, __FUNCTION__);
1348 xbt_free(savedvals);
1354 int PMPI_Waitsome(int incount, MPI_Request requests[], int *outcount, int *indices, MPI_Status status[])
1359 if (outcount == nullptr) {
1360 retval = MPI_ERR_ARG;
1362 *outcount = simgrid::smpi::Request::waitsome(incount, requests, indices, status);
1363 retval = MPI_SUCCESS;
1369 int PMPI_Testsome(int incount, MPI_Request requests[], int* outcount, int* indices, MPI_Status status[])
1374 if (outcount == nullptr) {
1375 retval = MPI_ERR_ARG;
1377 *outcount = simgrid::smpi::Request::testsome(incount, requests, indices, status);
1378 retval = MPI_SUCCESS;
1385 int PMPI_Bcast(void *buf, int count, MPI_Datatype datatype, int root, MPI_Comm comm)
1391 if (comm == MPI_COMM_NULL) {
1392 retval = MPI_ERR_COMM;
1393 } else if (not datatype->is_valid()) {
1394 retval = MPI_ERR_ARG;
1396 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1397 int root_traced = comm->group()->index(root);
1399 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
1400 extra->type = TRACING_BCAST;
1401 extra->root = root_traced;
1403 extra->datatype1 = encode_datatype(datatype, &known);
1404 int dt_size_send = 1;
1406 dt_size_send = datatype->size();
1407 extra->send_size = count * dt_size_send;
1408 TRACE_smpi_collective_in(rank, root_traced, __FUNCTION__, extra);
1409 if (comm->size() > 1)
1410 simgrid::smpi::Colls::bcast(buf, count, datatype, root, comm);
1411 retval = MPI_SUCCESS;
1413 TRACE_smpi_collective_out(rank, root_traced, __FUNCTION__);
1419 int PMPI_Barrier(MPI_Comm comm)
1425 if (comm == MPI_COMM_NULL) {
1426 retval = MPI_ERR_COMM;
1428 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1429 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
1430 extra->type = TRACING_BARRIER;
1431 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, extra);
1433 simgrid::smpi::Colls::barrier(comm);
1435 //Barrier can be used to synchronize RMA calls. Finish all requests from comm before.
1436 comm->finish_rma_calls();
1438 retval = MPI_SUCCESS;
1440 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
1447 int PMPI_Gather(void *sendbuf, int sendcount, MPI_Datatype sendtype,void *recvbuf, int recvcount, MPI_Datatype recvtype,
1448 int root, MPI_Comm comm)
1454 if (comm == MPI_COMM_NULL) {
1455 retval = MPI_ERR_COMM;
1456 } else if ((( sendbuf != MPI_IN_PLACE) && (sendtype == MPI_DATATYPE_NULL)) ||
1457 ((comm->rank() == root) && (recvtype == MPI_DATATYPE_NULL))){
1458 retval = MPI_ERR_TYPE;
1459 } else if ((( sendbuf != MPI_IN_PLACE) && (sendcount <0)) || ((comm->rank() == root) && (recvcount <0))){
1460 retval = MPI_ERR_COUNT;
1463 char* sendtmpbuf = static_cast<char*>(sendbuf);
1464 int sendtmpcount = sendcount;
1465 MPI_Datatype sendtmptype = sendtype;
1466 if( (comm->rank() == root) && (sendbuf == MPI_IN_PLACE )) {
1468 sendtmptype=recvtype;
1470 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1471 int root_traced = comm->group()->index(root);
1472 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
1473 extra->type = TRACING_GATHER;
1474 extra->root = root_traced;
1476 extra->datatype1 = encode_datatype(sendtmptype, &known);
1477 int dt_size_send = 1;
1479 dt_size_send = sendtmptype->size();
1480 extra->send_size = sendtmpcount * dt_size_send;
1481 extra->datatype2 = encode_datatype(recvtype, &known);
1482 int dt_size_recv = 1;
1483 if ((comm->rank() == root) && known == 0)
1484 dt_size_recv = recvtype->size();
1485 extra->recv_size = recvcount * dt_size_recv;
1487 TRACE_smpi_collective_in(rank, root_traced, __FUNCTION__, extra);
1489 simgrid::smpi::Colls::gather(sendtmpbuf, sendtmpcount, sendtmptype, recvbuf, recvcount, recvtype, root, comm);
1491 retval = MPI_SUCCESS;
1492 TRACE_smpi_collective_out(rank, root_traced, __FUNCTION__);
1499 int PMPI_Gatherv(void *sendbuf, int sendcount, MPI_Datatype sendtype, void *recvbuf, int *recvcounts, int *displs,
1500 MPI_Datatype recvtype, int root, MPI_Comm comm)
1506 if (comm == MPI_COMM_NULL) {
1507 retval = MPI_ERR_COMM;
1508 } else if ((( sendbuf != MPI_IN_PLACE) && (sendtype == MPI_DATATYPE_NULL)) ||
1509 ((comm->rank() == root) && (recvtype == MPI_DATATYPE_NULL))){
1510 retval = MPI_ERR_TYPE;
1511 } else if (( sendbuf != MPI_IN_PLACE) && (sendcount <0)){
1512 retval = MPI_ERR_COUNT;
1513 } else if (recvcounts == nullptr || displs == nullptr) {
1514 retval = MPI_ERR_ARG;
1516 char* sendtmpbuf = static_cast<char*>(sendbuf);
1517 int sendtmpcount = sendcount;
1518 MPI_Datatype sendtmptype = sendtype;
1519 if( (comm->rank() == root) && (sendbuf == MPI_IN_PLACE )) {
1521 sendtmptype=recvtype;
1524 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1525 int root_traced = comm->group()->index(root);
1526 int size = comm->size();
1527 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
1528 extra->type = TRACING_GATHERV;
1529 extra->num_processes = size;
1530 extra->root = root_traced;
1532 extra->datatype1 = encode_datatype(sendtmptype, &known);
1533 int dt_size_send = 1;
1535 dt_size_send = sendtype->size();
1536 extra->send_size = sendtmpcount * dt_size_send;
1537 extra->datatype2 = encode_datatype(recvtype, &known);
1538 int dt_size_recv = 1;
1540 dt_size_recv = recvtype->size();
1541 if (comm->rank() == root) {
1542 extra->recvcounts = xbt_new(int, size);
1543 for (int i = 0; i < size; i++) // copy data to avoid bad free
1544 extra->recvcounts[i] = recvcounts[i] * dt_size_recv;
1546 TRACE_smpi_collective_in(rank, root_traced, __FUNCTION__, extra);
1548 retval = simgrid::smpi::Colls::gatherv(sendtmpbuf, sendtmpcount, sendtmptype, recvbuf, recvcounts, displs, recvtype, root, comm);
1549 TRACE_smpi_collective_out(rank, root_traced, __FUNCTION__);
1556 int PMPI_Allgather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
1557 void *recvbuf, int recvcount, MPI_Datatype recvtype, MPI_Comm comm)
1563 if (comm == MPI_COMM_NULL) {
1564 retval = MPI_ERR_COMM;
1565 } else if ((( sendbuf != MPI_IN_PLACE) && (sendtype == MPI_DATATYPE_NULL)) ||
1566 (recvtype == MPI_DATATYPE_NULL)){
1567 retval = MPI_ERR_TYPE;
1568 } else if ((( sendbuf != MPI_IN_PLACE) && (sendcount <0)) ||
1570 retval = MPI_ERR_COUNT;
1572 if(sendbuf == MPI_IN_PLACE) {
1573 sendbuf=static_cast<char*>(recvbuf)+recvtype->get_extent()*recvcount*comm->rank();
1574 sendcount=recvcount;
1577 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1578 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
1579 extra->type = TRACING_ALLGATHER;
1581 extra->datatype1 = encode_datatype(sendtype, &known);
1582 int dt_size_send = 1;
1584 dt_size_send = sendtype->size();
1585 extra->send_size = sendcount * dt_size_send;
1586 extra->datatype2 = encode_datatype(recvtype, &known);
1587 int dt_size_recv = 1;
1589 dt_size_recv = recvtype->size();
1590 extra->recv_size = recvcount * dt_size_recv;
1592 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, extra);
1594 simgrid::smpi::Colls::allgather(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, comm);
1595 retval = MPI_SUCCESS;
1596 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
1602 int PMPI_Allgatherv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
1603 void *recvbuf, int *recvcounts, int *displs, MPI_Datatype recvtype, MPI_Comm comm)
1609 if (comm == MPI_COMM_NULL) {
1610 retval = MPI_ERR_COMM;
1611 } else if (((sendbuf != MPI_IN_PLACE) && (sendtype == MPI_DATATYPE_NULL)) || (recvtype == MPI_DATATYPE_NULL)) {
1612 retval = MPI_ERR_TYPE;
1613 } else if (( sendbuf != MPI_IN_PLACE) && (sendcount <0)){
1614 retval = MPI_ERR_COUNT;
1615 } else if (recvcounts == nullptr || displs == nullptr) {
1616 retval = MPI_ERR_ARG;
1619 if(sendbuf == MPI_IN_PLACE) {
1620 sendbuf=static_cast<char*>(recvbuf)+recvtype->get_extent()*displs[comm->rank()];
1621 sendcount=recvcounts[comm->rank()];
1624 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1626 int size = comm->size();
1627 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
1628 extra->type = TRACING_ALLGATHERV;
1629 extra->num_processes = size;
1631 extra->datatype1 = encode_datatype(sendtype, &known);
1632 int dt_size_send = 1;
1634 dt_size_send = sendtype->size();
1635 extra->send_size = sendcount * dt_size_send;
1636 extra->datatype2 = encode_datatype(recvtype, &known);
1637 int dt_size_recv = 1;
1639 dt_size_recv = recvtype->size();
1640 extra->recvcounts = xbt_new(int, size);
1641 for (i = 0; i < size; i++) // copy data to avoid bad free
1642 extra->recvcounts[i] = recvcounts[i] * dt_size_recv;
1644 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, extra);
1646 simgrid::smpi::Colls::allgatherv(sendbuf, sendcount, sendtype, recvbuf, recvcounts, displs, recvtype, comm);
1647 retval = MPI_SUCCESS;
1648 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
1655 int PMPI_Scatter(void *sendbuf, int sendcount, MPI_Datatype sendtype,
1656 void *recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm)
1662 if (comm == MPI_COMM_NULL) {
1663 retval = MPI_ERR_COMM;
1664 } else if (((comm->rank() == root) && (not sendtype->is_valid())) ||
1665 ((recvbuf != MPI_IN_PLACE) && (not recvtype->is_valid()))) {
1666 retval = MPI_ERR_TYPE;
1667 } else if ((sendbuf == recvbuf) ||
1668 ((comm->rank()==root) && sendcount>0 && (sendbuf == nullptr))){
1669 retval = MPI_ERR_BUFFER;
1672 if (recvbuf == MPI_IN_PLACE) {
1673 recvtype = sendtype;
1674 recvcount = sendcount;
1676 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1677 int root_traced = comm->group()->index(root);
1678 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
1679 extra->type = TRACING_SCATTER;
1680 extra->root = root_traced;
1682 extra->datatype1 = encode_datatype(sendtype, &known);
1683 int dt_size_send = 1;
1684 if ((comm->rank() == root) && known == 0)
1685 dt_size_send = sendtype->size();
1686 extra->send_size = sendcount * dt_size_send;
1687 extra->datatype2 = encode_datatype(recvtype, &known);
1688 int dt_size_recv = 1;
1690 dt_size_recv = recvtype->size();
1691 extra->recv_size = recvcount * dt_size_recv;
1692 TRACE_smpi_collective_in(rank, root_traced, __FUNCTION__, extra);
1694 simgrid::smpi::Colls::scatter(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, root, comm);
1695 retval = MPI_SUCCESS;
1696 TRACE_smpi_collective_out(rank, root_traced, __FUNCTION__);
1703 int PMPI_Scatterv(void *sendbuf, int *sendcounts, int *displs,
1704 MPI_Datatype sendtype, void *recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm)
1710 if (comm == MPI_COMM_NULL) {
1711 retval = MPI_ERR_COMM;
1712 } else if (sendcounts == nullptr || displs == nullptr) {
1713 retval = MPI_ERR_ARG;
1714 } else if (((comm->rank() == root) && (sendtype == MPI_DATATYPE_NULL)) ||
1715 ((recvbuf != MPI_IN_PLACE) && (recvtype == MPI_DATATYPE_NULL))) {
1716 retval = MPI_ERR_TYPE;
1718 if (recvbuf == MPI_IN_PLACE) {
1719 recvtype = sendtype;
1720 recvcount = sendcounts[comm->rank()];
1722 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1723 int root_traced = comm->group()->index(root);
1724 int size = comm->size();
1725 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
1726 extra->type = TRACING_SCATTERV;
1727 extra->num_processes = size;
1728 extra->root = root_traced;
1730 extra->datatype1 = encode_datatype(sendtype, &known);
1731 int dt_size_send = 1;
1733 dt_size_send = sendtype->size();
1734 if (comm->rank() == root) {
1735 extra->sendcounts = xbt_new(int, size);
1736 for (int i = 0; i < size; i++) // copy data to avoid bad free
1737 extra->sendcounts[i] = sendcounts[i] * dt_size_send;
1739 extra->datatype2 = encode_datatype(recvtype, &known);
1740 int dt_size_recv = 1;
1742 dt_size_recv = recvtype->size();
1743 extra->recv_size = recvcount * dt_size_recv;
1744 TRACE_smpi_collective_in(rank, root_traced, __FUNCTION__, extra);
1746 retval = simgrid::smpi::Colls::scatterv(sendbuf, sendcounts, displs, sendtype, recvbuf, recvcount, recvtype, root, comm);
1748 TRACE_smpi_collective_out(rank, root_traced, __FUNCTION__);
1755 int PMPI_Reduce(void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, int root, MPI_Comm comm)
1761 if (comm == MPI_COMM_NULL) {
1762 retval = MPI_ERR_COMM;
1763 } else if (not datatype->is_valid() || op == MPI_OP_NULL) {
1764 retval = MPI_ERR_ARG;
1766 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1767 int root_traced = comm->group()->index(root);
1768 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
1769 extra->type = TRACING_REDUCE;
1771 extra->datatype1 = encode_datatype(datatype, &known);
1772 int dt_size_send = 1;
1774 dt_size_send = datatype->size();
1775 extra->send_size = count * dt_size_send;
1776 extra->root = root_traced;
1778 TRACE_smpi_collective_in(rank, root_traced, __FUNCTION__, extra);
1780 simgrid::smpi::Colls::reduce(sendbuf, recvbuf, count, datatype, op, root, comm);
1782 retval = MPI_SUCCESS;
1783 TRACE_smpi_collective_out(rank, root_traced, __FUNCTION__);
1790 int PMPI_Reduce_local(void *inbuf, void *inoutbuf, int count, MPI_Datatype datatype, MPI_Op op){
1794 if (not datatype->is_valid() || op == MPI_OP_NULL) {
1795 retval = MPI_ERR_ARG;
1797 op->apply(inbuf, inoutbuf, &count, datatype);
1798 retval = MPI_SUCCESS;
1804 int PMPI_Allreduce(void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
1810 if (comm == MPI_COMM_NULL) {
1811 retval = MPI_ERR_COMM;
1812 } else if (not datatype->is_valid()) {
1813 retval = MPI_ERR_TYPE;
1814 } else if (op == MPI_OP_NULL) {
1815 retval = MPI_ERR_OP;
1818 char* sendtmpbuf = static_cast<char*>(sendbuf);
1819 if( sendbuf == MPI_IN_PLACE ) {
1820 sendtmpbuf = static_cast<char*>(xbt_malloc(count*datatype->get_extent()));
1821 simgrid::smpi::Datatype::copy(recvbuf, count, datatype,sendtmpbuf, count, datatype);
1823 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1824 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
1825 extra->type = TRACING_ALLREDUCE;
1827 extra->datatype1 = encode_datatype(datatype, &known);
1828 int dt_size_send = 1;
1830 dt_size_send = datatype->size();
1831 extra->send_size = count * dt_size_send;
1833 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, extra);
1835 simgrid::smpi::Colls::allreduce(sendtmpbuf, recvbuf, count, datatype, op, comm);
1837 if( sendbuf == MPI_IN_PLACE )
1838 xbt_free(sendtmpbuf);
1840 retval = MPI_SUCCESS;
1841 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
1848 int PMPI_Scan(void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
1854 if (comm == MPI_COMM_NULL) {
1855 retval = MPI_ERR_COMM;
1856 } else if (not datatype->is_valid()) {
1857 retval = MPI_ERR_TYPE;
1858 } else if (op == MPI_OP_NULL) {
1859 retval = MPI_ERR_OP;
1861 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1862 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
1863 extra->type = TRACING_SCAN;
1865 extra->datatype1 = encode_datatype(datatype, &known);
1866 int dt_size_send = 1;
1868 dt_size_send = datatype->size();
1869 extra->send_size = count * dt_size_send;
1871 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, extra);
1873 retval = simgrid::smpi::Colls::scan(sendbuf, recvbuf, count, datatype, op, comm);
1875 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
1882 int PMPI_Exscan(void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm){
1887 if (comm == MPI_COMM_NULL) {
1888 retval = MPI_ERR_COMM;
1889 } else if (not datatype->is_valid()) {
1890 retval = MPI_ERR_TYPE;
1891 } else if (op == MPI_OP_NULL) {
1892 retval = MPI_ERR_OP;
1894 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1895 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
1896 extra->type = TRACING_EXSCAN;
1898 extra->datatype1 = encode_datatype(datatype, &known);
1899 int dt_size_send = 1;
1901 dt_size_send = datatype->size();
1902 extra->send_size = count * dt_size_send;
1903 void* sendtmpbuf = sendbuf;
1904 if (sendbuf == MPI_IN_PLACE) {
1905 sendtmpbuf = static_cast<void*>(xbt_malloc(count * datatype->size()));
1906 memcpy(sendtmpbuf, recvbuf, count * datatype->size());
1908 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, extra);
1910 retval = simgrid::smpi::Colls::exscan(sendtmpbuf, recvbuf, count, datatype, op, comm);
1912 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
1913 if (sendbuf == MPI_IN_PLACE)
1914 xbt_free(sendtmpbuf);
1921 int PMPI_Reduce_scatter(void *sendbuf, void *recvbuf, int *recvcounts, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
1926 if (comm == MPI_COMM_NULL) {
1927 retval = MPI_ERR_COMM;
1928 } else if (not datatype->is_valid()) {
1929 retval = MPI_ERR_TYPE;
1930 } else if (op == MPI_OP_NULL) {
1931 retval = MPI_ERR_OP;
1932 } else if (recvcounts == nullptr) {
1933 retval = MPI_ERR_ARG;
1935 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1937 int size = comm->size();
1938 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
1939 extra->type = TRACING_REDUCE_SCATTER;
1940 extra->num_processes = size;
1942 extra->datatype1 = encode_datatype(datatype, &known);
1943 int dt_size_send = 1;
1945 dt_size_send = datatype->size();
1946 extra->send_size = 0;
1947 extra->recvcounts = xbt_new(int, size);
1949 for (i = 0; i < size; i++) { // copy data to avoid bad free
1950 extra->recvcounts[i] = recvcounts[i] * dt_size_send;
1951 totalcount += recvcounts[i];
1953 void* sendtmpbuf = sendbuf;
1954 if (sendbuf == MPI_IN_PLACE) {
1955 sendtmpbuf = static_cast<void*>(xbt_malloc(totalcount * datatype->size()));
1956 memcpy(sendtmpbuf, recvbuf, totalcount * datatype->size());
1959 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, extra);
1961 simgrid::smpi::Colls::reduce_scatter(sendtmpbuf, recvbuf, recvcounts, datatype, op, comm);
1962 retval = MPI_SUCCESS;
1963 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
1965 if (sendbuf == MPI_IN_PLACE)
1966 xbt_free(sendtmpbuf);
1973 int PMPI_Reduce_scatter_block(void *sendbuf, void *recvbuf, int recvcount,
1974 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
1979 if (comm == MPI_COMM_NULL) {
1980 retval = MPI_ERR_COMM;
1981 } else if (not datatype->is_valid()) {
1982 retval = MPI_ERR_TYPE;
1983 } else if (op == MPI_OP_NULL) {
1984 retval = MPI_ERR_OP;
1985 } else if (recvcount < 0) {
1986 retval = MPI_ERR_ARG;
1988 int count = comm->size();
1990 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
1991 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
1992 extra->type = TRACING_REDUCE_SCATTER;
1993 extra->num_processes = count;
1995 extra->datatype1 = encode_datatype(datatype, &known);
1996 int dt_size_send = 1;
1998 dt_size_send = datatype->size();
1999 extra->send_size = 0;
2000 extra->recvcounts = xbt_new(int, count);
2001 for (int i = 0; i < count; i++) // copy data to avoid bad free
2002 extra->recvcounts[i] = recvcount * dt_size_send;
2003 void* sendtmpbuf = sendbuf;
2004 if (sendbuf == MPI_IN_PLACE) {
2005 sendtmpbuf = static_cast<void*>(xbt_malloc(recvcount * count * datatype->size()));
2006 memcpy(sendtmpbuf, recvbuf, recvcount * count * datatype->size());
2009 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, extra);
2011 int* recvcounts = static_cast<int*>(xbt_malloc(count * sizeof(int)));
2012 for (int i = 0; i < count; i++)
2013 recvcounts[i] = recvcount;
2014 simgrid::smpi::Colls::reduce_scatter(sendtmpbuf, recvbuf, recvcounts, datatype, op, comm);
2015 xbt_free(recvcounts);
2016 retval = MPI_SUCCESS;
2018 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
2020 if (sendbuf == MPI_IN_PLACE)
2021 xbt_free(sendtmpbuf);
2028 int PMPI_Alltoall(void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int recvcount,
2029 MPI_Datatype recvtype, MPI_Comm comm)
2034 if (comm == MPI_COMM_NULL) {
2035 retval = MPI_ERR_COMM;
2036 } else if ((sendbuf != MPI_IN_PLACE && sendtype == MPI_DATATYPE_NULL) || recvtype == MPI_DATATYPE_NULL) {
2037 retval = MPI_ERR_TYPE;
2039 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
2040 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
2041 extra->type = TRACING_ALLTOALL;
2043 void* sendtmpbuf = static_cast<char*>(sendbuf);
2044 int sendtmpcount = sendcount;
2045 MPI_Datatype sendtmptype = sendtype;
2046 if (sendbuf == MPI_IN_PLACE) {
2047 sendtmpbuf = static_cast<void*>(xbt_malloc(recvcount * comm->size() * recvtype->size()));
2048 memcpy(sendtmpbuf, recvbuf, recvcount * comm->size() * recvtype->size());
2049 sendtmpcount = recvcount;
2050 sendtmptype = recvtype;
2054 extra->datatype1 = encode_datatype(sendtmptype, &known);
2056 extra->send_size = sendtmpcount * sendtmptype->size();
2058 extra->send_size = sendtmpcount;
2059 extra->datatype2 = encode_datatype(recvtype, &known);
2061 extra->recv_size = recvcount * recvtype->size();
2063 extra->recv_size = recvcount;
2065 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, extra);
2067 retval = simgrid::smpi::Colls::alltoall(sendtmpbuf, sendtmpcount, sendtmptype, recvbuf, recvcount, recvtype, comm);
2069 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
2071 if (sendbuf == MPI_IN_PLACE)
2072 xbt_free(sendtmpbuf);
2079 int PMPI_Alltoallv(void* sendbuf, int* sendcounts, int* senddisps, MPI_Datatype sendtype, void* recvbuf,
2080 int* recvcounts, int* recvdisps, MPI_Datatype recvtype, MPI_Comm comm)
2086 if (comm == MPI_COMM_NULL) {
2087 retval = MPI_ERR_COMM;
2088 } else if (sendtype == MPI_DATATYPE_NULL || recvtype == MPI_DATATYPE_NULL) {
2089 retval = MPI_ERR_TYPE;
2090 } else if ((sendbuf != MPI_IN_PLACE && (sendcounts == nullptr || senddisps == nullptr)) || recvcounts == nullptr ||
2091 recvdisps == nullptr) {
2092 retval = MPI_ERR_ARG;
2094 int rank = comm != MPI_COMM_NULL ? smpi_process()->index() : -1;
2096 int size = comm->size();
2097 instr_extra_data extra = xbt_new0(s_instr_extra_data_t, 1);
2098 extra->type = TRACING_ALLTOALLV;
2099 extra->send_size = 0;
2100 extra->recv_size = 0;
2101 extra->recvcounts = xbt_new(int, size);
2102 extra->sendcounts = xbt_new(int, size);
2104 extra->datatype2 = encode_datatype(recvtype, &known);
2105 int dt_size_recv = recvtype->size();
2107 void* sendtmpbuf = static_cast<char*>(sendbuf);
2108 int* sendtmpcounts = sendcounts;
2109 int* sendtmpdisps = senddisps;
2110 MPI_Datatype sendtmptype = sendtype;
2112 for (i = 0; i < size; i++) { // copy data to avoid bad free
2113 extra->recv_size += recvcounts[i] * dt_size_recv;
2114 extra->recvcounts[i] = recvcounts[i] * dt_size_recv;
2115 if (((recvdisps[i] + recvcounts[i]) * dt_size_recv) > maxsize)
2116 maxsize = (recvdisps[i] + recvcounts[i]) * dt_size_recv;
2119 if (sendbuf == MPI_IN_PLACE) {
2120 sendtmpbuf = static_cast<void*>(xbt_malloc(maxsize));
2121 memcpy(sendtmpbuf, recvbuf, maxsize);
2122 sendtmpcounts = static_cast<int*>(xbt_malloc(size * sizeof(int)));
2123 memcpy(sendtmpcounts, recvcounts, size * sizeof(int));
2124 sendtmpdisps = static_cast<int*>(xbt_malloc(size * sizeof(int)));
2125 memcpy(sendtmpdisps, recvdisps, size * sizeof(int));
2126 sendtmptype = recvtype;
2129 extra->datatype1 = encode_datatype(sendtmptype, &known);
2130 int dt_size_send = 1;
2131 dt_size_send = sendtmptype->size();
2133 for (i = 0; i < size; i++) { // copy data to avoid bad free
2134 extra->send_size += sendtmpcounts[i] * dt_size_send;
2135 extra->sendcounts[i] = sendtmpcounts[i] * dt_size_send;
2137 extra->num_processes = size;
2138 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, extra);
2139 retval = simgrid::smpi::Colls::alltoallv(sendtmpbuf, sendtmpcounts, sendtmpdisps, sendtmptype, recvbuf, recvcounts,
2140 recvdisps, recvtype, comm);
2141 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
2143 if (sendbuf == MPI_IN_PLACE) {
2144 xbt_free(sendtmpbuf);
2145 xbt_free(sendtmpcounts);
2146 xbt_free(sendtmpdisps);
2155 int PMPI_Get_processor_name(char *name, int *resultlen)
2157 strncpy(name, SIMIX_host_self()->cname(), strlen(SIMIX_host_self()->cname()) < MPI_MAX_PROCESSOR_NAME - 1
2158 ? strlen(SIMIX_host_self()->cname()) + 1
2159 : MPI_MAX_PROCESSOR_NAME - 1);
2160 *resultlen = strlen(name) > MPI_MAX_PROCESSOR_NAME ? MPI_MAX_PROCESSOR_NAME : strlen(name);
2165 int PMPI_Get_count(MPI_Status * status, MPI_Datatype datatype, int *count)
2167 if (status == nullptr || count == nullptr) {
2169 } else if (not datatype->is_valid()) {
2170 return MPI_ERR_TYPE;
2172 size_t size = datatype->size();
2176 } else if (status->count % size != 0) {
2177 return MPI_UNDEFINED;
2179 *count = simgrid::smpi::Status::get_count(status, datatype);
2185 int PMPI_Type_contiguous(int count, MPI_Datatype old_type, MPI_Datatype* new_type) {
2186 if (old_type == MPI_DATATYPE_NULL) {
2187 return MPI_ERR_TYPE;
2188 } else if (count<0){
2189 return MPI_ERR_COUNT;
2191 return simgrid::smpi::Datatype::create_contiguous(count, old_type, 0, new_type);
2195 int PMPI_Type_commit(MPI_Datatype* datatype) {
2196 if (datatype == nullptr || *datatype == MPI_DATATYPE_NULL) {
2197 return MPI_ERR_TYPE;
2199 (*datatype)->commit();
2204 int PMPI_Type_vector(int count, int blocklen, int stride, MPI_Datatype old_type, MPI_Datatype* new_type) {
2205 if (old_type == MPI_DATATYPE_NULL) {
2206 return MPI_ERR_TYPE;
2207 } else if (count<0 || blocklen<0){
2208 return MPI_ERR_COUNT;
2210 return simgrid::smpi::Datatype::create_vector(count, blocklen, stride, old_type, new_type);
2214 int PMPI_Type_hvector(int count, int blocklen, MPI_Aint stride, MPI_Datatype old_type, MPI_Datatype* new_type) {
2215 if (old_type == MPI_DATATYPE_NULL) {
2216 return MPI_ERR_TYPE;
2217 } else if (count<0 || blocklen<0){
2218 return MPI_ERR_COUNT;
2220 return simgrid::smpi::Datatype::create_hvector(count, blocklen, stride, old_type, new_type);
2224 int PMPI_Type_create_hvector(int count, int blocklen, MPI_Aint stride, MPI_Datatype old_type, MPI_Datatype* new_type) {
2225 return MPI_Type_hvector(count, blocklen, stride, old_type, new_type);
2228 int PMPI_Type_indexed(int count, int* blocklens, int* indices, MPI_Datatype old_type, MPI_Datatype* new_type) {
2229 if (old_type == MPI_DATATYPE_NULL) {
2230 return MPI_ERR_TYPE;
2231 } else if (count<0){
2232 return MPI_ERR_COUNT;
2234 return simgrid::smpi::Datatype::create_indexed(count, blocklens, indices, old_type, new_type);
2238 int PMPI_Type_create_indexed(int count, int* blocklens, int* indices, MPI_Datatype old_type, MPI_Datatype* new_type) {
2239 if (old_type == MPI_DATATYPE_NULL) {
2240 return MPI_ERR_TYPE;
2241 } else if (count<0){
2242 return MPI_ERR_COUNT;
2244 return simgrid::smpi::Datatype::create_indexed(count, blocklens, indices, old_type, new_type);
2248 int PMPI_Type_create_indexed_block(int count, int blocklength, int* indices, MPI_Datatype old_type,
2249 MPI_Datatype* new_type)
2251 if (old_type == MPI_DATATYPE_NULL) {
2252 return MPI_ERR_TYPE;
2253 } else if (count<0){
2254 return MPI_ERR_COUNT;
2256 int* blocklens=static_cast<int*>(xbt_malloc(blocklength*count*sizeof(int)));
2257 for (int i = 0; i < count; i++)
2258 blocklens[i]=blocklength;
2259 int retval = simgrid::smpi::Datatype::create_indexed(count, blocklens, indices, old_type, new_type);
2260 xbt_free(blocklens);
2265 int PMPI_Type_hindexed(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype old_type, MPI_Datatype* new_type)
2267 if (old_type == MPI_DATATYPE_NULL) {
2268 return MPI_ERR_TYPE;
2269 } else if (count<0){
2270 return MPI_ERR_COUNT;
2272 return simgrid::smpi::Datatype::create_hindexed(count, blocklens, indices, old_type, new_type);
2276 int PMPI_Type_create_hindexed(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype old_type,
2277 MPI_Datatype* new_type) {
2278 return PMPI_Type_hindexed(count, blocklens,indices,old_type,new_type);
2281 int PMPI_Type_create_hindexed_block(int count, int blocklength, MPI_Aint* indices, MPI_Datatype old_type,
2282 MPI_Datatype* new_type) {
2283 if (old_type == MPI_DATATYPE_NULL) {
2284 return MPI_ERR_TYPE;
2285 } else if (count<0){
2286 return MPI_ERR_COUNT;
2288 int* blocklens=(int*)xbt_malloc(blocklength*count*sizeof(int));
2289 for (int i = 0; i < count; i++)
2290 blocklens[i] = blocklength;
2291 int retval = simgrid::smpi::Datatype::create_hindexed(count, blocklens, indices, old_type, new_type);
2292 xbt_free(blocklens);
2297 int PMPI_Type_struct(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype* old_types, MPI_Datatype* new_type) {
2299 return MPI_ERR_COUNT;
2301 return simgrid::smpi::Datatype::create_struct(count, blocklens, indices, old_types, new_type);
2305 int PMPI_Type_create_struct(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype* old_types,
2306 MPI_Datatype* new_type) {
2307 return PMPI_Type_struct(count, blocklens, indices, old_types, new_type);
2310 int PMPI_Error_class(int errorcode, int* errorclass) {
2311 // assume smpi uses only standard mpi error codes
2312 *errorclass=errorcode;
2316 int PMPI_Initialized(int* flag) {
2317 *flag=(smpi_process()!=nullptr && smpi_process()->initialized());
2321 /* The topo part of MPI_COMM_WORLD should always be nullptr. When other topologies will be implemented, not only should we
2322 * check if the topology is nullptr, but we should check if it is the good topology type (so we have to add a
2323 * MPIR_Topo_Type field, and replace the MPI_Topology field by an union)*/
2325 int PMPI_Cart_create(MPI_Comm comm_old, int ndims, int* dims, int* periodic, int reorder, MPI_Comm* comm_cart) {
2326 if (comm_old == MPI_COMM_NULL){
2327 return MPI_ERR_COMM;
2328 } else if (ndims < 0 || (ndims > 0 && (dims == nullptr || periodic == nullptr)) || comm_cart == nullptr) {
2331 simgrid::smpi::Topo_Cart* topo = new simgrid::smpi::Topo_Cart(comm_old, ndims, dims, periodic, reorder, comm_cart);
2332 if(*comm_cart==MPI_COMM_NULL)
2338 int PMPI_Cart_rank(MPI_Comm comm, int* coords, int* rank) {
2339 if(comm == MPI_COMM_NULL || comm->topo() == nullptr) {
2340 return MPI_ERR_TOPOLOGY;
2342 if (coords == nullptr) {
2345 MPIR_Cart_Topology topo = static_cast<MPIR_Cart_Topology>(comm->topo());
2346 if (topo==nullptr) {
2349 return topo->rank(coords, rank);
2352 int PMPI_Cart_shift(MPI_Comm comm, int direction, int displ, int* source, int* dest) {
2353 if(comm == MPI_COMM_NULL || comm->topo() == nullptr) {
2354 return MPI_ERR_TOPOLOGY;
2356 if (source == nullptr || dest == nullptr || direction < 0 ) {
2359 MPIR_Cart_Topology topo = static_cast<MPIR_Cart_Topology>(comm->topo());
2360 if (topo==nullptr) {
2363 return topo->shift(direction, displ, source, dest);
2366 int PMPI_Cart_coords(MPI_Comm comm, int rank, int maxdims, int* coords) {
2367 if(comm == MPI_COMM_NULL || comm->topo() == nullptr) {
2368 return MPI_ERR_TOPOLOGY;
2370 if (rank < 0 || rank >= comm->size()) {
2371 return MPI_ERR_RANK;
2376 if(coords == nullptr) {
2379 MPIR_Cart_Topology topo = static_cast<MPIR_Cart_Topology>(comm->topo());
2380 if (topo==nullptr) {
2383 return topo->coords(rank, maxdims, coords);
2386 int PMPI_Cart_get(MPI_Comm comm, int maxdims, int* dims, int* periods, int* coords) {
2387 if(comm == nullptr || comm->topo() == nullptr) {
2388 return MPI_ERR_TOPOLOGY;
2390 if(maxdims <= 0 || dims == nullptr || periods == nullptr || coords == nullptr) {
2393 MPIR_Cart_Topology topo = static_cast<MPIR_Cart_Topology>(comm->topo());
2394 if (topo==nullptr) {
2397 return topo->get(maxdims, dims, periods, coords);
2400 int PMPI_Cartdim_get(MPI_Comm comm, int* ndims) {
2401 if (comm == MPI_COMM_NULL || comm->topo() == nullptr) {
2402 return MPI_ERR_TOPOLOGY;
2404 if (ndims == nullptr) {
2407 MPIR_Cart_Topology topo = static_cast<MPIR_Cart_Topology>(comm->topo());
2408 if (topo==nullptr) {
2411 return topo->dim_get(ndims);
2414 int PMPI_Dims_create(int nnodes, int ndims, int* dims) {
2415 if(dims == nullptr) {
2418 if (ndims < 1 || nnodes < 1) {
2419 return MPI_ERR_DIMS;
2421 return simgrid::smpi::Topo_Cart::Dims_create(nnodes, ndims, dims);
2424 int PMPI_Cart_sub(MPI_Comm comm, int* remain_dims, MPI_Comm* comm_new) {
2425 if(comm == MPI_COMM_NULL || comm->topo() == nullptr) {
2426 return MPI_ERR_TOPOLOGY;
2428 if (comm_new == nullptr) {
2431 MPIR_Cart_Topology topo = static_cast<MPIR_Cart_Topology>(comm->topo());
2432 if (topo==nullptr) {
2435 MPIR_Cart_Topology cart = topo->sub(remain_dims, comm_new);
2436 if(*comm_new==MPI_COMM_NULL)
2443 int PMPI_Type_create_resized(MPI_Datatype oldtype,MPI_Aint lb, MPI_Aint extent, MPI_Datatype *newtype){
2444 if (oldtype == MPI_DATATYPE_NULL) {
2445 return MPI_ERR_TYPE;
2447 int blocks[3] = {1, 1, 1};
2448 MPI_Aint disps[3] = {lb, 0, lb + extent};
2449 MPI_Datatype types[3] = {MPI_LB, oldtype, MPI_UB};
2451 *newtype = new simgrid::smpi::Type_Struct(oldtype->size(), lb, lb + extent, DT_FLAG_DERIVED, 3, blocks, disps, types);
2453 (*newtype)->addflag(~DT_FLAG_COMMITED);
2457 int PMPI_Win_create( void *base, MPI_Aint size, int disp_unit, MPI_Info info, MPI_Comm comm, MPI_Win *win){
2460 if (comm == MPI_COMM_NULL) {
2461 retval= MPI_ERR_COMM;
2462 }else if ((base == nullptr && size != 0) || disp_unit <= 0 || size < 0 ){
2463 retval= MPI_ERR_OTHER;
2465 *win = new simgrid::smpi::Win( base, size, disp_unit, info, comm);
2466 retval = MPI_SUCCESS;
2472 int PMPI_Win_allocate( MPI_Aint size, int disp_unit, MPI_Info info, MPI_Comm comm, void *base, MPI_Win *win){
2475 if (comm == MPI_COMM_NULL) {
2476 retval= MPI_ERR_COMM;
2477 }else if (disp_unit <= 0 || size < 0 ){
2478 retval= MPI_ERR_OTHER;
2480 void* ptr = xbt_malloc(size);
2482 return MPI_ERR_NO_MEM;
2483 *static_cast<void**>(base) = ptr;
2484 *win = new simgrid::smpi::Win( ptr, size, disp_unit, info, comm,1);
2485 retval = MPI_SUCCESS;
2491 int PMPI_Win_create_dynamic( MPI_Info info, MPI_Comm comm, MPI_Win *win){
2494 if (comm == MPI_COMM_NULL) {
2495 retval= MPI_ERR_COMM;
2497 *win = new simgrid::smpi::Win(info, comm);
2498 retval = MPI_SUCCESS;
2504 int PMPI_Win_attach(MPI_Win win, void *base, MPI_Aint size){
2507 if(win == MPI_WIN_NULL){
2508 retval = MPI_ERR_WIN;
2509 } else if ((base == nullptr && size != 0) || size < 0 ){
2510 retval= MPI_ERR_OTHER;
2512 retval = win->attach(base, size);
2518 int PMPI_Win_detach(MPI_Win win, void *base){
2521 if(win == MPI_WIN_NULL){
2522 retval = MPI_ERR_WIN;
2523 } else if (base == nullptr){
2524 retval= MPI_ERR_OTHER;
2526 retval = win->detach(base);
2533 int PMPI_Win_free( MPI_Win* win){
2536 if (win == nullptr || *win == MPI_WIN_NULL) {
2537 retval = MPI_ERR_WIN;
2546 int PMPI_Win_set_name(MPI_Win win, char * name)
2548 if (win == MPI_WIN_NULL) {
2549 return MPI_ERR_TYPE;
2550 } else if (name == nullptr) {
2553 win->set_name(name);
2558 int PMPI_Win_get_name(MPI_Win win, char * name, int* len)
2560 if (win == MPI_WIN_NULL) {
2562 } else if (name == nullptr) {
2565 win->get_name(name, len);
2570 int PMPI_Win_get_info(MPI_Win win, MPI_Info* info)
2572 if (win == MPI_WIN_NULL) {
2575 *info = win->info();
2580 int PMPI_Win_set_info(MPI_Win win, MPI_Info info)
2582 if (win == MPI_WIN_NULL) {
2583 return MPI_ERR_TYPE;
2585 win->set_info(info);
2590 int PMPI_Win_get_group(MPI_Win win, MPI_Group * group){
2591 if (win == MPI_WIN_NULL) {
2594 win->get_group(group);
2600 int PMPI_Win_fence( int assert, MPI_Win win){
2603 if (win == MPI_WIN_NULL) {
2604 retval = MPI_ERR_WIN;
2606 int rank = smpi_process()->index();
2607 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, nullptr);
2608 retval = win->fence(assert);
2609 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
2615 int PMPI_Get( void *origin_addr, int origin_count, MPI_Datatype origin_datatype, int target_rank,
2616 MPI_Aint target_disp, int target_count, MPI_Datatype target_datatype, MPI_Win win){
2619 if (win == MPI_WIN_NULL) {
2620 retval = MPI_ERR_WIN;
2621 } else if (target_rank == MPI_PROC_NULL) {
2622 retval = MPI_SUCCESS;
2623 } else if (target_rank <0){
2624 retval = MPI_ERR_RANK;
2625 } else if (win->dynamic()==0 && target_disp <0){
2626 //in case of dynamic window, target_disp can be mistakenly seen as negative, as it is an address
2627 retval = MPI_ERR_ARG;
2628 } else if ((origin_count < 0 || target_count < 0) ||
2629 (origin_addr==nullptr && origin_count > 0)){
2630 retval = MPI_ERR_COUNT;
2631 } else if ((not origin_datatype->is_valid()) || (not target_datatype->is_valid())) {
2632 retval = MPI_ERR_TYPE;
2634 int rank = smpi_process()->index();
2636 win->get_group(&group);
2637 int src_traced = group->index(target_rank);
2638 TRACE_smpi_ptp_in(rank, src_traced, rank, __FUNCTION__, nullptr);
2640 retval = win->get( origin_addr, origin_count, origin_datatype, target_rank, target_disp, target_count,
2643 TRACE_smpi_ptp_out(rank, src_traced, rank, __FUNCTION__);
2649 int PMPI_Rget( void *origin_addr, int origin_count, MPI_Datatype origin_datatype, int target_rank,
2650 MPI_Aint target_disp, int target_count, MPI_Datatype target_datatype, MPI_Win win, MPI_Request* request){
2653 if (win == MPI_WIN_NULL) {
2654 retval = MPI_ERR_WIN;
2655 } else if (target_rank == MPI_PROC_NULL) {
2656 *request = MPI_REQUEST_NULL;
2657 retval = MPI_SUCCESS;
2658 } else if (target_rank <0){
2659 retval = MPI_ERR_RANK;
2660 } else if (win->dynamic()==0 && target_disp <0){
2661 //in case of dynamic window, target_disp can be mistakenly seen as negative, as it is an address
2662 retval = MPI_ERR_ARG;
2663 } else if ((origin_count < 0 || target_count < 0) ||
2664 (origin_addr==nullptr && origin_count > 0)){
2665 retval = MPI_ERR_COUNT;
2666 } else if ((not origin_datatype->is_valid()) || (not target_datatype->is_valid())) {
2667 retval = MPI_ERR_TYPE;
2668 } else if(request == nullptr){
2669 retval = MPI_ERR_REQUEST;
2671 int rank = smpi_process()->index();
2673 win->get_group(&group);
2674 int src_traced = group->index(target_rank);
2675 TRACE_smpi_ptp_in(rank, src_traced, rank, __FUNCTION__, nullptr);
2677 retval = win->get( origin_addr, origin_count, origin_datatype, target_rank, target_disp, target_count,
2678 target_datatype, request);
2680 TRACE_smpi_ptp_out(rank, src_traced, rank, __FUNCTION__);
2686 int PMPI_Put( void *origin_addr, int origin_count, MPI_Datatype origin_datatype, int target_rank,
2687 MPI_Aint target_disp, int target_count, MPI_Datatype target_datatype, MPI_Win win){
2690 if (win == MPI_WIN_NULL) {
2691 retval = MPI_ERR_WIN;
2692 } else if (target_rank == MPI_PROC_NULL) {
2693 retval = MPI_SUCCESS;
2694 } else if (target_rank <0){
2695 retval = MPI_ERR_RANK;
2696 } else if (win->dynamic()==0 && target_disp <0){
2697 //in case of dynamic window, target_disp can be mistakenly seen as negative, as it is an address
2698 retval = MPI_ERR_ARG;
2699 } else if ((origin_count < 0 || target_count < 0) ||
2700 (origin_addr==nullptr && origin_count > 0)){
2701 retval = MPI_ERR_COUNT;
2702 } else if ((not origin_datatype->is_valid()) || (not target_datatype->is_valid())) {
2703 retval = MPI_ERR_TYPE;
2705 int rank = smpi_process()->index();
2707 win->get_group(&group);
2708 int dst_traced = group->index(target_rank);
2709 TRACE_smpi_ptp_in(rank, rank, dst_traced, __FUNCTION__, nullptr);
2710 TRACE_smpi_send(rank, rank, dst_traced, SMPI_RMA_TAG, origin_count*origin_datatype->size());
2712 retval = win->put( origin_addr, origin_count, origin_datatype, target_rank, target_disp, target_count,
2715 TRACE_smpi_ptp_out(rank, rank, dst_traced, __FUNCTION__);
2721 int PMPI_Rput( void *origin_addr, int origin_count, MPI_Datatype origin_datatype, int target_rank,
2722 MPI_Aint target_disp, int target_count, MPI_Datatype target_datatype, MPI_Win win, MPI_Request* request){
2725 if (win == MPI_WIN_NULL) {
2726 retval = MPI_ERR_WIN;
2727 } else if (target_rank == MPI_PROC_NULL) {
2728 *request = MPI_REQUEST_NULL;
2729 retval = MPI_SUCCESS;
2730 } else if (target_rank <0){
2731 retval = MPI_ERR_RANK;
2732 } else if (win->dynamic()==0 && target_disp <0){
2733 //in case of dynamic window, target_disp can be mistakenly seen as negative, as it is an address
2734 retval = MPI_ERR_ARG;
2735 } else if ((origin_count < 0 || target_count < 0) ||
2736 (origin_addr==nullptr && origin_count > 0)){
2737 retval = MPI_ERR_COUNT;
2738 } else if ((not origin_datatype->is_valid()) || (not target_datatype->is_valid())) {
2739 retval = MPI_ERR_TYPE;
2740 } else if(request == nullptr){
2741 retval = MPI_ERR_REQUEST;
2743 int rank = smpi_process()->index();
2745 win->get_group(&group);
2746 int dst_traced = group->index(target_rank);
2747 TRACE_smpi_ptp_in(rank, rank, dst_traced, __FUNCTION__, nullptr);
2748 TRACE_smpi_send(rank, rank, dst_traced, SMPI_RMA_TAG, origin_count*origin_datatype->size());
2750 retval = win->put( origin_addr, origin_count, origin_datatype, target_rank, target_disp, target_count,
2751 target_datatype, request);
2753 TRACE_smpi_ptp_out(rank, rank, dst_traced, __FUNCTION__);
2759 int PMPI_Accumulate( void *origin_addr, int origin_count, MPI_Datatype origin_datatype, int target_rank,
2760 MPI_Aint target_disp, int target_count, MPI_Datatype target_datatype, MPI_Op op, MPI_Win win){
2763 if (win == MPI_WIN_NULL) {
2764 retval = MPI_ERR_WIN;
2765 } else if (target_rank == MPI_PROC_NULL) {
2766 retval = MPI_SUCCESS;
2767 } else if (target_rank <0){
2768 retval = MPI_ERR_RANK;
2769 } else if (win->dynamic()==0 && target_disp <0){
2770 //in case of dynamic window, target_disp can be mistakenly seen as negative, as it is an address
2771 retval = MPI_ERR_ARG;
2772 } else if ((origin_count < 0 || target_count < 0) ||
2773 (origin_addr==nullptr && origin_count > 0)){
2774 retval = MPI_ERR_COUNT;
2775 } else if ((not origin_datatype->is_valid()) || (not target_datatype->is_valid())) {
2776 retval = MPI_ERR_TYPE;
2777 } else if (op == MPI_OP_NULL) {
2778 retval = MPI_ERR_OP;
2780 int rank = smpi_process()->index();
2782 win->get_group(&group);
2783 int src_traced = group->index(target_rank);
2784 TRACE_smpi_ptp_in(rank, src_traced, rank, __FUNCTION__, nullptr);
2786 retval = win->accumulate( origin_addr, origin_count, origin_datatype, target_rank, target_disp, target_count,
2787 target_datatype, op);
2789 TRACE_smpi_ptp_out(rank, src_traced, rank, __FUNCTION__);
2795 int PMPI_Raccumulate( void *origin_addr, int origin_count, MPI_Datatype origin_datatype, int target_rank,
2796 MPI_Aint target_disp, int target_count, MPI_Datatype target_datatype, MPI_Op op, MPI_Win win, MPI_Request* request){
2799 if (win == MPI_WIN_NULL) {
2800 retval = MPI_ERR_WIN;
2801 } else if (target_rank == MPI_PROC_NULL) {
2802 *request = MPI_REQUEST_NULL;
2803 retval = MPI_SUCCESS;
2804 } else if (target_rank <0){
2805 retval = MPI_ERR_RANK;
2806 } else if (win->dynamic()==0 && target_disp <0){
2807 //in case of dynamic window, target_disp can be mistakenly seen as negative, as it is an address
2808 retval = MPI_ERR_ARG;
2809 } else if ((origin_count < 0 || target_count < 0) ||
2810 (origin_addr==nullptr && origin_count > 0)){
2811 retval = MPI_ERR_COUNT;
2812 } else if ((not origin_datatype->is_valid()) || (not target_datatype->is_valid())) {
2813 retval = MPI_ERR_TYPE;
2814 } else if (op == MPI_OP_NULL) {
2815 retval = MPI_ERR_OP;
2816 } else if(request == nullptr){
2817 retval = MPI_ERR_REQUEST;
2819 int rank = smpi_process()->index();
2821 win->get_group(&group);
2822 int src_traced = group->index(target_rank);
2823 TRACE_smpi_ptp_in(rank, src_traced, rank, __FUNCTION__, nullptr);
2825 retval = win->accumulate( origin_addr, origin_count, origin_datatype, target_rank, target_disp, target_count,
2826 target_datatype, op, request);
2828 TRACE_smpi_ptp_out(rank, src_traced, rank, __FUNCTION__);
2834 int PMPI_Get_accumulate(void *origin_addr, int origin_count, MPI_Datatype origin_datatype, void *result_addr,
2835 int result_count, MPI_Datatype result_datatype, int target_rank, MPI_Aint target_disp, int target_count,
2836 MPI_Datatype target_datatype, MPI_Op op, MPI_Win win){
2839 if (win == MPI_WIN_NULL) {
2840 retval = MPI_ERR_WIN;
2841 } else if (target_rank == MPI_PROC_NULL) {
2842 retval = MPI_SUCCESS;
2843 } else if (target_rank <0){
2844 retval = MPI_ERR_RANK;
2845 } else if (win->dynamic()==0 && target_disp <0){
2846 //in case of dynamic window, target_disp can be mistakenly seen as negative, as it is an address
2847 retval = MPI_ERR_ARG;
2848 } else if ((origin_count < 0 || target_count < 0 || result_count <0) ||
2849 (origin_addr==nullptr && origin_count > 0 && op != MPI_NO_OP) ||
2850 (result_addr==nullptr && result_count > 0)){
2851 retval = MPI_ERR_COUNT;
2852 } else if ((origin_datatype != MPI_DATATYPE_NULL && not origin_datatype->is_valid()) ||
2853 (not target_datatype->is_valid()) || (not result_datatype->is_valid())) {
2854 retval = MPI_ERR_TYPE;
2855 } else if (op == MPI_OP_NULL) {
2856 retval = MPI_ERR_OP;
2858 int rank = smpi_process()->index();
2860 win->get_group(&group);
2861 int src_traced = group->index(target_rank);
2862 TRACE_smpi_ptp_in(rank, src_traced, rank, __FUNCTION__, nullptr);
2864 retval = win->get_accumulate( origin_addr, origin_count, origin_datatype, result_addr,
2865 result_count, result_datatype, target_rank, target_disp,
2866 target_count, target_datatype, op);
2868 TRACE_smpi_ptp_out(rank, src_traced, rank, __FUNCTION__);
2875 int PMPI_Rget_accumulate(void *origin_addr, int origin_count, MPI_Datatype origin_datatype, void *result_addr,
2876 int result_count, MPI_Datatype result_datatype, int target_rank, MPI_Aint target_disp, int target_count,
2877 MPI_Datatype target_datatype, MPI_Op op, MPI_Win win, MPI_Request* request){
2880 if (win == MPI_WIN_NULL) {
2881 retval = MPI_ERR_WIN;
2882 } else if (target_rank == MPI_PROC_NULL) {
2883 *request = MPI_REQUEST_NULL;
2884 retval = MPI_SUCCESS;
2885 } else if (target_rank <0){
2886 retval = MPI_ERR_RANK;
2887 } else if (win->dynamic()==0 && target_disp <0){
2888 //in case of dynamic window, target_disp can be mistakenly seen as negative, as it is an address
2889 retval = MPI_ERR_ARG;
2890 } else if ((origin_count < 0 || target_count < 0 || result_count <0) ||
2891 (origin_addr==nullptr && origin_count > 0 && op != MPI_NO_OP) ||
2892 (result_addr==nullptr && result_count > 0)){
2893 retval = MPI_ERR_COUNT;
2894 } else if ((origin_datatype != MPI_DATATYPE_NULL && not origin_datatype->is_valid()) ||
2895 (not target_datatype->is_valid()) || (not result_datatype->is_valid())) {
2896 retval = MPI_ERR_TYPE;
2897 } else if (op == MPI_OP_NULL) {
2898 retval = MPI_ERR_OP;
2899 } else if(request == nullptr){
2900 retval = MPI_ERR_REQUEST;
2902 int rank = smpi_process()->index();
2904 win->get_group(&group);
2905 int src_traced = group->index(target_rank);
2906 TRACE_smpi_ptp_in(rank, src_traced, rank, __FUNCTION__, nullptr);
2908 retval = win->get_accumulate( origin_addr, origin_count, origin_datatype, result_addr,
2909 result_count, result_datatype, target_rank, target_disp,
2910 target_count, target_datatype, op, request);
2912 TRACE_smpi_ptp_out(rank, src_traced, rank, __FUNCTION__);
2918 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){
2919 return PMPI_Get_accumulate(origin_addr, origin_addr==nullptr?0:1, dtype, result_addr, 1, dtype, target_rank, target_disp, 1, dtype, op, win);
2922 int PMPI_Compare_and_swap(void *origin_addr, void *compare_addr,
2923 void *result_addr, MPI_Datatype datatype, int target_rank,
2924 MPI_Aint target_disp, MPI_Win win){
2927 if (win == MPI_WIN_NULL) {
2928 retval = MPI_ERR_WIN;
2929 } else if (target_rank == MPI_PROC_NULL) {
2930 retval = MPI_SUCCESS;
2931 } else if (target_rank <0){
2932 retval = MPI_ERR_RANK;
2933 } else if (win->dynamic()==0 && target_disp <0){
2934 //in case of dynamic window, target_disp can be mistakenly seen as negative, as it is an address
2935 retval = MPI_ERR_ARG;
2936 } else if (origin_addr==nullptr || result_addr==nullptr || compare_addr==nullptr){
2937 retval = MPI_ERR_COUNT;
2938 } else if (not datatype->is_valid()) {
2939 retval = MPI_ERR_TYPE;
2941 int rank = smpi_process()->index();
2943 win->get_group(&group);
2944 int src_traced = group->index(target_rank);
2945 TRACE_smpi_ptp_in(rank, src_traced, rank, __FUNCTION__, nullptr);
2947 retval = win->compare_and_swap( origin_addr, compare_addr, result_addr, datatype,
2948 target_rank, target_disp);
2950 TRACE_smpi_ptp_out(rank, src_traced, rank, __FUNCTION__);
2956 int PMPI_Win_post(MPI_Group group, int assert, MPI_Win win){
2959 if (win == MPI_WIN_NULL) {
2960 retval = MPI_ERR_WIN;
2961 } else if (group==MPI_GROUP_NULL){
2962 retval = MPI_ERR_GROUP;
2964 int rank = smpi_process()->index();
2965 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, nullptr);
2966 retval = win->post(group,assert);
2967 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
2973 int PMPI_Win_start(MPI_Group group, int assert, MPI_Win win){
2976 if (win == MPI_WIN_NULL) {
2977 retval = MPI_ERR_WIN;
2978 } else if (group==MPI_GROUP_NULL){
2979 retval = MPI_ERR_GROUP;
2981 int rank = smpi_process()->index();
2982 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, nullptr);
2983 retval = win->start(group,assert);
2984 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
2990 int PMPI_Win_complete(MPI_Win win){
2993 if (win == MPI_WIN_NULL) {
2994 retval = MPI_ERR_WIN;
2996 int rank = smpi_process()->index();
2997 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, nullptr);
2999 retval = win->complete();
3001 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
3007 int PMPI_Win_wait(MPI_Win win){
3010 if (win == MPI_WIN_NULL) {
3011 retval = MPI_ERR_WIN;
3013 int rank = smpi_process()->index();
3014 TRACE_smpi_collective_in(rank, -1, __FUNCTION__, nullptr);
3016 retval = win->wait();
3018 TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
3024 int PMPI_Win_lock(int lock_type, int rank, int assert, MPI_Win win){
3027 if (win == MPI_WIN_NULL) {
3028 retval = MPI_ERR_WIN;
3029 } else if (lock_type != MPI_LOCK_EXCLUSIVE &&
3030 lock_type != MPI_LOCK_SHARED) {
3031 retval = MPI_ERR_LOCKTYPE;
3032 } else if (rank == MPI_PROC_NULL){
3033 retval = MPI_SUCCESS;
3035 int myrank = smpi_process()->index();
3036 TRACE_smpi_collective_in(myrank, -1, __FUNCTION__, nullptr);
3037 retval = win->lock(lock_type,rank,assert);
3038 TRACE_smpi_collective_out(myrank, -1, __FUNCTION__);
3044 int PMPI_Win_unlock(int rank, MPI_Win win){
3047 if (win == MPI_WIN_NULL) {
3048 retval = MPI_ERR_WIN;
3049 } else if (rank == MPI_PROC_NULL){
3050 retval = MPI_SUCCESS;
3052 int myrank = smpi_process()->index();
3053 TRACE_smpi_collective_in(myrank, -1, __FUNCTION__, nullptr);
3054 retval = win->unlock(rank);
3055 TRACE_smpi_collective_out(myrank, -1, __FUNCTION__);
3061 int PMPI_Win_lock_all(int assert, MPI_Win win){
3064 if (win == MPI_WIN_NULL) {
3065 retval = MPI_ERR_WIN;
3067 int myrank = smpi_process()->index();
3068 TRACE_smpi_collective_in(myrank, -1, __FUNCTION__, nullptr);
3069 retval = win->lock_all(assert);
3070 TRACE_smpi_collective_out(myrank, -1, __FUNCTION__);
3076 int PMPI_Win_unlock_all(MPI_Win win){
3079 if (win == MPI_WIN_NULL) {
3080 retval = MPI_ERR_WIN;
3082 int myrank = smpi_process()->index();
3083 TRACE_smpi_collective_in(myrank, -1, __FUNCTION__, nullptr);
3084 retval = win->unlock_all();
3085 TRACE_smpi_collective_out(myrank, -1, __FUNCTION__);
3091 int PMPI_Win_flush(int rank, MPI_Win win){
3094 if (win == MPI_WIN_NULL) {
3095 retval = MPI_ERR_WIN;
3096 } else if (rank == MPI_PROC_NULL){
3097 retval = MPI_SUCCESS;
3099 int myrank = smpi_process()->index();
3100 TRACE_smpi_collective_in(myrank, -1, __FUNCTION__, nullptr);
3101 retval = win->flush(rank);
3102 TRACE_smpi_collective_out(myrank, -1, __FUNCTION__);
3108 int PMPI_Win_flush_local(int rank, MPI_Win win){
3111 if (win == MPI_WIN_NULL) {
3112 retval = MPI_ERR_WIN;
3113 } else if (rank == MPI_PROC_NULL){
3114 retval = MPI_SUCCESS;
3116 int myrank = smpi_process()->index();
3117 TRACE_smpi_collective_in(myrank, -1, __FUNCTION__, nullptr);
3118 retval = win->flush_local(rank);
3119 TRACE_smpi_collective_out(myrank, -1, __FUNCTION__);
3125 int PMPI_Win_flush_all(MPI_Win win){
3128 if (win == MPI_WIN_NULL) {
3129 retval = MPI_ERR_WIN;
3131 int myrank = smpi_process()->index();
3132 TRACE_smpi_collective_in(myrank, -1, __FUNCTION__, nullptr);
3133 retval = win->flush_all();
3134 TRACE_smpi_collective_out(myrank, -1, __FUNCTION__);
3140 int PMPI_Win_flush_local_all(MPI_Win win){
3143 if (win == MPI_WIN_NULL) {
3144 retval = MPI_ERR_WIN;
3146 int myrank = smpi_process()->index();
3147 TRACE_smpi_collective_in(myrank, -1, __FUNCTION__, nullptr);
3148 retval = win->flush_local_all();
3149 TRACE_smpi_collective_out(myrank, -1, __FUNCTION__);
3155 int PMPI_Alloc_mem(MPI_Aint size, MPI_Info info, void *baseptr){
3156 void *ptr = xbt_malloc(size);
3158 return MPI_ERR_NO_MEM;
3160 *static_cast<void**>(baseptr) = ptr;
3165 int PMPI_Free_mem(void *baseptr){
3170 int PMPI_Type_set_name(MPI_Datatype datatype, char * name)
3172 if (datatype == MPI_DATATYPE_NULL) {
3173 return MPI_ERR_TYPE;
3174 } else if (name == nullptr) {
3177 datatype->set_name(name);
3182 int PMPI_Type_get_name(MPI_Datatype datatype, char * name, int* len)
3184 if (datatype == MPI_DATATYPE_NULL) {
3185 return MPI_ERR_TYPE;
3186 } else if (name == nullptr) {
3189 datatype->get_name(name, len);
3194 MPI_Datatype PMPI_Type_f2c(MPI_Fint datatype){
3195 return static_cast<MPI_Datatype>(simgrid::smpi::F2C::f2c(datatype));
3198 MPI_Fint PMPI_Type_c2f(MPI_Datatype datatype){
3199 return datatype->c2f();
3202 MPI_Group PMPI_Group_f2c(MPI_Fint group){
3203 return simgrid::smpi::Group::f2c(group);
3206 MPI_Fint PMPI_Group_c2f(MPI_Group group){
3207 return group->c2f();
3210 MPI_Request PMPI_Request_f2c(MPI_Fint request){
3211 return static_cast<MPI_Request>(simgrid::smpi::Request::f2c(request));
3214 MPI_Fint PMPI_Request_c2f(MPI_Request request) {
3215 return request->c2f();
3218 MPI_Win PMPI_Win_f2c(MPI_Fint win){
3219 return static_cast<MPI_Win>(simgrid::smpi::Win::f2c(win));
3222 MPI_Fint PMPI_Win_c2f(MPI_Win win){
3226 MPI_Op PMPI_Op_f2c(MPI_Fint op){
3227 return static_cast<MPI_Op>(simgrid::smpi::Op::f2c(op));
3230 MPI_Fint PMPI_Op_c2f(MPI_Op op){
3234 MPI_Comm PMPI_Comm_f2c(MPI_Fint comm){
3235 return static_cast<MPI_Comm>(simgrid::smpi::Comm::f2c(comm));
3238 MPI_Fint PMPI_Comm_c2f(MPI_Comm comm){
3242 MPI_Info PMPI_Info_f2c(MPI_Fint info){
3243 return static_cast<MPI_Info>(simgrid::smpi::Info::f2c(info));
3246 MPI_Fint PMPI_Info_c2f(MPI_Info info){
3250 int PMPI_Keyval_create(MPI_Copy_function* copy_fn, MPI_Delete_function* delete_fn, int* keyval, void* extra_state) {
3251 smpi_copy_fn _copy_fn={copy_fn,nullptr,nullptr};
3252 smpi_delete_fn _delete_fn={delete_fn,nullptr,nullptr};
3253 return simgrid::smpi::Keyval::keyval_create<simgrid::smpi::Comm>(_copy_fn, _delete_fn, keyval, extra_state);
3256 int PMPI_Keyval_free(int* keyval) {
3257 return simgrid::smpi::Keyval::keyval_free<simgrid::smpi::Comm>(keyval);
3260 int PMPI_Attr_delete(MPI_Comm comm, int keyval) {
3261 if(keyval == MPI_TAG_UB||keyval == MPI_HOST||keyval == MPI_IO ||keyval == MPI_WTIME_IS_GLOBAL||keyval == MPI_APPNUM
3262 ||keyval == MPI_UNIVERSE_SIZE||keyval == MPI_LASTUSEDCODE)
3264 else if (comm==MPI_COMM_NULL)
3265 return MPI_ERR_COMM;
3267 return comm->attr_delete<simgrid::smpi::Comm>(keyval);
3270 int PMPI_Attr_get(MPI_Comm comm, int keyval, void* attr_value, int* flag) {
3272 static int zero = 0;
3273 static int tag_ub = INT_MAX;
3274 static int last_used_code = MPI_ERR_LASTCODE;
3276 if (comm==MPI_COMM_NULL){
3278 return MPI_ERR_COMM;
3286 *static_cast<int**>(attr_value) = &zero;
3288 case MPI_UNIVERSE_SIZE:
3290 *static_cast<int**>(attr_value) = &smpi_universe_size;
3292 case MPI_LASTUSEDCODE:
3294 *static_cast<int**>(attr_value) = &last_used_code;
3298 *static_cast<int**>(attr_value) = &tag_ub;
3300 case MPI_WTIME_IS_GLOBAL:
3302 *static_cast<int**>(attr_value) = &one;
3305 return comm->attr_get<simgrid::smpi::Comm>(keyval, attr_value, flag);
3309 int PMPI_Attr_put(MPI_Comm comm, int keyval, void* attr_value) {
3310 if(keyval == MPI_TAG_UB||keyval == MPI_HOST||keyval == MPI_IO ||keyval == MPI_WTIME_IS_GLOBAL||keyval == MPI_APPNUM
3311 ||keyval == MPI_UNIVERSE_SIZE||keyval == MPI_LASTUSEDCODE)
3313 else if (comm==MPI_COMM_NULL)
3314 return MPI_ERR_COMM;
3316 return comm->attr_put<simgrid::smpi::Comm>(keyval, attr_value);
3319 int PMPI_Comm_get_attr (MPI_Comm comm, int comm_keyval, void *attribute_val, int *flag)
3321 return PMPI_Attr_get(comm, comm_keyval, attribute_val,flag);
3324 int PMPI_Comm_set_attr (MPI_Comm comm, int comm_keyval, void *attribute_val)
3326 return PMPI_Attr_put(comm, comm_keyval, attribute_val);
3329 int PMPI_Comm_delete_attr (MPI_Comm comm, int comm_keyval)
3331 return PMPI_Attr_delete(comm, comm_keyval);
3334 int PMPI_Comm_create_keyval(MPI_Comm_copy_attr_function* copy_fn, MPI_Comm_delete_attr_function* delete_fn, int* keyval,
3337 return PMPI_Keyval_create(copy_fn, delete_fn, keyval, extra_state);
3340 int PMPI_Comm_free_keyval(int* keyval) {
3341 return PMPI_Keyval_free(keyval);
3344 int PMPI_Type_get_attr (MPI_Datatype type, int type_keyval, void *attribute_val, int* flag)
3346 if (type==MPI_DATATYPE_NULL)
3347 return MPI_ERR_TYPE;
3349 return type->attr_get<simgrid::smpi::Datatype>(type_keyval, attribute_val, flag);
3352 int PMPI_Type_set_attr (MPI_Datatype type, int type_keyval, void *attribute_val)
3354 if (type==MPI_DATATYPE_NULL)
3355 return MPI_ERR_TYPE;
3357 return type->attr_put<simgrid::smpi::Datatype>(type_keyval, attribute_val);
3360 int PMPI_Type_delete_attr (MPI_Datatype type, int type_keyval)
3362 if (type==MPI_DATATYPE_NULL)
3363 return MPI_ERR_TYPE;
3365 return type->attr_delete<simgrid::smpi::Datatype>(type_keyval);
3368 int PMPI_Type_create_keyval(MPI_Type_copy_attr_function* copy_fn, MPI_Type_delete_attr_function* delete_fn, int* keyval,
3371 smpi_copy_fn _copy_fn={nullptr,copy_fn,nullptr};
3372 smpi_delete_fn _delete_fn={nullptr,delete_fn,nullptr};
3373 return simgrid::smpi::Keyval::keyval_create<simgrid::smpi::Datatype>(_copy_fn, _delete_fn, keyval, extra_state);
3376 int PMPI_Type_free_keyval(int* keyval) {
3377 return simgrid::smpi::Keyval::keyval_free<simgrid::smpi::Datatype>(keyval);
3380 int PMPI_Win_get_attr (MPI_Win win, int keyval, void *attribute_val, int* flag)
3382 static MPI_Aint size;
3383 static int disp_unit;
3384 if (win==MPI_WIN_NULL)
3385 return MPI_ERR_TYPE;
3389 *static_cast<void**>(attribute_val) = win->base();
3394 *static_cast<MPI_Aint**>(attribute_val) = &size;
3397 case MPI_WIN_DISP_UNIT :
3398 disp_unit=win->disp_unit();
3399 *static_cast<int**>(attribute_val) = &disp_unit;
3403 return win->attr_get<simgrid::smpi::Win>(keyval, attribute_val, flag);
3409 int PMPI_Win_set_attr (MPI_Win win, int type_keyval, void *attribute_val)
3411 if (win==MPI_WIN_NULL)
3412 return MPI_ERR_TYPE;
3414 return win->attr_put<simgrid::smpi::Win>(type_keyval, attribute_val);
3417 int PMPI_Win_delete_attr (MPI_Win win, int type_keyval)
3419 if (win==MPI_WIN_NULL)
3420 return MPI_ERR_TYPE;
3422 return win->attr_delete<simgrid::smpi::Win>(type_keyval);
3425 int PMPI_Win_create_keyval(MPI_Win_copy_attr_function* copy_fn, MPI_Win_delete_attr_function* delete_fn, int* keyval,
3428 smpi_copy_fn _copy_fn={nullptr, nullptr, copy_fn};
3429 smpi_delete_fn _delete_fn={nullptr, nullptr, delete_fn};
3430 return simgrid::smpi::Keyval::keyval_create<simgrid::smpi::Win>(_copy_fn, _delete_fn, keyval, extra_state);
3433 int PMPI_Win_free_keyval(int* keyval) {
3434 return simgrid::smpi::Keyval::keyval_free<simgrid::smpi::Win>(keyval);
3437 int PMPI_Info_create( MPI_Info *info){
3438 if (info == nullptr)
3440 *info = new simgrid::smpi::Info();
3444 int PMPI_Info_set( MPI_Info info, char *key, char *value){
3445 if (info == nullptr || key == nullptr || value == nullptr)
3447 info->set(key, value);
3451 int PMPI_Info_free( MPI_Info *info){
3452 if (info == nullptr || *info==nullptr)
3454 simgrid::smpi::Info::unref(*info);
3455 *info=MPI_INFO_NULL;
3459 int PMPI_Info_get(MPI_Info info,char *key,int valuelen, char *value, int *flag){
3461 if (info == nullptr || key == nullptr || valuelen <0)
3463 if (value == nullptr)
3464 return MPI_ERR_INFO_VALUE;
3465 return info->get(key, valuelen, value, flag);
3468 int PMPI_Info_dup(MPI_Info info, MPI_Info *newinfo){
3469 if (info == nullptr || newinfo==nullptr)
3471 *newinfo = new simgrid::smpi::Info(info);
3475 int PMPI_Info_delete(MPI_Info info, char *key){
3476 if (info == nullptr || key==nullptr)
3478 return info->remove(key);
3481 int PMPI_Info_get_nkeys( MPI_Info info, int *nkeys){
3482 if (info == nullptr || nkeys==nullptr)
3484 return info->get_nkeys(nkeys);
3487 int PMPI_Info_get_nthkey( MPI_Info info, int n, char *key){
3488 if (info == nullptr || key==nullptr || n<0 || n> MPI_MAX_INFO_KEY)
3490 return info->get_nthkey(n, key);
3493 int PMPI_Info_get_valuelen( MPI_Info info, char *key, int *valuelen, int *flag){
3495 if (info == nullptr || key == nullptr || valuelen==nullptr)
3497 return info->get_valuelen(key, valuelen, flag);
3500 int PMPI_Unpack(void* inbuf, int incount, int* position, void* outbuf, int outcount, MPI_Datatype type, MPI_Comm comm) {
3501 if(incount<0 || outcount < 0 || inbuf==nullptr || outbuf==nullptr)
3503 if (not type->is_valid())
3504 return MPI_ERR_TYPE;
3505 if(comm==MPI_COMM_NULL)
3506 return MPI_ERR_COMM;
3507 return type->unpack(inbuf, incount, position, outbuf,outcount, comm);
3510 int PMPI_Pack(void* inbuf, int incount, MPI_Datatype type, void* outbuf, int outcount, int* position, MPI_Comm comm) {
3511 if(incount<0 || outcount < 0|| inbuf==nullptr || outbuf==nullptr)
3513 if (not type->is_valid())
3514 return MPI_ERR_TYPE;
3515 if(comm==MPI_COMM_NULL)
3516 return MPI_ERR_COMM;
3517 return type->pack(inbuf, incount, outbuf,outcount,position, comm);
3520 int PMPI_Pack_size(int incount, MPI_Datatype datatype, MPI_Comm comm, int* size) {
3523 if (not datatype->is_valid())
3524 return MPI_ERR_TYPE;
3525 if(comm==MPI_COMM_NULL)
3526 return MPI_ERR_COMM;
3528 *size=incount*datatype->size();