1 /* Copyright (c) 2013-2014. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
8 * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
9 * University Research and Technology
10 * Corporation. All rights reserved.
11 * Copyright (c) 2004-2012 The University of Tennessee and The University
12 * of Tennessee Research Foundation. All rights
14 * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
15 * University of Stuttgart. All rights reserved.
16 * Copyright (c) 2004-2005 The Regents of the University of California.
17 * All rights reserved.
18 * Copyright (c) 2008 Sun Microsystems, Inc. All rights reserved.
19 * Copyright (c) 2009 University of Houston. All rights reserved.
21 * Additional copyrights may follow
26 * (C) 2001 by Argonne National Laboratory.
27 * See COPYRIGHT in top-level directory.
29 /* Copyright (c) 2001-2014, The Ohio State University. All rights
32 * This file is part of the MVAPICH2 software package developed by the
33 * team members of The Ohio State University's Network-Based Computing
34 * Laboratory (NBCL), headed by Professor Dhabaleswar K. (DK) Panda.
36 * For detailed copyright and licensing information, please refer to the
37 * copyright file COPYRIGHT in the top level MVAPICH2 directory.
41 #include "colls_private.h"
42 extern int mv2_reduce_intra_knomial_factor;
43 extern int mv2_reduce_inter_knomial_factor;
45 #define SMPI_DEFAULT_KNOMIAL_FACTOR 4
47 // int mv2_reduce_knomial_factor = 2;
51 static int MPIR_Reduce_knomial_trace(int root, int reduce_knomial_factor,
52 MPI_Comm comm, int *dst, int *expected_send_count,
53 int *expected_recv_count, int **src_array)
55 int mask=0x1, k, comm_size, src, rank, relative_rank, lroot=0;
57 int recv_iter=0, send_iter=0;
58 int *knomial_reduce_src_array=NULL;
59 comm_size = smpi_comm_size(comm);
60 rank = smpi_comm_rank(comm);
63 relative_rank = (rank - lroot + comm_size) % comm_size;
65 /* First compute to whom we need to send data */
66 while (mask < comm_size) {
67 if (relative_rank % (reduce_knomial_factor*mask)) {
68 *dst = relative_rank/(reduce_knomial_factor*mask)*
69 (reduce_knomial_factor*mask)+root;
70 if (*dst >= comm_size) {
76 mask *= reduce_knomial_factor;
78 mask /= reduce_knomial_factor;
80 /* Now compute how many children we have in the knomial-tree */
83 for(k=1;k<reduce_knomial_factor;k++) {
84 if (relative_rank + mask*k < comm_size) {
88 mask /= reduce_knomial_factor;
91 /* Finally, fill up the src array */
93 knomial_reduce_src_array = smpi_get_tmp_sendbuffer(sizeof(int)*recv_iter);
99 for(k=1;k<reduce_knomial_factor;k++) {
100 if (relative_rank + mask*k < comm_size) {
102 if (src >= comm_size) {
105 knomial_reduce_src_array[recv_iter++] = src;
108 mask /= reduce_knomial_factor;
111 *expected_recv_count = recv_iter;
112 *expected_send_count = send_iter;
113 *src_array = knomial_reduce_src_array;
117 int smpi_coll_tuned_reduce_mvapich2_knomial (
121 MPI_Datatype datatype,
126 int mpi_errno = MPI_SUCCESS;
127 int rank, is_commutative;
129 MPI_Request send_request;
131 MPI_Aint true_lb, true_extent, extent;
133 int recv_iter=0, dst=-1, expected_send_count, expected_recv_count;
136 MPI_Request *requests=NULL;
139 if (count == 0) return MPI_SUCCESS;
141 rank = smpi_comm_rank(comm);
143 /* Create a temporary buffer */
145 smpi_datatype_extent(datatype, &true_lb, &true_extent);
146 extent = smpi_datatype_get_extent(datatype);
148 is_commutative = smpi_op_is_commute(op);
151 recvbuf=(void *)smpi_get_tmp_recvbuffer(count*(MAX(extent,true_extent)));
152 recvbuf = (void *)((char*)recvbuf - true_lb);
155 if ((rank != root) || (sendbuf != MPI_IN_PLACE)) {
156 mpi_errno = smpi_datatype_copy(sendbuf, count, datatype, recvbuf,
161 if(mv2_reduce_intra_knomial_factor<0)
163 mv2_reduce_intra_knomial_factor = SMPI_DEFAULT_KNOMIAL_FACTOR;
165 if(mv2_reduce_inter_knomial_factor<0)
167 mv2_reduce_inter_knomial_factor = SMPI_DEFAULT_KNOMIAL_FACTOR;
171 MPIR_Reduce_knomial_trace(root, mv2_reduce_intra_knomial_factor, comm,
172 &dst, &expected_send_count, &expected_recv_count, &src_array);
174 if(expected_recv_count > 0 ) {
175 tmp_buf = xbt_malloc(sizeof(void *)*expected_recv_count);
176 requests = xbt_malloc(sizeof(MPI_Request)*expected_recv_count);
177 for(k=0; k < expected_recv_count; k++ ) {
178 tmp_buf[k] = smpi_get_tmp_sendbuffer(count*(MAX(extent,true_extent)));
179 tmp_buf[k] = (void *)((char*)tmp_buf[k] - true_lb);
182 while(recv_iter < expected_recv_count) {
183 src = src_array[expected_recv_count - (recv_iter+1)];
185 requests[recv_iter]=smpi_mpi_irecv (tmp_buf[recv_iter], count, datatype ,src,
186 COLL_TAG_REDUCE, comm);
192 while(recv_iter < expected_recv_count) {
193 index=smpi_mpi_waitany(expected_recv_count, requests,
197 if (is_commutative) {
198 smpi_op_apply(op, tmp_buf[index], recvbuf, &count, &datatype);
202 for(k=0; k < expected_recv_count; k++ ) {
203 smpi_free_tmp_buffer(tmp_buf[k]);
209 if(src_array != NULL) {
214 send_request=smpi_mpi_isend(recvbuf,count, datatype, dst,
215 COLL_TAG_REDUCE,comm);
217 smpi_mpi_waitall(1, &send_request, &status);
220 /* --END ERROR HANDLING-- */