3
/* stdlib.h needed for malloc declaration */
6
#if defined(NEEDS_STDLIB_PROTOTYPES)
11
* This file tests MPI_Type_hindexed by describing parts of a triangular
12
* matrix, stored in a square matrix, and sending sending it.
14
* The matrix is stored in column-major, and the tests use
15
* MPI_Type_vector or MPI_Type_struct to define the elements that are sent
18
int main( int argc, char **argv )
20
MPI_Datatype rowtype, mattype;
23
static int blens[2] = { 1, 1 };
24
MPI_Datatype types[2] = { MPI_INT, MPI_UB };
31
MPI_Init( &argc, &argv );
34
MPI_Comm_rank( MPI_COMM_WORLD, &rank );
36
sbuf = (int *) malloc( mat_n * mat_n * sizeof(int) );
37
rbuf = (int *) malloc( mat_n * mat_n * sizeof(int) );
39
MPI_Abort( MPI_COMM_WORLD, 1 );
42
/* Define a row type based on a strided struct type */
44
displs[1] = mat_n*sizeof(int);
45
MPI_Type_struct( 2, blens, displs, types, &rowtype );
46
MPI_Type_commit( &rowtype );
48
/* Define an hindexed type that defines all of the rows of the
49
triangular part of sbuf */
51
mat_blens = (int *)malloc( mat_n * sizeof(int) );
52
mat_displs = (MPI_Aint *)malloc( mat_n * sizeof(MPI_Aint) );
53
for (i=0; i<mat_n; i++) {
54
mat_blens[i] = mat_n - i;
55
MPI_Address( &sbuf[i + i * mat_n], &mat_displs[i] );
57
mat_displs[i] = mat_displs[i] - mat_displs[0];
60
MPI_Type_hindexed( mat_n, mat_blens, mat_displs, rowtype, &mattype );
61
MPI_Type_commit( &mattype );
62
// MPI_Type_free( &rowtype );
64
/* Load up the data */
65
for (i=0; i<mat_n * mat_n; i++) {
70
/* Send it and receive it in the same order */
71
MPI_Sendrecv( sbuf, 1, mattype, rank, 0, rbuf, 1, mattype, rank, 0,
72
MPI_COMM_WORLD, &status );
74
for (row = 0; row<mat_n; row++) {
75
for (col = row; col<mat_n; col++) {
76
if (rbuf[row + col*mat_n] != sbuf[row + col*mat_n]) {
78
fprintf( stderr, "mat(%d,%d) = %d, not %d\n",
79
row, col, rbuf[row+col*mat_n], sbuf[row+col*mat_n] );
84
/* Send hindexed and receive contiguous */
85
MPI_Sendrecv( sbuf, 1, mattype, rank, 1,
86
rbuf, (mat_n * (mat_n + 1))/2, MPI_INT, rank, 1,
87
MPI_COMM_WORLD, &status );
89
for (row = 0; row<mat_n; row++) {
90
for (col = row; col<mat_n; col++) {
91
if (rbuf[i] != sbuf[row + col*mat_n]) {
93
fprintf( stderr, "rbuf(%d,%d) = %d, not %d\n",
94
row, col, rbuf[i], sbuf[row+col*mat_n] );
100
MPI_Type_free( &mattype );
101
if (err == 0) printf( "Test passed\n" );
102
else printf( "Test failed with %d errors\n", err );