Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Reduce the size of partial shared malloc tests.
[simgrid.git] / teshsuite / smpi / mpich3-test / rma / pscw_ordering.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 /* This test checks an oddball case for generalized active target
8  * synchronization where the start occurs before the post.  Since start can
9  * block until the corresponding post, the group passed to start must be
10  * disjoint from the group passed to post and processes must avoid a circular
11  * wait.  Here, odd/even groups are used to accomplish this and the even group
12  * reverses its start/post calls.
13  */
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <mpi.h>
18 #include "mpitest.h"
19 #include "squelch.h"
20
21 int main(int argc, char **argv)
22 {
23     int i, rank, nproc, errors = 0;
24
25     int *win_buf;
26     MPI_Win win;
27
28     int odd_nproc, even_nproc;
29     int *odd_ranks, *even_ranks;
30     MPI_Group odd_group, even_group, world_group;
31
32     MTest_Init(&argc, &argv);
33
34     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
35     MPI_Comm_size(MPI_COMM_WORLD, &nproc);
36
37     if (nproc < 2) {
38         if (rank == 0)
39             printf("Error: this test requires two or more processes\n");
40         MPI_Abort(MPI_COMM_WORLD, 100);
41     }
42
43     /* Set up odd/even groups and buffers */
44
45     odd_nproc = nproc / 2;
46     even_nproc = nproc / 2 + ((nproc % 2 == 0) ? 0 : 1);
47
48     odd_ranks = malloc(sizeof(int) * odd_nproc);
49     even_ranks = malloc(sizeof(int) * even_nproc);
50
51     for (i = 0; i < even_nproc; i++)
52         even_ranks[i] = i * 2;
53
54     for (i = 0; i < odd_nproc; i++)
55         odd_ranks[i] = i * 2 + 1;
56
57     MPI_Comm_group(MPI_COMM_WORLD, &world_group);
58     MPI_Group_incl(world_group, odd_nproc, odd_ranks, &odd_group);
59     MPI_Group_incl(world_group, even_nproc, even_ranks, &even_group);
60
61     /* Create the window */
62
63 #ifdef USE_WIN_ALLOCATE
64     MPI_Win_allocate(nproc * sizeof(int), sizeof(int), MPI_INFO_NULL,
65                      MPI_COMM_WORLD, &win_buf, &win);
66 #else
67     MPI_Alloc_mem(nproc * sizeof(int), MPI_INFO_NULL, &win_buf);
68     MPI_Win_create(win_buf, nproc * sizeof(int), sizeof(int), MPI_INFO_NULL, MPI_COMM_WORLD, &win);
69 #endif
70     MPI_Win_lock(MPI_LOCK_SHARED, rank, 0, win);
71     for (i = 0; i < nproc; i++)
72         win_buf[i] = -1;
73     MPI_Win_unlock(rank, win);
74
75     /* Perform PSCW communication: Odd/even matchup */
76
77     if (rank % 2 == 0) {
78         MPI_Win_start(odd_group, 0, win);       /* Even-numbered procs target odd procs */
79         MPI_Win_post(odd_group, 0, win);        /* Even procs are targeted by odd procs */
80
81         /* Write to my slot at each target */
82         for (i = 0; i < odd_nproc; i++)
83             MPI_Put(&rank, 1, MPI_INT, odd_ranks[i], rank, 1, MPI_INT, win);
84     }
85     else {
86         MPI_Win_post(even_group, 0, win);       /* Odd procs are targeted by even procs */
87         MPI_Win_start(even_group, 0, win);      /* Odd-numbered procs target even procs */
88
89         /* Write to my slot at each target */
90         for (i = 0; i < even_nproc; i++)
91             MPI_Put(&rank, 1, MPI_INT, even_ranks[i], rank, 1, MPI_INT, win);
92     }
93
94
95     MPI_Win_complete(win);
96     MPI_Win_wait(win);
97
98     /* Perform PSCW communication: Odd/odd and even/even matchup */
99
100     if (rank % 2 == 0) {
101         MPI_Win_post(even_group, 0, win);       /* Even procs are targeted by even procs */
102         MPI_Win_start(even_group, 0, win);      /* Even-numbered procs target even procs */
103
104         /* Write to my slot at each target */
105         for (i = 0; i < even_nproc; i++)
106             MPI_Put(&rank, 1, MPI_INT, even_ranks[i], rank, 1, MPI_INT, win);
107     }
108     else {
109         MPI_Win_post(odd_group, 0, win);        /* Odd procs are targeted by odd procs */
110         MPI_Win_start(odd_group, 0, win);       /* Odd-numbered procs target odd procs */
111
112         /* Write to my slot at each target */
113         for (i = 0; i < odd_nproc; i++)
114             MPI_Put(&rank, 1, MPI_INT, odd_ranks[i], rank, 1, MPI_INT, win);
115     }
116
117
118     MPI_Win_complete(win);
119     MPI_Win_wait(win);
120
121     for (i = 0; i < nproc; i++) {
122         if (win_buf[i] != i) {
123             errors++;
124
125             SQUELCH(printf("%d: Error -- win_buf[%d] = %d, expected %d\n", rank, i, win_buf[i], i);
126 );
127         }
128     }
129
130     MPI_Win_free(&win);
131 #ifndef USE_WIN_ALLOCATE
132     MPI_Free_mem(win_buf);
133 #endif
134
135     MPI_Group_free(&world_group);
136     MPI_Group_free(&odd_group);
137     MPI_Group_free(&even_group);
138
139     free(odd_ranks);
140     free(even_ranks);
141
142     MTest_Finalize(errors);
143     MPI_Finalize();
144     return MTestReturnValue(errors);
145 }