GADGET-4
sizelimited_sendrecv.cc
Go to the documentation of this file.
1 /*******************************************************************************
2  * \copyright This file is part of the GADGET4 N-body/SPH code developed
3  * \copyright by Volker Springel. Copyright (C) 2014-2020 by Volker Springel
4  * \copyright (vspringel@mpa-garching.mpg.de) and all contributing authors.
5  *******************************************************************************/
6 
12 #include "gadgetconfig.h"
13 
14 #include <math.h>
15 #include <mpi.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 
20 #include "../data/allvars.h"
21 #include "../data/dtypes.h"
22 #include "../mpi_utils/mpi_utils.h"
23 
24 int myMPI_Sendrecv(void *sendb, size_t sendcount, MPI_Datatype sendtype, int dest, int sendtag, void *recvb, size_t recvcount,
25  MPI_Datatype recvtype, int source, int recvtag, MPI_Comm comm, MPI_Status *status)
26 {
27  int iter = 0, size_sendtype, size_recvtype, send_now, recv_now;
28  char *sendbuf = (char *)sendb;
29  char *recvbuf = (char *)recvb;
30 
31  if(dest != source)
32  Terminate("dest != source");
33 
34  MPI_Type_size(sendtype, &size_sendtype);
35  MPI_Type_size(recvtype, &size_recvtype);
36 
37  int thistask;
38  MPI_Comm_rank(comm, &thistask);
39 
40  if(dest == thistask)
41  {
42  memcpy(recvbuf, sendbuf, recvcount * size_recvtype);
43  return 0;
44  }
45 
46  size_t count_limit = MPI_MESSAGE_SIZELIMIT_IN_BYTES / size_sendtype;
47 
48  while(sendcount > 0 || recvcount > 0)
49  {
50  if(sendcount > count_limit)
51  {
52  send_now = count_limit;
53 
54  iter++;
55  }
56  else
57  send_now = sendcount;
58 
59  if(recvcount > count_limit)
60  recv_now = count_limit;
61  else
62  recv_now = recvcount;
63 
64  MPI_Sendrecv(sendbuf, send_now, sendtype, dest, sendtag, recvbuf, recv_now, recvtype, source, recvtag, comm, status);
65 
66  sendcount -= send_now;
67  recvcount -= recv_now;
68 
69  sendbuf += send_now * size_sendtype;
70  recvbuf += recv_now * size_recvtype;
71  }
72 
73  return 0;
74 }
#define MPI_MESSAGE_SIZELIMIT_IN_BYTES
Definition: constants.h:53
#define Terminate(...)
Definition: macros.h:19
int myMPI_Sendrecv(void *sendb, size_t sendcount, MPI_Datatype sendtype, int dest, int sendtag, void *recvb, size_t recvcount, MPI_Datatype recvtype, int source, int recvtag, MPI_Comm comm, MPI_Status *status)