Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of https://github.com/mpoquet/simgrid
[simgrid.git] / teshsuite / smpi / mpich3-test / rma / putfidx.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 "mpitest.h"
10 #ifdef HAVE_STDLIB_H
11 #include <stdlib.h>
12 #endif
13
14 /*
15 static char MTEST_Descrip[] = "Put with Fence for an indexed datatype";
16 */
17
18 int CheckMPIErr( int err );
19
20 int main( int argc, char *argv[] )
21 {
22     int           errs = 0, err;
23     int           i, rank, size, source, dest;
24     int           blksize, totsize;
25     int           *recvBuf = 0, *srcBuf = 0;
26     MPI_Comm      comm;
27     MPI_Win       win;
28     MPI_Aint      extent;
29     MPI_Datatype  originType;
30     int           counts[2];
31     int           displs[2];
32
33     MTest_Init( &argc, &argv );
34
35     /* Select the communicator and datatypes */
36     comm = MPI_COMM_WORLD;
37
38     /* Create the datatype */
39     /* One MPI Implementation fails this test with sufficiently large 
40        values of blksize - it appears to convert this type to an 
41        incorrect contiguous move */
42     blksize = 2048;
43     counts[0] = blksize;
44     counts[1] = blksize;
45     displs[0] = 0;
46     displs[1] = blksize + 1;
47     MPI_Type_indexed( 2, counts, displs, MPI_INT, &originType );
48     MPI_Type_commit( &originType );
49
50     totsize = 2 * blksize;
51
52     /* Determine the sender and receiver */
53     MPI_Comm_rank( comm, &rank );
54     MPI_Comm_size( comm, &size );
55     source = 0;
56     dest   = size - 1;
57         
58     recvBuf = (int *) malloc( totsize * sizeof(int) );
59     srcBuf  = (int *) malloc( (totsize + 1) * sizeof(int) ) ;
60     
61     if (!recvBuf || !srcBuf) {
62         fprintf( stderr, "Could not allocate buffers\n" );
63         MPI_Abort( MPI_COMM_WORLD, 1 );
64     }
65     
66     /* Initialize the send and recv buffers */
67     for (i=0; i<totsize; i++) {
68         recvBuf[i] = -1;
69     }
70     for (i=0; i<blksize; i++) {
71         srcBuf[i] = i;
72         srcBuf[blksize+1+i] = blksize+i;
73     }
74     srcBuf[blksize] = -1;
75
76     MPI_Type_extent( MPI_INT, &extent );
77     MPI_Win_create( recvBuf, totsize * extent, extent, 
78                     MPI_INFO_NULL, comm, &win );
79     MPI_Win_fence( 0, win );
80     if (rank == source) {
81         /* To improve reporting of problems about operations, we
82            change the error handler to errors return */
83         MPI_Win_set_errhandler( win, MPI_ERRORS_RETURN );
84
85         err = MPI_Put( srcBuf, 1, originType, dest, 0, 
86                        totsize, MPI_INT, win );
87         errs += CheckMPIErr( err );
88         err = MPI_Win_fence( 0, win );
89         errs += CheckMPIErr( err );
90     }
91     else if (rank == dest) {
92         MPI_Win_fence( 0, win );
93         for (i=0; i<totsize; i++) {
94             if (recvBuf[i] != i) {
95                 errs++;
96                 if (errs < 10) {
97                     printf( "recvBuf[%d] = %d should = %d\n", 
98                             i, recvBuf[i], i );
99                 }
100             }
101         }
102     }
103     else {
104         MPI_Win_fence( 0, win );
105     }
106     
107     MPI_Type_free( &originType );
108     MPI_Win_free( &win );
109     free( recvBuf );
110     free( srcBuf );
111
112     MTest_Finalize( errs );
113     MPI_Finalize();
114     return 0;
115 }
116
117 int CheckMPIErr( int err )
118 {
119     int rc = 0;
120     if (err != MPI_SUCCESS) { 
121         MTestPrintError( err );
122         rc = 1;
123     }
124     return rc;
125 }