Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'mc'
[simgrid.git] / teshsuite / smpi / mpich3-test / rma / selfrma.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 <string.h>
11 #include "mpitest.h"
12
13 /*
14 static char MTEST_Descrip[] = "RMA to self";
15 */
16
17 int main( int argc, char *argv[] )
18 {
19     int errs = 0;
20     int rank, size, i, j;
21     MPI_Comm      comm;
22     MPI_Win       win;
23     int           *winbuf, count;
24     int           *sbuf, scount, vcount;
25     MPI_Datatype  vectype;
26
27     MTest_Init( &argc, &argv );
28
29     comm = MPI_COMM_WORLD;
30
31     MPI_Comm_rank( comm, &rank );
32     MPI_Comm_size( comm, &size );
33
34     /* Allocate and initialize sbuf */
35     scount = 1000;
36     count  = 1000;
37     sbuf   = (int *)malloc( scount * sizeof(int) );
38     if (!sbuf) {
39         fprintf( stderr, "Could not allocate send buffer f size %d\n", 
40                  scount );
41         MPI_Abort( MPI_COMM_WORLD, 0 );
42     }
43     for (i=0; i<scount; i++) sbuf[i] = i;
44
45     MPI_Alloc_mem( count*sizeof(int), MPI_INFO_NULL, &winbuf );
46
47     /* This is a simple vector type */
48     vcount = count / 4;
49     MPI_Type_vector( vcount, 1, 2, MPI_INT, &vectype );
50     MPI_Type_commit( &vectype );
51     MPI_Win_create( winbuf, count * sizeof(int), sizeof(int), MPI_INFO_NULL, 
52                     comm, &win );
53
54     /* Check with different combination of types, including non-contig on 
55        both sides */
56     
57     /* Clear winbuf */
58     memset( winbuf, 0, count*sizeof(int) );
59     MPI_Win_lock( MPI_LOCK_EXCLUSIVE, rank, 0, win );
60     MPI_Put( sbuf, 1, vectype, rank, 0, 1, vectype, win );
61     MPI_Win_unlock( rank, win );
62     /* Check results */
63     j = 0;
64     for (i=0; i<vcount; i++) {
65         if (winbuf[j] != sbuf[j]) {
66             errs ++;
67             fprintf( stderr, "VecPut: winbuf[%d] = %d, should = %d\n", 
68                      winbuf[j], j, sbuf[j] );
69         }
70         j += 2;
71     }
72
73     memset( winbuf, 0, count*sizeof(int) );
74     MPI_Win_lock( MPI_LOCK_SHARED, rank, 0, win );
75     MPI_Accumulate( sbuf, 1, vectype, rank, 0, 1, vectype, MPI_SUM, win );
76     MPI_Win_unlock( rank, win );
77     /* Check results */
78     j = 0;
79     for (i=0; i<vcount; i++) {
80         if (winbuf[j] != sbuf[j]) {
81             errs ++;
82             fprintf( stderr, "VecAcc: winbuf[%d] = %d, should = %d\n", 
83                      winbuf[j], j, sbuf[j] );
84         }
85         j += 2;
86     }
87
88     /* Now, use get to fetch back the results that we just wrote */
89     memset( sbuf, 0, count*sizeof(int) );
90     MPI_Win_lock( MPI_LOCK_SHARED, rank, 0, win );
91     MPI_Get( sbuf, 1, vectype, rank, 0, 1, vectype, win );
92     MPI_Win_unlock( rank, win );
93     /* Check results */
94     j = 0;
95     for (i=0; i<vcount; i++) {
96         if (winbuf[j] != sbuf[j]) {
97             errs ++;
98             fprintf( stderr, "VecAcc: winbuf[%d] = %d, should = %d\n", 
99                      winbuf[j], j, sbuf[j] );
100         }
101         j += 2;
102     }
103
104     MPI_Win_free( &win );
105     MPI_Free_mem( winbuf );
106     free( sbuf );
107     MPI_Type_free( &vectype );
108
109     MTest_Finalize( errs );
110
111     MPI_Finalize();
112     return 0;
113 }