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 / transpose5.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-cum-accumulate operation. Uses  vector and
12    hvector datatypes (Example 3.32 from MPI 1.1 Standard). Run on 2
13    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] = i*NCOLS + j;
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_Accumulate(&A[0][0], NROWS*NCOLS, MPI_INT, 1, 0, 1, xpose, MPI_SUM, win);
63
64             MPI_Type_free(&column);
65             MPI_Type_free(&xpose);
66
67             MPI_Win_fence(0, win);
68         }
69         else
70         { /* rank=1 */
71             for (i=0; i<NROWS; i++)
72                 for (j=0; j<NCOLS; j++)
73                     A[i][j] = i*NCOLS + j;
74             MPI_Win_create(&A[0][0], NROWS*NCOLS*sizeof(int), sizeof(int), MPI_INFO_NULL, CommDeuce, &win);
75             MPI_Win_fence(0, win);
76
77             MPI_Win_fence(0, win);
78
79             for (j=0; j<NCOLS; j++)
80             {
81                 for (i=0; i<NROWS; i++)
82                 {
83                     if (A[j][i] != i*NCOLS + j + j*NCOLS + i)
84                     {
85                         if (errs < 50)
86                         {
87                             printf("Error: A[%d][%d]=%d should be %d\n", j, i,
88                                    A[j][i], i*NCOLS + j + j*NCOLS + i);
89                         }
90                         errs++;
91                     }
92                 }
93             }
94             if (errs >= 50)
95             {
96                 printf("Total number of errors: %d\n", errs);
97             }
98         }
99
100         MPI_Win_free(&win);
101
102         free(A_data);
103         free(A);
104
105     }
106
107     MPI_Comm_free(&CommDeuce);
108     MTest_Finalize(errs);
109     MPI_Finalize(); 
110     return 0; 
111