Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
3d2a549e64b9703697c1cc15546a96841d1875f1
[simgrid.git] / teshsuite / smpi / mpich3-test / rma / transpose7.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 /* This does a transpose with a get operation, fence, and derived
12    datatypes. Uses vector and hvector (Example 3.32 from MPI 1.1
13    Standard). Run on 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     {
39         A_data = (int *) malloc(NROWS * NCOLS * sizeof(int));
40         A = (int **) malloc(NROWS * sizeof(int *));
41
42         A[0] = A_data;
43         for (i=1; i<NROWS; i++)
44             A[i] = A[i-1] + NCOLS;
45
46         if (rank == 0)
47         {
48             for (i=0; i<NROWS; i++)
49                 for (j=0; j<NCOLS; j++)
50                     A[i][j] = -1;
51
52             /* create datatype for one column */
53             MPI_Type_vector(NROWS, 1, NCOLS, MPI_INT, &column);
54             /* create datatype for matrix in column-major order */
55             MPI_Type_hvector(NCOLS, 1, sizeof(int), column, &xpose);
56             MPI_Type_commit(&xpose);
57
58             MPI_Win_create(NULL, 0, 1, MPI_INFO_NULL, CommDeuce, &win);
59
60             MPI_Win_fence(0, win);
61
62             MPI_Get(&A[0][0], NROWS*NCOLS, MPI_INT, 1, 0, 1, xpose, win);
63
64             MPI_Type_free(&column);
65             MPI_Type_free(&xpose);
66
67             MPI_Win_fence(0, win);
68
69             for (j=0; j<NCOLS; j++)
70             {
71                 for (i=0; i<NROWS; i++)
72                 {
73                     if (A[j][i] != i*NCOLS + j)
74                     {
75                         if (errs < 50)
76                         {
77                             printf("Error: A[%d][%d]=%d should be %d\n", j, i,
78                                    A[j][i], i*NCOLS + j);
79                         }
80                         errs++;
81                     }
82                 }
83             }
84             if (errs >= 50)
85             {
86                 printf("Total number of errors: %d\n", errs);
87             }
88         }
89         else
90         {
91             for (i=0; i<NROWS; i++)
92                 for (j=0; j<NCOLS; j++)
93                     A[i][j] = i*NCOLS + j;
94
95             MPI_Win_create(&A[0][0], NROWS*NCOLS*sizeof(int), sizeof(int), MPI_INFO_NULL, CommDeuce, &win);
96             MPI_Win_fence(0, win);
97             MPI_Win_fence(0, win);
98         }
99         MPI_Win_free(&win);
100         free(A);
101     }
102     MPI_Comm_free(&CommDeuce);
103     MTest_Finalize(errs);
104     MPI_Finalize(); 
105     return 0; 
106