1 /*
2  * Copyright (C) by Argonne National Laboratory
3  *     See COPYRIGHT in top-level directory
4  */
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 
main(int argc,char * argv[])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             for (i = 0; i < NROWS; i++)
47                 for (j = 0; j < NCOLS; j++)
48                     A[i][j] = i * NCOLS + j;
49 
50             /* create datatype for one column */
51             MPI_Type_vector(NROWS, 1, NCOLS, MPI_INT, &column);
52             /* create datatype for matrix in column-major order */
53             MPI_Type_create_hvector(NCOLS, 1, sizeof(int), column, &xpose);
54             MPI_Type_commit(&xpose);
55 
56             MPI_Win_create(NULL, 0, 1, MPI_INFO_NULL, CommDeuce, &win);
57 
58             MPI_Win_fence(0, win);
59 
60             MPI_Put(&A[0][0], NROWS * NCOLS, MPI_INT, 1, 0, 1, xpose, win);
61 
62             MPI_Type_free(&column);
63             MPI_Type_free(&xpose);
64 
65             MPI_Win_fence(0, win);
66         } else if (rank == 1) {
67             for (i = 0; i < NROWS; i++)
68                 for (j = 0; j < NCOLS; j++)
69                     A[i][j] = -1;
70             MPI_Win_create(&A[0][0], NROWS * NCOLS * sizeof(int), sizeof(int), MPI_INFO_NULL,
71                            CommDeuce, &win);
72             MPI_Win_fence(0, win);
73 
74             MPI_Win_fence(0, win);
75 
76             for (j = 0; j < NCOLS; j++) {
77                 for (i = 0; i < NROWS; i++) {
78                     if (A[j][i] != i * NCOLS + j) {
79                         if (errs < 50) {
80                             printf("Error: A[%d][%d]=%d should be %d\n", j, i,
81                                    A[j][i], i * NCOLS + j);
82                         }
83                         errs++;
84                     }
85                 }
86             }
87             if (errs >= 50) {
88                 printf("Total number of errors: %d\n", errs);
89             }
90         }
91 
92         MPI_Win_free(&win);
93 
94         free(A_data);
95         free(A);
96     }
97     MPI_Comm_free(&CommDeuce);
98     MTest_Finalize(errs);
99     return MTestReturnValue(errs);
100 }
101