Revision | 4e91aae83ea0476ef7112e00d6fe2b9a5031574c (tree) |
---|---|
Time | 2013-07-12 15:59:42 |
Author | Mikiya Fujii <mikiya.fujii@gmai...> |
Commiter | Mikiya Fujii |
Too big messages are split to be send by MPI. #31588
git-svn-id: https://svn.sourceforge.jp/svnroot/molds/branches/mpi-cis@1388 1136aad2-a195-0410-b898-f5ea1d11b9d8
@@ -37,6 +37,7 @@ MpiProcess::MpiProcess(){ | ||
37 | 37 | MpiProcess::MpiProcess(int argc, char *argv[]){ |
38 | 38 | this->environment = new boost::mpi::environment(argc, argv); |
39 | 39 | this->communicator = new boost::mpi::communicator(); |
40 | + this->messageLimit = INT_MAX; | |
40 | 41 | } |
41 | 42 | |
42 | 43 | MpiProcess::~MpiProcess(){ |
@@ -18,9 +18,9 @@ | ||
18 | 18 | //************************************************************************// |
19 | 19 | #ifndef INCLUDED_MPIPROCESS |
20 | 20 | #define INCLUDED_MPIPROCESS |
21 | +#include<limits.h> | |
21 | 22 | #include<boost/mpi.hpp> |
22 | 23 | namespace MolDS_mpi{ |
23 | - | |
24 | 24 | // MpiProcess is singleton |
25 | 25 | class MpiProcess: private MolDS_base::Uncopyable{ |
26 | 26 | public: |
@@ -29,9 +29,28 @@ public: | ||
29 | 29 | static MpiProcess* GetInstance(); |
30 | 30 | int GetRank() const{return this->communicator->rank();} |
31 | 31 | int GetSize() const{return this->communicator->size();} |
32 | - template<typename T> void Send(int dest, int tag, const T* values, int n) const{this->communicator->send(dest, tag, values, n);} | |
33 | - template<typename T> void Recv(int source, int tag, T* values, int n) const{this->communicator->recv(source, tag, values, n);} | |
34 | - template<typename T> void Broadcast(T* values, int n, int root) const{broadcast(*this->communicator, values, n, root);} | |
32 | + template<typename T> void Send(int dest, int tag, const T* values, intptr_t num) const{ | |
33 | + std::vector<Chunk> chunks; | |
34 | + this->SplitMessage2Chunks(chunks, tag, values, num); | |
35 | + for(intptr_t i=0; i<chunks.size(); i++){ | |
36 | + this->communicator->send(dest, chunks[i].tag, &values[chunks[i].first], chunks[i].num); | |
37 | + } | |
38 | + } | |
39 | + template<typename T> void Recv(int source, int tag, T* values, intptr_t num) const{ | |
40 | + std::vector<Chunk> chunks; | |
41 | + this->SplitMessage2Chunks(chunks, tag, values, num); | |
42 | + for(intptr_t i=0; i<chunks.size(); i++){ | |
43 | + this->communicator->recv(source, chunks[i].tag, &values[chunks[i].first], chunks[i].num); | |
44 | + } | |
45 | + } | |
46 | + template<typename T> void Broadcast(T* values, intptr_t num, int root) const{ | |
47 | + std::vector<Chunk> chunks; | |
48 | + intptr_t tag=0; | |
49 | + this->SplitMessage2Chunks(chunks, tag, values, num); | |
50 | + for(intptr_t i=0; i<chunks.size(); i++){ | |
51 | + broadcast(*this->communicator, &values[chunks[i].first], chunks[i].num, root); | |
52 | + } | |
53 | + } | |
35 | 54 | private: |
36 | 55 | static MpiProcess* mpiProcess; |
37 | 56 | MpiProcess(); |
@@ -39,6 +58,31 @@ private: | ||
39 | 58 | ~MpiProcess(); |
40 | 59 | boost::mpi::environment* environment; |
41 | 60 | boost::mpi::communicator* communicator; |
61 | + struct Chunk{int tag; intptr_t first; int num;}; | |
62 | + double messageLimit; | |
63 | + template<typename T> void SplitMessage2Chunks(std::vector<Chunk>& chunks, const int origianlTag, T* values, intptr_t num) const{ | |
64 | + if(this->messageLimit < static_cast<double>(sizeof(T))*static_cast<double>(num) ){ | |
65 | + int elementsLimit = static_cast<intptr_t>(messageLimit/sizeof(T)); | |
66 | + intptr_t numChunks = num/elementsLimit; | |
67 | + int remaining = num%elementsLimit; | |
68 | + if(0 < remaining){ | |
69 | + numChunks++; | |
70 | + } | |
71 | + int tagBase = origianlTag*numChunks; | |
72 | + for(intptr_t i=0; i<numChunks; i++){ | |
73 | + int tag = tagBase+i; | |
74 | + Chunk chunk = {tag, i*elementsLimit, elementsLimit}; | |
75 | + chunks.push_back(chunk); | |
76 | + } | |
77 | + if(0 < remaining){ | |
78 | + chunks[numChunks-1].num = remaining; | |
79 | + } | |
80 | + } | |
81 | + else{ | |
82 | + Chunk chunk = {origianlTag, 0, num}; | |
83 | + chunks.push_back(chunk); | |
84 | + } | |
85 | + } | |
42 | 86 | }; |
43 | 87 | |
44 | 88 | } |