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 / accfence2_am.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 <stdlib.h>
10 #include "mpitest.h"
11
12 #ifndef MAX_INT
13 #define MAX_INT 0x7fffffff
14 #endif
15
16 /*
17 static char MTEST_Descrip[] = "Test MPI_Accumulate with fence";
18 */
19
20 /* same as accfence2.c, but uses alloc_mem */
21
22 int main( int argc, char *argv[] )
23 {
24     int errs = 0;
25     int rank, size, source, dest;
26     int minsize = 2, count, i; 
27     MPI_Comm      comm;
28     MPI_Win       win;
29     MPI_Datatype  datatype;
30     int           *winbuf, *sbuf;
31
32     MTest_Init( &argc, &argv );
33
34     /* The following illustrates the use of the routines to 
35        run through a selection of communicators and datatypes.
36        Use subsets of these for tests that do not involve combinations 
37        of communicators, datatypes, and counts of datatypes */
38     while (MTestGetIntracommGeneral( &comm, minsize, 1 )) {
39         if (comm == MPI_COMM_NULL) continue;
40         /* Determine the sender and receiver */
41         MPI_Comm_rank( comm, &rank );
42         MPI_Comm_size( comm, &size );
43         source = 0;
44         dest   = size - 1;
45         
46         for (count = 32768; count < 65000; count = count * 2) {
47             datatype = MPI_INT;
48
49             /* We compare with an integer value that can be as large as
50                size * (count * count + (1/2)*(size-1))
51                For large machines (size large), this can exceed the 
52                maximum integer for some large values of count.  We check
53                that in advance and break this loop if the above value 
54                would exceed MAX_INT.  Specifically,
55
56                size*count*count + (1/2)*size*(size-1) > MAX_INT
57                count*count > (MAX_INT/size - (1/2)*(size-1))
58             */
59             if (count * count > (MAX_INT/size - (size-1)/2)) break;
60
61             MPI_Alloc_mem( count * sizeof(int), MPI_INFO_NULL, &winbuf );
62             MPI_Alloc_mem( count * sizeof(int), MPI_INFO_NULL, &sbuf );
63
64             for (i=0; i<count; i++) winbuf[i] = 0;
65             for (i=0; i<count; i++) sbuf[i] = rank + i * count;
66             MPI_Win_create( winbuf, count * sizeof(int), sizeof(int),
67                             MPI_INFO_NULL, comm, &win );
68             MPI_Win_fence( 0, win );
69             MPI_Accumulate( sbuf, count, MPI_INT, source, 0, count, MPI_INT,
70                                 MPI_SUM, win );
71             MPI_Win_fence( 0, win );
72             if (rank == source) {
73                 /* Check the results */
74                 for (i=0; i<count; i++) {
75                     int result = i * count * size + (size*(size-1))/2;
76                     if (winbuf[i] != result) {
77                         if (errs < 10) {
78                             fprintf( stderr, "Winbuf[%d] = %d, expected %d (count = %d, size = %d)\n",
79                                      i, winbuf[i], result, count, size );
80                         }
81                         errs++;
82                     }
83                 }
84             }
85
86             MPI_Win_free( &win );
87
88             MPI_Free_mem(winbuf);
89             MPI_Free_mem(sbuf);
90         }
91         MTestFreeComm(&comm);
92     }
93
94     MTest_Finalize( errs );
95     MPI_Finalize();
96     return 0;
97 }