#include "mpi.h" #include /***************************************************************************** * Function: bcast_linear * Return: int * Inputs: buff: send input buffer count: number of elements to send data_type: data type of elements being sent root: source of data comm: communicator * Descrp: broadcasts using a linear scheme (root loops sending to all nodes) * Auther: LAM, but modified by Ahmad Faraj ****************************************************************************/ int bcast_flattree(void * buff, int count, MPI_Datatype data_type, int root, MPI_Comm comm) { MPI_Request * req_ptr; MPI_Request * reqs; int i, rank, num_procs; int tag = 1; int success = 0; int failure = 1; MPI_Comm_rank(comm, &rank); MPI_Comm_size(comm, &num_procs); if (rank != root) { MPIC_Recv(buff, count, data_type, root, tag, comm, MPI_STATUS_IGNORE); return success; } reqs = (MPI_Request *) malloc((num_procs - 1) * sizeof(MPI_Request)); if (!reqs) { printf("allgather-gather-bcast:66: cannot allocate memory\n"); MPI_Finalize(); exit(failure); } req_ptr = reqs; // Root sends data to all others for (i = 0; i < num_procs; i++) { if (i == rank) continue; MPIC_Isend(buff, count, data_type, i, tag, comm, req_ptr++); } // wait on all requests MPI_Waitall(num_procs - 1, reqs, MPI_STATUSES_IGNORE); free(reqs); return success; }