Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add MPICH3 rma tests (15 out of 88 should be passing now)
[simgrid.git] / teshsuite / smpi / mpich3-test / rma / req_example.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *
4  *  (C) 2003 by Argonne National Laboratory.
5  *      See COPYRIGHT in top-level directory.
6  */
7 #include <mpi.h>
8 #include <stdio.h>
9 #include <assert.h>
10 #include "mpitest.h"
11
12 #define NSTEPS 100
13 #define N 1000
14 #define M 10
15
16 /* This is example 11.21 from the MPI 3.0 spec:
17  *
18  * The following example shows how request-based operations can be used to
19  * overlap communication with computation. Each process fetches, processes,
20  * and writes the result for NSTEPS chunks of data. Instead of a single
21  * buffer, M local buffers are used to allow up to M communication operations
22  * to overlap with computation.
23  */
24
25 /* Use a global variable to inhibit compiler optimizations in the compute
26  * function. */
27 double junk = 0.0;
28
29 void compute(int step, double *data) {
30     int i;
31
32     for (i = 0; i < N; i++)
33         junk += data[i] * (double) step;
34 }
35
36 int main( int argc, char *argv[] )
37 {
38     int i, rank, nproc;
39     int errors = 0, all_errors = 0;
40     MPI_Win win;
41     MPI_Request put_req[M] = { MPI_REQUEST_NULL };
42     MPI_Request get_req;
43     double *baseptr;
44     double data[M][N]; /* M buffers of length N */
45
46     MPI_Init(&argc, &argv);
47     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
48     MPI_Comm_size(MPI_COMM_WORLD, &nproc);
49
50     assert(M < NSTEPS);
51
52     MPI_Win_allocate(NSTEPS*N*sizeof(double), sizeof(double), MPI_INFO_NULL,
53                      MPI_COMM_WORLD, &baseptr, &win);
54
55     MPI_Win_lock_all(0, win);
56
57     for (i = 0; i < NSTEPS; i++) {
58         int target = (rank+1) % nproc;
59         int j;
60
61         /* Find a free put request */
62         if (i < M) {
63             j = i;
64         } else {
65             MPI_Waitany(M, put_req, &j, MPI_STATUS_IGNORE);
66         }
67
68         MPI_Rget(data[j], N, MPI_DOUBLE, target, i*N, N, MPI_DOUBLE, win,
69                  &get_req);
70         MPI_Wait(&get_req,MPI_STATUS_IGNORE);
71
72         compute(i, data[j]);
73         MPI_Rput(data[j], N, MPI_DOUBLE, target, i*N, N, MPI_DOUBLE, win,
74                  &put_req[j]);
75
76     }
77
78     MPI_Waitall(M, put_req, MPI_STATUSES_IGNORE);
79     MPI_Win_unlock_all(win);
80
81     MPI_Win_free(&win);
82
83     MPI_Reduce(&errors, &all_errors, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
84
85     if (rank == 0 && all_errors == 0)
86         printf(" No Errors\n");
87
88     MPI_Finalize();
89
90     return 0;
91 }