1.23
2.0
acw/focus-triggers
main
test-branch
| Line | |
|---|
| 1 | #include<mpi.h>
|
|---|
| 2 | #include<civlc.cvh>
|
|---|
| 3 | #include<string.h>
|
|---|
| 4 | #include<stdlib.h>
|
|---|
| 5 |
|
|---|
| 6 | /* A collective sum-reduction operation */
|
|---|
| 7 |
|
|---|
| 8 | int reduce_sum(void* sendbuf, void* recvbuf, MPI_Datatype datatype,
|
|---|
| 9 | int count, int root, MPI_Comm comm) {
|
|---|
| 10 | int rank;
|
|---|
| 11 | int REDUCE_TAG = 999;
|
|---|
| 12 |
|
|---|
| 13 | MPI_Comm_rank(comm, &rank);
|
|---|
| 14 | if (rank != root)
|
|---|
| 15 | MPI_Send(sendbuf, count, datatype, root, REDUCE_TAG, comm);
|
|---|
| 16 | else {
|
|---|
| 17 | int nprocs;
|
|---|
| 18 | int size;
|
|---|
| 19 | int * sum;
|
|---|
| 20 |
|
|---|
| 21 | MPI_Comm_size(comm, &nprocs);
|
|---|
| 22 | size = count * sizeof(int);
|
|---|
| 23 | memcpy(recvbuf, sendbuf, size);
|
|---|
| 24 | sum = (int *)malloc(sizeof(int) * count);
|
|---|
| 25 | for (int i = 0; i<nprocs; i++) {
|
|---|
| 26 | if (i != root){
|
|---|
| 27 | MPI_Recv(recvbuf, count, datatype, i, REDUCE_TAG, comm, MPI_STATUS_IGNORE);
|
|---|
| 28 |
|
|---|
| 29 | for (int i = 0; i < count; i++)
|
|---|
| 30 | sum[i] = sum[i] + recvbuf[i];
|
|---|
| 31 | }
|
|---|
| 32 | }
|
|---|
| 33 | memcpy(recvbuf, sum, size);
|
|---|
| 34 | }
|
|---|
| 35 | return 0;
|
|---|
| 36 | }
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.