Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update RMA tests
[simgrid.git] / teshsuite / smpi / mpich3-test / rma / strided_acc_subarray.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *  (C) 2001 by Argonne National Laboratory.
4  *      See COPYRIGHT in top-level directory.
5  */
6
7 /* One-Sided MPI 2-D Strided Accumulate Test
8  *
9  * Author: James Dinan <dinan@mcs.anl.gov>
10  * Date  : December, 2010
11  *
12  * This code performs N accumulates into a 2d patch of a shared array.  The
13  * array has dimensions [X, Y] and the subarray has dimensions [SUB_X, SUB_Y]
14  * and begins at index [0, 0].  The input and output buffers are specified
15  * using an MPI subarray type.
16  */
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <math.h>
21 #include <mpi.h>
22 #include "mpitest.h"
23 #include "squelch.h"
24
25 #define XDIM 1024
26 #define YDIM 1024
27 #define SUB_XDIM 512
28 #define SUB_YDIM 512
29 #define ITERATIONS 10
30
31 int main(int argc, char **argv)
32 {
33     int i, j, rank, nranks, peer, bufsize, errors;
34     double *win_buf, *src_buf;
35     MPI_Win buf_win;
36
37     MTest_Init(&argc, &argv);
38
39     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
40     MPI_Comm_size(MPI_COMM_WORLD, &nranks);
41
42     bufsize = XDIM * YDIM * sizeof(double);
43     MPI_Alloc_mem(bufsize, MPI_INFO_NULL, &win_buf);
44     MPI_Alloc_mem(bufsize, MPI_INFO_NULL, &src_buf);
45
46     for (i = 0; i < XDIM * YDIM; i++) {
47         *(win_buf + i) = -1.0;
48         *(src_buf + i) = 1.0 + rank;
49     }
50
51     MPI_Win_create(win_buf, bufsize, 1, MPI_INFO_NULL, MPI_COMM_WORLD, &buf_win);
52
53     peer = (rank + 1) % nranks;
54
55     /* Perform ITERATIONS strided accumulate operations */
56
57     for (i = 0; i < ITERATIONS; i++) {
58         int ndims = 2;
59         int src_arr_sizes[2] = { XDIM, YDIM };
60         int src_arr_subsizes[2] = { SUB_XDIM, SUB_YDIM };
61         int src_arr_starts[2] = { 0, 0 };
62         int dst_arr_sizes[2] = { XDIM, YDIM };
63         int dst_arr_subsizes[2] = { SUB_XDIM, SUB_YDIM };
64         int dst_arr_starts[2] = { 0, 0 };
65         MPI_Datatype src_type, dst_type;
66
67         MPI_Type_create_subarray(ndims, src_arr_sizes, src_arr_subsizes, src_arr_starts,
68                                  MPI_ORDER_C, MPI_DOUBLE, &src_type);
69
70         MPI_Type_create_subarray(ndims, dst_arr_sizes, dst_arr_subsizes, dst_arr_starts,
71                                  MPI_ORDER_C, MPI_DOUBLE, &dst_type);
72
73         MPI_Type_commit(&src_type);
74         MPI_Type_commit(&dst_type);
75
76         MPI_Win_lock(MPI_LOCK_EXCLUSIVE, peer, 0, buf_win);
77
78         MPI_Accumulate(src_buf, 1, src_type, peer, 0, 1, dst_type, MPI_SUM, buf_win);
79
80         MPI_Win_unlock(peer, buf_win);
81
82         MPI_Type_free(&src_type);
83         MPI_Type_free(&dst_type);
84     }
85
86     MPI_Barrier(MPI_COMM_WORLD);
87
88     /* Verify that the results are correct */
89
90     MPI_Win_lock(MPI_LOCK_EXCLUSIVE, rank, 0, buf_win);
91     errors = 0;
92     for (i = 0; i < SUB_XDIM; i++) {
93         for (j = 0; j < SUB_YDIM; j++) {
94             const double actual = *(win_buf + i + j * XDIM);
95             const double expected = -1.0 + (1.0 + ((rank + nranks - 1) % nranks)) * (ITERATIONS);
96             if (fabs(actual - expected) > 1.0e-10) {
97                 SQUELCH(printf("%d: Data validation failed at [%d, %d] expected=%f actual=%f\n",
98                                rank, j, i, expected, actual););
99                 errors++;
100                 fflush(stdout);
101             }
102         }
103     }
104     for (i = SUB_XDIM; i < XDIM; i++) {
105         for (j = 0; j < SUB_YDIM; j++) {
106             const double actual = *(win_buf + i + j * XDIM);
107             const double expected = -1.0;
108             if (fabs(actual - expected) > 1.0e-10) {
109                 SQUELCH(printf("%d: Data validation failed at [%d, %d] expected=%f actual=%f\n",
110                                rank, j, i, expected, actual););
111                 errors++;
112                 fflush(stdout);
113             }
114         }
115     }
116     for (i = 0; i < XDIM; i++) {
117         for (j = SUB_YDIM; j < YDIM; j++) {
118             const double actual = *(win_buf + i + j * XDIM);
119             const double expected = -1.0;
120             if (fabs(actual - expected) > 1.0e-10) {
121                 SQUELCH(printf("%d: Data validation failed at [%d, %d] expected=%f actual=%f\n",
122                                rank, j, i, expected, actual););
123                 errors++;
124                 fflush(stdout);
125             }
126         }
127     }
128     MPI_Win_unlock(rank, buf_win);
129
130     MPI_Win_free(&buf_win);
131     MPI_Free_mem(win_buf);
132     MPI_Free_mem(src_buf);
133
134     MTest_Finalize(errors);
135     MPI_Finalize();
136     return MTestReturnValue(errors);
137 }