Loading...
Searching...
No Matches
Functions
G4MPIutils.cc File Reference
#include "G4MPIutils.hh"
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <functional>
#include <assert.h>
#include <utility>
#include "globals.hh"

Go to the source code of this file.

Functions

int _testMe (int argc, char **argv)
 

Function Documentation

◆ _testMe()

int _testMe ( int  argc,
char **  argv 
)

Definition at line 93 of file G4MPIutils.cc.

93 {
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;
100
101 assert( myRank < worldSize);
102 //MPI function stubs
103 auto MPI_Receive = [](const rank_t& s, const rank_t& r) {
104 std::cout<<"MPI_Receive from: "<<s<<" to "<<r<<std::endl;
105 return 0;
106 };
107 auto MPI_Send = [](const rank_t& s, const rank_t& r) {
108 std::cout<<"MPI_Send from: "<<s<<" to "<<r<<std::endl;;
109 return 0;
110 };
111 auto MPI_Barrier = [] {
112 std::cout<<"MPI_Barrier"<<std::endl;
113 return 0;
114 };
115
116 //Build the initial network of ranks
117 std::vector<rank_t> ranks(worldSize);
118 for ( unsigned int i = 0 ; i<worldSize ; ++i ) {
119 if ( i != 2 )
120 { ranks.push_back(i);
121 } else {
122 ranks.push_back(i);
123 ranks.push_back(i);
124 }
125 }
126 //Remove duplicates
127 ranks.erase( std::unique(ranks.begin(),ranks.end()),ranks.end());
128
129 //Optimize network trafic
130 if ( ranks.size() == 1 ) {
131 std::cout<<"only one rank, nothing to do"<<std::endl;
132 return 0;
133 }
134 auto comms = G4mpi::buildCommunicationMap( ranks );
135 assert( ranks.size() == 1 && ranks[0] == 0 );
136
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<<", ";
142 }
143 std::cout<<std::endl;
144 }
145
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); }
152 }
153 //Important: Wait for this cycle to end before going to the next, even if
154 //this rank did not do anything
155 //This is needed to be sure that the redcutions are done correctly
156 MPI_Barrier();
157 }
158 return 0;
159}
commMap_t buildCommunicationMap(std::vector< rank_t > &input)
unsigned int rank_t
Definition G4MPIutils.hh:45

Applications | User Support | Publications | Collaboration