38 std::vector<G4mpi::rank_t>& input ) {
39 using namespace G4mpi;
41 std::sort(input.begin(),input.end());
42 if ( input.size() < 1 || input[0] != 0 ) {
44 "Empty input or cannot find rank 0 in input.");
47 std::vector<rank_t>
copy(input.size());
53 "There are duplicates in list of input ranks.");
61 std::vector<couple_t> couples;
65 std::vector<rank_t> receiving;
69 while ( input.size() > 1 ) {
71 std::sort( input.begin(),input.end() );
73 const auto& send_ = input.back();
75 const auto& rec_ = input.back();
79 receiving.push_back( rec_ );
81 couples.push_back( std::make_pair(send_,rec_) );
84 mymap[cycle++]=couples;
86 input.insert( input.end() , receiving.begin() , receiving.end() );
88 }
while ( input.size()!=1 );
94 using namespace G4mpi;
95 unsigned int worldSize = 10;
96 if ( argc > 1 ) worldSize = atoi(argv[1]);
97 unsigned int myRank = worldSize-1;
98 if ( argc > 2 ) myRank = atoi(argv[2]);
99 std::cout<<
"World size: "<<worldSize<<std::endl;
101 assert( myRank < worldSize);
104 std::cout<<
"MPI_Receive from: "<<
s<<
" to "<<
r<<std::endl;
108 std::cout<<
"MPI_Send from: "<<
s<<
" to "<<
r<<std::endl;;
111 auto MPI_Barrier = [] {
112 std::cout<<
"MPI_Barrier"<<std::endl;
117 std::vector<rank_t> ranks(worldSize);
118 for (
unsigned int i = 0 ; i<worldSize ; ++i ) {
120 { ranks.push_back(i);
127 ranks.erase( std::unique(ranks.begin(),ranks.end()),ranks.end());
130 if ( ranks.size() == 1 ) {
131 std::cout<<
"only one rank, nothing to do"<<std::endl;
135 assert( ranks.size() == 1 && ranks[0] == 0 );
137 std::cout<<
"Communiction Map (size: "<<comms.size()<<
"):"<<std::endl;
138 for (
const auto&
x : comms ) {
139 std::cout<<
"Cycle "<<
x.first<<
": ";
140 for (
const auto&
y :
x.second ) {
141 std::cout<<
y.first<<
"->"<<
y.second<<
", ";
143 std::cout<<std::endl;
146 std::cout<<
"Simulate communication pattern for rank: "<<myRank<<std::endl;
147 for (
const auto&
x: comms ) {
148 std::cout<<
"Cycle "<<
x.first<<std::endl;
149 for (
const auto&
y :
x.second ) {
150 if ( myRank ==
y.first ) {
MPI_Send(
y.first,
y.second); }
151 else if ( myRank ==
y.second ) { MPI_Receive(
y.first,
y.second); }
164 unsigned int commSize ,
165 unsigned int myrank) {
167 std::vector<G4mpi::rank_t> ranks(commSize);
168 std::iota(ranks.begin(),ranks.end(),0);
171 for (
const auto& cycle : comms ) {
174 for (
const auto& pattern : cycle.second ) {
176 if ( myrank == pattern.first ) {
178 senderF(pattern.second);
180 else if ( myrank == pattern.second ) {
182 receiverF(pattern.first);