Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Finish pulling changes from mpich trunk testsuite
[simgrid.git] / teshsuite / smpi / mpich3-test / rma / acc-loc.c
1 /*
2  *  (C) 2006 by Argonne National Laboratory.
3  *      See COPYRIGHT in top-level directory.
4  *
5  *  Portions of this code were written by Intel Corporation.
6  *  Copyright (C) 2011-2012 Intel Corporation.  Intel provides this material
7  *  to Argonne National Laboratory subject to Software Grant and Corporate
8  *  Contributor License Agreement dated February 8, 2012.
9  */
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <mpi.h>
14 #include "mpitest.h"
15
16 typedef struct {
17     int val;
18     int loc;
19 } twoint_t;
20
21 static int errors = 0;
22
23 int main(int argc, char **argv) {
24     int me, nproc;
25     twoint_t *data = NULL;
26     twoint_t mine;
27     MPI_Win win;
28
29     MTest_Init(&argc, &argv);
30
31     MPI_Comm_rank(MPI_COMM_WORLD, &me);
32     MPI_Comm_size(MPI_COMM_WORLD, &nproc);
33
34     if (me == 0) {
35         MPI_Alloc_mem(sizeof(twoint_t), MPI_INFO_NULL, &data);
36     }
37
38     MPI_Win_create(data, me == 0 ? sizeof(twoint_t) : 0, 1, MPI_INFO_NULL, MPI_COMM_WORLD, &win);
39     MPI_Win_fence(MPI_MODE_NOPRECEDE, win);
40
41     /* All processes perform MAXLOC and MINLOC operations on a 2INT on rank 0.
42      * The loc is the origin process' rank, and the value is (nproc-me).  In
43      * the case of MAXLOC, rank 0 should win and in the case of MINLOC, rank
44      * nproc-1 should win.
45      */
46
47     /** Test MAXLOC **/
48
49     if (me == 0) {
50         data->val = 0;
51         data->loc = -1;
52     }
53     MPI_Win_fence(0, win);
54
55     mine.loc = me;
56     mine.val = nproc - me;
57     MPI_Accumulate(&mine, 1, MPI_2INT, 0, 0, 1, MPI_2INT, MPI_MAXLOC, win);
58     MPI_Win_fence(0, win);
59
60     if (me == 0 && (data->loc != 0 || data->val != nproc)) {
61         errors++;
62         printf("Expected: { loc = %d, val = %d }  Actual: { loc = %d, val = %d }\n",
63                0, nproc, data->loc, data->val);
64     }
65
66     /** Test MINLOC **/
67
68     if (me == 0) {
69         data->val = nproc;
70         data->loc = -1;
71     }
72     MPI_Win_fence(0, win);
73
74     mine.loc = me;
75     mine.val = nproc - me;
76     MPI_Accumulate(&mine, 1, MPI_2INT, 0, 0, 1, MPI_2INT, MPI_MINLOC, win);
77     MPI_Win_fence(0, win);
78
79     if (me == 0 && (data->loc != nproc-1 || data->val != 1)) {
80         errors++;
81         printf("Expected: { loc = %d, val = %d }  Actual: { loc = %d, val = %d }\n",
82                nproc-1, 1, data->loc, data->val);
83     }
84
85     /* All processes perform MAXLOC and MINLOC operations on a 2INT on rank 0.
86      * The loc is the origin process' rank, and the value is 1.  In both cases,
87      * rank 0 should win because the values are equal and it has the lowest
88      * loc.
89      */
90
91     /** Test MAXLOC **/
92
93     if (me == 0) {
94         data->val = 0;
95         data->loc = -1;
96     }
97     MPI_Win_fence(MPI_MODE_NOSUCCEED, win);
98
99     mine.loc = me;
100     mine.val = 1;
101
102     MPI_Win_lock(MPI_LOCK_SHARED, 0, MPI_MODE_NOCHECK, win);
103     MPI_Accumulate(&mine, 1, MPI_2INT, 0, 0, 1, MPI_2INT, MPI_MAXLOC, win);
104     MPI_Win_unlock(0, win);
105
106     MPI_Barrier(MPI_COMM_WORLD);
107
108     if (me == 0 && (data->loc != 0 || data->val != 1)) {
109         errors++;
110         printf("Expected: { loc = %d, val = %d }  Actual: { loc = %d, val = %d }\n",
111                0, 1, data->loc, data->val);
112     }
113     MPI_Win_fence(MPI_MODE_NOPRECEDE, win);
114
115     /** Test MINLOC **/
116
117     if (me == 0) {
118         data->val = nproc;
119         data->loc = -1;
120     }
121     MPI_Win_fence(MPI_MODE_NOSUCCEED, win);
122
123     mine.loc = me;
124     mine.val = 1;
125
126     MPI_Win_lock(MPI_LOCK_SHARED, 0, MPI_MODE_NOCHECK, win);
127     MPI_Accumulate(&mine, 1, MPI_2INT, 0, 0, 1, MPI_2INT, MPI_MINLOC, win);
128     MPI_Win_unlock(0, win);
129
130     MPI_Barrier(MPI_COMM_WORLD);
131
132     if (me == 0 && (data->loc != 0 || data->val != 1)) {
133         errors++;
134         printf("Expected: { loc = %d, val = %d }  Actual: { loc = %d, val = %d }\n",
135                0, 1, data->loc, data->val);
136     }
137
138     MPI_Win_free(&win);
139
140     if (me == 0) {
141         MPI_Free_mem(data);
142     }
143
144     MTest_Finalize(errors);
145     MPI_Finalize();
146     return 0;
147 }