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
13#include "gadgetconfig.h"
14
15#include <math.h>
16#include <mpi.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20
21#include "../data/allvars.h"
22#include "../data/dtypes.h"
23#include "../mpi_utils/mpi_utils.h"
24
25int myMPI_Sendrecv(void *sendb, size_t sendcount, MPI_Datatype sendtype, int dest, int sendtag, void *recvb, size_t recvcount,
26 MPI_Datatype recvtype, int source, int recvtag, MPI_Comm comm, MPI_Status *status)
27{
28 int iter = 0, size_sendtype, size_recvtype, send_now, recv_now;
29 char *sendbuf = (char *)sendb;
30 char *recvbuf = (char *)recvb;
31
32 if(dest != source)
33 Terminate("dest != source");
34
35 MPI_Type_size(sendtype, &size_sendtype);
36 MPI_Type_size(recvtype, &size_recvtype);
37
38 int thistask;
39 MPI_Comm_rank(comm, &thistask);
40
41 if(dest == thistask)
42 {
43 memcpy(recvbuf, sendbuf, recvcount * size_recvtype);
44 return 0;
45 }
46
47 size_t count_limit = MPI_MESSAGE_SIZELIMIT_IN_BYTES / size_sendtype;
48
49 while(sendcount > 0 || recvcount > 0)
50 {
51 if(sendcount > count_limit)
52 {
53 send_now = count_limit;
54
55 iter++;
56 }
57 else
58 send_now = sendcount;
59
60 if(recvcount > count_limit)
61 recv_now = count_limit;
62 else
63 recv_now = recvcount;
64
65 MPI_Sendrecv(sendbuf, send_now, sendtype, dest, sendtag, recvbuf, recv_now, recvtype, source, recvtag, comm, status);
66
67 sendcount -= send_now;
68 recvcount -= recv_now;
69
70 sendbuf += send_now * size_sendtype;
71 recvbuf += recv_now * size_recvtype;
72 }
73
74 return 0;
75}
#define MPI_MESSAGE_SIZELIMIT_IN_BYTES
Definition: constants.h:53
#define Terminate(...)
Definition: macros.h:15
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)