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 // int mv2_reduce_knomial_factor = 2;
47 static int MPIR_Reduce_knomial_trace(int root, int reduce_knomial_factor,
48 MPI_Comm comm, int *dst, int *expected_send_count,
49 int *expected_recv_count, int **src_array)
51 int mask=0x1, k, comm_size, src, rank, relative_rank, lroot=0;
53 int recv_iter=0, send_iter=0;
54 int *knomial_reduce_src_array=NULL;
55 comm_size = smpi_comm_size(comm);
56 rank = smpi_comm_rank(comm);
59 relative_rank = (rank - lroot + comm_size) % comm_size;
61 /* First compute to whom we need to send data */
62 while (mask < comm_size) {
63 if (relative_rank % (reduce_knomial_factor*mask)) {
64 *dst = relative_rank/(reduce_knomial_factor*mask)*
65 (reduce_knomial_factor*mask)+root;
66 if (*dst >= comm_size) {
72 mask *= reduce_knomial_factor;
74 mask /= reduce_knomial_factor;
76 /* Now compute how many children we have in the knomial-tree */
79 for(k=1;k<reduce_knomial_factor;k++) {
80 if (relative_rank + mask*k < comm_size) {
84 mask /= reduce_knomial_factor;
87 /* Finally, fill up the src array */
89 knomial_reduce_src_array = xbt_malloc(sizeof(int)*recv_iter);
95 for(k=1;k<reduce_knomial_factor;k++) {
96 if (relative_rank + mask*k < comm_size) {
98 if (src >= comm_size) {
101 knomial_reduce_src_array[recv_iter++] = src;
104 mask /= reduce_knomial_factor;
107 *expected_recv_count = recv_iter;
108 *expected_send_count = send_iter;
109 *src_array = knomial_reduce_src_array;
113 int smpi_coll_tuned_reduce_mvapich2_knomial (
117 MPI_Datatype datatype,
122 int mpi_errno = MPI_SUCCESS;
123 int rank, is_commutative;
125 MPI_Request send_request;
127 MPI_Aint true_lb, true_extent, extent;
129 int recv_iter=0, dst=-1, expected_send_count, expected_recv_count;
132 MPI_Request *requests=NULL;
135 if (count == 0) return MPI_SUCCESS;
137 rank = smpi_comm_rank(comm);
139 /* Create a temporary buffer */
141 smpi_datatype_extent(datatype, &true_lb, &true_extent);
142 extent = smpi_datatype_get_extent(datatype);
144 is_commutative = smpi_op_is_commute(op);
147 recvbuf=(void *)xbt_malloc(count*(MAX(extent,true_extent)));
148 recvbuf = (void *)((char*)recvbuf - true_lb);
151 if ((rank != root) || (sendbuf != MPI_IN_PLACE)) {
152 mpi_errno = smpi_datatype_copy(sendbuf, count, datatype, recvbuf,
158 MPIR_Reduce_knomial_trace(root, mv2_reduce_intra_knomial_factor, comm,
159 &dst, &expected_send_count, &expected_recv_count, &src_array);
161 if(expected_recv_count > 0 ) {
162 tmp_buf = xbt_malloc(sizeof(void *)*expected_recv_count);
163 requests = xbt_malloc(sizeof(MPI_Request)*expected_recv_count);
164 for(k=0; k < expected_recv_count; k++ ) {
165 tmp_buf[k] = xbt_malloc(count*(MAX(extent,true_extent)));
166 tmp_buf[k] = (void *)((char*)tmp_buf[k] - true_lb);
169 while(recv_iter < expected_recv_count) {
170 src = src_array[expected_recv_count - (recv_iter+1)];
172 requests[recv_iter]=smpi_mpi_irecv (tmp_buf[recv_iter], count, datatype ,src,
173 COLL_TAG_REDUCE, comm);
179 while(recv_iter < expected_recv_count) {
180 index=smpi_mpi_waitany(expected_recv_count, requests,
184 if (is_commutative) {
185 smpi_op_apply(op, tmp_buf[index], recvbuf, &count, &datatype);
189 for(k=0; k < expected_recv_count; k++ ) {
190 xbt_free(tmp_buf[k]);
196 if(src_array != NULL) {
201 send_request=smpi_mpi_isend(recvbuf,count, datatype, dst,
202 COLL_TAG_REDUCE,comm);
204 smpi_mpi_waitall(1, &send_request, &status);
207 /* --END ERROR HANDLING-- */