Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
change some tests to avoid useless global variables
[simgrid.git] / teshsuite / smpi / mpich3-test / rma / rget-unlock.c
1 /*
2  *  (C) 2016 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-2016 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  *  This is a test case to make sure synchronization in unlock works correctly.
12  *
13  *  Essentially this program does the following:
14  *
15  *  lock_all
16  *  req=rget(buf)
17  *  unlock_all
18  *  re-use buf
19  *  wait(req)
20  *
21  *  This program is valid but if unlock_all does not implement the synchronization
22  *  semantics correctly reusing the buffer would race with outstanding rgets.
23  *
24  */
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <memory.h>
29 #include <mpi.h>
30
31 #define N_ELMS (128)
32 #define BLOCKSIZE (1)
33 #define N_BLOCKS (N_ELMS/BLOCKSIZE)
34
35 int main(int argc, char *argv[])
36 {
37     MPI_Win win;
38     int i;
39     int *rbuf, *lbuf;
40     int rank, size, trg;
41     MPI_Request *reqs;
42     int n_errors = 0;
43
44     MPI_Init(&argc, &argv);
45
46     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
47     MPI_Comm_size(MPI_COMM_WORLD, &size);
48
49     trg = (rank + 1) % size;
50
51     rbuf = malloc(sizeof(int) * N_ELMS);
52     for (i = 0; i < N_ELMS; i++)
53         rbuf[i] = rank;
54
55     lbuf = malloc(sizeof(int) * N_ELMS);
56     memset(lbuf, -1, sizeof(int) * N_ELMS);
57
58     reqs = malloc(sizeof(MPI_Request) * N_BLOCKS);
59
60     MPI_Win_create(rbuf, sizeof(int) * N_ELMS, sizeof(int), MPI_INFO_NULL, MPI_COMM_WORLD, &win);
61     MPI_Win_lock_all(MPI_MODE_NOCHECK, win);
62     for (i = 0; i < N_BLOCKS; i++)
63         MPI_Rget(lbuf+i*BLOCKSIZE, BLOCKSIZE, MPI_INT, trg, i*BLOCKSIZE, BLOCKSIZE, MPI_INT, win, &reqs[i]);
64     MPI_Win_unlock_all(win);
65     for (i = 0; i < N_ELMS; i++)
66         lbuf[i] = -2;
67
68     MPI_Waitall(N_BLOCKS, reqs, MPI_STATUSES_IGNORE);
69     for (i = 0; i < N_ELMS; i++) {
70         int v = lbuf[i];
71         if (v != -2) {
72             printf("lbuf[%d]=%d, expected -2\n", i, v);
73             n_errors++;
74         }
75     }
76     MPI_Win_free(&win);
77
78     free(reqs);
79     free(lbuf);
80     free(rbuf);
81
82     if (rank == 0 && n_errors == 0)
83         printf(" No Errors\n");
84
85     MPI_Finalize();
86     return 0;
87 }