1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
3 * (C) 2001 by Argonne National Laboratory.
4 * See COPYRIGHT in top-level directory.
11 /* transposes a matrix using post/start/complete/wait and derived
12 datatypes. Uses vector and hvector (Example 3.32 from MPI 1.1
13 Standard). Run on 2 processes */
18 int main(int argc, char *argv[])
20 int rank, nprocs, i, j, destrank;
23 MPI_Datatype column, xpose;
24 MPI_Group comm_group, group;
27 MTest_Init(&argc, &argv);
28 MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
29 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
32 printf("Run this program with 2 or more processes\n");
33 MPI_Abort(MPI_COMM_WORLD, 1);
36 MPI_Comm_split(MPI_COMM_WORLD, (rank < 2), rank, &CommDeuce);
39 MPI_Comm_group(CommDeuce, &comm_group);
44 for (i = 0; i < NROWS; i++)
45 for (j = 0; j < NCOLS; j++)
46 A[i][j] = i * NCOLS + j;
48 /* create datatype for one column */
49 MPI_Type_vector(NROWS, 1, NCOLS, MPI_INT, &column);
50 /* create datatype for matrix in column-major order */
51 MPI_Type_hvector(NCOLS, 1, sizeof(int), column, &xpose);
52 MPI_Type_commit(&xpose);
54 #ifdef USE_WIN_ALLOCATE
56 MPI_Win_allocate(0, 1, MPI_INFO_NULL, CommDeuce, &base_ptr, &win);
58 MPI_Win_create(NULL, 0, 1, MPI_INFO_NULL, CommDeuce, &win);
62 MPI_Group_incl(comm_group, 1, &destrank, &group);
63 MPI_Win_start(group, 0, win);
65 MPI_Put(A, NROWS * NCOLS, MPI_INT, 1, 0, 1, xpose, win);
67 MPI_Type_free(&column);
68 MPI_Type_free(&xpose);
70 MPI_Win_complete(win);
74 #ifdef USE_WIN_ALLOCATE
75 MPI_Win_allocate(NROWS * NCOLS * sizeof(int), sizeof(int), MPI_INFO_NULL, CommDeuce, &A,
78 MPI_Alloc_mem(NROWS * NCOLS * sizeof(int), MPI_INFO_NULL, &A);
79 MPI_Win_create(A, NROWS * NCOLS * sizeof(int), sizeof(int), MPI_INFO_NULL, CommDeuce,
82 MPI_Win_lock(MPI_LOCK_SHARED, rank, 0, win);
83 for (i = 0; i < NROWS; i++)
84 for (j = 0; j < NCOLS; j++)
85 A[i * NCOLS + j] = -1;
86 MPI_Win_unlock(rank, win);
89 MPI_Group_incl(comm_group, 1, &destrank, &group);
90 MPI_Win_post(group, 0, win);
93 for (j = 0; j < NCOLS; j++) {
94 for (i = 0; i < NROWS; i++) {
95 if (A[j * NROWS + i] != i * NCOLS + j) {
97 SQUELCH(printf("Error: A[%d][%d]=%d should be %d\n", j, i,
98 A[j * NROWS + i], i * NCOLS + j););
105 printf("Total number of errors: %d\n", errs);
107 #ifndef USE_WIN_ALLOCATE
112 MPI_Group_free(&group);
113 MPI_Group_free(&comm_group);
116 MPI_Comm_free(&CommDeuce);
117 MTest_Finalize(errs);