31 #include "NamespaceHeader.H" 36 template <
class P>
void mpi_scatter_part(map<
unsigned,
List<P> >& a_p,
37 vector<map<
unsigned,
List<P> > >& a_pp)
41 const size_t psize = P().size();
42 vector<unsigned int> snd_sizes(
numProc(),0), rcv_sizes(
numProc(),0);
44 typename map<unsigned int,List<P> >::iterator vi;
48 for (vi=a_pp[p].begin(); vi!=a_pp[p].end(); ++vi)
49 snd_sizes[p] += vi->second.length();
51 snd_sizes[p] *= psize;
53 snd_sizes[p] += a_pp[p].size() * (
sizeof(
unsigned int)+
sizeof(
size_t));
56 int err = MPI_Alltoall(&snd_sizes[0],1,MPI_UNSIGNED,
57 &rcv_sizes[0],1,MPI_UNSIGNED,
59 if (err != MPI_SUCCESS)
66 vector<MPI_Request> rcv_req(
numProc()-1);
67 vector<char*> rcv_buf(
numProc(),NULL);
74 rcv_buf[p] =
new char[rcv_sizes[p]];
75 MPI_Irecv(rcv_buf[p], rcv_sizes[p], MPI_CHAR, p, 1,
76 MPI_COMM_WORLD, &rcv_req[rcv_req_cnt] );
81 vector<MPI_Request> snd_req(
numProc()-1);
82 vector<char*> snd_buf(
numProc(),NULL);
90 snd_buf[p] =
new char[snd_sizes[p]];
92 char* data = snd_buf[p];
93 for (vi=a_pp[p].begin(); vi!=a_pp[p].end(); ++vi)
95 *((
unsigned int*)data)=vi->first;
96 data +=
sizeof(
unsigned int);
97 *((
size_t*)data)=vi->second.length();
98 data +=
sizeof(size_t);
101 li().linearOut((
void*)data);
106 MPI_Isend(snd_buf[p], snd_sizes[p], MPI_CHAR, p, 1,
107 MPI_COMM_WORLD, &snd_req[snd_req_cnt] );
115 int err = MPI_Waitall(snd_req_cnt, &snd_req[0], &snd_stat[0]);
116 if (err != MPI_SUCCESS)
118 MayDay::Error(
"mpi_scatter_part: send communication failed");
124 int err = MPI_Waitall(rcv_req_cnt, &rcv_req[0], &rcv_stat[0]);
125 if (err != MPI_SUCCESS)
127 MayDay::Error(
"mpi_scatter_part: receive communication failed");
132 for (
int p=0; p<
numProc(); ++p)
135 if (rcv_sizes[p] > 0)
137 char* data = rcv_buf[p];
139 while (in < rcv_sizes[p])
141 unsigned int idx=*((
unsigned int*)data);
142 data +=
sizeof(
unsigned int);
143 size_t nps = *((
size_t*)data);
144 data +=
sizeof(size_t);
148 q.linearIn((
void*)data);
152 in +=
sizeof(
unsigned int)+
sizeof(
size_t)+psize*nps;
158 for (
int p=0; p<
numProc(); ++p)
161 if (snd_sizes[p] > 0)
delete snd_buf[p];
162 if (rcv_sizes[p] > 0)
delete rcv_buf[p];
166 #include "NamespaceFooter.H" #define CH_assert(cond)
Definition: CHArray.H:37
unsigned int numProc()
number of parallel processes
bool ok() const
Return true if the iterator is not past the end of the list.
Definition: List.H:465
Iterator over a List.
Definition: List.H:20
static void Error(const char *const a_msg=m_nullString, int m_exitCode=CH_DEFAULT_ERROR_CODE)
Print out message to cerr and exit with the specified exit code.
int procID()
local process ID