1 #include "colls_private.h"
3 int bcast_arrival_pattern_aware_wait_segment_size_in_byte = 8192;
5 #ifndef BCAST_ARRIVAL_PATTERN_AWARE_HEADER_SIZE
6 #define BCAST_ARRIVAL_PATTERN_AWARE_HEADER_SIZE 1024
9 #ifndef BCAST_ARRIVAL_PATTERN_AWARE_MAX_NODE
10 #define BCAST_ARRIVAL_PATTERN_AWARE_MAX_NODE 128
13 /* Non-topology-specific pipelined linear-bcast function */
14 int smpi_coll_tuned_bcast_arrival_pattern_aware_wait(void *buf, int count,
15 MPI_Datatype datatype,
16 int root, MPI_Comm comm)
20 MPI_Request *send_request_array;
21 MPI_Request *recv_request_array;
22 MPI_Status *send_status_array;
23 MPI_Status *recv_status_array;
26 MPI_Status temp_status_array[BCAST_ARRIVAL_PATTERN_AWARE_MAX_NODE];
30 int tag = -COLL_TAG_BCAST;
31 int will_send[BCAST_ARRIVAL_PATTERN_AWARE_MAX_NODE];
35 int flag_array[BCAST_ARRIVAL_PATTERN_AWARE_MAX_NODE];
36 int already_sent[BCAST_ARRIVAL_PATTERN_AWARE_MAX_NODE];
38 int header_buf[BCAST_ARRIVAL_PATTERN_AWARE_HEADER_SIZE];
39 char temp_buf[BCAST_ARRIVAL_PATTERN_AWARE_MAX_NODE];
41 int max_node = BCAST_ARRIVAL_PATTERN_AWARE_MAX_NODE;
42 int header_size = BCAST_ARRIVAL_PATTERN_AWARE_HEADER_SIZE;
45 extent = smpi_datatype_get_extent(datatype);
47 /* source and destination */
52 rank = smpi_comm_rank(comm);
53 size = smpi_comm_size(comm);
56 /* segment is segment size in number of elements (not bytes) */
57 int segment = bcast_arrival_pattern_aware_wait_segment_size_in_byte / extent;
60 int pipe_length = count / segment;
62 /* use for buffer offset for sending and receiving data = segment size in byte */
63 int increment = segment * extent;
65 /* if the input size is not divisible by segment size =>
66 the small remainder will be done with native implementation */
67 int remainder = count % segment;
69 /* if root is not zero send to rank zero first
70 this can be modified to make it faster by using logical src, dst.
74 smpi_mpi_send(buf, count, datatype, 0, tag, comm);
75 } else if (rank == 0) {
76 smpi_mpi_recv(buf, count, datatype, root, tag, comm, &status);
81 /* value == 0 means root has not send data (or header) to the node yet */
82 for (i = 0; i < max_node; i++) {
86 /* when a message is smaller than a block size => no pipeline */
87 if (count <= segment) {
92 /* start pipeline bcast */
95 (MPI_Request *) xbt_malloc((size + pipe_length) * sizeof(MPI_Request));
97 (MPI_Request *) xbt_malloc((size + pipe_length) * sizeof(MPI_Request));
99 (MPI_Status *) xbt_malloc((size + pipe_length) * sizeof(MPI_Status));
101 (MPI_Status *) xbt_malloc((size + pipe_length) * sizeof(MPI_Status));
108 for (i = 0; i < BCAST_ARRIVAL_PATTERN_AWARE_MAX_NODE; i++)
110 while (sent_count < (size - 1)) {
113 /* loop k times to let more processes arrive before start sending data */
114 for (k = 0; k < 3; k++) {
115 for (i = 1; i < size; i++) {
116 if ((already_sent[i] == 0) && (will_send[i] == 0)) {
117 smpi_mpi_iprobe(i, MPI_ANY_TAG, comm, &flag_array[i],
118 &temp_status_array[i]);
119 if (flag_array[i] == 1) {
121 smpi_mpi_recv(&temp_buf[i], 1, MPI_CHAR, i, tag, comm,
131 /* recv 1-byte message */
132 for (i = 1; i < size; i++) {
134 if ((will_send[i] == 1) && (already_sent[i] == 0)) {
135 header_buf[header_index] = i;
139 /* will send in the next step */
144 /* send header followed by data */
145 if (header_index != 0) {
146 header_buf[header_index] = -1;
150 smpi_mpi_send(header_buf, header_size, MPI_INT, to, tag, comm);
152 /* send data - pipeline */
153 for (i = 0; i < pipe_length; i++) {
154 send_request_array[i] = smpi_mpi_isend((char *)buf + (i * increment), segment, datatype, to, tag, comm);
156 smpi_mpi_waitall((pipe_length), send_request_array, send_status_array);
160 /* end - send header followed by data */
161 /* randomly MPI_Send to one node */
162 /* this part has been commented out - performance-wise */
164 /* search for the first node that never received data before */
165 for (i = 0; i < size; i++) {
168 if (already_sent[i] == 0) {
173 smpi_mpi_send(header_buf, header_size, MPI_INT, to, tag, comm);
175 /* still need to chop data so that we can use the same non-root code */
176 for (j = 0; j < pipe_length; j++) {
177 smpi_mpi_send((char *)buf + (j * increment), segment, datatype, to, tag, comm);
182 } /* end - while (send_count < size-1) loop */
189 /* send 1-byte message to root */
190 smpi_mpi_send(temp_buf, 1, MPI_CHAR, 0, tag, comm);
192 /* wait for header forward when required */
193 request = smpi_mpi_irecv(header_buf, header_size, MPI_INT, MPI_ANY_SOURCE, tag, comm);
194 smpi_mpi_wait(&request, MPI_STATUS_IGNORE);
196 /* search for where it is */
198 while (rank != header_buf[myordering]) {
202 to = header_buf[myordering + 1];
203 if (myordering == 0) {
206 from = header_buf[myordering - 1];
209 /* send header when required */
211 smpi_mpi_send(header_buf, header_size, MPI_INT, to, tag, comm);
216 for (i = 0; i < pipe_length; i++) {
217 recv_request_array[i] = smpi_mpi_irecv((char *)buf + (i * increment), segment, datatype, from, tag, comm);
222 for (i = 0; i < pipe_length; i++) {
223 smpi_mpi_wait(&recv_request_array[i], MPI_STATUS_IGNORE);
224 send_request_array[i] = smpi_mpi_isend((char *)buf + (i * increment), segment, datatype, to, tag, comm);
226 smpi_mpi_waitall((pipe_length), send_request_array, send_status_array);
231 smpi_mpi_waitall((pipe_length), recv_request_array, recv_status_array);
235 free(send_request_array);
236 free(recv_request_array);
237 free(send_status_array);
238 free(recv_status_array);
241 /* when count is not divisible by block size, use default BCAST for the remainder */
242 if ((remainder != 0) && (count > segment)) {
243 XBT_WARN("MPI_bcast_arrival_pattern_aware_wait use default MPI_bcast.");
244 smpi_mpi_bcast((char *)buf + (pipe_length * increment), remainder, datatype, root, comm);