15 #include "tml/message/packed_multi_message.h"
25 MPI_Bcast(&data,1,GetType(data),rank(),m_comm);
37 MPI_Bcast(data,ndata,GetType(*data),rank(),m_comm);
48 int data_size=data.size();
51 typename T::value_type *buffer=
new typename T::value_type[data_size];
55 for(
typename T::const_iterator iter=data.begin();
58 void* buf=
reinterpret_cast<void*
>(&(buffer[count]));
59 new(buf)(
typename T::value_type)(*iter);
88 int nb_data=data.size();
92 for(
typename T::const_iterator iter=data.begin();
112 template <
typename T>
115 MPI_Bcast(&data,1,GetType(data),root,m_comm);
125 template <
typename T>
128 MPI_Bcast(data,ndata,GetType(*data),root,m_comm);
138 template <
typename T>
146 typename T::value_type *buffer=
new typename T::value_type[data_size];
151 for(
int i=0;i<data_size;i++){
152 data.insert(data.end(),buffer[i]);
165 template <
typename T>
183 for(
int i=0;i<nb_data;i++){
184 typename T::value_type tv;
186 data.insert(data.end(),tv);
200 template <
typename T>
204 int total_size=data.size();
205 T* buffer=
new T[total_size];
206 int comm_size=size();
207 int *size_buffer=
new int[comm_size];
208 int *offs_buffer=
new int[comm_size];
211 for(
int i=0;i<comm_size;i++){
212 typename multimap<int,T>::const_iterator begin=data.find(i);
213 typename multimap<int,T>::const_iterator end=data.upper_bound(i);
214 if(begin!=data.end()){
215 for(
typename multimap<int,T>::const_iterator iter=begin;
218 buffer[count]=iter->second;
222 size_buffer[i]=count-count_p;
227 MPI_Scatter(size_buffer,1,MPI_INT,&dummy,1,MPI_INT,rank(),m_comm);
230 for(
int i=1;i<comm_size;i++){
231 offs_buffer[i]=offs_buffer[i-1]+size_buffer[i-1];
235 MPI_Scatterv(buffer,size_buffer,offs_buffer,GetType(*buffer),&dummy2,0,GetType(*buffer),rank(),m_comm);
237 delete [] size_buffer;
238 delete [] offs_buffer;
248 template <
typename T>
253 MPI_Scatter(NULL,0,MPI_INT,&size,1,MPI_INT,root,m_comm);
255 typename T::value_type *buffer=
new typename T::value_type[size];
257 MPI_Scatterv(NULL,NULL,NULL,MPI_INT,buffer,size,GetType(*buffer),root,m_comm);
259 for(
int i=0;i<size;i++){
260 data.insert(data.end(),buffer[i]);
272 template <
typename T>
276 int comm_size=size();
277 int *size_buffer=
new int[comm_size];
278 int *offs_buffer=
new int[comm_size];
280 MPI_Gather(&dummy,1,MPI_INT,size_buffer,1,MPI_INT,rank(),m_comm);
282 for(
int i=0;i<comm_size;i++){
283 totalsize+=size_buffer[i];
286 T *buffer=
new T[totalsize];
289 for(
int i=1;i<comm_size;i++){
290 offs_buffer[i]=offs_buffer[i-1]+size_buffer[i-1];
294 MPI_Gatherv(&dummy2,0,GetType(dummy),buffer,size_buffer,offs_buffer,GetType(*buffer),rank(),m_comm);
296 for(
int i=0;i<comm_size;i++){
297 for(
int j=offs_buffer[i];j<offs_buffer[i]+size_buffer[i];j++){
298 data.insert(pair<int,T>(i,buffer[j]));
302 delete [] size_buffer;
303 delete [] offs_buffer;
314 template <
typename T>
318 int comm_size=size();
319 int *size_buffer=
new int[comm_size];
320 int *offs_buffer=
new int[comm_size];
322 MPI_Gather(&dummy,1,MPI_INT,size_buffer,1,MPI_INT,rank(),m_comm);
324 for(
int i=0;i<comm_size;i++){
325 console.
Debug() <<
"buffer size " << i <<
" - " << size_buffer[i] <<
"\n";
326 totalsize+=size_buffer[i];
329 T *buffer=
new T[totalsize];
332 for(
int i=1;i<comm_size;i++){
333 offs_buffer[i]=offs_buffer[i-1]+size_buffer[i-1];
337 MPI_Gatherv(&dummy2,0,GetType(dummy),buffer,size_buffer,offs_buffer,GetType(*buffer),rank(),m_comm);
339 for(
int i=0;i<comm_size;i++){
340 for(
int j=offs_buffer[i];j<offs_buffer[i]+size_buffer[i];j++){
341 data.insert(pair<int,T>(i,buffer[j]));
345 delete [] size_buffer;
346 delete [] offs_buffer;
356 template <
typename T>
360 int size=data.size();
361 MPI_Gather(&size,1,MPI_INT,NULL,0,MPI_INT,root,m_comm);
363 typename T::value_type *buffer=
new typename T::value_type[size];
366 for(
typename T::const_iterator iter=data.begin();
373 MPI_Gatherv(buffer,size,GetType(*buffer),NULL,NULL,NULL,MPI_INT,root,m_comm);
385 template <
typename T>
389 int size=data.size();
390 console.
Debug() <<
"send size :" << size <<
"\n";
391 MPI_Gather(&size,1,MPI_INT,NULL,0,MPI_INT,root,m_comm);
393 typename T::value_type *buffer=
new typename T::value_type[size];
396 for(
typename T::const_iterator iter=data.begin();
402 console.
Debug() <<
"send count :" << count <<
"\n";
404 MPI_Gatherv(buffer,size,GetType(*buffer),NULL,NULL,NULL,MPI_INT,root,m_comm);
417 template <
typename T>
420 int comm_size=size();
424 for(
int i=1;i<comm_size;i++){
425 typename multimap<int,T>::const_iterator begin=data.find(i);
426 typename multimap<int,T>::const_iterator end=data.upper_bound(i);
427 (*msg)[i].pack(
int(data.count(i)));
428 if(begin!=data.end()){
429 for(
typename multimap<int,T>::const_iterator iter=begin;
432 (*msg)[i].pack(iter->second);
438 MPI_Scatter(msg->sizes(),1,MPI_INT,&dummy,1,MPI_INT,rank(),m_comm);
441 MPI_Datatype data_type=GetType(*(msg->buffer()));
442 MPI_Scatterv(msg->buffer(),msg->sizes(),msg->offsets(),data_type,
443 &dummy2,0,data_type,rank(),m_comm);
454 template <
typename T>
459 MPI_Scatter(NULL,0,MPI_INT,&msg_size,1,MPI_INT,root,m_comm);
464 MPI_Datatype data_type=GetType(*(msg->buffer()));
465 MPI_Scatterv(NULL,NULL,NULL,MPI_INT,msg->buffer(),msg_size,data_type,root,m_comm);
470 typename T::value_type item;
471 for(
int i=0;i<nitems;i++){
473 data.insert(data.end(),item);
479 template <
typename T>
480 void TML_Comm::gather_packed(multimap<int,T> &data)
482 console.
Debug() <<
"TML_Comm::gather_packed: enter\n";
484 int comm_size=size();
485 int *size_buffer=
new int[comm_size];
486 int *offs_buffer=
new int[comm_size];
489 <<
"TML_Comm::gather_packed: gathering sizes\n";
490 MPI_Gather(&dummy,1,MPI_INT,size_buffer,1,MPI_INT,rank(),m_comm);
492 for(
int i=0;i<comm_size;i++){
496 totalsize+=size_buffer[i];
499 <<
"TML_Comm::gather_packed: total msg size = " << totalsize <<
"\n";
506 for(
int i=1;i<comm_size;i++){
507 offs_buffer[i]=offs_buffer[i-1]+size_buffer[i-1];
512 <<
"TML_Comm::gather_packed: gathering data\n";
514 &dummy2,0, GetType(dummy), msg->buffer(), size_buffer, offs_buffer,
515 GetType(*(msg->buffer())),rank(),m_comm
519 <<
"TML_Comm::gather_packed: unpacking into multi-map\n";
520 for(
int i=0;i<comm_size;i++){
521 if (size_buffer[i] > 0)
523 const int numElems = msg->
pop_int();
524 for(
int j=0; j < numElems; j++){
530 data.insert(pair<int,T>(i, t));
534 console.
Debug() <<
"TML_Comm::gather_packed: done unpacking into multi-map\n";
536 delete [] size_buffer;
537 delete [] offs_buffer;
539 console.
Debug() <<
"TML_Comm::gather_packed: exit\n";
542 template <
typename T>
543 void TML_Comm::send_gather_packed(
const T &data,
int root)
545 console.
Debug() <<
"TML_Comm::send_gather_packed: enter\n";
550 std::max(static_cast<size_t>(64), data.size()*
sizeof(
typename T::value_type))
553 const int numElems = data.size();
555 for(
typename T::const_iterator iter=data.begin();
562 int size = msg->size();
563 console.
Debug() <<
"TML_Comm::send_gather_packed: sending data size...\n";
564 MPI_Gather(&size,1,MPI_INT,NULL,0,MPI_INT,root,m_comm);
567 console.
Debug() <<
"TML_Comm::send_gather_packed: sending data...\n";
569 msg->buffer(), msg->size(),
570 GetType(*(msg->buffer())),
571 NULL, NULL, NULL, MPI_INT, root, m_comm
575 console.
Debug() <<
"TML_Comm::send_gather_packed: exit\n";
578 template <
typename T>
579 T TML_Comm::sum_all(
const T& data)
583 MPI_Datatype data_type=GetType(data);
584 MPI_Allreduce((
void*)(&data),(
void*)(&res),1,data_type,MPI_SUM,m_comm);