Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines with new year.
[simgrid.git] / src / smpi / colls / bcast / bcast-NTSB.cpp
1 /* Copyright (c) 2013-2020. 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.hpp"
8
9 int bcast_NTSB_segment_size_in_byte = 8192;
10 namespace simgrid{
11 namespace smpi{
12 int bcast__NTSB(void *buf, int count, MPI_Datatype datatype,
13                 int root, MPI_Comm comm)
14 {
15   int tag = COLL_TAG_BCAST;
16   MPI_Status status;
17   int rank, size;
18   int i;
19
20   MPI_Aint extent;
21   extent = datatype->get_extent();
22
23   rank = comm->rank();
24   size = comm->size();
25
26   /* source node and destination nodes (same through out the functions) */
27   int from = (rank - 1) / 2;
28   int to_left = rank * 2 + 1;
29   int to_right = rank * 2 + 2;
30   if (to_left >= size)
31     to_left = -1;
32   if (to_right >= size)
33     to_right = -1;
34
35   /* segment is segment size in number of elements (not bytes) */
36   int segment = bcast_NTSB_segment_size_in_byte / extent;
37   segment =  segment == 0 ? 1 :segment;
38   /* pipeline length */
39   int pipe_length = count / segment;
40
41   /* use for buffer offset for sending and receiving data = segment size in byte */
42   int increment = segment * extent;
43
44   /* if the input size is not divisible by segment size =>
45      the small remainder will be done with native implementation */
46   int remainder = count % segment;
47
48   /* if root is not zero send to rank zero first */
49   if (root != 0) {
50     if (rank == root) {
51       Request::send(buf, count, datatype, 0, tag, comm);
52     } else if (rank == 0) {
53       Request::recv(buf, count, datatype, root, tag, comm, &status);
54     }
55   }
56
57   /* when a message is smaller than a block size => no pipeline */
58   if (count <= segment) {
59
60     /* case: root */
61     if (rank == 0) {
62       /* case root has only a left child */
63       if (to_right == -1) {
64         Request::send(buf, count, datatype, to_left, tag, comm);
65       }
66       /* case root has both left and right children */
67       else {
68         Request::send(buf, count, datatype, to_left, tag, comm);
69         Request::send(buf, count, datatype, to_right, tag, comm);
70       }
71     }
72
73     /* case: leaf ==> receive only */
74     else if (to_left == -1) {
75       Request::recv(buf, count, datatype, from, tag, comm, &status);
76     }
77
78     /* case: intermediate node with only left child ==> relay message */
79     else if (to_right == -1) {
80       Request::recv(buf, count, datatype, from, tag, comm, &status);
81       Request::send(buf, count, datatype, to_left, tag, comm);
82     }
83
84     /* case: intermediate node with both left and right children ==> relay message */
85     else {
86       Request::recv(buf, count, datatype, from, tag, comm, &status);
87       Request::send(buf, count, datatype, to_left, tag, comm);
88       Request::send(buf, count, datatype, to_right, tag, comm);
89     }
90     return MPI_SUCCESS;
91   }
92   // pipelining
93   else {
94
95     MPI_Request* send_request_array = new MPI_Request[2 * (size + pipe_length)];
96     MPI_Request* recv_request_array = new MPI_Request[size + pipe_length];
97     MPI_Status* send_status_array   = new MPI_Status[2 * (size + pipe_length)];
98     MPI_Status* recv_status_array   = new MPI_Status[size + pipe_length];
99
100     /* case: root */
101     if (rank == 0) {
102       /* case root has only a left child */
103       if (to_right == -1) {
104         for (i = 0; i < pipe_length; i++) {
105           send_request_array[i] = Request::isend((char *) buf + (i * increment), segment, datatype, to_left,
106                     tag + i, comm);
107         }
108         Request::waitall((pipe_length), send_request_array, send_status_array);
109       }
110       /* case root has both left and right children */
111       else {
112         for (i = 0; i < pipe_length; i++) {
113           send_request_array[i] = Request::isend((char *) buf + (i * increment), segment, datatype, to_left,
114                     tag + i, comm);
115           send_request_array[i + pipe_length] = Request::isend((char *) buf + (i * increment), segment, datatype, to_right,
116                     tag + i, comm);
117         }
118         Request::waitall((2 * pipe_length), send_request_array, send_status_array);
119       }
120     }
121
122     /* case: leaf ==> receive only */
123     else if (to_left == -1) {
124       for (i = 0; i < pipe_length; i++) {
125         recv_request_array[i] = Request::irecv((char *) buf + (i * increment), segment, datatype, from,
126                   tag + i, comm);
127       }
128       Request::waitall((pipe_length), recv_request_array, recv_status_array);
129     }
130
131     /* case: intermediate node with only left child ==> relay message */
132     else if (to_right == -1) {
133       for (i = 0; i < pipe_length; i++) {
134         recv_request_array[i] = Request::irecv((char *) buf + (i * increment), segment, datatype, from,
135                   tag + i, comm);
136       }
137       for (i = 0; i < pipe_length; i++) {
138         Request::wait(&recv_request_array[i], &status);
139         send_request_array[i] = Request::isend((char *) buf + (i * increment), segment, datatype, to_left,
140                   tag + i, comm);
141       }
142       Request::waitall(pipe_length, send_request_array, send_status_array);
143
144     }
145     /* case: intermediate node with both left and right children ==> relay message */
146     else {
147       for (i = 0; i < pipe_length; i++) {
148         recv_request_array[i] = Request::irecv((char *) buf + (i * increment), segment, datatype, from,
149                   tag + i, comm);
150       }
151       for (i = 0; i < pipe_length; i++) {
152         Request::wait(&recv_request_array[i], &status);
153         send_request_array[i] = Request::isend((char *) buf + (i * increment), segment, datatype, to_left,
154                   tag + i, comm);
155         send_request_array[i + pipe_length] = Request::isend((char *) buf + (i * increment), segment, datatype, to_right,
156                   tag + i, comm);
157       }
158       Request::waitall((2 * pipe_length), send_request_array, send_status_array);
159     }
160
161     delete[] send_request_array;
162     delete[] recv_request_array;
163     delete[] send_status_array;
164     delete[] recv_status_array;
165   }                             /* end pipeline */
166
167   /* when count is not divisible by block size, use default BCAST for the remainder */
168   if ((remainder != 0) && (count > segment)) {
169     XBT_WARN("MPI_bcast_NTSB use default MPI_bcast.");
170     colls::bcast((char*)buf + (pipe_length * increment), remainder, datatype, root, comm);
171   }
172
173   return MPI_SUCCESS;
174 }
175
176 }
177 }