Logo AND Algorithmique Numérique Distribuée

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