56 const char BRIEF_DESC[] =
"Example of gather scatter with tuple lists \n";
62 int main(
int argc,
char** argv )
65 MPI_Init( &argc, &argv );
75 LONG_DESC <<
"This program does a gather scatter with a list of tuples. \n"
76 " It tries to see how much communication costs in terms of time and memory. \n"
77 <<
"It starts with creating a list of tuples to be sent from each processor, \n to a "
78 "list of other processors.\n"
79 <<
"The number of tuples and how many tasks to communicate to are controlled by "
81 <<
"After communication, we verify locally if we received what we expected. \n";
86 opts.
addOpt<
int >(
"num_comms,n",
"each task will send to about num_comms other tasks some tuples (default 2)",
90 opts.
addOpt<
int >(
"num_tuples,t",
"each task will send to some task about num_tuples tuples (default 4)",
93 int reportrank =
size + 1;
94 opts.
addOpt<
int >(
"reporting_rank,r",
95 "this rank will report the tuples sent and the tuples received; it could "
96 "be higher than num_procs, then no reporting",
101 if( rank == reportrank || ( reportrank >=
size && 0 == rank ) )
103 cout <<
" There are " <<
size <<
" tasks in example.\n";
104 cout <<
" We will send groups of " << num_tuples <<
" from each task towards " << num_comms
105 <<
" other tasks.\n";
112 long total_n_tuples = num_comms * num_tuples;
115 if( rank <
size / 2 )
120 if( rank <
size / 3 )
131 tl.
initialize( 1, 1, 0, 1, num_tuples * num_comms );
134 unsigned int n = tl.
get_n();
135 for(
int i = 0; i < num_comms; i++ )
137 int sendTo = rank + i *
size / 2 + 1;
138 sendTo = sendTo %
size;
139 long intToSend = 1000 * rank + 100000 * sendTo;
140 for(
int j = 0; j < num_tuples; j++ )
143 tl.
vi_wr[n] = sendTo;
144 tl.
vl_wr[n] = intToSend + j;
145 tl.
vr_wr[n] = 10000. * rank + j;
150 if( rank == reportrank )
152 cout <<
"rank " << rank <<
"\n";
153 tl.
print(
" before sending" );
156 clock_t tt = clock();
161 if( rank == reportrank || ( reportrank >=
size && 0 == rank ) )
163 secs = ( clock() - tt ) / (
double)CLOCKS_PER_SEC;
165 if( rank == reportrank )
167 cout <<
"rank " << rank <<
"\n";
168 tl.
print(
" after transfer" );
172 unsigned int received = tl.
get_n();
173 for(
int i = 0; i < (int)received; i++ )
175 int from = tl.
vi_rd[i];
176 long valrec = tl.
vl_rd[i];
177 int remainder = valrec - 100000 * rank - 1000 * from;
178 if( remainder < 0 || remainder >= num_tuples * 4 )
179 cout <<
" error: tuple " << i <<
" received at proc rank " << rank <<
" from proc " << from <<
" has value "
180 << valrec <<
" remainder " << remainder <<
"\n";
183 if( rank == reportrank || ( reportrank >=
size && 0 == rank ) )
185 cout <<
"communication of about " << total_n_tuples <<
" tuples/per proc took " << secs <<
" seconds"
195 std::cout <<
" Build with MPI for this example to work\n";