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 / transpose4.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 "mpitest.h"
9
10 /* transposes a matrix using passive target RMA and derived
11    datatypes. Uses  vector and hvector (Example 3.32 from MPI 1.1
12    Standard). Run on 2 processes. */
13
14 #define NROWS 100
15 #define NCOLS 100
16
17 int main(int argc, char *argv[]) 
18
19     int rank, nprocs, A[NROWS][NCOLS], i, j;
20     MPI_Comm CommDeuce;
21     MPI_Win win;
22     MPI_Datatype column, xpose;
23     int errs = 0;
24
25     MTest_Init(&argc,&argv); 
26     MPI_Comm_size(MPI_COMM_WORLD,&nprocs); 
27     MPI_Comm_rank(MPI_COMM_WORLD,&rank); 
28
29     if (nprocs < 2) {
30         printf("Run this program with 2 or more processes\n");
31         MPI_Abort(MPI_COMM_WORLD, 1);
32     }
33
34     MPI_Comm_split(MPI_COMM_WORLD, (rank < 2), rank, &CommDeuce);
35
36     if (rank < 2)
37     {
38         if (rank == 0) {
39             for (i=0; i<NROWS; i++)
40                 for (j=0; j<NCOLS; j++)
41                     A[i][j] = i*NCOLS + j;
42
43             /* create datatype for one column */
44             MPI_Type_vector(NROWS, 1, NCOLS, MPI_INT, &column);
45             /* create datatype for matrix in column-major order */
46             MPI_Type_hvector(NCOLS, 1, sizeof(int), column, &xpose);
47             MPI_Type_commit(&xpose);
48
49             MPI_Win_create(NULL, 0, 1, MPI_INFO_NULL, CommDeuce, &win);
50
51             MPI_Win_lock(MPI_LOCK_SHARED, 1, 0, win);
52
53             MPI_Put(A, NROWS*NCOLS, MPI_INT, 1, 0, 1, xpose, win);
54
55             MPI_Type_free(&column);
56             MPI_Type_free(&xpose);
57
58             MPI_Win_unlock(1, win);
59             MPI_Win_free(&win);
60         }
61         else
62         { /* rank=1 */
63             for (i=0; i<NROWS; i++) 
64                 for (j=0; j<NCOLS; j++)
65                     A[i][j] = -1;
66             MPI_Win_create(A, NROWS*NCOLS*sizeof(int), sizeof(int), MPI_INFO_NULL, CommDeuce, &win);
67
68             MPI_Win_free(&win);
69
70             for (j=0; j<NCOLS; j++)
71                 for (i=0; i<NROWS; i++)
72                     if (A[j][i] != i*NCOLS + j) {
73                         printf("Error: A[%d][%d]=%d should be %d\n", j, i,
74                                A[j][i], i*NCOLS + j);
75                         errs++;
76                     }
77         }
78     }
79
80     MPI_Comm_free(&CommDeuce);
81     MTest_Finalize(errs);
82     MPI_Finalize(); 
83     return 0; 
84