Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Finish pulling changes from mpich trunk testsuite
[simgrid.git] / teshsuite / smpi / mpich3-test / rma / strided_getacc_indexed_shared.c
1 /* -*- Mode: C; c-basic-offset:4 ; -*- */
2 /*
3  *  (C) 2011 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  : November, 2012
11  *
12  * This code performs N strided put operations followed by get operations into
13  * a 2d patch of a shared array.  The array has dimensions [X, Y] and the
14  * subarray has dimensions [SUB_X, SUB_Y] and begins at index [0, 0].  The
15  * input and output buffers are specified using an MPI indexed 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 8
26 #define YDIM 1024
27 #define SUB_XDIM 1
28 #define SUB_YDIM 2
29 #define ITERATIONS 10
30
31 int main(int argc, char **argv) {
32     int rank, nranks, rank_world, nranks_world;
33     int i, j, peer, bufsize, errors;
34     double *win_buf, *src_buf, *dst_buf;
35     MPI_Win buf_win;
36     MPI_Comm shr_comm;
37
38     MTest_Init(&argc, &argv);
39
40     MPI_Comm_rank(MPI_COMM_WORLD, &rank_world);
41     MPI_Comm_size(MPI_COMM_WORLD, &nranks_world);
42
43     MPI_Comm_split_type(MPI_COMM_WORLD, MPI_COMM_TYPE_SHARED, rank, MPI_INFO_NULL, &shr_comm);
44
45     MPI_Comm_rank(shr_comm, &rank);
46     MPI_Comm_size(shr_comm, &nranks);
47
48     bufsize = XDIM * YDIM * sizeof(double);
49     MPI_Alloc_mem(bufsize, MPI_INFO_NULL, &src_buf);
50     MPI_Alloc_mem(bufsize, MPI_INFO_NULL, &dst_buf);
51
52     MPI_Win_allocate_shared(bufsize, 1, MPI_INFO_NULL, shr_comm, &win_buf, &buf_win);
53
54     MPI_Win_fence(0, buf_win);
55
56     for (i = 0; i < XDIM*YDIM; i++) {
57         *(win_buf + i) = -1.0;
58         *(src_buf + i) =  1.0 + rank;
59     }
60
61     MPI_Win_fence(0, buf_win);
62
63     peer = (rank+1) % nranks;
64
65     /* Perform ITERATIONS strided accumulate operations */
66
67     for (i = 0; i < ITERATIONS; i++) {
68       int idx_rem[SUB_YDIM];
69       int blk_len[SUB_YDIM];
70       MPI_Datatype src_type, dst_type;
71
72       for (j = 0; j < SUB_YDIM; j++) {
73         idx_rem[j] = j*XDIM;
74         blk_len[j] = SUB_XDIM;
75       }
76
77       MPI_Type_indexed(SUB_YDIM, blk_len, idx_rem, MPI_DOUBLE, &src_type);
78       MPI_Type_indexed(SUB_YDIM, blk_len, idx_rem, MPI_DOUBLE, &dst_type);
79
80       MPI_Type_commit(&src_type);
81       MPI_Type_commit(&dst_type);
82
83       /* PUT */
84       MPI_Win_lock(MPI_LOCK_EXCLUSIVE, peer, 0, buf_win);
85       MPI_Get_accumulate(src_buf, 1, src_type, dst_buf, 1, src_type, peer, 0,
86                           1, dst_type, MPI_REPLACE, buf_win);
87       MPI_Win_unlock(peer, buf_win);
88
89       /* GET */
90       MPI_Win_lock(MPI_LOCK_EXCLUSIVE, peer, 0, buf_win);
91       MPI_Get_accumulate(src_buf, 1, src_type, dst_buf, 1, src_type, peer, 0,
92                           1, dst_type, MPI_NO_OP, buf_win);
93       MPI_Win_unlock(peer, buf_win);
94
95       MPI_Type_free(&src_type);
96       MPI_Type_free(&dst_type);
97     }
98
99     MPI_Barrier(MPI_COMM_WORLD);
100
101     /* Verify that the results are correct */
102
103     MPI_Win_lock(MPI_LOCK_EXCLUSIVE, rank, 0, buf_win);
104     errors = 0;
105     for (i = 0; i < SUB_XDIM; i++) {
106       for (j = 0; j < SUB_YDIM; j++) {
107         const double actual   = *(win_buf + i + j*XDIM);
108         const double expected = (1.0 + ((rank+nranks-1)%nranks));
109         if (fabs(actual - expected) > 1.0e-10) {
110           SQUELCH( printf("%d: Data validation failed at [%d, %d] expected=%f actual=%f\n",
111               rank, j, i, expected, actual); );
112           errors++;
113           fflush(stdout);
114         }
115       }
116     }
117     for (i = SUB_XDIM; i < XDIM; i++) {
118       for (j = 0; j < SUB_YDIM; j++) {
119         const double actual   = *(win_buf + i + j*XDIM);
120         const double expected = -1.0;
121         if (fabs(actual - expected) > 1.0e-10) {
122           SQUELCH( printf("%d: Data validation failed at [%d, %d] expected=%f actual=%f\n",
123               rank, j, i, expected, actual); );
124           errors++;
125           fflush(stdout);
126         }
127       }
128     }
129     for (i = 0; i < XDIM; i++) {
130       for (j = SUB_YDIM; j < YDIM; j++) {
131         const double actual   = *(win_buf + i + j*XDIM);
132         const double expected = -1.0;
133         if (fabs(actual - expected) > 1.0e-10) {
134           SQUELCH( printf("%d: Data validation failed at [%d, %d] expected=%f actual=%f\n",
135               rank, j, i, expected, actual); );
136           errors++;
137           fflush(stdout);
138         }
139       }
140     }
141     MPI_Win_unlock(rank, buf_win);
142
143     MPI_Win_free(&buf_win);
144     MPI_Free_mem(src_buf);
145     MPI_Free_mem(dst_buf);
146     MPI_Comm_free(&shr_comm);
147
148     MTest_Finalize( errors );
149     MPI_Finalize();
150     return MTestReturnValue( errors );
151 }