File: lockopts.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 (192 lines) | stat: -rw-r--r-- 6,072 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
 * Copyright (C) by Argonne National Laboratory
 *     See COPYRIGHT in top-level directory
 */

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

/* tests passive target RMA on 2 processes. tests the lock-single_op-unlock
   optimization for less common cases:

   origin datatype derived, target datatype predefined

*/
int main(int argc, char *argv[])
{
    int wrank, nprocs, *srcbuf, *rmabuf, i;
    int memsize;
    MPI_Datatype vectype;
    MPI_Win win;
    int errs = 0;

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

    if (nprocs < 2) {
        printf("Run this program with 2 or more processes\n");
        MPI_Abort(MPI_COMM_WORLD, 1);
    }

    memsize = 10 * 4 * nprocs;
    /* Create and initialize data areas */
    srcbuf = (int *) malloc(sizeof(int) * memsize);
    MPI_Alloc_mem(sizeof(int) * memsize, MPI_INFO_NULL, &rmabuf);
    if (!srcbuf || !rmabuf) {
        printf("Unable to allocate srcbuf and rmabuf of size %d\n", memsize);
        MPI_Abort(MPI_COMM_WORLD, 1);
    }
    for (i = 0; i < memsize; i++) {
        rmabuf[i] = -i;
        srcbuf[i] = i;
    }

    MPI_Win_create(rmabuf, memsize * sizeof(int), sizeof(int), MPI_INFO_NULL, MPI_COMM_WORLD, &win);

    /* Vector of 10 elements, separated by 4 */
    MPI_Type_vector(10, 1, 4, MPI_INT, &vectype);
    MPI_Type_commit(&vectype);

    /* Accumulate with a derived origin type and target predefined type */
    if (wrank == 0) {
        MPI_Barrier(MPI_COMM_WORLD);
        MPI_Win_lock(MPI_LOCK_EXCLUSIVE, 0, 0, win);
        for (i = 0; i < 10; i++) {
            if (rmabuf[i] != -i + 4 * i) {
                errs++;
                printf("Acc: expected rmabuf[%d] = %d but saw %d\n", i, -i + 4 * i, rmabuf[i]);
            }
            rmabuf[i] = -i;
        }
        for (i = 10; i < memsize; i++) {
            if (rmabuf[i] != -i) {
                errs++;
                printf("Acc: expected rmabuf[%d] = %d but saw %d\n", i, -i, rmabuf[i]);
                rmabuf[i] = -i;
            }
        }
        MPI_Win_unlock(0, win);
    } else if (wrank == 1) {
        MPI_Win_lock(MPI_LOCK_SHARED, 0, 0, win);
        MPI_Accumulate(srcbuf, 1, vectype, 0, 0, 10, MPI_INT, MPI_SUM, win);
        MPI_Win_unlock(0, win);
        MPI_Barrier(MPI_COMM_WORLD);
    } else {
        MPI_Barrier(MPI_COMM_WORLD);
    }

    MPI_Barrier(MPI_COMM_WORLD);

    /* Put with a derived origin type and target predefined type */
    if (wrank == 0) {
        MPI_Barrier(MPI_COMM_WORLD);
        MPI_Win_lock(MPI_LOCK_EXCLUSIVE, 0, 0, win);
        for (i = 0; i < 10; i++) {
            if (rmabuf[i] != 4 * i) {
                errs++;
                printf("Put: expected rmabuf[%d] = %d but saw %d\n", i, 4 * i, rmabuf[i]);
            }
            rmabuf[i] = -i;
        }
        for (i = 10; i < memsize; i++) {
            if (rmabuf[i] != -i) {
                errs++;
                printf("Put: expected rmabuf[%d] = %d but saw %d\n", i, -i, rmabuf[i]);
                rmabuf[i] = -i;
            }
        }
        MPI_Win_unlock(0, win);
    } else if (wrank == 1) {
        MPI_Win_lock(MPI_LOCK_SHARED, 0, 0, win);
        MPI_Put(srcbuf, 1, vectype, 0, 0, 10, MPI_INT, win);
        MPI_Win_unlock(0, win);
        MPI_Barrier(MPI_COMM_WORLD);
    } else {
        MPI_Barrier(MPI_COMM_WORLD);
    }

    MPI_Barrier(MPI_COMM_WORLD);

    /* Put with a derived origin type and target predefined type, with
     * a get (see the move-to-end optimization) */
    if (wrank == 0) {
        MPI_Barrier(MPI_COMM_WORLD);
        MPI_Win_lock(MPI_LOCK_EXCLUSIVE, 0, 0, win);
        for (i = 0; i < 10; i++) {
            if (rmabuf[i] != 4 * i) {
                errs++;
                printf("Put: expected rmabuf[%d] = %d but saw %d\n", i, 4 * i, rmabuf[i]);
            }
            rmabuf[i] = -i;
        }
        for (i = 10; i < memsize; i++) {
            if (rmabuf[i] != -i) {
                errs++;
                printf("Put: expected rmabuf[%d] = %d but saw %d\n", i, -i, rmabuf[i]);
                rmabuf[i] = -i;
            }
        }
        MPI_Win_unlock(0, win);
    } else if (wrank == 1) {
        int val;
        MPI_Win_lock(MPI_LOCK_SHARED, 0, 0, win);
        MPI_Get(&val, 1, MPI_INT, 0, 10, 1, MPI_INT, win);
        MPI_Put(srcbuf, 1, vectype, 0, 0, 10, MPI_INT, win);
        MPI_Win_unlock(0, win);
        MPI_Barrier(MPI_COMM_WORLD);
        if (val != -10) {
            errs++;
            printf("Get: Expected -10, got %d\n", val);
        }
    } else {
        MPI_Barrier(MPI_COMM_WORLD);
    }

    MPI_Barrier(MPI_COMM_WORLD);

    /* Put with a derived origin type and target predefined type, with
     * a get already at the end (see the move-to-end optimization) */
    if (wrank == 0) {
        MPI_Barrier(MPI_COMM_WORLD);
        MPI_Win_lock(MPI_LOCK_EXCLUSIVE, 0, 0, win);
        for (i = 0; i < 10; i++) {
            if (rmabuf[i] != 4 * i) {
                errs++;
                printf("Put: expected rmabuf[%d] = %d but saw %d\n", i, 4 * i, rmabuf[i]);
            }
            rmabuf[i] = -i;
        }
        for (i = 10; i < memsize; i++) {
            if (rmabuf[i] != -i) {
                errs++;
                printf("Put: expected rmabuf[%d] = %d but saw %d\n", i, -i, rmabuf[i]);
                rmabuf[i] = -i;
            }
        }
        MPI_Win_unlock(0, win);
    } else if (wrank == 1) {
        int val;
        MPI_Win_lock(MPI_LOCK_SHARED, 0, 0, win);
        MPI_Put(srcbuf, 1, vectype, 0, 0, 10, MPI_INT, win);
        MPI_Get(&val, 1, MPI_INT, 0, 10, 1, MPI_INT, win);
        MPI_Win_unlock(0, win);
        MPI_Barrier(MPI_COMM_WORLD);
        if (val != -10) {
            errs++;
            printf("Get: Expected -10, got %d\n", val);
        }
    } else {
        MPI_Barrier(MPI_COMM_WORLD);
    }

    MPI_Win_free(&win);
    MPI_Free_mem(rmabuf);
    free(srcbuf);
    MPI_Type_free(&vectype);

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