1 #include "colls_private.h"
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_pattern_aware(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 extent = smpi_datatype_get_extent(datatype);
42 rank = smpi_comm_rank(MPI_COMM_WORLD);
43 size = smpi_comm_size(MPI_COMM_WORLD);
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 smpi_mpi_send(buf, count, datatype, 0, tag, comm);
65 } else if (rank == 0) {
66 smpi_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++) {
75 /* when a message is smaller than a block size => no pipeline */
76 if (count <= segment) {
80 while (sent_count < (size - 1)) {
81 for (i = 1; i < size; i++) {
82 smpi_mpi_iprobe(i, MPI_ANY_TAG, MPI_COMM_WORLD, &flag_array[i],
87 /* recv 1-byte message */
88 for (i = 1; i < size; i++) {
91 if ((flag_array[i] == 1) && (already_sent[i] == 0)) {
92 smpi_mpi_recv(temp_buf, 1, MPI_CHAR, i, tag, MPI_COMM_WORLD, &status);
93 header_buf[header_index] = i;
97 /* will send in the next step */
102 /* send header followed by data */
103 if (header_index != 0) {
104 header_buf[header_index] = -1;
106 smpi_mpi_send(header_buf, HEADER_SIZE, MPI_INT, to, tag, comm);
107 smpi_mpi_send(buf, count, datatype, to, tag, comm);
110 /* randomly MPI_Send to one */
112 /* search for the first node that never received data before */
113 for (i = 1; i < size; i++) {
114 if (already_sent[i] == 0) {
117 smpi_mpi_send(header_buf, HEADER_SIZE, MPI_INT, i, tag, comm);
118 smpi_mpi_send(buf, count, datatype, i, tag, comm);
133 /* send 1-byte message to root */
134 smpi_mpi_send(temp_buf, 1, MPI_CHAR, 0, tag, comm);
136 /* wait for header and data, forward when required */
137 smpi_mpi_recv(header_buf, HEADER_SIZE, MPI_INT, MPI_ANY_SOURCE, tag, comm,
139 smpi_mpi_recv(buf, count, datatype, MPI_ANY_SOURCE, tag, comm, &status);
141 /* search for where it is */
143 while (rank != header_buf[myordering]) {
147 /* send header followed by data */
148 if (header_buf[myordering + 1] != -1) {
149 smpi_mpi_send(header_buf, HEADER_SIZE, MPI_INT, header_buf[myordering + 1],
151 smpi_mpi_send(buf, count, datatype, header_buf[myordering + 1], tag, comm);
158 (MPI_Request *) xbt_malloc((size + pipe_length) * sizeof(MPI_Request));
160 (MPI_Request *) xbt_malloc((size + pipe_length) * sizeof(MPI_Request));
162 (MPI_Status *) xbt_malloc((size + pipe_length) * sizeof(MPI_Status));
164 (MPI_Status *) xbt_malloc((size + pipe_length) * sizeof(MPI_Status));
167 //double start2 = MPI_Wtime();
170 while (sent_count < (size - 1)) {
172 //start = MPI_Wtime();
173 for (i = 1; i < size; i++) {
174 smpi_mpi_iprobe(i, MPI_ANY_TAG, MPI_COMM_WORLD, &flag_array[i],
175 &temp_status_array[i]);
177 //total = MPI_Wtime() - start;
179 //printf("Iprobe time = %.2f\n",total);
183 /* recv 1-byte message */
184 for (i = 1; i < size; i++) {
186 if ((flag_array[i] == 1) && (already_sent[i] == 0)) {
187 smpi_mpi_recv(&temp_buf[i], 1, MPI_CHAR, i, tag, MPI_COMM_WORLD,
189 header_buf[header_index] = i;
193 /* will send in the next step */
197 //total = MPI_Wtime() - start;
199 //printf("Recv 1-byte time = %.2f\n",total);
202 if (header_index != 0) {
203 printf("header index = %d node = ",header_index);
204 for (i=0;i<header_index;i++) {
205 printf("%d ",header_buf[i]);
211 /* send header followed by data */
212 if (header_index != 0) {
213 header_buf[header_index] = -1;
216 //start = MPI_Wtime();
219 smpi_mpi_send(header_buf, HEADER_SIZE, MPI_INT, to, tag, comm);
221 //total = MPI_Wtime() - start;
223 //printf("\tSend header to %d time = %.2f\n",to,total);
225 //start = MPI_Wtime();
227 /* send data - non-pipeline case */
230 //if (header_index == 1) {
231 smpi_mpi_send(buf, count, datatype, to, tag, comm);
235 /* send data - pipeline */
237 for (i = 0; i < pipe_length; i++) {
238 smpi_mpi_send((char *)buf + (i * increment), segment, datatype, to, tag, comm);
240 //smpi_mpi_waitall((pipe_length), send_request_array, send_status_array);
242 //total = MPI_Wtime() - start;
244 //printf("\tSend data to %d time = %.2f\n",to,total);
250 /* randomly MPI_Send to one node */
252 /* search for the first node that never received data before */
253 for (i = 1; i < size; i++) {
254 if (already_sent[i] == 0) {
259 //start = MPI_Wtime();
260 smpi_mpi_send(header_buf, HEADER_SIZE, MPI_INT, to, tag, comm);
262 /* still need to chop data so that we can use the same non-root code */
263 for (j = 0; j < pipe_length; j++) {
264 smpi_mpi_send((char *)buf + (j * increment), segment, datatype, to, tag,
268 //smpi_mpi_send(buf,count,datatype,to,tag,comm);
269 //smpi_mpi_wait(&request,MPI_STATUS_IGNORE);
271 //total = MPI_Wtime() - start;
273 //printf("SEND TO SINGLE node %d time = %.2f\n",i,total);
285 //total = MPI_Wtime() - start2;
287 //printf("Node zero iter = %d time = %.2f\n",iteration,total);
293 /* send 1-byte message to root */
294 smpi_mpi_send(temp_buf, 1, MPI_CHAR, 0, tag, comm);
296 /* wait for header forward when required */
297 request = smpi_mpi_irecv(header_buf, HEADER_SIZE, MPI_INT, MPI_ANY_SOURCE, tag, comm);
298 smpi_mpi_wait(&request, MPI_STATUS_IGNORE);
300 /* search for where it is */
302 while (rank != header_buf[myordering]) {
306 /* send header when required */
307 if (header_buf[myordering + 1] != -1) {
308 smpi_mpi_send(header_buf, HEADER_SIZE, MPI_INT, header_buf[myordering + 1],
315 //if (header_buf[1] == -1) {
316 request = smpi_mpi_irecv(buf, count, datatype, 0, tag, comm);
317 smpi_mpi_wait(&request, MPI_STATUS_IGNORE);
318 //printf("\t\tnode %d ordering = %d receive data from root\n",rank,myordering);
320 for (i = 0; i < pipe_length; i++) {
321 recv_request_array[i] = smpi_mpi_irecv((char *)buf + (i * increment), segment, datatype, MPI_ANY_SOURCE,
327 if (header_buf[myordering + 1] != -1) {
328 for (i = 0; i < pipe_length; i++) {
329 smpi_mpi_wait(&recv_request_array[i], MPI_STATUS_IGNORE);
330 send_request_array[i] = smpi_mpi_isend((char *)buf + (i * increment), segment, datatype,
331 header_buf[myordering + 1], tag, comm);
333 smpi_mpi_waitall((pipe_length), send_request_array, send_status_array);
338 free(send_request_array);
339 free(recv_request_array);
340 free(send_status_array);
341 free(recv_status_array);
344 /* when count is not divisible by block size, use default BCAST for the remainder */
345 if ((remainder != 0) && (count > segment)) {
346 XBT_WARN("MPI_bcast_arrival_pattern_aware use default MPI_bcast.");
347 smpi_mpi_bcast((char *)buf + (pipe_length * increment), remainder, datatype, root, comm);