File: acc_loc.c

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 (142 lines) | stat: -rw-r--r-- 3,609 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
 * Copyright (C) by Argonne National Laboratory
 *     See COPYRIGHT in top-level directory
 */

#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#include "mpitest.h"

typedef struct {
    int val;
    int loc;
} twoint_t;

static int errs = 0;

int main(int argc, char **argv)
{
    int me, nproc;
    twoint_t *data = NULL;
    twoint_t mine;
    MPI_Win win;

    MTest_Init(&argc, &argv);

    MPI_Comm_rank(MPI_COMM_WORLD, &me);
    MPI_Comm_size(MPI_COMM_WORLD, &nproc);

    if (me == 0) {
        MPI_Alloc_mem(sizeof(twoint_t), MPI_INFO_NULL, &data);
    }

    MPI_Win_create(data, me == 0 ? sizeof(twoint_t) : 0, 1, MPI_INFO_NULL, MPI_COMM_WORLD, &win);
    MPI_Win_fence(MPI_MODE_NOPRECEDE, win);

    /* All processes perform MAXLOC and MINLOC operations on a 2INT on rank 0.
     * The loc is the origin process' rank, and the value is (nproc-me).  In
     * the case of MAXLOC, rank 0 should win and in the case of MINLOC, rank
     * nproc-1 should win.
     */

    /** Test MAXLOC **/

    if (me == 0) {
        data->val = 0;
        data->loc = -1;
    }
    MPI_Win_fence(0, win);

    mine.loc = me;
    mine.val = nproc - me;
    MPI_Accumulate(&mine, 1, MPI_2INT, 0, 0, 1, MPI_2INT, MPI_MAXLOC, win);
    MPI_Win_fence(0, win);

    if (me == 0 && (data->loc != 0 || data->val != nproc)) {
        errs++;
        printf("Expected: { loc = %d, val = %d }  Actual: { loc = %d, val = %d }\n",
               0, nproc, data->loc, data->val);
    }

    /** Test MINLOC **/

    if (me == 0) {
        data->val = nproc;
        data->loc = -1;
    }
    MPI_Win_fence(0, win);

    mine.loc = me;
    mine.val = nproc - me;
    MPI_Accumulate(&mine, 1, MPI_2INT, 0, 0, 1, MPI_2INT, MPI_MINLOC, win);
    MPI_Win_fence(0, win);

    if (me == 0 && (data->loc != nproc - 1 || data->val != 1)) {
        errs++;
        printf("Expected: { loc = %d, val = %d }  Actual: { loc = %d, val = %d }\n",
               nproc - 1, 1, data->loc, data->val);
    }

    /* All processes perform MAXLOC and MINLOC operations on a 2INT on rank 0.
     * The loc is the origin process' rank, and the value is 1.  In both cases,
     * rank 0 should win because the values are equal and it has the lowest
     * loc.
     */

    /** Test MAXLOC **/

    if (me == 0) {
        data->val = 0;
        data->loc = -1;
    }
    MPI_Win_fence(MPI_MODE_NOSUCCEED, win);

    mine.loc = me;
    mine.val = 1;

    MPI_Win_lock(MPI_LOCK_SHARED, 0, MPI_MODE_NOCHECK, win);
    MPI_Accumulate(&mine, 1, MPI_2INT, 0, 0, 1, MPI_2INT, MPI_MAXLOC, win);
    MPI_Win_unlock(0, win);

    MPI_Barrier(MPI_COMM_WORLD);

    if (me == 0 && (data->loc != 0 || data->val != 1)) {
        errs++;
        printf("Expected: { loc = %d, val = %d }  Actual: { loc = %d, val = %d }\n",
               0, 1, data->loc, data->val);
    }
    MPI_Win_fence(MPI_MODE_NOPRECEDE, win);

    /** Test MINLOC **/

    if (me == 0) {
        data->val = nproc;
        data->loc = -1;
    }
    MPI_Win_fence(MPI_MODE_NOSUCCEED, win);

    mine.loc = me;
    mine.val = 1;

    MPI_Win_lock(MPI_LOCK_SHARED, 0, MPI_MODE_NOCHECK, win);
    MPI_Accumulate(&mine, 1, MPI_2INT, 0, 0, 1, MPI_2INT, MPI_MINLOC, win);
    MPI_Win_unlock(0, win);

    MPI_Barrier(MPI_COMM_WORLD);

    if (me == 0 && (data->loc != 0 || data->val != 1)) {
        errs++;
        printf("Expected: { loc = %d, val = %d }  Actual: { loc = %d, val = %d }\n",
               0, 1, data->loc, data->val);
    }

    MPI_Win_free(&win);

    if (me == 0) {
        MPI_Free_mem(data);
    }

    MTest_Finalize(errs);
    return MTestReturnValue(errs);
}