/* William Blair * CS518 - Parallel Computing * 02/21/2018 * * HW 5 - MPI Tree Broadcast Function * * compile with 'mpicc homework5.c -o homework5 -lm' * * note you must link the math library (-lm) for this code */ #include #include #include #include // just some example data to test #define SIZE 5 int *myData=NULL; void BJ_MPI_Broadcast(int *data, int amount, int rank, int numProcs) { int i,j; int startAt; // which iteration the process will start sending from int recvFrom; // which proc we'll get the data from int sendTo; // the process to send the data to // figure out the number of levels we need to go for all processes to // receive the data int numIters = (int)ceil(log2(numProcs)); if(rank != 0) { // calculate which iteration we'll start sending out from startAt = (int)log2(rank); // calculate which process we'll recieve from recvFrom = rank - (int)pow(2, startAt); // Wait for our data MPI_Recv(data, // void *data amount, // int count MPI_INT, // MPI_DataType datatype recvFrom, // int source 0, // int tag MPI_COMM_WORLD, // MPI_Comm communicator MPI_STATUS_IGNORE // MPI_Status *status ); // send our data the appropriate amount of times based on our // calculated starting iteration for(j=startAt+1; j numProcs-1) break; MPI_Send(data, // void *data amount, // int count MPI_INT, // MPI_DataType datatype sendTo, // int destination 0, // int tag MPI_COMM_WORLD // MPI_Comm communicator ); } } else { // for the first process just loop through the number of iterations // and don't receive anything for(j=0; j