Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[simgrid.git] / teshsuite / smpi / mpich3-test / rma / transpose1.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 <stdlib.h>
9 #include "mpitest.h"
10
11 /* transposes a matrix using put, fence, and derived datatypes. Uses
12    vector and hvector (Example 3.32 from MPI 1.1 Standard). Run on
13    2 processes */
14
15 #define NROWS 1000
16 #define NCOLS 1000
17
18 int main(int argc, char *argv[]) 
19
20     int rank, nprocs, **A, *A_data, i, j;
21     MPI_Comm CommDeuce;
22     MPI_Win win;
23     MPI_Datatype column, xpose;
24     int errs = 0;
25
26     MTest_Init(&argc,&argv); 
27     MPI_Comm_size(MPI_COMM_WORLD,&nprocs); 
28     MPI_Comm_rank(MPI_COMM_WORLD,&rank); 
29
30     if (nprocs < 2) {
31         printf("Run this program with 2 or more processes\n");
32         MPI_Abort(MPI_COMM_WORLD, 1);
33     }
34
35     MPI_Comm_split(MPI_COMM_WORLD, (rank < 2), rank, &CommDeuce);
36
37     if (rank < 2) {
38         A_data = (int *) malloc(NROWS * NCOLS * sizeof(int));
39         A = (int **) malloc(NROWS * sizeof(int *));
40
41         A[0] = A_data;
42         for (i=1; i<NROWS; i++)
43             A[i] = A[i-1] + NCOLS;
44
45         if (rank == 0)
46         {
47             for (i=0; i<NROWS; i++)
48                 for (j=0; j<NCOLS; j++)
49                     A[i][j] = i*NCOLS + j;
50
51             /* create datatype for one column */
52             MPI_Type_vector(NROWS, 1, NCOLS, MPI_INT, &column);
53             /* create datatype for matrix in column-major order */
54             MPI_Type_hvector(NCOLS, 1, sizeof(int), column, &xpose);
55             MPI_Type_commit(&xpose);
56
57             MPI_Win_create(NULL, 0, 1, MPI_INFO_NULL, CommDeuce, &win);
58
59             MPI_Win_fence(0, win);
60
61             MPI_Put(&A[0][0], NROWS*NCOLS, MPI_INT, 1, 0, 1, xpose, win);
62
63             MPI_Type_free(&column);
64             MPI_Type_free(&xpose);
65
66             MPI_Win_fence(0, win);
67         }
68         else if (rank == 1)
69         {
70             for (i=0; i<NROWS; i++)
71                 for (j=0; j<NCOLS; j++)
72                     A[i][j] = -1;
73             MPI_Win_create(&A[0][0], NROWS*NCOLS*sizeof(int), sizeof(int), MPI_INFO_NULL, CommDeuce, &win);
74             MPI_Win_fence(0, win);
75
76             MPI_Win_fence(0, win);
77
78             for (j=0; j<NCOLS; j++)
79             {
80                 for (i=0; i<NROWS; i++)
81                 {
82                     if (A[j][i] != i*NCOLS + j)
83                     {
84                         if (errs < 50)
85                         {
86                             printf("Error: A[%d][%d]=%d should be %d\n", j, i,
87                                    A[j][i], i*NCOLS + j);
88                         }
89                         errs++;
90                     }
91                 }
92             }
93             if (errs >= 50)
94             {
95                 printf("Total number of errors: %d\n", errs);
96             }
97         }
98
99         MPI_Win_free(&win);
100
101         free(A_data);
102         free(A);
103
104     }
105     MPI_Comm_free(&CommDeuce);
106     MTest_Finalize(errs);
107     MPI_Finalize(); 
108     return 0; 
109