| [a9a9973] | 1 | int MPI_Reduce(void* _sendbuf, void* _recvbuf, int _count, MPI_Datatype _datatype,
|
|---|
| 2 | MPI_Op _op, int _root, MPI_Comm _comm) {
|
|---|
| 3 | int _i;
|
|---|
| 4 | int _j;
|
|---|
| 5 | double _tempBuffer[_count];
|
|---|
| 6 | int _rank;
|
|---|
| 7 | int _nprocs;
|
|---|
| 8 |
|
|---|
| 9 | #pragma TASS assert MPIX_State == MPIX_INITIALIZED, "Attempt to call MPI_Reduce when MPI is not initialized";
|
|---|
| 10 | #pragma TASS assert _op == MPI_SUM;
|
|---|
| 11 | #pragma TASS assert _datatype == MPI_DOUBLE;
|
|---|
| 12 | MPI_Comm_rank(MPI_COMM_WORLD, &_rank);
|
|---|
| 13 | MPI_Comm_size(MPI_COMM_WORLD, &_nprocs);
|
|---|
| 14 | if (_rank == _root) {
|
|---|
| 15 | for (_j=0; _j<_count; _j++) _tempBuffer[_j] = ((double*)_sendbuf)[_j];
|
|---|
| 16 | for (_i=0; _i<_nprocs; _i++) {
|
|---|
| 17 | if (_i != _root) {
|
|---|
| 18 | MPI_Recv(_recvbuf, _count, _datatype, _i, MPIX_REDUCE_TAG,
|
|---|
| 19 | _comm, MPI_STATUS_IGNORE);
|
|---|
| 20 | for(_j=0; _j<_count; _j++) _tempBuffer[_j] += ((double*)_recvbuf)[_j];
|
|---|
| 21 | }
|
|---|
| 22 | }
|
|---|
| 23 | for (_j=0; _j<_count; _j++) ((double*)_recvbuf)[_j]=_tempBuffer[_j];
|
|---|
| 24 | } else {
|
|---|
| 25 | MPI_Send(_sendbuf, _count, _datatype, _root, MPIX_REDUCE_TAG, _comm);
|
|---|
| 26 | }
|
|---|
| 27 | return 0;
|
|---|
| 28 | }
|
|---|
| 29 |
|
|---|
| 30 | /* For now: just doubles and sum */
|
|---|
| 31 | int MPI_Allreduce(void* _sendbuf, void* _recvbuf, int _count, MPI_Datatype _datatype,
|
|---|
| 32 | MPI_Op _op, MPI_Comm _comm) {
|
|---|
| 33 | int _i;
|
|---|
| 34 | int _j;
|
|---|
| 35 | double _tempBuffer[_count];
|
|---|
| 36 |
|
|---|
| 37 | #pragma TASS assert MPIX_State == MPIX_INITIALIZED, "Attempt to call MPI_Allreduce when MPI is not initialized";
|
|---|
| 38 | #pragma TASS assert _op == MPI_SUM;
|
|---|
| 39 | #pragma TASS assert _datatype == MPI_DOUBLE;
|
|---|
| 40 | if (PID == 0) {
|
|---|
| 41 | for (_j=0; _j<_count; _j++) _tempBuffer[_j] = ((double*)_sendbuf)[_j];
|
|---|
| 42 | for (_i=1; _i<NPROCS; _i++) {
|
|---|
| 43 | MPI_Recv(_recvbuf, _count, _datatype, _i, MPIX_ALLREDUCE_TAG,
|
|---|
| 44 | _comm, MPI_STATUS_IGNORE);
|
|---|
| 45 | for(_j=0; _j<_count; _j++) _tempBuffer[_j] += ((double*)_recvbuf)[_j];
|
|---|
| 46 | }
|
|---|
| 47 | for (_j=0; _j<_count; _j++) ((double*)_recvbuf)[_j]=_tempBuffer[_j];
|
|---|
| 48 | for (_i=1; _i<NPROCS; _i++) {
|
|---|
| 49 | MPI_Send(_recvbuf, _count, _datatype, _i, MPIX_ALLREDUCE_TAG, _comm);
|
|---|
| 50 | }
|
|---|
| 51 | } else {
|
|---|
| 52 | MPI_Send(_sendbuf, _count, _datatype, 0, MPIX_ALLREDUCE_TAG, _comm);
|
|---|
| 53 | MPI_Recv(_recvbuf, _count, _datatype, 0, MPIX_ALLREDUCE_TAG, _comm,
|
|---|
| 54 | MPI_STATUS_IGNORE);
|
|---|
| 55 | }
|
|---|
| 56 | return 0;
|
|---|
| 57 | }
|
|---|