Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'mc'
[simgrid.git] / teshsuite / smpi / mpich3-test / rma / contig_displ.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
7 #include <stdio.h>
8 #include <mpi.h>
9 #include "mpitest.h"
10
11 /* Run with 1 process.
12
13    This program does an MPI_Get with an indexed datatype. The datatype
14    comprises a single integer at an initial displacement of 1 integer. 
15    That is, the first integer in the array is to be skipped.
16
17    This program found a bug in IBM's MPI in which MPI_Get ignored the
18    displacement and got the first integer instead of the second. 
19 */
20
21 int main(int argc, char **argv)
22 {
23     int rank, nprocs, mpi_err, *array;
24     int getval, disp, errs=0;
25     MPI_Win win;
26     MPI_Datatype type;
27     
28     MTest_Init(&argc,&argv); 
29
30     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
31     MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
32
33     if (rank == 0) {
34         /* To improve reporting of problems about operations, we
35            change the error handler to errors return */
36         MPI_Comm_set_errhandler( MPI_COMM_SELF, MPI_ERRORS_RETURN );
37  
38         /* create an indexed datatype that points to the second integer 
39            in an array (the first integer is skipped). */
40         disp  =  1;
41         mpi_err = MPI_Type_create_indexed_block(1, 1, &disp, MPI_INT, &type);
42         if (mpi_err != MPI_SUCCESS) goto err_return;
43         mpi_err = MPI_Type_commit(&type);
44         if (mpi_err != MPI_SUCCESS) goto err_return;
45  
46         /* allocate window of size 2 integers*/
47         mpi_err = MPI_Alloc_mem(2*sizeof(int), MPI_INFO_NULL, &array);
48         if (mpi_err != MPI_SUCCESS) goto err_return;
49  
50         /* create window object */
51         mpi_err = MPI_Win_create(array, 2*sizeof(int), sizeof(int), MPI_INFO_NULL, MPI_COMM_SELF, &win);
52         if (mpi_err != MPI_SUCCESS) goto err_return;
53   
54         /* initialize array */
55         array[0] = 100;
56         array[1] = 200;
57  
58         getval = 0;
59         
60         /* To improve reporting of problems about operations, we
61            change the error handler to errors return */
62         MPI_Win_set_errhandler( win, MPI_ERRORS_RETURN );
63  
64         mpi_err = MPI_Win_lock(MPI_LOCK_EXCLUSIVE, 0, 0, win);
65         if (mpi_err != MPI_SUCCESS) goto err_return;
66  
67         /* get the current value of element array[1] */
68         mpi_err = MPI_Get(&getval, 1, MPI_INT, 0, 0, 1, type, win);
69         if (mpi_err != MPI_SUCCESS) goto err_return;
70  
71         mpi_err = MPI_Win_unlock(0, win);
72         if (mpi_err != MPI_SUCCESS) goto err_return;
73  
74         /* getval should contain the value of array[1] */
75         if (getval != array[1]) {
76             errs++;
77             printf("getval=%d, should be %d\n", getval, array[1]);
78         }
79  
80         MPI_Free_mem(array);
81         MPI_Win_free(&win);
82         MPI_Type_free(&type);
83     }
84
85     MTest_Finalize(errs);
86     MPI_Finalize();
87     return 0;
88
89  err_return:
90     printf("MPI function error returned an error\n");
91     MTestPrintError( mpi_err );
92     errs++;
93     MTest_Finalize(errs);
94     MPI_Finalize();
95     return 1;
96 }
97
98