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 / aint.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *
4  *  (C) 2014 by Argonne National Laboratory.
5  *      See COPYRIGHT in top-level directory.
6  */
7
8 /*
9  * This program tests MPI_Aint_add/diff in MPI-3.1.
10  * The two functions are often used in RMA code.
11  * See https://svn.mpi-forum.org/trac/mpi-forum-web/ticket/349
12  */
13
14 #include <stdio.h>
15 #include <mpi.h>
16 #include "mpitest.h"
17
18 int main(int argc, char **argv)
19 {
20     int rank, nproc;
21     int errs = 0;
22     int array[1024];
23     int val = 0;
24     int target_rank;
25     MPI_Aint bases[2];
26     MPI_Aint disp, offset;
27     MPI_Win win;
28
29     MTest_Init(&argc, &argv);
30
31     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
32     MPI_Comm_size(MPI_COMM_WORLD, &nproc);
33
34     if (rank == 0 && nproc != 2) {
35         MTestError("Must run with 2 ranks\n");
36     }
37
38     /* Get the base address in the middle of the array */
39     if (rank == 0) {
40         target_rank = 1;
41         array[0] = 1234;
42         MPI_Get_address(&array[512], &bases[0]);
43     }
44     else if (rank == 1) {
45         target_rank = 0;
46         array[1023] = 1234;
47         MPI_Get_address(&array[512], &bases[1]);
48     }
49
50     /* Exchange bases */
51     MPI_Allgather(MPI_IN_PLACE, 0, MPI_DATATYPE_NULL, bases, 1, MPI_AINT, MPI_COMM_WORLD);
52
53     MPI_Win_create_dynamic(MPI_INFO_NULL, MPI_COMM_WORLD, &win);
54     MPI_Win_attach(win, array, sizeof(int) * 1024);
55
56     /* Do MPI_Aint addressing arithmetic */
57     if (rank == 0) {
58         disp = sizeof(int) * 511;
59         offset = MPI_Aint_add(bases[1], disp);  /* offset points to array[1023] */
60     }
61     else if (rank == 1) {
62         disp = sizeof(int) * 512;
63         offset = MPI_Aint_diff(bases[0], disp); /* offset points to array[0] */
64     }
65
66     /* Get val and verify it */
67     MPI_Win_fence(MPI_MODE_NOPRECEDE, win);
68     MPI_Get(&val, 1, MPI_INT, target_rank, offset, 1, MPI_INT, win);
69     MPI_Win_fence(MPI_MODE_NOSUCCEED, win);
70
71     if (val != 1234) {
72         errs++;
73         printf("%d -- Got %d, expected 1234\n", rank, val);
74     }
75
76     MPI_Win_detach(win, array);
77     MPI_Win_free(&win);
78
79     MTest_Finalize(errs);
80     MPI_Finalize();
81     return 0;
82 }