1 /* Copyright (c) 2013-2020. 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-2009 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.
19 * Additional copyrights may follow
22 #include "../coll_tuned_topo.hpp"
23 #include "../colls_private.hpp"
28 int gather__ompi_binomial(const void* sbuf, int scount, MPI_Datatype sdtype, void* rbuf, int rcount,
29 MPI_Datatype rdtype, int root, MPI_Comm comm)
37 unsigned char* ptmp = nullptr;
38 unsigned char* tempbuf = nullptr;
39 const unsigned char* src_buf;
41 ompi_coll_tree_t* bmtree;
43 MPI_Aint sextent, slb, strue_lb, strue_extent;
44 MPI_Aint rextent, rlb, rtrue_lb, rtrue_extent;
50 XBT_DEBUG("smpi_coll_tuned_gather_ompi_binomial rank %d", rank);
52 /* create the binomial tree */
53 // COLL_TUNED_UPDATE_IN_ORDER_BMTREE( comm, tuned_module, root );
54 bmtree = ompi_coll_tuned_topo_build_in_order_bmtree(comm, root);
55 // data->cached_in_order_bmtree;
57 sdtype->extent(&slb, &sextent);
58 sdtype->extent(&strue_lb, &strue_extent);
60 vrank = (rank - root + size) % size;
63 rdtype->extent(&rlb, &rextent);
64 rdtype->extent(&rtrue_lb, &rtrue_extent);
66 /* root on 0, just use the recv buffer */
67 ptmp = static_cast<unsigned char*>(rbuf);
68 if (sbuf != MPI_IN_PLACE) {
69 err = Datatype::copy(sbuf, scount, sdtype, ptmp, rcount, rdtype);
70 if (MPI_SUCCESS != err) {
76 /* root is not on 0, allocate temp buffer for recv,
77 * rotate data at the end */
78 tempbuf = smpi_get_tmp_recvbuffer(rtrue_extent + (rcount * size - 1) * rextent);
79 if (nullptr == tempbuf) {
86 if (sbuf != MPI_IN_PLACE) {
87 /* copy from sbuf to temp buffer */
88 err = Datatype::copy(sbuf, scount, sdtype, ptmp, rcount, rdtype);
89 if (MPI_SUCCESS != err) {
94 /* copy from rbuf to temp buffer */
95 err = Datatype::copy((char*)rbuf + rank * rextent * rcount, rcount, rdtype, ptmp, rcount, rdtype);
96 if (MPI_SUCCESS != err) {
104 } else if (!(vrank % 2)) {
105 /* other non-leaf nodes, allocate temp buffer for data received from
106 * children, the most we need is half of the total data elements due
107 * to the property of binomial tree */
108 tempbuf = smpi_get_tmp_sendbuffer(strue_extent + (scount * size - 1) * sextent);
109 if (nullptr == tempbuf) {
115 ptmp = tempbuf - slb;
116 /* local copy to tempbuf */
117 err = Datatype::copy(sbuf, scount, sdtype, ptmp, scount, sdtype);
118 if (MPI_SUCCESS != err) {
123 /* use sdtype,scount as rdtype,rdcount since they are ignored on
131 /* leaf nodes, no temp buffer needed, use sdtype,scount as
132 * rdtype,rdcount since they are ignored on non-root procs */
134 src_buf = static_cast<const unsigned char*>(sbuf);
138 /* all non-leaf nodes recv from children */
139 for (i = 0; i < bmtree->tree_nextsize; i++) {
140 int mycount = 0, vkid;
141 /* figure out how much data I have to send to this child */
142 vkid = (bmtree->tree_next[i] - root + size) % size;
143 mycount = vkid - vrank;
144 if (mycount > (size - vkid))
145 mycount = size - vkid;
148 XBT_DEBUG("smpi_coll_tuned_gather_ompi_binomial rank %d recv %d mycount = %d", rank, bmtree->tree_next[i],
151 Request::recv(ptmp + total_recv * rextent, mycount, rdtype, bmtree->tree_next[i], COLL_TAG_GATHER, comm,
154 total_recv += mycount;
159 /* all nodes except root send to parents */
160 XBT_DEBUG("smpi_coll_tuned_gather_ompi_binomial rank %d send %d count %d\n", rank, bmtree->tree_prev, total_recv);
162 Request::send(src_buf, total_recv, sdtype, bmtree->tree_prev, COLL_TAG_GATHER, comm);
166 /* rotate received data on root if root != 0 */
167 err = Datatype::copy(ptmp, rcount * (size - root), rdtype, (char*)rbuf + rextent * root * rcount,
168 rcount * (size - root), rdtype);
169 if (MPI_SUCCESS != err) {
174 err = Datatype::copy(ptmp + rextent * rcount * (size - root), rcount * root, rdtype, (char*)rbuf, rcount * root,
176 if (MPI_SUCCESS != err) {
181 smpi_free_tmp_buffer(tempbuf);
183 } else if (!(vrank % 2)) {
184 /* other non-leaf nodes */
185 smpi_free_tmp_buffer(tempbuf);
187 ompi_coll_tuned_topo_destroy_tree(&bmtree);
191 if (nullptr != tempbuf)
192 smpi_free_tmp_buffer(tempbuf);
194 XBT_DEBUG("%s:%4d\tError occurred %d, rank %2d", __FILE__, line, err, rank);
199 * gather_intra_linear_sync
201 * Function: - synchronized gather operation with
202 * Accepts: - same arguments as MPI_Gather(), first segment size
203 * Returns: - MPI_SUCCESS or error code
205 int gather__ompi_linear_sync(const void *sbuf, int scount,
207 void *rbuf, int rcount,
215 int first_segment_count;
220 int first_segment_size=0;
224 size_t dsize, block_size;
226 dsize= rdtype->size();
227 block_size = dsize * rcount;
229 dsize=sdtype->size();
230 block_size = dsize * scount;
233 if (block_size > 92160){
234 first_segment_size = 32768;
236 first_segment_size = 1024;
239 XBT_DEBUG("smpi_coll_tuned_gather_ompi_linear_sync rank %d, segment %d", rank, first_segment_size);
242 /* Non-root processes:
243 - receive zero byte message from the root,
244 - send the first segment of the data synchronously,
245 - send the second segment of the data.
248 typelng = sdtype->size();
249 sdtype->extent(&lb, &extent);
250 first_segment_count = scount;
251 COLL_TUNED_COMPUTED_SEGCOUNT((size_t)first_segment_size, typelng, first_segment_count);
253 Request::recv(nullptr, 0, MPI_BYTE, root, COLL_TAG_GATHER, comm, MPI_STATUS_IGNORE);
255 Request::send(sbuf, first_segment_count, sdtype, root, COLL_TAG_GATHER, comm);
257 Request::send((char*)sbuf + extent * first_segment_count, (scount - first_segment_count), sdtype, root,
258 COLL_TAG_GATHER, comm);
263 - For every non-root node:
264 - post irecv for the first segment of the message
265 - send zero byte message to signal node to send the message
266 - post irecv for the second segment of the message
267 - wait for the first segment to complete
268 - Copy local data if necessary
269 - Waitall for all the second segments to complete.
272 MPI_Request first_segment_req;
273 auto* reqs = new (std::nothrow) MPI_Request[size];
274 if (nullptr == reqs) {
280 typelng=rdtype->size();
281 rdtype->extent(&lb, &extent);
282 first_segment_count = rcount;
283 COLL_TUNED_COMPUTED_SEGCOUNT( (size_t)first_segment_size, typelng,
284 first_segment_count );
286 for (i = 0; i < size; ++i) {
289 reqs[i] = MPI_REQUEST_NULL;
293 /* irecv for the first segment from i */
294 ptmp = (char*)rbuf + i * rcount * extent;
295 first_segment_req = Request::irecv(ptmp, first_segment_count, rdtype, i,
296 COLL_TAG_GATHER, comm
299 /* send sync message */
300 Request::send(rbuf, 0, MPI_BYTE, i,
304 /* irecv for the second segment */
305 ptmp = (char*)rbuf + (i * rcount + first_segment_count) * extent;
306 reqs[i]=Request::irecv(ptmp, (rcount - first_segment_count),
307 rdtype, i, COLL_TAG_GATHER, comm
310 /* wait on the first segment to complete */
311 Request::wait(&first_segment_req, MPI_STATUS_IGNORE);
314 /* copy local data if necessary */
315 if (MPI_IN_PLACE != sbuf) {
316 ret = Datatype::copy(sbuf, scount, sdtype,
317 (char*)rbuf + rank * rcount * extent,
319 if (ret != MPI_SUCCESS) { line = __LINE__; goto error_hndl; }
322 /* wait all second segments to complete */
323 ret = Request::waitall(size, reqs, MPI_STATUSES_IGNORE);
324 if (ret != MPI_SUCCESS) { line = __LINE__; goto error_hndl; }
334 "ERROR_HNDL: node %d file %s line %d error %d\n",
335 rank, __FILE__, line, ret );
340 * Linear functions are copied from the BASIC coll module
341 * they do not segment the message and are simple implementations
342 * but for some small number of nodes and/or small data sizes they
343 * are just as fast as tuned/tree based segmenting operations
344 * and as such may be selected by the decision functions
345 * These are copied into this module due to the way we select modules
346 * in V1. i.e. in V2 we will handle this differently and so will not
347 * have to duplicate code.
348 * JPG following the examples from other coll_tuned implementations. Dec06.
351 /* copied function (with appropriate renaming) starts here */
355 * Function: - basic gather operation
356 * Accepts: - same arguments as MPI_Gather()
357 * Returns: - MPI_SUCCESS or error code
359 int gather__ompi_basic_linear(const void* sbuf, int scount, MPI_Datatype sdtype, void* rbuf, int rcount,
360 MPI_Datatype rdtype, int root, MPI_Comm comm)
374 /* Everyone but root sends data and returns. */
375 XBT_DEBUG("ompi_coll_tuned_gather_intra_basic_linear rank %d", rank);
378 Request::send(sbuf, scount, sdtype, root,
384 /* I am the root, loop receiving the data. */
386 rdtype->extent(&lb, &extent);
387 incr = extent * rcount;
388 for (i = 0, ptmp = (char *) rbuf; i < size; ++i, ptmp += incr) {
390 if (MPI_IN_PLACE != sbuf) {
391 err = Datatype::copy(sbuf, scount, sdtype,
392 ptmp, rcount, rdtype);
397 Request::recv(ptmp, rcount, rdtype, i,
399 comm, MPI_STATUS_IGNORE);
402 if (MPI_SUCCESS != err) {