| [dc71280] | 1 | #include <mpi.h>
|
|---|
| 2 | #include <stdlib.h>
|
|---|
| 3 | #include <assert.h>
|
|---|
| 4 |
|
|---|
| 5 | void test_sum_int() {
|
|---|
| 6 | int inbuf[3] = {1, 2, 3};
|
|---|
| 7 | int inoutbuf[3] = {4, 5, 6};
|
|---|
| 8 | MPI_Reduce_local(inbuf, inoutbuf, 3, MPI_INT, MPI_SUM);
|
|---|
| 9 |
|
|---|
| 10 | assert(inoutbuf[0] == 5); // 1 + 4
|
|---|
| 11 | assert(inoutbuf[1] == 7); // 2 + 5
|
|---|
| 12 | assert(inoutbuf[2] == 9); // 3 + 6
|
|---|
| 13 | }
|
|---|
| 14 |
|
|---|
| 15 | void test_max_float() {
|
|---|
| 16 | float inbuf[2] = {1.5, 7.0};
|
|---|
| 17 | float inoutbuf[2] = {2.0, 5.0};
|
|---|
| 18 | MPI_Reduce_local(inbuf, inoutbuf, 2, MPI_FLOAT, MPI_MAX);
|
|---|
| 19 |
|
|---|
| 20 | assert(inoutbuf[0] == 2.0f); // max(1.5, 2.0)
|
|---|
| 21 | assert(inoutbuf[1] == 7.0f); // max(7.0, 5.0)
|
|---|
| 22 | }
|
|---|
| 23 |
|
|---|
| 24 | void test_product_double() {
|
|---|
| 25 | double inbuf[4] = {1.0, 2.0, 3.0, 4.0};
|
|---|
| 26 | double inoutbuf[4] = {2.0, 2.0, 2.0, 2.0};
|
|---|
| 27 | MPI_Reduce_local(inbuf, inoutbuf, 4, MPI_DOUBLE, MPI_PROD);
|
|---|
| 28 |
|
|---|
| 29 | assert(inoutbuf[0] == 2.0); // 1.0 * 2.0
|
|---|
| 30 | assert(inoutbuf[1] == 4.0); // 2.0 * 2.0
|
|---|
| 31 | assert(inoutbuf[2] == 6.0); // 3.0 * 2.0
|
|---|
| 32 | assert(inoutbuf[3] == 8.0); // 4.0 * 2.0
|
|---|
| 33 | }
|
|---|
| 34 |
|
|---|
| 35 | void test_min_int() {
|
|---|
| 36 | int inbuf[2] = {100, -50};
|
|---|
| 37 | int inoutbuf[2] = {99, -30};
|
|---|
| 38 | MPI_Reduce_local(inbuf, inoutbuf, 2, MPI_INT, MPI_MIN);
|
|---|
| 39 |
|
|---|
| 40 | assert(inoutbuf[0] == 99); // min(100, 99)
|
|---|
| 41 | assert(inoutbuf[1] == -50); // min(-50, -30)
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | int main(int argc, char **argv) {
|
|---|
| 45 | MPI_Init(&argc, &argv);
|
|---|
| 46 |
|
|---|
| 47 | test_sum_int();
|
|---|
| 48 | test_max_float();
|
|---|
| 49 | test_product_double();
|
|---|
| 50 | test_min_int();
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 | MPI_Finalize();
|
|---|
| 54 | return 0;
|
|---|
| 55 | }
|
|---|