Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[simgrid.git] / teshsuite / smpi / mpich3-test / rma / fetchandadd.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
11 /* Fetch and add example from Using MPI-2 (the non-scalable version,
12    Fig. 6.12). */ 
13
14
15 #define NTIMES 20  /* no of times each process calls the counter
16                       routine */
17
18 int localvalue=0;  /* contribution of this process to the counter. We
19                     define it as a global variable because attribute
20                     caching on the window is not enabled yet. */ 
21
22 void Get_nextval(MPI_Win win, int *val_array, MPI_Datatype get_type,
23                  int rank, int nprocs, int *value);
24
25 int compar(const void *a, const void *b);
26
27 int main(int argc, char *argv[]) 
28
29     int rank, nprocs, i, blens[2], disps[2], *counter_mem, *val_array,
30         *results, *counter_vals;
31     MPI_Datatype get_type;
32     MPI_Win win;
33     int errs = 0;
34  
35     MTest_Init(&argc,&argv); 
36     MPI_Comm_size(MPI_COMM_WORLD,&nprocs); 
37     MPI_Comm_rank(MPI_COMM_WORLD,&rank); 
38
39     if (rank == 0) {
40         /* allocate counter memory and initialize to 0 */
41         counter_mem = (int *) calloc(nprocs, sizeof(int));
42         MPI_Win_create(counter_mem, nprocs*sizeof(int), sizeof(int),
43                        MPI_INFO_NULL, MPI_COMM_WORLD, &win);
44
45         MPI_Win_free(&win); 
46         free(counter_mem);
47
48         /* gather the results from other processes, sort them, and check 
49            whether they represent a counter being incremented by 1 */
50
51         results = (int *) malloc(NTIMES*nprocs*sizeof(int));
52         for (i=0; i<NTIMES*nprocs; i++)
53             results[i] = -1;
54
55         MPI_Gather(MPI_IN_PLACE, 0, MPI_DATATYPE_NULL, results, NTIMES, MPI_INT, 
56                    0, MPI_COMM_WORLD);
57
58         qsort(results+NTIMES, NTIMES*(nprocs-1), sizeof(int), compar);
59
60         for (i=NTIMES+1; i<(NTIMES*nprocs); i++)
61             if (results[i] != results[i-1] + 1)
62                 errs++;
63         
64         free(results);
65     }
66     else {
67         blens[0] = rank;
68         disps[0] = 0;
69         blens[1] = nprocs - rank - 1;
70         disps[1] = rank + 1;
71
72         MPI_Type_indexed(2, blens, disps, MPI_INT, &get_type);
73         MPI_Type_commit(&get_type);
74
75         val_array = (int *) malloc(nprocs * sizeof(int));
76
77         /* allocate array to store the values obtained from the 
78            fetch-and-add counter */
79         counter_vals = (int *) malloc(NTIMES * sizeof(int));
80
81         MPI_Win_create(NULL, 0, 1, MPI_INFO_NULL, MPI_COMM_WORLD, &win); 
82
83         for (i=0; i<NTIMES; i++) {
84             Get_nextval(win, val_array, get_type, rank, nprocs, counter_vals+i);
85             /* printf("Rank %d, counter %d\n", rank, value); */
86         }
87
88         MPI_Win_free(&win);
89
90         free(val_array);
91         MPI_Type_free(&get_type);
92
93         /* gather the results to the root */
94         MPI_Gather(counter_vals, NTIMES, MPI_INT, NULL, 0, MPI_DATATYPE_NULL, 
95                    0, MPI_COMM_WORLD);
96         free(counter_vals);
97     }
98
99     MTest_Finalize(errs);
100     MPI_Finalize(); 
101     return 0; 
102
103
104
105 void Get_nextval(MPI_Win win, int *val_array, MPI_Datatype get_type,
106                  int rank, int nprocs, int *value) 
107 {
108     int one=1, i;
109
110     MPI_Win_lock(MPI_LOCK_EXCLUSIVE, 0, 0, win);
111     MPI_Accumulate(&one, 1, MPI_INT, 0, rank, 1, MPI_INT, MPI_SUM, win);
112     MPI_Get(val_array, 1, get_type, 0, 0, 1, get_type, win); 
113     MPI_Win_unlock(0, win);
114
115     *value = 0;
116     val_array[rank] = localvalue;
117     for (i=0; i<nprocs; i++)
118         *value = *value + val_array[i];
119
120     localvalue++;
121 }
122
123 int compar(const void *a, const void *b)
124 {
125     return (*((int *)a) - *((int *)b));
126 }
127