- if (comm == MPI_COMM_NULL) {
- retval = MPI_ERR_COMM;
- } else if ((sendbuf != MPI_IN_PLACE && sendtypes == nullptr) || recvtypes == nullptr) {
- retval = MPI_ERR_TYPE;
- } else if ((sendbuf != MPI_IN_PLACE && (sendcounts == nullptr || senddisps == nullptr)) || recvcounts == nullptr ||
- recvdisps == nullptr) {
- retval = MPI_ERR_ARG;
- } else if (request == nullptr){
- retval = MPI_ERR_ARG;
- } else {
- int rank = simgrid::s4u::this_actor::get_pid();
- int size = comm->size();
- int send_size = 0;
- int recv_size = 0;
- std::vector<int>* trace_sendcounts = new std::vector<int>;
- std::vector<int>* trace_recvcounts = new std::vector<int>;
-
- void* sendtmpbuf = static_cast<char*>(sendbuf);
- int* sendtmpcounts = sendcounts;
- int* sendtmpdisps = senddisps;
- MPI_Datatype* sendtmptypes = sendtypes;
- unsigned long maxsize = 0;
- for (int i = 0; i < size; i++) { // copy data to avoid bad free
- if(recvtypes[i]==MPI_DATATYPE_NULL){
- delete trace_recvcounts;
- delete trace_sendcounts;
- return MPI_ERR_TYPE;
- }
- recv_size += recvcounts[i] * recvtypes[i]->size();
- trace_recvcounts->push_back(recvcounts[i] * recvtypes[i]->size());
- if ((recvdisps[i] + (recvcounts[i] * recvtypes[i]->size())) > maxsize)
- maxsize = recvdisps[i] + (recvcounts[i] * recvtypes[i]->size());
- }
-
- if (sendbuf == MPI_IN_PLACE) {
- sendtmpbuf = static_cast<void*>(xbt_malloc(maxsize));
- memcpy(sendtmpbuf, recvbuf, maxsize);
- sendtmpcounts = static_cast<int*>(xbt_malloc(size * sizeof(int)));
- memcpy(sendtmpcounts, recvcounts, size * sizeof(int));
- sendtmpdisps = static_cast<int*>(xbt_malloc(size * sizeof(int)));
- memcpy(sendtmpdisps, recvdisps, size * sizeof(int));
- sendtmptypes = static_cast<MPI_Datatype*>(xbt_malloc(size * sizeof(MPI_Datatype)));
- memcpy(sendtmptypes, recvtypes, size * sizeof(MPI_Datatype));
- }
+ int send_size = 0;
+ int recv_size = 0;
+ auto trace_sendcounts = std::make_shared<std::vector<int>>();
+ auto trace_recvcounts = std::make_shared<std::vector<int>>();
+
+ const int* real_sendcounts = sendcounts;
+ const int* real_senddispls = senddispls;
+ const MPI_Datatype* real_sendtypes = sendtypes;
+ unsigned long maxsize = 0;
+ for (int i = 0; i < size; i++) { // copy data to avoid bad free
+ if (recvtypes[i] == MPI_DATATYPE_NULL)
+ return MPI_ERR_TYPE;
+ recv_size += recvcounts[i] * recvtypes[i]->size();
+ trace_recvcounts->push_back(recvcounts[i] * recvtypes[i]->size());
+ if ((recvdispls[i] + (recvcounts[i] * recvtypes[i]->size())) > maxsize)
+ maxsize = recvdispls[i] + (recvcounts[i] * recvtypes[i]->size());
+ }