|
Packit |
0848f5 |
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
|
|
Packit |
0848f5 |
/*
|
|
Packit |
0848f5 |
* (C) 2001 by Argonne National Laboratory.
|
|
Packit |
0848f5 |
* See COPYRIGHT in top-level directory.
|
|
Packit |
0848f5 |
*/
|
|
Packit |
0848f5 |
#include "mpi.h"
|
|
Packit |
0848f5 |
#include "stdio.h"
|
|
Packit |
0848f5 |
#include "mpitest.h"
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
/* transposes a matrix using put, fence, and derived
|
|
Packit |
0848f5 |
datatypes. Uses vector and struct (Example 3.33 from MPI 1.1
|
|
Packit |
0848f5 |
Standard). We could use vector and type_create_resized instead. Run
|
|
Packit |
0848f5 |
on 2 processes */
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
#define NROWS 100
|
|
Packit |
0848f5 |
#define NCOLS 100
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
int main(int argc, char *argv[])
|
|
Packit |
0848f5 |
{
|
|
Packit |
0848f5 |
int rank, nprocs, A[NROWS][NCOLS], i, j, blocklen[2];
|
|
Packit |
0848f5 |
MPI_Comm CommDeuce;
|
|
Packit |
0848f5 |
MPI_Aint disp[2];
|
|
Packit |
0848f5 |
MPI_Win win;
|
|
Packit |
0848f5 |
MPI_Datatype column, column1, type[2];
|
|
Packit |
0848f5 |
int errs = 0;
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
MTest_Init(&argc, &argv);
|
|
Packit |
0848f5 |
MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
|
|
Packit |
0848f5 |
MPI_Comm_rank(MPI_COMM_WORLD, &rank;;
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
if (nprocs < 2) {
|
|
Packit |
0848f5 |
printf("Run this program with 2 or more processes\n");
|
|
Packit |
0848f5 |
MPI_Abort(MPI_COMM_WORLD, 1);
|
|
Packit |
0848f5 |
}
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
MPI_Comm_split(MPI_COMM_WORLD, (rank < 2), rank, &CommDeuce);
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
if (rank < 2) {
|
|
Packit |
0848f5 |
if (rank == 0) {
|
|
Packit |
0848f5 |
for (i = 0; i < NROWS; i++)
|
|
Packit |
0848f5 |
for (j = 0; j < NCOLS; j++)
|
|
Packit |
0848f5 |
A[i][j] = i * NCOLS + j;
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
/* create datatype for one column */
|
|
Packit |
0848f5 |
MPI_Type_vector(NROWS, 1, NCOLS, MPI_INT, &column);
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
/* create datatype for one column, with the extent of one
|
|
Packit |
0848f5 |
* integer. we could use type_create_resized instead. */
|
|
Packit |
0848f5 |
disp[0] = 0;
|
|
Packit |
0848f5 |
disp[1] = sizeof(int);
|
|
Packit |
0848f5 |
type[0] = column;
|
|
Packit |
0848f5 |
type[1] = MPI_UB;
|
|
Packit |
0848f5 |
blocklen[0] = 1;
|
|
Packit |
0848f5 |
blocklen[1] = 1;
|
|
Packit |
0848f5 |
MPI_Type_struct(2, blocklen, disp, type, &column1);
|
|
Packit |
0848f5 |
MPI_Type_commit(&column1);
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
MPI_Win_create(NULL, 0, 1, MPI_INFO_NULL, CommDeuce, &win);
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
MPI_Win_fence(0, win);
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
MPI_Put(A, NROWS * NCOLS, MPI_INT, 1, 0, NCOLS, column1, win);
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
MPI_Type_free(&column);
|
|
Packit |
0848f5 |
MPI_Type_free(&column1);
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
MPI_Win_fence(0, win);
|
|
Packit |
0848f5 |
}
|
|
Packit |
0848f5 |
else { /* rank=1 */
|
|
Packit |
0848f5 |
for (i = 0; i < NROWS; i++)
|
|
Packit |
0848f5 |
for (j = 0; j < NCOLS; j++)
|
|
Packit |
0848f5 |
A[i][j] = -1;
|
|
Packit |
0848f5 |
MPI_Win_create(A, NROWS * NCOLS * sizeof(int), sizeof(int), MPI_INFO_NULL, CommDeuce,
|
|
Packit |
0848f5 |
&win);
|
|
Packit |
0848f5 |
MPI_Win_fence(0, win);
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
MPI_Win_fence(0, win);
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
for (j = 0; j < NCOLS; j++) {
|
|
Packit |
0848f5 |
for (i = 0; i < NROWS; i++) {
|
|
Packit |
0848f5 |
if (A[j][i] != i * NCOLS + j) {
|
|
Packit |
0848f5 |
if (errs < 50) {
|
|
Packit |
0848f5 |
printf("Error: A[%d][%d]=%d should be %d\n", j, i,
|
|
Packit |
0848f5 |
A[j][i], i * NCOLS + j);
|
|
Packit |
0848f5 |
}
|
|
Packit |
0848f5 |
errs++;
|
|
Packit |
0848f5 |
}
|
|
Packit |
0848f5 |
}
|
|
Packit |
0848f5 |
}
|
|
Packit |
0848f5 |
if (errs >= 50) {
|
|
Packit |
0848f5 |
printf("Total number of errors: %d\n", errs);
|
|
Packit |
0848f5 |
}
|
|
Packit |
0848f5 |
}
|
|
Packit |
0848f5 |
MPI_Win_free(&win);
|
|
Packit |
0848f5 |
}
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
MPI_Comm_free(&CommDeuce);
|
|
Packit |
0848f5 |
MTest_Finalize(errs);
|
|
Packit |
0848f5 |
MPI_Finalize();
|
|
Packit |
0848f5 |
return 0;
|
|
Packit |
0848f5 |
}
|