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 / transpose2.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 put, fence, and derived
11    datatypes. Uses vector and struct (Example 3.33 from MPI 1.1
12    Standard). We could use vector and type_create_resized instead. Run
13    on 2 processes */ 
14
15 #define NROWS 100
16 #define NCOLS 100
17
18 int main(int argc, char *argv[]) 
19
20     int rank, nprocs, A[NROWS][NCOLS], i, j, blocklen[2];
21     MPI_Comm CommDeuce;
22     MPI_Aint disp[2];
23     MPI_Win win;
24     MPI_Datatype column, column1, type[2];
25     int errs=0;
26
27     MTest_Init(&argc,&argv); 
28     MPI_Comm_size(MPI_COMM_WORLD,&nprocs); 
29     MPI_Comm_rank(MPI_COMM_WORLD,&rank); 
30
31     if (nprocs < 2) {
32         printf("Run this program with 2 or more processes\n");
33         MPI_Abort(MPI_COMM_WORLD, 1);
34     }
35
36     MPI_Comm_split(MPI_COMM_WORLD, (rank < 2), rank, &CommDeuce);
37
38     if (rank < 2) {
39         if (rank == 0)
40         {
41             for (i=0; i<NROWS; i++)
42                 for (j=0; j<NCOLS; j++)
43                     A[i][j] = i*NCOLS + j;
44
45             /* create datatype for one column */
46             MPI_Type_vector(NROWS, 1, NCOLS, MPI_INT, &column);
47
48             /* create datatype for one column, with the extent of one
49            integer. we could use type_create_resized instead. */
50             disp[0] = 0;
51             disp[1] = sizeof(int);
52             type[0]  = column;
53             type[1]  = MPI_UB;
54             blocklen[0]  = 1;
55             blocklen[1]  = 1;
56             MPI_Type_struct(2, blocklen, disp, type, &column1);
57             MPI_Type_commit(&column1);
58
59             MPI_Win_create(NULL, 0, 1, MPI_INFO_NULL, CommDeuce, &win);
60
61             MPI_Win_fence(0, win);
62
63             MPI_Put(A, NROWS*NCOLS, MPI_INT, 1, 0, NCOLS, column1, win);
64
65             MPI_Type_free(&column);
66             MPI_Type_free(&column1);
67
68             MPI_Win_fence(0, win);
69         }
70         else
71         { /* rank=1 */
72             for (i=0; i<NROWS; i++)
73                 for (j=0; j<NCOLS; j++)
74                     A[i][j] = -1;
75             MPI_Win_create(A, NROWS*NCOLS*sizeof(int), sizeof(int), MPI_INFO_NULL, CommDeuce, &win);
76             MPI_Win_fence(0, win);
77
78             MPI_Win_fence(0, win);
79
80             for (j=0; j<NCOLS; j++)
81             {
82                 for (i=0; i<NROWS; i++)
83                 {
84                     if (A[j][i] != i*NCOLS + j)
85                     {
86                         if (errs < 50)
87                         {
88                             printf("Error: A[%d][%d]=%d should be %d\n", j, i,
89                                    A[j][i], i*NCOLS + j);
90                         }
91                         errs++;
92                     }
93                 }
94             }
95             if (errs >= 50)
96             {
97                 printf("Total number of errors: %d\n", errs);
98             }
99         }
100         MPI_Win_free(&win);
101     }
102
103     MPI_Comm_free(&CommDeuce);
104     MTest_Finalize(errs);
105     MPI_Finalize(); 
106     return 0; 
107