Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[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 {
25     int me, nproc;
26     twoint_t *data = NULL;
27     twoint_t mine;
28     MPI_Win win;
29
30     MTest_Init(&argc, &argv);
31
32     MPI_Comm_rank(MPI_COMM_WORLD, &me);
33     MPI_Comm_size(MPI_COMM_WORLD, &nproc);
34
35     if (me == 0) {
36         MPI_Alloc_mem(sizeof(twoint_t), MPI_INFO_NULL, &data);
37     }
38
39     MPI_Win_create(data, me == 0 ? sizeof(twoint_t) : 0, 1, MPI_INFO_NULL, MPI_COMM_WORLD, &win);
40     MPI_Win_fence(MPI_MODE_NOPRECEDE, win);
41
42     /* All processes perform MAXLOC and MINLOC operations on a 2INT on rank 0.
43      * The loc is the origin process' rank, and the value is (nproc-me).  In
44      * the case of MAXLOC, rank 0 should win and in the case of MINLOC, rank
45      * nproc-1 should win.
46      */
47
48     /** Test MAXLOC **/
49
50     if (me == 0) {
51         data->val = 0;
52         data->loc = -1;
53     }
54     MPI_Win_fence(0, win);
55
56     mine.loc = me;
57     mine.val = nproc - me;
58     MPI_Accumulate(&mine, 1, MPI_2INT, 0, 0, 1, MPI_2INT, MPI_MAXLOC, win);
59     MPI_Win_fence(0, win);
60
61     if (me == 0 && (data->loc != 0 || data->val != nproc)) {
62         errors++;
63         printf("Expected: { loc = %d, val = %d }  Actual: { loc = %d, val = %d }\n",
64                0, nproc, data->loc, data->val);
65     }
66
67     /** Test MINLOC **/
68
69     if (me == 0) {
70         data->val = nproc;
71         data->loc = -1;
72     }
73     MPI_Win_fence(0, win);
74
75     mine.loc = me;
76     mine.val = nproc - me;
77     MPI_Accumulate(&mine, 1, MPI_2INT, 0, 0, 1, MPI_2INT, MPI_MINLOC, win);
78     MPI_Win_fence(0, win);
79
80     if (me == 0 && (data->loc != nproc - 1 || data->val != 1)) {
81         errors++;
82         printf("Expected: { loc = %d, val = %d }  Actual: { loc = %d, val = %d }\n",
83                nproc - 1, 1, data->loc, data->val);
84     }
85
86     /* All processes perform MAXLOC and MINLOC operations on a 2INT on rank 0.
87      * The loc is the origin process' rank, and the value is 1.  In both cases,
88      * rank 0 should win because the values are equal and it has the lowest
89      * loc.
90      */
91
92     /** Test MAXLOC **/
93
94     if (me == 0) {
95         data->val = 0;
96         data->loc = -1;
97     }
98     MPI_Win_fence(MPI_MODE_NOSUCCEED, win);
99
100     mine.loc = me;
101     mine.val = 1;
102
103     MPI_Win_lock(MPI_LOCK_SHARED, 0, MPI_MODE_NOCHECK, win);
104     MPI_Accumulate(&mine, 1, MPI_2INT, 0, 0, 1, MPI_2INT, MPI_MAXLOC, win);
105     MPI_Win_unlock(0, win);
106
107     MPI_Barrier(MPI_COMM_WORLD);
108
109     if (me == 0 && (data->loc != 0 || data->val != 1)) {
110         errors++;
111         printf("Expected: { loc = %d, val = %d }  Actual: { loc = %d, val = %d }\n",
112                0, 1, data->loc, data->val);
113     }
114     MPI_Win_fence(MPI_MODE_NOPRECEDE, win);
115
116     /** Test MINLOC **/
117
118     if (me == 0) {
119         data->val = nproc;
120         data->loc = -1;
121     }
122     MPI_Win_fence(MPI_MODE_NOSUCCEED, win);
123
124     mine.loc = me;
125     mine.val = 1;
126
127     MPI_Win_lock(MPI_LOCK_SHARED, 0, MPI_MODE_NOCHECK, win);
128     MPI_Accumulate(&mine, 1, MPI_2INT, 0, 0, 1, MPI_2INT, MPI_MINLOC, win);
129     MPI_Win_unlock(0, win);
130
131     MPI_Barrier(MPI_COMM_WORLD);
132
133     if (me == 0 && (data->loc != 0 || data->val != 1)) {
134         errors++;
135         printf("Expected: { loc = %d, val = %d }  Actual: { loc = %d, val = %d }\n",
136                0, 1, data->loc, data->val);
137     }
138
139     MPI_Win_free(&win);
140
141     if (me == 0) {
142         MPI_Free_mem(data);
143     }
144
145     MTest_Finalize(errors);
146     MPI_Finalize();
147     return 0;
148 }