Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
remove (void) in C++
[simgrid.git] / src / smpi / colls / smpi_mvapich2_selector.cpp
1 /* selector for collective algorithms based on mvapich decision logic */
2
3 /* Copyright (c) 2009-2010, 2013-2017. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #include "colls_private.h"
10
11 #include "smpi_mvapich2_selector_stampede.h"
12
13 namespace simgrid{
14 namespace smpi{
15
16
17 int Coll_alltoall_mvapich2::alltoall( void *sendbuf, int sendcount, 
18     MPI_Datatype sendtype,
19     void* recvbuf, int recvcount,
20     MPI_Datatype recvtype,
21     MPI_Comm comm)
22 {
23
24   if(mv2_alltoall_table_ppn_conf==NULL)
25     init_mv2_alltoall_tables_stampede();
26
27   int sendtype_size, recvtype_size, comm_size;
28   char * tmp_buf = NULL;
29   int mpi_errno=MPI_SUCCESS;
30   int range = 0;
31   int range_threshold = 0;
32   int conf_index = 0;
33   comm_size =  comm->size();
34
35   sendtype_size=sendtype->size();
36   recvtype_size=recvtype->size();
37   long nbytes = sendtype_size * sendcount;
38
39   /* check if safe to use partial subscription mode */
40
41   /* Search for the corresponding system size inside the tuning table */
42   while ((range < (mv2_size_alltoall_tuning_table[conf_index] - 1)) &&
43       (comm_size > mv2_alltoall_thresholds_table[conf_index][range].numproc)) {
44       range++;
45   }
46   /* Search for corresponding inter-leader function */
47   while ((range_threshold < (mv2_alltoall_thresholds_table[conf_index][range].size_table - 1))
48       && (nbytes >
49   mv2_alltoall_thresholds_table[conf_index][range].algo_table[range_threshold].max)
50   && (mv2_alltoall_thresholds_table[conf_index][range].algo_table[range_threshold].max != -1)) {
51       range_threshold++;
52   }
53   MV2_Alltoall_function = mv2_alltoall_thresholds_table[conf_index][range].algo_table[range_threshold]
54                                                                                       .MV2_pt_Alltoall_function;
55
56   if(sendbuf != MPI_IN_PLACE) {
57       mpi_errno = MV2_Alltoall_function(sendbuf, sendcount, sendtype,
58           recvbuf, recvcount, recvtype,
59           comm);
60   } else {
61       range_threshold = 0;
62       if(nbytes <
63           mv2_alltoall_thresholds_table[conf_index][range].in_place_algo_table[range_threshold].min
64           ||nbytes > mv2_alltoall_thresholds_table[conf_index][range].in_place_algo_table[range_threshold].max
65       ) {
66           tmp_buf = (char *)smpi_get_tmp_sendbuffer( comm_size * recvcount * recvtype_size );
67           mpi_errno = Datatype::copy((char *)recvbuf,
68               comm_size*recvcount, recvtype,
69               (char *)tmp_buf,
70               comm_size*recvcount, recvtype);
71
72           mpi_errno = MV2_Alltoall_function(tmp_buf, recvcount, recvtype,
73               recvbuf, recvcount, recvtype,
74               comm );
75           smpi_free_tmp_buffer(tmp_buf);
76       } else {
77           mpi_errno = MPIR_Alltoall_inplace_MV2(sendbuf, sendcount, sendtype,
78               recvbuf, recvcount, recvtype,
79               comm );
80       }
81   }
82
83
84   return (mpi_errno);
85 }
86
87 int Coll_allgather_mvapich2::allgather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
88     void *recvbuf, int recvcount, MPI_Datatype recvtype,
89     MPI_Comm comm)
90 {
91
92   int mpi_errno = MPI_SUCCESS;
93   long nbytes = 0, comm_size, recvtype_size;
94   int range = 0;
95   int partial_sub_ok = 0;
96   int conf_index = 0;
97   int range_threshold = 0;
98   int is_two_level = 0;
99   int local_size = -1;
100   MPI_Comm shmem_comm;
101   //MPI_Comm *shmem_commptr=NULL;
102   /* Get the size of the communicator */
103   comm_size = comm->size();
104   recvtype_size=recvtype->size();
105   nbytes = recvtype_size * recvcount;
106
107   if(mv2_allgather_table_ppn_conf==NULL)
108     init_mv2_allgather_tables_stampede();
109     
110   if(comm->get_leaders_comm()==MPI_COMM_NULL){
111     comm->init_smp();
112   }
113
114   int i;
115   if (comm->is_uniform()){
116     shmem_comm = comm->get_intra_comm();
117     local_size = shmem_comm->size();
118     i = 0;
119     if (mv2_allgather_table_ppn_conf[0] == -1) {
120       // Indicating user defined tuning
121       conf_index = 0;
122       goto conf_check_end;
123     }
124     do {
125       if (local_size == mv2_allgather_table_ppn_conf[i]) {
126         conf_index = i;
127         partial_sub_ok = 1;
128         break;
129       }
130       i++;
131     } while(i < mv2_allgather_num_ppn_conf);
132   }
133   conf_check_end:
134   if (partial_sub_ok != 1) {
135     conf_index = 0;
136   }
137   
138   /* Search for the corresponding system size inside the tuning table */
139   while ((range < (mv2_size_allgather_tuning_table[conf_index] - 1)) &&
140       (comm_size >
141   mv2_allgather_thresholds_table[conf_index][range].numproc)) {
142       range++;
143   }
144   /* Search for corresponding inter-leader function */
145   while ((range_threshold <
146       (mv2_allgather_thresholds_table[conf_index][range].size_inter_table - 1))
147       && (nbytes > mv2_allgather_thresholds_table[conf_index][range].inter_leader[range_threshold].max)
148       && (mv2_allgather_thresholds_table[conf_index][range].inter_leader[range_threshold].max !=
149           -1)) {
150       range_threshold++;
151   }
152
153   /* Set inter-leader pt */
154   MV2_Allgatherction =
155       mv2_allgather_thresholds_table[conf_index][range].inter_leader[range_threshold].
156       MV2_pt_Allgatherction;
157
158   is_two_level =  mv2_allgather_thresholds_table[conf_index][range].two_level[range_threshold];
159
160   /* intracommunicator */
161   if(is_two_level ==1){
162     if(partial_sub_ok ==1){
163       if (comm->is_blocked()){
164       mpi_errno = MPIR_2lvl_Allgather_MV2(sendbuf, sendcount, sendtype,
165                             recvbuf, recvcount, recvtype,
166                             comm);
167       }else{
168       mpi_errno = Coll_allgather_mpich::allgather(sendbuf, sendcount, sendtype,
169                             recvbuf, recvcount, recvtype,
170                             comm);
171       }
172     } else {
173       mpi_errno = MPIR_Allgather_RD_MV2(sendbuf, sendcount, sendtype,
174           recvbuf, recvcount, recvtype,
175           comm);
176     }
177   } else if(MV2_Allgatherction == &MPIR_Allgather_Bruck_MV2
178       || MV2_Allgatherction == &MPIR_Allgather_RD_MV2
179       || MV2_Allgatherction == &MPIR_Allgather_Ring_MV2) {
180       mpi_errno = MV2_Allgatherction(sendbuf, sendcount, sendtype,
181           recvbuf, recvcount, recvtype,
182           comm);
183   }else{
184       return MPI_ERR_OTHER;
185   }
186
187   return mpi_errno;
188 }
189
190 int Coll_gather_mvapich2::gather(void *sendbuf,
191     int sendcnt,
192     MPI_Datatype sendtype,
193     void *recvbuf,
194     int recvcnt,
195     MPI_Datatype recvtype,
196     int root, MPI_Comm  comm)
197 {
198   if(mv2_gather_thresholds_table==NULL)
199     init_mv2_gather_tables_stampede();
200
201   int mpi_errno = MPI_SUCCESS;
202   int range = 0;
203   int range_threshold = 0;
204   int range_intra_threshold = 0;
205   long nbytes = 0;
206   int comm_size = 0;
207   int recvtype_size, sendtype_size;
208   int rank = -1;
209   comm_size = comm->size();
210   rank = comm->rank();
211
212   if (rank == root) {
213       recvtype_size=recvtype->size();
214       nbytes = recvcnt * recvtype_size;
215   } else {
216       sendtype_size=sendtype->size();
217       nbytes = sendcnt * sendtype_size;
218   }
219
220   /* Search for the corresponding system size inside the tuning table */
221   while ((range < (mv2_size_gather_tuning_table - 1)) &&
222       (comm_size > mv2_gather_thresholds_table[range].numproc)) {
223       range++;
224   }
225   /* Search for corresponding inter-leader function */
226   while ((range_threshold < (mv2_gather_thresholds_table[range].size_inter_table - 1))
227       && (nbytes >
228   mv2_gather_thresholds_table[range].inter_leader[range_threshold].max)
229   && (mv2_gather_thresholds_table[range].inter_leader[range_threshold].max !=
230       -1)) {
231       range_threshold++;
232   }
233
234   /* Search for corresponding intra node function */
235   while ((range_intra_threshold < (mv2_gather_thresholds_table[range].size_intra_table - 1))
236       && (nbytes >
237   mv2_gather_thresholds_table[range].intra_node[range_intra_threshold].max)
238   && (mv2_gather_thresholds_table[range].intra_node[range_intra_threshold].max !=
239       -1)) {
240       range_intra_threshold++;
241   }
242   
243     if (comm->is_blocked() ) {
244         // Set intra-node function pt for gather_two_level 
245         MV2_Gather_intra_node_function = 
246                               mv2_gather_thresholds_table[range].intra_node[range_intra_threshold].
247                               MV2_pt_Gather_function;
248         //Set inter-leader pt 
249         MV2_Gather_inter_leader_function =
250                               mv2_gather_thresholds_table[range].inter_leader[range_threshold].
251                               MV2_pt_Gather_function;
252         // We call Gather function 
253         mpi_errno =
254             MV2_Gather_inter_leader_function(sendbuf, sendcnt, sendtype, recvbuf, recvcnt,
255                                              recvtype, root, comm);
256
257     } else {
258   // Indeed, direct (non SMP-aware)gather is MPICH one
259   mpi_errno = Coll_gather_mpich::gather(sendbuf, sendcnt, sendtype,
260       recvbuf, recvcnt, recvtype,
261       root, comm);
262   }
263
264   return mpi_errno;
265 }
266
267 int Coll_allgatherv_mvapich2::allgatherv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
268     void *recvbuf, int *recvcounts, int *displs,
269     MPI_Datatype recvtype, MPI_Comm  comm )
270 {
271   int mpi_errno = MPI_SUCCESS;
272   int range = 0, comm_size, total_count, recvtype_size, i;
273   int range_threshold = 0;
274   long nbytes = 0;
275
276   if(mv2_allgatherv_thresholds_table==NULL)
277     init_mv2_allgatherv_tables_stampede();
278
279   comm_size = comm->size();
280   total_count = 0;
281   for (i = 0; i < comm_size; i++)
282     total_count += recvcounts[i];
283
284   recvtype_size=recvtype->size();
285   nbytes = total_count * recvtype_size;
286
287   /* Search for the corresponding system size inside the tuning table */
288   while ((range < (mv2_size_allgatherv_tuning_table - 1)) &&
289       (comm_size > mv2_allgatherv_thresholds_table[range].numproc)) {
290       range++;
291   }
292   /* Search for corresponding inter-leader function */
293   while ((range_threshold < (mv2_allgatherv_thresholds_table[range].size_inter_table - 1))
294       && (nbytes >
295   comm_size * mv2_allgatherv_thresholds_table[range].inter_leader[range_threshold].max)
296   && (mv2_allgatherv_thresholds_table[range].inter_leader[range_threshold].max !=
297       -1)) {
298       range_threshold++;
299   }
300   /* Set inter-leader pt */
301   MV2_Allgatherv_function =
302       mv2_allgatherv_thresholds_table[range].inter_leader[range_threshold].
303       MV2_pt_Allgatherv_function;
304
305   if (MV2_Allgatherv_function == &MPIR_Allgatherv_Rec_Doubling_MV2)
306     {
307       if(!(comm_size & (comm_size - 1)))
308         {
309           mpi_errno =
310               MPIR_Allgatherv_Rec_Doubling_MV2(sendbuf, sendcount,
311                   sendtype, recvbuf,
312                   recvcounts, displs,
313                   recvtype, comm);
314         } else {
315             mpi_errno =
316                 MPIR_Allgatherv_Bruck_MV2(sendbuf, sendcount,
317                     sendtype, recvbuf,
318                     recvcounts, displs,
319                     recvtype, comm);
320         }
321     } else {
322         mpi_errno =
323             MV2_Allgatherv_function(sendbuf, sendcount, sendtype,
324                 recvbuf, recvcounts, displs,
325                 recvtype, comm);
326     }
327
328   return mpi_errno;
329 }
330
331
332
333 int Coll_allreduce_mvapich2::allreduce(void *sendbuf,
334     void *recvbuf,
335     int count,
336     MPI_Datatype datatype,
337     MPI_Op op, MPI_Comm comm)
338 {
339
340   int mpi_errno = MPI_SUCCESS;
341   //int rank = 0,
342   int comm_size = 0;
343
344   comm_size = comm->size();
345   //rank = comm->rank();
346
347   if (count == 0) {
348       return MPI_SUCCESS;
349   }
350
351   if (mv2_allreduce_thresholds_table == NULL)
352     init_mv2_allreduce_tables_stampede();
353
354   /* check if multiple threads are calling this collective function */
355
356   MPI_Aint sendtype_size = 0;
357   long nbytes = 0;
358   int range = 0, range_threshold = 0, range_threshold_intra = 0;
359   int is_two_level = 0;
360   int is_commutative = 0;
361   MPI_Aint true_lb, true_extent;
362
363   sendtype_size=datatype->size();
364   nbytes = count * sendtype_size;
365
366   datatype->extent(&true_lb, &true_extent);
367   //MPI_Op *op_ptr;
368   //is_commutative = op->is_commutative();
369
370   {
371     /* Search for the corresponding system size inside the tuning table */
372     while ((range < (mv2_size_allreduce_tuning_table - 1)) &&
373         (comm_size > mv2_allreduce_thresholds_table[range].numproc)) {
374         range++;
375     }
376     /* Search for corresponding inter-leader function */
377     /* skip mcast poiters if mcast is not available */
378     if(mv2_allreduce_thresholds_table[range].mcast_enabled != 1){
379         while ((range_threshold < (mv2_allreduce_thresholds_table[range].size_inter_table - 1))
380             && ((mv2_allreduce_thresholds_table[range].
381                 inter_leader[range_threshold].MV2_pt_Allreducection
382                 == &MPIR_Allreduce_mcst_reduce_redscat_gather_MV2) ||
383                 (mv2_allreduce_thresholds_table[range].
384                     inter_leader[range_threshold].MV2_pt_Allreducection
385                     == &MPIR_Allreduce_mcst_reduce_two_level_helper_MV2)
386             )) {
387             range_threshold++;
388         }
389     }
390     while ((range_threshold < (mv2_allreduce_thresholds_table[range].size_inter_table - 1))
391         && (nbytes >
392     mv2_allreduce_thresholds_table[range].inter_leader[range_threshold].max)
393     && (mv2_allreduce_thresholds_table[range].inter_leader[range_threshold].max != -1)) {
394         range_threshold++;
395     }
396     if(mv2_allreduce_thresholds_table[range].is_two_level_allreduce[range_threshold] == 1){
397         is_two_level = 1;
398     }
399     /* Search for corresponding intra-node function */
400     while ((range_threshold_intra <
401         (mv2_allreduce_thresholds_table[range].size_intra_table - 1))
402         && (nbytes >
403     mv2_allreduce_thresholds_table[range].intra_node[range_threshold_intra].max)
404     && (mv2_allreduce_thresholds_table[range].intra_node[range_threshold_intra].max !=
405         -1)) {
406         range_threshold_intra++;
407     }
408
409     MV2_Allreducection = mv2_allreduce_thresholds_table[range].inter_leader[range_threshold]
410                                                                                 .MV2_pt_Allreducection;
411
412     MV2_Allreduce_intra_function = mv2_allreduce_thresholds_table[range].intra_node[range_threshold_intra]
413                                                                                     .MV2_pt_Allreducection;
414
415     /* check if mcast is ready, otherwise replace mcast with other algorithm */
416     if((MV2_Allreducection == &MPIR_Allreduce_mcst_reduce_redscat_gather_MV2)||
417         (MV2_Allreducection == &MPIR_Allreduce_mcst_reduce_two_level_helper_MV2)){
418         {
419           MV2_Allreducection = &MPIR_Allreduce_pt2pt_rd_MV2;
420         }
421         if(is_two_level != 1) {
422             MV2_Allreducection = &MPIR_Allreduce_pt2pt_rd_MV2;
423         }
424     }
425
426     if(is_two_level == 1){
427         // check if shm is ready, if not use other algorithm first
428         if (is_commutative) {
429           if(comm->get_leaders_comm()==MPI_COMM_NULL){
430             comm->init_smp();
431           }
432           mpi_errno = MPIR_Allreduce_two_level_MV2(sendbuf, recvbuf, count,
433                                                      datatype, op, comm);
434                 } else {
435         mpi_errno = MPIR_Allreduce_pt2pt_rd_MV2(sendbuf, recvbuf, count,
436             datatype, op, comm);
437         }
438     } else {
439         mpi_errno = MV2_Allreducection(sendbuf, recvbuf, count,
440             datatype, op, comm);
441     }
442   }
443
444   //comm->ch.intra_node_done=0;
445
446   return (mpi_errno);
447
448
449 }
450
451
452 int Coll_alltoallv_mvapich2::alltoallv(void *sbuf, int *scounts, int *sdisps,
453     MPI_Datatype sdtype,
454     void *rbuf, int *rcounts, int *rdisps,
455     MPI_Datatype rdtype,
456     MPI_Comm  comm
457 )
458 {
459
460   if (sbuf == MPI_IN_PLACE) {
461       return Coll_alltoallv_ompi_basic_linear::alltoallv(sbuf, scounts, sdisps, sdtype,
462           rbuf, rcounts, rdisps,rdtype,
463           comm);
464   } else     /* For starters, just keep the original algorithm. */
465   return Coll_alltoallv_ring::alltoallv(sbuf, scounts, sdisps, sdtype,
466       rbuf, rcounts, rdisps,rdtype,
467       comm);
468 }
469
470
471 int Coll_barrier_mvapich2::barrier(MPI_Comm  comm)
472 {   
473   return Coll_barrier_mvapich2_pair::barrier(comm);
474 }
475
476
477
478
479 int Coll_bcast_mvapich2::bcast(void *buffer,
480     int count,
481     MPI_Datatype datatype,
482     int root, MPI_Comm comm)
483 {
484     int mpi_errno = MPI_SUCCESS;
485     int comm_size/*, rank*/;
486     int two_level_bcast = 1;
487     long nbytes = 0; 
488     int range = 0;
489     int range_threshold = 0;
490     int range_threshold_intra = 0;
491     int is_homogeneous, is_contig;
492     MPI_Aint type_size;
493     //, position;
494     void *tmp_buf = NULL;
495     MPI_Comm shmem_comm;
496     //MPID_Datatype *dtp;
497
498     if (count == 0)
499         return MPI_SUCCESS;
500     if(comm->get_leaders_comm()==MPI_COMM_NULL){
501       comm->init_smp();
502     }
503     if (not mv2_bcast_thresholds_table)
504       init_mv2_bcast_tables_stampede();
505     comm_size = comm->size();
506     //rank = comm->rank();
507
508     is_contig=1;
509 /*    if (HANDLE_GET_KIND(datatype) == HANDLE_KIND_BUILTIN)*/
510 /*        is_contig = 1;*/
511 /*    else {*/
512 /*        MPID_Datatype_get_ptr(datatype, dtp);*/
513 /*        is_contig = dtp->is_contig;*/
514 /*    }*/
515
516     is_homogeneous = 1;
517
518     /* MPI_Type_size() might not give the accurate size of the packed
519      * datatype for heterogeneous systems (because of padding, encoding,
520      * etc). On the other hand, MPI_Pack_size() can become very
521      * expensive, depending on the implementation, especially for
522      * heterogeneous systems. We want to use MPI_Type_size() wherever
523      * possible, and MPI_Pack_size() in other places.
524      */
525     //if (is_homogeneous) {
526         type_size=datatype->size();
527
528    /* } else {
529         MPIR_Pack_size_impl(1, datatype, &type_size);
530     }*/
531     nbytes =  (count) * (type_size);
532
533     /* Search for the corresponding system size inside the tuning table */
534     while ((range < (mv2_size_bcast_tuning_table - 1)) &&
535            (comm_size > mv2_bcast_thresholds_table[range].numproc)) {
536         range++;
537     }
538     /* Search for corresponding inter-leader function */
539     while ((range_threshold < (mv2_bcast_thresholds_table[range].size_inter_table - 1))
540            && (nbytes >
541                mv2_bcast_thresholds_table[range].inter_leader[range_threshold].max)
542            && (mv2_bcast_thresholds_table[range].inter_leader[range_threshold].max != -1)) {
543         range_threshold++;
544     }
545
546     /* Search for corresponding intra-node function */
547     while ((range_threshold_intra <
548             (mv2_bcast_thresholds_table[range].size_intra_table - 1))
549            && (nbytes >
550                mv2_bcast_thresholds_table[range].intra_node[range_threshold_intra].max)
551            && (mv2_bcast_thresholds_table[range].intra_node[range_threshold_intra].max !=
552                -1)) {
553         range_threshold_intra++;
554     }
555
556     MV2_Bcast_function =
557         mv2_bcast_thresholds_table[range].inter_leader[range_threshold].
558         MV2_pt_Bcast_function;
559
560     MV2_Bcast_intra_node_function =
561         mv2_bcast_thresholds_table[range].
562         intra_node[range_threshold_intra].MV2_pt_Bcast_function;
563
564 /*    if (mv2_user_bcast_intra == NULL && */
565 /*            MV2_Bcast_intra_node_function == &MPIR_Knomial_Bcast_intra_node_MV2) {*/
566 /*            MV2_Bcast_intra_node_function = &MPIR_Shmem_Bcast_MV2;*/
567 /*    }*/
568
569     if (mv2_bcast_thresholds_table[range].inter_leader[range_threshold].
570         zcpy_pipelined_knomial_factor != -1) {
571         zcpy_knomial_factor = 
572             mv2_bcast_thresholds_table[range].inter_leader[range_threshold].
573             zcpy_pipelined_knomial_factor;
574     }
575
576     if (mv2_pipelined_zcpy_knomial_factor != -1) {
577         zcpy_knomial_factor = mv2_pipelined_zcpy_knomial_factor;
578     }
579
580     if(MV2_Bcast_intra_node_function == NULL) {
581         /* if tuning table do not have any intra selection, set func pointer to
582         ** default one for mcast intra node */
583         MV2_Bcast_intra_node_function = &MPIR_Shmem_Bcast_MV2;
584     }
585
586     /* Set value of pipeline segment size */
587     bcast_segment_size = mv2_bcast_thresholds_table[range].bcast_segment_size;
588     
589     /* Set value of inter node knomial factor */
590     mv2_inter_node_knomial_factor = mv2_bcast_thresholds_table[range].inter_node_knomial_factor;
591
592     /* Set value of intra node knomial factor */
593     mv2_intra_node_knomial_factor = mv2_bcast_thresholds_table[range].intra_node_knomial_factor;
594
595     /* Check if we will use a two level algorithm or not */
596     two_level_bcast =
597 #if defined(_MCST_SUPPORT_)
598         mv2_bcast_thresholds_table[range].is_two_level_bcast[range_threshold] 
599         || comm->ch.is_mcast_ok;
600 #else
601         mv2_bcast_thresholds_table[range].is_two_level_bcast[range_threshold];
602 #endif
603      if (two_level_bcast == 1) {
604        if (not is_contig || not is_homogeneous) {
605          tmp_buf = (void*)smpi_get_tmp_sendbuffer(nbytes);
606
607          /*            position = 0;*/
608          /*            if (rank == root) {*/
609          /*                mpi_errno =*/
610          /*                    MPIR_Pack_impl(buffer, count, datatype, tmp_buf, nbytes, &position);*/
611          /*                if (mpi_errno)*/
612          /*                    MPIU_ERR_POP(mpi_errno);*/
613          /*            }*/
614         }
615 #ifdef CHANNEL_MRAIL_GEN2
616         if ((mv2_enable_zcpy_bcast == 1) &&
617               (&MPIR_Pipelined_Bcast_Zcpy_MV2 == MV2_Bcast_function)) {
618           if (not is_contig || not is_homogeneous) {
619             mpi_errno = MPIR_Pipelined_Bcast_Zcpy_MV2(tmp_buf, nbytes, MPI_BYTE, root, comm);
620             } else { 
621                 mpi_errno = MPIR_Pipelined_Bcast_Zcpy_MV2(buffer, count, datatype,
622                                                  root, comm);
623             } 
624         } else 
625 #endif /* defined(CHANNEL_MRAIL_GEN2) */
626         { 
627             shmem_comm = comm->get_intra_comm();
628             if (not is_contig || not is_homogeneous) {
629               mpi_errno = MPIR_Bcast_tune_inter_node_helper_MV2(tmp_buf, nbytes, MPI_BYTE, root, comm);
630             } else {
631                 mpi_errno =
632                     MPIR_Bcast_tune_inter_node_helper_MV2(buffer, count, datatype, root,
633                                                           comm);
634             }
635
636             /* We are now done with the inter-node phase */
637
638
639                     root = INTRA_NODE_ROOT;
640
641                     if (not is_contig || not is_homogeneous) {
642                       mpi_errno = MV2_Bcast_intra_node_function(tmp_buf, nbytes, MPI_BYTE, root, shmem_comm);
643                 } else {
644                     mpi_errno = MV2_Bcast_intra_node_function(buffer, count,
645                                                               datatype, root, shmem_comm);
646
647                 }
648         }
649         /*        if (not is_contig || not is_homogeneous) {*/
650         /*            if (rank != root) {*/
651         /*                position = 0;*/
652         /*                mpi_errno = MPIR_Unpack_impl(tmp_buf, nbytes, &position, buffer,*/
653         /*                                             count, datatype);*/
654         /*            }*/
655         /*        }*/
656     } else {
657         /* We use Knomial for intra node */
658         MV2_Bcast_intra_node_function = &MPIR_Knomial_Bcast_intra_node_MV2;
659 /*        if (mv2_enable_shmem_bcast == 0) {*/
660             /* Fall back to non-tuned version */
661 /*            MPIR_Bcast_intra_MV2(buffer, count, datatype, root, comm);*/
662 /*        } else {*/
663             mpi_errno = MV2_Bcast_function(buffer, count, datatype, root,
664                                            comm);
665
666 /*        }*/
667     }
668
669
670     return mpi_errno;
671
672 }
673
674
675
676 int Coll_reduce_mvapich2::reduce( void *sendbuf,
677     void *recvbuf,
678     int count,
679     MPI_Datatype datatype,
680     MPI_Op op, int root, MPI_Comm comm)
681 {
682   if(mv2_reduce_thresholds_table == NULL)
683     init_mv2_reduce_tables_stampede();
684
685   int mpi_errno = MPI_SUCCESS;
686   int range = 0;
687   int range_threshold = 0;
688   int range_intra_threshold = 0;
689   int is_commutative, pof2;
690   int comm_size = 0;
691   long nbytes = 0;
692   int sendtype_size;
693   int is_two_level = 0;
694
695   comm_size = comm->size();
696   sendtype_size=datatype->size();
697   nbytes = count * sendtype_size;
698
699   if (count == 0)
700     return MPI_SUCCESS;
701
702   is_commutative = (op==MPI_OP_NULL || op->is_commutative());
703
704   /* find nearest power-of-two less than or equal to comm_size */
705   for( pof2 = 1; pof2 <= comm_size; pof2 <<= 1 );
706   pof2 >>=1;
707
708
709   /* Search for the corresponding system size inside the tuning table */
710   while ((range < (mv2_size_reduce_tuning_table - 1)) &&
711       (comm_size > mv2_reduce_thresholds_table[range].numproc)) {
712       range++;
713   }
714   /* Search for corresponding inter-leader function */
715   while ((range_threshold < (mv2_reduce_thresholds_table[range].size_inter_table - 1))
716       && (nbytes >
717   mv2_reduce_thresholds_table[range].inter_leader[range_threshold].max)
718   && (mv2_reduce_thresholds_table[range].inter_leader[range_threshold].max !=
719       -1)) {
720       range_threshold++;
721   }
722
723   /* Search for corresponding intra node function */
724   while ((range_intra_threshold < (mv2_reduce_thresholds_table[range].size_intra_table - 1))
725       && (nbytes >
726   mv2_reduce_thresholds_table[range].intra_node[range_intra_threshold].max)
727   && (mv2_reduce_thresholds_table[range].intra_node[range_intra_threshold].max !=
728       -1)) {
729       range_intra_threshold++;
730   }
731
732   /* Set intra-node function pt for reduce_two_level */
733   MV2_Reduce_intra_function =
734       mv2_reduce_thresholds_table[range].intra_node[range_intra_threshold].
735       MV2_pt_Reduce_function;
736   /* Set inter-leader pt */
737   MV2_Reduce_function =
738       mv2_reduce_thresholds_table[range].inter_leader[range_threshold].
739       MV2_pt_Reduce_function;
740
741   if(mv2_reduce_intra_knomial_factor<0)
742     {
743       mv2_reduce_intra_knomial_factor = mv2_reduce_thresholds_table[range].intra_k_degree;
744     }
745   if(mv2_reduce_inter_knomial_factor<0)
746     {
747       mv2_reduce_inter_knomial_factor = mv2_reduce_thresholds_table[range].inter_k_degree;
748     }
749   if(mv2_reduce_thresholds_table[range].is_two_level_reduce[range_threshold] == 1){
750       is_two_level = 1;
751   }
752   /* We call Reduce function */
753   if(is_two_level == 1)
754     {
755        if (is_commutative == 1) {
756          if(comm->get_leaders_comm()==MPI_COMM_NULL){
757            comm->init_smp();
758          }
759          mpi_errno = MPIR_Reduce_two_level_helper_MV2(sendbuf, recvbuf, count, 
760                                            datatype, op, root, comm);
761         } else {
762       mpi_errno = MPIR_Reduce_binomial_MV2(sendbuf, recvbuf, count,
763           datatype, op, root, comm);
764       }
765     } else if(MV2_Reduce_function == &MPIR_Reduce_inter_knomial_wrapper_MV2 ){
766         if(is_commutative ==1)
767           {
768             mpi_errno = MV2_Reduce_function(sendbuf, recvbuf, count, 
769                 datatype, op, root, comm);
770           } else {
771               mpi_errno = MPIR_Reduce_binomial_MV2(sendbuf, recvbuf, count,
772                   datatype, op, root, comm);
773           }
774     } else if(MV2_Reduce_function == &MPIR_Reduce_redscat_gather_MV2){
775         if (/*(HANDLE_GET_KIND(op) == HANDLE_KIND_BUILTIN) &&*/ (count >= pof2))
776           {
777             mpi_errno = MV2_Reduce_function(sendbuf, recvbuf, count, 
778                 datatype, op, root, comm);
779           } else {
780               mpi_errno = MPIR_Reduce_binomial_MV2(sendbuf, recvbuf, count,
781                   datatype, op, root, comm);
782           }
783     } else {
784         mpi_errno = MV2_Reduce_function(sendbuf, recvbuf, count, 
785             datatype, op, root, comm);
786     }
787
788
789   return mpi_errno;
790
791 }
792
793
794 int Coll_reduce_scatter_mvapich2::reduce_scatter(void *sendbuf, void *recvbuf, int *recvcnts,
795     MPI_Datatype datatype, MPI_Op op,
796     MPI_Comm comm)
797 {
798   int mpi_errno = MPI_SUCCESS;
799   int i = 0, comm_size = comm->size(), total_count = 0, type_size =
800       0, nbytes = 0;
801   int range = 0;
802   int range_threshold = 0;
803   int is_commutative = 0;
804   int *disps = static_cast<int*>(xbt_malloc(comm_size * sizeof (int)));
805
806   if(mv2_red_scat_thresholds_table==NULL)
807     init_mv2_reduce_scatter_tables_stampede();
808
809   is_commutative=(op==MPI_OP_NULL || op->is_commutative());
810   for (i = 0; i < comm_size; i++) {
811       disps[i] = total_count;
812       total_count += recvcnts[i];
813   }
814
815   type_size=datatype->size();
816   nbytes = total_count * type_size;
817
818   if (is_commutative) {
819
820       /* Search for the corresponding system size inside the tuning table */
821       while ((range < (mv2_size_red_scat_tuning_table - 1)) &&
822           (comm_size > mv2_red_scat_thresholds_table[range].numproc)) {
823           range++;
824       }
825       /* Search for corresponding inter-leader function */
826       while ((range_threshold < (mv2_red_scat_thresholds_table[range].size_inter_table - 1))
827           && (nbytes >
828       mv2_red_scat_thresholds_table[range].inter_leader[range_threshold].max)
829       && (mv2_red_scat_thresholds_table[range].inter_leader[range_threshold].max !=
830           -1)) {
831           range_threshold++;
832       }
833
834       /* Set inter-leader pt */
835       MV2_Red_scat_function =
836           mv2_red_scat_thresholds_table[range].inter_leader[range_threshold].
837           MV2_pt_Red_scat_function;
838
839       mpi_errno = MV2_Red_scat_function(sendbuf, recvbuf,
840           recvcnts, datatype,
841           op, comm);
842   } else {
843       int is_block_regular = 1;
844       for (i = 0; i < (comm_size - 1); ++i) {
845           if (recvcnts[i] != recvcnts[i+1]) {
846               is_block_regular = 0;
847               break;
848           }
849       }
850       int pof2 = 1;
851       while (pof2 < comm_size) pof2 <<= 1;
852       if (pof2 == comm_size && is_block_regular) {
853           /* noncommutative, pof2 size, and block regular */
854           mpi_errno = MPIR_Reduce_scatter_non_comm_MV2(sendbuf, recvbuf,
855               recvcnts, datatype,
856               op, comm);
857       }
858       mpi_errno =  Coll_reduce_scatter_mpich_rdb::reduce_scatter(sendbuf, recvbuf,
859           recvcnts, datatype,
860           op, comm);
861   }
862   xbt_free(disps);
863   return mpi_errno;
864
865 }
866
867
868
869 int Coll_scatter_mvapich2::scatter(void *sendbuf,
870     int sendcnt,
871     MPI_Datatype sendtype,
872     void *recvbuf,
873     int recvcnt,
874     MPI_Datatype recvtype,
875     int root, MPI_Comm comm)
876 {
877   int range = 0, range_threshold = 0, range_threshold_intra = 0;
878   int mpi_errno = MPI_SUCCESS;
879   //   int mpi_errno_ret = MPI_SUCCESS;
880   int rank, nbytes, comm_size;
881   int recvtype_size, sendtype_size;
882   int partial_sub_ok = 0;
883   int conf_index = 0;
884     int local_size = -1;
885     int i;
886      MPI_Comm shmem_comm;
887   //    MPID_Comm *shmem_commptr=NULL;
888   if(mv2_scatter_thresholds_table==NULL)
889     init_mv2_scatter_tables_stampede();
890
891   if(comm->get_leaders_comm()==MPI_COMM_NULL){
892     comm->init_smp();
893   }
894   
895   comm_size = comm->size();
896
897   rank = comm->rank();
898
899   if (rank == root) {
900       sendtype_size=sendtype->size();
901       nbytes = sendcnt * sendtype_size;
902   } else {
903       recvtype_size=recvtype->size();
904       nbytes = recvcnt * recvtype_size;
905   }
906   
907     // check if safe to use partial subscription mode 
908     if (comm->is_uniform()) {
909
910         shmem_comm = comm->get_intra_comm();
911         local_size = shmem_comm->size();
912         i = 0;
913         if (mv2_scatter_table_ppn_conf[0] == -1) {
914             // Indicating user defined tuning 
915             conf_index = 0;
916         }else{
917             do {
918                 if (local_size == mv2_scatter_table_ppn_conf[i]) {
919                     conf_index = i;
920                     partial_sub_ok = 1;
921                     break;
922                 }
923                 i++;
924             } while(i < mv2_scatter_num_ppn_conf);
925         }
926     }
927    
928   if (partial_sub_ok != 1) {
929       conf_index = 0;
930   }
931
932   /* Search for the corresponding system size inside the tuning table */
933   while ((range < (mv2_size_scatter_tuning_table[conf_index] - 1)) &&
934       (comm_size > mv2_scatter_thresholds_table[conf_index][range].numproc)) {
935       range++;
936   }
937   /* Search for corresponding inter-leader function */
938   while ((range_threshold < (mv2_scatter_thresholds_table[conf_index][range].size_inter_table - 1))
939       && (nbytes >
940   mv2_scatter_thresholds_table[conf_index][range].inter_leader[range_threshold].max)
941   && (mv2_scatter_thresholds_table[conf_index][range].inter_leader[range_threshold].max != -1)) {
942       range_threshold++;
943   }
944
945   /* Search for corresponding intra-node function */
946   while ((range_threshold_intra <
947       (mv2_scatter_thresholds_table[conf_index][range].size_intra_table - 1))
948       && (nbytes >
949   mv2_scatter_thresholds_table[conf_index][range].intra_node[range_threshold_intra].max)
950   && (mv2_scatter_thresholds_table[conf_index][range].intra_node[range_threshold_intra].max !=
951       -1)) {
952       range_threshold_intra++;
953   }
954
955   MV2_Scatter_function = mv2_scatter_thresholds_table[conf_index][range].inter_leader[range_threshold]
956                                                                                       .MV2_pt_Scatter_function;
957
958   if(MV2_Scatter_function == &MPIR_Scatter_mcst_wrap_MV2) {
959 #if defined(_MCST_SUPPORT_)
960       if(comm->ch.is_mcast_ok == 1
961           && mv2_use_mcast_scatter == 1
962           && comm->ch.shmem_coll_ok == 1) {
963           MV2_Scatter_function = &MPIR_Scatter_mcst_MV2;
964       } else
965 #endif /*#if defined(_MCST_SUPPORT_) */
966         {
967           if(mv2_scatter_thresholds_table[conf_index][range].inter_leader[range_threshold + 1].
968               MV2_pt_Scatter_function != NULL) {
969               MV2_Scatter_function = mv2_scatter_thresholds_table[conf_index][range].inter_leader[range_threshold + 1]
970                                                                                                   .MV2_pt_Scatter_function;
971           } else {
972               /* Fallback! */
973               MV2_Scatter_function = &MPIR_Scatter_MV2_Binomial;
974           }
975         } 
976   }
977
978   if( (MV2_Scatter_function == &MPIR_Scatter_MV2_two_level_Direct) ||
979       (MV2_Scatter_function == &MPIR_Scatter_MV2_two_level_Binomial)) {
980        if( comm->is_blocked()) {
981              MV2_Scatter_intra_function = mv2_scatter_thresholds_table[conf_index][range].intra_node[range_threshold_intra]
982                                 .MV2_pt_Scatter_function;
983
984              mpi_errno =
985                    MV2_Scatter_function(sendbuf, sendcnt, sendtype,
986                                         recvbuf, recvcnt, recvtype, root,
987                                         comm);
988          } else {
989       mpi_errno = MPIR_Scatter_MV2_Binomial(sendbuf, sendcnt, sendtype,
990           recvbuf, recvcnt, recvtype, root,
991           comm);
992
993       }
994   } else {
995       mpi_errno = MV2_Scatter_function(sendbuf, sendcnt, sendtype,
996           recvbuf, recvcnt, recvtype, root,
997           comm);
998   }
999   return (mpi_errno);
1000 }
1001
1002 }
1003 }
1004
1005 void smpi_coll_cleanup_mvapich2()
1006 {
1007   int i = 0;
1008   if (mv2_alltoall_thresholds_table)
1009     xbt_free(mv2_alltoall_thresholds_table[i]);
1010   xbt_free(mv2_alltoall_thresholds_table);
1011   xbt_free(mv2_size_alltoall_tuning_table);
1012   xbt_free(mv2_alltoall_table_ppn_conf);
1013
1014   xbt_free(mv2_gather_thresholds_table);
1015   if (mv2_allgather_thresholds_table)
1016     xbt_free(mv2_allgather_thresholds_table[0]);
1017   xbt_free(mv2_size_allgather_tuning_table);
1018   xbt_free(mv2_allgather_table_ppn_conf);
1019   xbt_free(mv2_allgather_thresholds_table);
1020
1021   xbt_free(mv2_allgatherv_thresholds_table);
1022   xbt_free(mv2_reduce_thresholds_table);
1023   xbt_free(mv2_red_scat_thresholds_table);
1024   xbt_free(mv2_allreduce_thresholds_table);
1025   xbt_free(mv2_bcast_thresholds_table);
1026   if (mv2_scatter_thresholds_table)
1027     xbt_free(mv2_scatter_thresholds_table[0]);
1028   xbt_free(mv2_scatter_thresholds_table);
1029   xbt_free(mv2_size_scatter_tuning_table);
1030   xbt_free(mv2_scatter_table_ppn_conf);
1031 }