| 1 | #include<mpi.h>
|
|---|
| 2 | #include<civlc.cvh>
|
|---|
| 3 | #include<stdio.h>
|
|---|
| 4 | #pragma CIVL ACSL
|
|---|
| 5 |
|
|---|
| 6 | /*@
|
|---|
| 7 | @ \mpi_collective(comm, P2P):
|
|---|
| 8 | @ requires 0 <= root && root < \mpi_comm_size;
|
|---|
| 9 | @ requires \mpi_agree(root) && \mpi_agree(count * \mpi_extent(datatype));
|
|---|
| 10 | @ requires 0 < count && 0 <= count * \mpi_extent(datatype);
|
|---|
| 11 | @ requires \mpi_valid(buf, count, datatype);
|
|---|
| 12 | @ ensures \mpi_agree(\mpi_region(buf, count, datatype));
|
|---|
| 13 | @ behavior nonroot:
|
|---|
| 14 | @ assumes \mpi_comm_rank != root;
|
|---|
| 15 | @ assigns \mpi_region(buf, count, datatype);
|
|---|
| 16 | @ waitsfor root;
|
|---|
| 17 | @*/
|
|---|
| 18 | int broadcast(void * buf, int count,
|
|---|
| 19 | MPI_Datatype datatype, int root, MPI_Comm comm) {
|
|---|
| 20 | int nprocs, rank;
|
|---|
| 21 | int tag = 999;
|
|---|
| 22 |
|
|---|
| 23 | MPI_Comm_size(comm, &nprocs);
|
|---|
| 24 | MPI_Comm_rank(comm, &rank);
|
|---|
| 25 | printf("my rank:%d, root:%d\n", rank, root);
|
|---|
| 26 | if (rank == root) {
|
|---|
| 27 | for (int i = 0; i < nprocs; i++)
|
|---|
| 28 | if (i != root)
|
|---|
| 29 | MPI_Send(buf, count, datatype, i, tag, comm);
|
|---|
| 30 | } else
|
|---|
| 31 | MPI_Recv(buf, count, datatype, root, tag, comm,
|
|---|
| 32 | MPI_STATUS_IGNORE);
|
|---|
| 33 | return 0;
|
|---|
| 34 | }
|
|---|