File: winscale2x.cxx

package info (click to toggle)
mpich 4.0.2-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 423,384 kB
  • sloc: ansic: 1,088,434; cpp: 71,364; javascript: 40,763; f90: 22,829; sh: 17,463; perl: 14,773; xml: 14,418; python: 10,265; makefile: 9,246; fortran: 8,008; java: 4,355; asm: 324; ruby: 176; lisp: 19; php: 8; sed: 4
file content (126 lines) | stat: -rw-r--r-- 3,565 bytes parent folder | download | duplicates (4)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
 * Copyright (C) by Argonne National Laboratory
 *     See COPYRIGHT in top-level directory
 */

#include "mpi.h"
#include "mpitestconf.h"
#ifdef HAVE_IOSTREAM
// Not all C++ compilers have iostream instead of iostream.h
#include <iostream>
#ifdef HAVE_NAMESPACE_STD
// Those that do often need the std namespace; otherwise, a bare "cout"
// is likely to fail to compile
using namespace std;
#endif
#else
#include <iostream.h>
#endif
#include "mpitestcxx.h"

/*
 * buf is a 2-d array, stored as a 1-d vector, with
 * buf(i,j) = buf[i + nrows*j], 0<=i<nrows, 0<=j<ncols+2
 * We store into cols 1<=j<=ncols, and put into the two
 * "ghost" columns.
 */
#define MAX_NROWS 25
#define MAX_NCOLS 12
int main(int argc, char *argv[])
{
    MPI::Intracomm comm;
    int errs = 0;
    MPI::Win win;
    int i, j, nrows = MAX_NROWS, ncols = MAX_NCOLS;
    int left, right, ans, size, rank;
    int buf[MAX_NROWS * (MAX_NCOLS + 2)];
    bool flag;
    MPI::Aint aint;
    MPI::Group group, group2;
    int nneighbors, nbrs[2];

    MTest_Init();

    while (MTestGetIntracommGeneral(comm, 2, false)) {
        aint = nrows * (ncols + 2) * sizeof(int);
        win = MPI::Win::Create(buf, aint, sizeof(int) * nrows, MPI::INFO_NULL, comm);
        size = comm.Get_size();
        rank = comm.Get_rank();

        // Create the group for the neighbors
        nneighbors = 0;
        left = rank - 1;
        if (left < 0)
            left = MPI::PROC_NULL;
        else {
            nbrs[nneighbors++] = left;
        }
        right = rank + 1;
        if (right >= size)
            right = MPI::PROC_NULL;
        else {
            nbrs[nneighbors++] = right;
        }

        group = comm.Get_group();
        group2 = group.Incl(nneighbors, nbrs);
        group.Free();

        // Initialize the buffer
        for (i = 0; i < nrows; i++) {
            buf[i] = -1;
            buf[(ncols + 1) * nrows + i] = -1;
        }
        for (j = 0; j < ncols; j++) {
            for (i = 0; i < nrows; i++) {
                buf[i + (j + 1) * nrows] = rank * (ncols * nrows) + i + j * nrows;
            }
        }

        // The actual exchange
        win.Post(group2, 0);
        win.Start(group2, 0);

        win.Put(&buf[0 + nrows], nrows, MPI::INT, left, ncols + 1, nrows, MPI::INT);
        win.Put(&buf[0 + ncols * nrows], nrows, MPI::INT, right, 0, nrows, MPI::INT);
        win.Complete();
        flag = false;
        while (!flag) {
            flag = win.Test();
        }

        // Check the results
        if (left != MPI::PROC_NULL) {
            for (i = 0; i < nrows; i++) {
                ans = rank * (ncols * nrows) - nrows + i;
                if (buf[i] != ans) {
                    errs++;
                    if (errs != 10) {
                        cout << rank << " buf[" << i << ",0] = " <<
                            buf[i] << " expected " << ans << "\n";
                    }
                }
            }
        }
        if (right != MPI::PROC_NULL) {
            for (i = 0; i < nrows; i++) {
                ans = (rank + 1) * (ncols * nrows) + i;
                if (buf[i + (ncols + 1) * nrows] != ans) {
                    errs++;
                    if (errs <= 10) {
                        cout << rank << " buf[" << i << ",ncols+1] = " <<
                            buf[i + (ncols + 1) * nrows] << " expected " << ans << "\n";
                    }
                }
            }
        }


        group2.Free();
        win.Free();
        MTestFreeComm(comm);
    }

    MTest_Finalize(errs);
    return 0;
}