Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
3aaf479d9a6cc720ae6da83265e98e7d98573895
[simgrid.git] / src / smpi / colls / allgather-rhv.c
1 /* Copyright (c) 2013-2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include "colls_private.h"
8
9 // now only work with power of two processes
10
11 int
12 smpi_coll_tuned_allgather_rhv(void *sbuf, int send_count,
13                               MPI_Datatype send_type, void *rbuf,
14                               int recv_count, MPI_Datatype recv_type,
15                               MPI_Comm comm)
16 {
17   MPI_Status status;
18   MPI_Aint s_extent, r_extent;
19
20   // local int variables
21   int i, dst, send_base_offset, recv_base_offset, send_chunk, recv_chunk,
22       send_offset, recv_offset;
23   int tag = COLL_TAG_ALLGATHER;
24   int mask;
25   int curr_count;
26
27   // get size of the communicator, followed by rank 
28   unsigned int num_procs = smpi_comm_size(comm);
29
30   if((num_procs&(num_procs-1)))
31     THROWF(arg_error,0, "allgather rhv algorithm can't be used with non power of two number of processes ! ");
32
33   unsigned int rank = smpi_comm_rank(comm);
34
35   // get size of single element's type for send buffer and recv buffer
36   s_extent = smpi_datatype_get_extent(send_type);
37   r_extent = smpi_datatype_get_extent(recv_type);
38
39   // multiply size of each element by number of elements to send or recv
40   send_chunk = s_extent * send_count;
41   recv_chunk = r_extent * recv_count;
42
43   if (send_chunk != recv_chunk) {
44     XBT_WARN("MPI_allgather_rhv use default MPI_allgather.");  
45     smpi_mpi_allgather(sbuf, send_count, send_type, rbuf, recv_count,
46                               recv_type, comm);
47     return MPI_SUCCESS;        
48   }
49
50   // compute starting offset location to perform local copy
51   int size = num_procs / 2;
52   int base_offset = 0;
53   mask = 1;
54   while (mask < num_procs) {
55     if (rank & mask) {
56       base_offset += size;
57     }
58     mask <<= 1;
59     size /= 2;
60   }
61
62   //  printf("node %d base_offset %d\n",rank,base_offset);
63
64   //perform a remote copy
65
66   dst = base_offset;
67   smpi_mpi_sendrecv(sbuf, send_count, send_type, dst, tag,
68                (char *)rbuf + base_offset * recv_chunk, recv_count, recv_type, dst, tag,
69                comm, &status);
70
71
72   mask >>= 1;
73   i = 1;
74   int phase = 0;
75   curr_count = recv_count;
76   while (mask >= 1) {
77     // destination pair for both send and recv
78     dst = rank ^ mask;
79
80     // compute offsets
81     send_base_offset = base_offset;
82     if (rank & mask) {
83       recv_base_offset = base_offset - i;
84       base_offset -= i;
85     } else {
86       recv_base_offset = base_offset + i;
87     }
88     send_offset = send_base_offset * recv_chunk;
89     recv_offset = recv_base_offset * recv_chunk;
90
91     //  printf("node %d send to %d in phase %d s_offset = %d r_offset = %d count = %d\n",rank,dst,phase, send_base_offset, recv_base_offset, curr_count);
92
93     smpi_mpi_sendrecv((char *)rbuf + send_offset, curr_count, recv_type, dst, tag,
94                  (char *)rbuf + recv_offset, curr_count, recv_type, dst, tag,
95                  comm, &status);
96
97
98     curr_count *= 2;
99     i *= 2;
100     mask >>= 1;
101     phase++;
102   }
103
104   return MPI_SUCCESS;
105 }