File: transpose6.c

package info (click to toggle)
mpich 3.2-7
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 81,040 kB
  • ctags: 68,664
  • sloc: ansic: 358,905; f90: 54,597; perl: 18,527; cpp: 10,203; sh: 9,839; xml: 8,195; fortran: 7,799; makefile: 4,868; ruby: 53; sed: 9; php: 8
file content (71 lines) | stat: -rw-r--r-- 2,063 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
/*
 *  (C) 2001 by Argonne National Laboratory.
 *      See COPYRIGHT in top-level directory.
 */
#include "mpi.h"
#include "stdio.h"
#include "mpitest.h"

/* This does a local transpose-cum-accumulate operation. Uses
   vector and hvector datatypes (Example 3.32 from MPI 1.1
   Standard). Run on 1 process. */

#define NROWS 100
#define NCOLS 100

int main(int argc, char *argv[])
{
    int rank, nprocs, A[NROWS][NCOLS], B[NROWS][NCOLS], i, j;
    MPI_Win win;
    MPI_Datatype column, xpose;
    int errs = 0;

    MTest_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &nprocs);

    if (rank == 0) {
        for (i = 0; i < NROWS; i++)
            for (j = 0; j < NCOLS; j++)
                A[i][j] = B[i][j] = i * NCOLS + j;

        /* create datatype for one column */
        MPI_Type_vector(NROWS, 1, NCOLS, MPI_INT, &column);
        /* create datatype for matrix in column-major order */
        MPI_Type_hvector(NCOLS, 1, sizeof(int), column, &xpose);
        MPI_Type_commit(&xpose);

        MPI_Win_create(B, NROWS * NCOLS * sizeof(int), sizeof(int), MPI_INFO_NULL, MPI_COMM_SELF,
                       &win);

        MPI_Win_fence(0, win);

        MPI_Accumulate(A, NROWS * NCOLS, MPI_INT, 0, 0, 1, xpose, MPI_SUM, win);

        MPI_Type_free(&column);
        MPI_Type_free(&xpose);

        MPI_Win_fence(0, win);

        for (j = 0; j < NCOLS; j++) {
            for (i = 0; i < NROWS; i++) {
                if (B[j][i] != i * NCOLS + j + j * NCOLS + i) {
                    if (errs < 20) {
                        printf("Error: B[%d][%d]=%d should be %d\n", j, i,
                               B[j][i], i * NCOLS + j + j * NCOLS + i);
                    }
                    errs++;
                }
            }
        }
        if (errs >= 20) {
            printf("Total number of errors: %d\n", errs);
        }

        MPI_Win_free(&win);
    }
    MTest_Finalize(errs);
    MPI_Finalize();
    return 0;
}