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