Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fix+activate rma test
[simgrid.git] / teshsuite / smpi / mpich3-test / rma / test3.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 #include "mpi.h"
7 #include "stdio.h"
8 #include "stdlib.h"
9 #include "mpitest.h"
10 #include "squelch.h"
11
12 /* Tests the example in Fig 6.8, pg 142, MPI-2 standard. Process 1 has
13    a blocking MPI_Recv between the Post and Wait. Therefore, this
14    example will not run if the one-sided operations are simply
15    implemented on top of MPI_Isends and Irecvs. They either need to be
16    implemented inside the progress engine or using threads with Isends
17    and Irecvs. In MPICH-2, they are implemented in the progress engine. */
18
19 #define SIZE 1048576
20
21 int main(int argc, char *argv[])
22 {
23     int rank, destrank, nprocs, *A, *B, i;
24     MPI_Comm CommDeuce;
25     MPI_Group comm_group, group;
26     MPI_Win win;
27     int errs = 0;
28
29     MTest_Init(&argc, &argv);
30     MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
31     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
32
33     if (nprocs < 2) {
34         printf("Run this program with 2 or more processes\n");
35         MPI_Abort(MPI_COMM_WORLD, 1);
36     }
37
38     MPI_Comm_split(MPI_COMM_WORLD, (rank < 2), rank, &CommDeuce);
39
40     if (rank < 2) {
41         A = (int *) malloc(SIZE * sizeof(int));
42         if (!A) {
43             printf("Can't allocate memory in test program\n");
44             MPI_Abort(MPI_COMM_WORLD, 1);
45         }
46
47         MPI_Comm_group(CommDeuce, &comm_group);
48
49         if (rank == 0) {
50             B = (int *) malloc(SIZE * sizeof(int));
51             if (!B) {
52                 printf("Can't allocate memory in test program\n");
53                 MPI_Abort(MPI_COMM_WORLD, 1);
54             }
55
56             for (i = 0; i < SIZE; i++) {
57                 A[i] = i;
58                 B[i] = SIZE + i;
59             }
60 #ifdef USE_WIN_ALLOCATE
61             char *base_ptr;
62             MPI_Win_allocate(0, 1, MPI_INFO_NULL, CommDeuce, &base_ptr, &win);
63 #else
64             MPI_Win_create(NULL, 0, 1, MPI_INFO_NULL, CommDeuce, &win);
65 #endif
66             destrank = 1;
67             MPI_Group_incl(comm_group, 1, &destrank, &group);
68             MPI_Win_start(group, 0, win);
69             MPI_Put(A, SIZE, MPI_INT, 1, 0, SIZE, MPI_INT, win);
70             MPI_Win_complete(win);
71             MPI_Send(B, SIZE, MPI_INT, 1, 100, MPI_COMM_WORLD);
72
73             free(B);
74         }
75
76         else if (rank == 1) {
77 #ifdef USE_WIN_ALLOCATE
78             MPI_Win_allocate(SIZE * sizeof(int), sizeof(int), MPI_INFO_NULL, CommDeuce, &B, &win);
79 #else
80             B = (int *) malloc(SIZE * sizeof(int));
81             if (!B) {
82                 printf("Can't allocate memory in test program\n");
83                 MPI_Abort(MPI_COMM_WORLD, 1);
84             }
85             MPI_Win_create(B, SIZE * sizeof(int), sizeof(int), MPI_INFO_NULL, CommDeuce, &win);
86 #endif
87             MPI_Win_lock(MPI_LOCK_SHARED, rank, 0, win);
88             for (i = 0; i < SIZE; i++)
89                 A[i] = B[i] = (-4) * i;
90             MPI_Win_unlock(rank, win);
91
92             destrank = 0;
93             MPI_Group_incl(comm_group, 1, &destrank, &group);
94             MPI_Win_post(group, 0, win);
95             MPI_Recv(A, SIZE, MPI_INT, 0, 100, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
96             MPI_Win_wait(win);
97
98             for (i = 0; i < SIZE; i++) {
99                 if (B[i] != i) {
100                     SQUELCH(printf("Rank 1: Put Error: B[i] is %d, should be %d\n", B[i], i););
101                     errs++;
102                 }
103                 if (A[i] != SIZE + i) {
104                     SQUELCH(printf
105                             ("Rank 1: Send/Recv Error: A[i] is %d, should be %d\n", A[i],
106                              SIZE + i););
107                     errs++;
108                 }
109             }
110 #ifndef USE_WIN_ALLOCATE
111             free(B);
112 #endif
113         }
114
115         MPI_Group_free(&group);
116         MPI_Group_free(&comm_group);
117         MPI_Win_free(&win);
118         free(A);
119     }
120     MPI_Comm_free(&CommDeuce);
121     MTest_Finalize(errs);
122     MPI_Finalize();
123     return 0;
124 }