mpi-mybroadcast.c 664 B

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