mpi-mybroadcast.c 607 B

12345678910111213141516171819
  1. void my_bcast(void* data, int count, MPI_Datatype type,
  2. int root, MPI_Comm comm) {
  3. int my_rank;
  4. MPI_Comm_rank(comm, &my_rank);
  5. int comm_size;
  6. MPI_Comm_size(comm, &comm_size);
  7. if (my_rank == root) {
  8. // If we are the root process, send our data to every one
  9. for (int i = 0; i < comm_size; i++) {
  10. if (i != my_rank) {
  11. MPI_Send(data, count, type, i, 0, comm);
  12. }
  13. }
  14. } else {
  15. // If we are a receiver process, receive the data from root
  16. MPI_Recv(data, count, type, root, 0, comm, MPI_STATUS_IGNORE);
  17. }
  18. }