3 static int bcast_NTSL_segment_size_in_byte = 8192;
5 #define HEADER_SIZE 1024
8 /* Non-topology-specific pipelined linear-bcast function */
9 int smpi_coll_tuned_bcast_arrival_nb(void *buf, int count,
10 MPI_Datatype datatype, int root,
16 MPI_Request *send_request_array;
17 MPI_Request *recv_request_array;
18 MPI_Status *send_status_array;
19 MPI_Status *recv_status_array;
21 MPI_Status temp_status_array[MAX_NODE];
28 int flag_array[MAX_NODE];
29 int already_sent[MAX_NODE];
31 int header_buf[HEADER_SIZE];
32 char temp_buf[MAX_NODE];
35 MPI_Type_extent(datatype, &extent);
42 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
43 MPI_Comm_size(MPI_COMM_WORLD, &size);
46 /* segment is segment size in number of elements (not bytes) */
47 int segment = bcast_NTSL_segment_size_in_byte / extent;
50 int pipe_length = count / segment;
52 /* use for buffer offset for sending and receiving data = segment size in byte */
53 int increment = segment * extent;
55 /* if the input size is not divisible by segment size =>
56 the small remainder will be done with native implementation */
57 int remainder = count % segment;
59 /* if root is not zero send to rank zero first
60 this can be modified to make it faster by using logical src, dst.
64 MPI_Send(buf, count, datatype, 0, tag, comm);
65 } else if (rank == 0) {
66 MPI_Recv(buf, count, datatype, root, tag, comm, &status);
70 /* value == 0 means root has not send data (or header) to the node yet */
71 for (i = 0; i < MAX_NODE; i++) {
76 /* when a message is smaller than a block size => no pipeline */
77 if (count <= segment) {
81 while (sent_count < (size - 1)) {
83 // for (j=0;j<1000;j++) {
84 for (i = 1; i < size; i++) {
85 if (already_sent[i] == 0)
86 MPI_Iprobe(i, MPI_ANY_TAG, MPI_COMM_WORLD, &flag_array[i],
92 /* recv 1-byte message */
93 for (i = 1; i < size; i++) {
96 if ((flag_array[i] == 1) && (already_sent[i] == 0)) {
97 MPI_Recv(temp_buf, 1, MPI_CHAR, i, tag, MPI_COMM_WORLD, &status);
98 header_buf[header_index] = i;
102 /* will send in the next step */
107 /* send header followed by data */
108 if (header_index != 0) {
109 header_buf[header_index] = -1;
111 MPI_Send(header_buf, HEADER_SIZE, MPI_INT, to, tag, comm);
112 MPI_Send(buf, count, datatype, to, tag, comm);
115 /* randomly MPI_Send to one */
117 /* search for the first node that never received data before */
118 for (i = 1; i < size; i++) {
119 if (already_sent[i] == 0) {
122 MPI_Send(header_buf, HEADER_SIZE, MPI_INT, i, tag, comm);
123 MPI_Send(buf, count, datatype, i, tag, comm);
138 /* send 1-byte message to root */
139 MPI_Send(temp_buf, 1, MPI_CHAR, 0, tag, comm);
141 /* wait for header and data, forward when required */
142 MPI_Recv(header_buf, HEADER_SIZE, MPI_INT, MPI_ANY_SOURCE, tag, comm,
144 MPI_Recv(buf, count, datatype, MPI_ANY_SOURCE, tag, comm, &status);
146 /* search for where it is */
148 while (rank != header_buf[myordering]) {
152 /* send header followed by data */
153 if (header_buf[myordering + 1] != -1) {
154 MPI_Send(header_buf, HEADER_SIZE, MPI_INT, header_buf[myordering + 1],
156 MPI_Send(buf, count, datatype, header_buf[myordering + 1], tag, comm);
163 (MPI_Request *) malloc((size + pipe_length) * sizeof(MPI_Request));
165 (MPI_Request *) malloc((size + pipe_length) * sizeof(MPI_Request));
167 (MPI_Status *) malloc((size + pipe_length) * sizeof(MPI_Status));
169 (MPI_Status *) malloc((size + pipe_length) * sizeof(MPI_Status));
176 for (i = 0; i < 1000; i++)
178 while (sent_count < (size - 1)) {
180 //start = MPI_Wtime();
183 for (k = 0; k < 3; k++) {
184 for (i = 1; i < size; i++) {
185 if ((already_sent[i] == 0) && (will_send[i] == 0)) {
186 MPI_Iprobe(i, MPI_ANY_TAG, MPI_COMM_WORLD, &flag_array[i],
187 &temp_status_array[i]);
188 if (flag_array[i] == 1) {
190 MPI_Recv(&temp_buf[i], 1, MPI_CHAR, i, tag, MPI_COMM_WORLD,
198 //total = MPI_Wtime() - start;
200 //printf("Iprobe time = %.2f\n",total);
203 //start = MPI_Wtime();
204 /* recv 1-byte message */
205 for (i = 1; i < size; i++) {
207 if ((will_send[i] == 1) && (already_sent[i] == 0)) {
208 header_buf[header_index] = i;
212 /* will send in the next step */
216 //printf("sent_count = %d\n",sent_count);
219 //total = MPI_Wtime() - start;
221 //printf("Recv 1-byte time = %.2f\n",total);
224 if (header_index != 0) {
225 printf("header index = %d node = ",header_index);
226 for (i=0;i<header_index;i++) {
227 printf("%d ",header_buf[i]);
233 /* send header followed by data */
234 if (header_index != 0) {
235 header_buf[header_index] = -1;
238 //start = MPI_Wtime();
241 MPI_Send(header_buf, HEADER_SIZE, MPI_INT, to, tag, comm);
243 //total = MPI_Wtime() - start;
245 //printf("\tSend header to %d time = %.2f\n",to,total);
247 //start = MPI_Wtime();
249 /* send data - non-pipeline case */
252 //if (header_index == 1) {
253 MPI_Send(buf, count, datatype, to, tag, comm);
257 /* send data - pipeline */
259 for (i = 0; i < pipe_length; i++) {
260 MPI_Send((char *)buf + (i * increment), segment, datatype, to, tag, comm);
262 //MPI_Waitall((pipe_length), send_request_array, send_status_array);
264 //total = MPI_Wtime() - start;
266 //printf("\tSend data to %d time = %.2f\n",to,total);
272 /* randomly MPI_Send to one node */
274 /* search for the first node that never received data before */
275 for (i = 1; i < size; i++) {
276 if (already_sent[i] == 0) {
281 //start = MPI_Wtime();
282 MPI_Send(header_buf, HEADER_SIZE, MPI_INT, to, tag, comm);
284 /* still need to chop data so that we can use the same non-root code */
285 for (j = 0; j < pipe_length; j++) {
286 MPI_Send((char *)buf + (j * increment), segment, datatype, to, tag,
290 //MPI_Send(buf,count,datatype,to,tag,comm);
291 //MPI_Wait(&request,MPI_STATUS_IGNORE);
293 //total = MPI_Wtime() - start;
295 //printf("SEND TO SINGLE node %d time = %.2f\n",i,total);
307 //total = MPI_Wtime() - start2;
309 //printf("Node zero iter = %d time = %.2f\n",iteration,total);
311 /* probe before exit in case there are messages to recv */
312 for (i = 1; i < size; i++) {
313 MPI_Iprobe(i, MPI_ANY_TAG, MPI_COMM_WORLD, &flag_array[i],
314 &temp_status_array[i]);
315 if (flag_array[i] == 1)
316 MPI_Recv(&temp_buf[i], 1, MPI_CHAR, i, tag, MPI_COMM_WORLD, &status);
324 /* if root already send a message to this node, don't send one-byte message */
325 MPI_Iprobe(0, MPI_ANY_TAG, MPI_COMM_WORLD, &flag_array[0], &status);
327 /* send 1-byte message to root */
328 if (flag_array[0] == 0)
329 MPI_Send(temp_buf, 1, MPI_CHAR, 0, tag, comm);
331 /* wait for header forward when required */
332 MPI_Irecv(header_buf, HEADER_SIZE, MPI_INT, MPI_ANY_SOURCE, tag, comm,
334 MPI_Wait(&request, MPI_STATUS_IGNORE);
336 /* search for where it is */
338 while (rank != header_buf[myordering]) {
342 /* send header when required */
343 if (header_buf[myordering + 1] != -1) {
344 MPI_Send(header_buf, HEADER_SIZE, MPI_INT, header_buf[myordering + 1],
351 //if (header_buf[1] == -1) {
352 MPI_Irecv(buf, count, datatype, 0, tag, comm, &request);
353 MPI_Wait(&request, MPI_STATUS_IGNORE);
354 //printf("\t\tnode %d ordering = %d receive data from root\n",rank,myordering);
356 for (i = 0; i < pipe_length; i++) {
357 MPI_Irecv((char *)buf + (i * increment), segment, datatype, MPI_ANY_SOURCE,
358 tag, comm, &recv_request_array[i]);
363 if (header_buf[myordering + 1] != -1) {
364 for (i = 0; i < pipe_length; i++) {
365 MPI_Wait(&recv_request_array[i], MPI_STATUS_IGNORE);
366 MPI_Isend((char *)buf + (i * increment), segment, datatype,
367 header_buf[myordering + 1], tag, comm,
368 &send_request_array[i]);
370 MPI_Waitall((pipe_length), send_request_array, send_status_array);
375 free(send_request_array);
376 free(recv_request_array);
377 free(send_status_array);
378 free(recv_status_array);
381 /* when count is not divisible by block size, use default BCAST for the remainder */
382 if ((remainder != 0) && (count > segment)) {
383 MPI_Bcast((char *)buf + (pipe_length * increment), remainder, datatype, root, comm);