File: lockcontention3.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 (468 lines) | stat: -rw-r--r-- 17,897 bytes parent folder | download | duplicates (3)
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*
 * Copyright (C) by Argonne National Laboratory
 *     See COPYRIGHT in top-level directory
 */

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

#define LAST_TEST 14
#define RMA_SIZE  2048
#define OFFSET_1  7
#define OFFSET_2  83
#define OFFSET_3  157

#define PUT_VAL 0xdcba97
#define ACC_VAL 10771134

/*
 * Additional tests for lock contention.  These are designed to exercise
 * some of the optimizations within MPICH, but all are valid MPI programs.
 * Tests structure includes
 *    lock local (must happen at this time since application can use load
 *                store after the lock)
 *    send message to partner
 *                                  receive message
 *                                  send ack
 *    receive ack
 *    Provide a delay so that
 *      the partner will see the
 *      conflict
 *                                  partner executes:
 *                                  lock         // Note: this may block
 *                                     rma operations (see below)
 *                                  unlock
 *
 *    unlock                        send back to partner
 *    receive from partner
 *    check for correct data
 *
 * The delay may be implemented as a ring of message communication; this
 * is likely to automatically scale the time to what is needed
 */

/* Define a datatype to be used with */
int stride = 11;
int veccount = 7;
MPI_Datatype vectype;
/* Define long RMA ops size */
int longcount = 512;
int medcount = 127;
int mednum = 4;

void RMATest(int i, MPI_Win win, int primary, int *srcbuf, int srcbufsize, int *getbuf,
             int getbufsize);
int RMACheck(int i, int *buf, MPI_Aint bufsize);
int RMACheckGet(int i, MPI_Win win, int *getbuf, MPI_Aint getsize);
void RMATestInit(int i, int *buf, MPI_Aint bufsize);

int main(int argc, char *argv[])
{
    int errs = 0;
    MPI_Win win;
    int *rmabuffer = 0, *getbuf = 0;
    MPI_Aint bufsize = 0, getbufsize = 0;
    int primary, partner, next, wrank, wsize, i;
    int ntest = LAST_TEST;
    int *srcbuf;

    MTest_Init(&argc, &argv);

    /* Determine who is responsible for each part of the test */
    MPI_Comm_rank(MPI_COMM_WORLD, &wrank);
    MPI_Comm_size(MPI_COMM_WORLD, &wsize);
    if (wsize < 3) {
        fprintf(stderr, "This test requires at least 3 processes\n");
        MPI_Abort(MPI_COMM_WORLD, 1);
    }

    primary = 0;
    partner = 1;
    next = wrank + 1;
    if (next == partner)
        next++;
    if (next >= wsize) {
        next = 0;
        if (next == partner)
            next++;
    }

    /* Determine the last test to run (by default, run them all) */
    for (i = 1; i < argc; i++) {
        if (strcmp("-ntest", argv[i]) == 0) {
            i++;
            if (i < argc) {
                ntest = atoi(argv[i]);
            } else {
                fprintf(stderr, "Missing value for -ntest\n");
                MPI_Abort(MPI_COMM_WORLD, 1);
            }
        }
    }

    MPI_Type_vector(veccount, 1, stride, MPI_INT, &vectype);
    MPI_Type_commit(&vectype);

    /* Create the RMA window */
    bufsize = 0;
    if (wrank == primary) {
        bufsize = RMA_SIZE;
        MPI_Alloc_mem(bufsize * sizeof(int), MPI_INFO_NULL, &rmabuffer);
    } else if (wrank == partner) {
        getbufsize = RMA_SIZE;
        getbuf = (int *) malloc(getbufsize * sizeof(int));
        if (!getbuf) {
            fprintf(stderr, "Unable to allocated %d bytes for getbuf\n", (int) getbufsize);
            MPI_Abort(MPI_COMM_WORLD, 1);
        }
    }
    srcbuf = malloc(RMA_SIZE * sizeof(*srcbuf));
    assert(srcbuf);

    MPI_Win_create(rmabuffer, bufsize * sizeof(int), sizeof(int), MPI_INFO_NULL,
                   MPI_COMM_WORLD, &win);

    /* Run a sequence of tests */
    for (i = 0; i <= ntest; i++) {
        if (wrank == primary) {
            MTestPrintfMsg(0, "Test %d\n", i);
            /* Because this lock is local, it must return only when the
             * lock is acquired */
            MPI_Win_lock(MPI_LOCK_EXCLUSIVE, 0, primary, win);
            RMATestInit(i, rmabuffer, bufsize);
            MPI_Send(MPI_BOTTOM, 0, MPI_INT, partner, i, MPI_COMM_WORLD);
            MPI_Send(MPI_BOTTOM, 0, MPI_INT, next, i, MPI_COMM_WORLD);
            MPI_Recv(MPI_BOTTOM, 0, MPI_INT, MPI_ANY_SOURCE, i, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
            MPI_Win_unlock(primary, win);
            MPI_Recv(MPI_BOTTOM, 0, MPI_INT, partner, i, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
            errs += RMACheck(i, rmabuffer, bufsize);
        } else if (wrank == partner) {
            MPI_Recv(MPI_BOTTOM, 0, MPI_INT, primary, i, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
            MPI_Win_lock(MPI_LOCK_EXCLUSIVE, 0, primary, win);
            RMATest(i, win, primary, srcbuf, RMA_SIZE, getbuf, getbufsize);
            MPI_Win_unlock(primary, win);
            errs += RMACheckGet(i, win, getbuf, getbufsize);
            MPI_Send(MPI_BOTTOM, 0, MPI_INT, primary, i, MPI_COMM_WORLD);
        } else {
            MPI_Recv(MPI_BOTTOM, 0, MPI_INT, MPI_ANY_SOURCE, i, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
            MPI_Send(MPI_BOTTOM, 0, MPI_INT, next, i, MPI_COMM_WORLD);
        }
    }

    if (rmabuffer) {
        MPI_Free_mem(rmabuffer);
    }
    if (getbuf) {
        free(getbuf);
    }
    free(srcbuf);
    MPI_Win_free(&win);
    MPI_Type_free(&vectype);

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

/* Perform the tests.
 *
 * The srcbuf must be passed in because the buffer must remain valid
 * until the subsequent unlock call. */
void RMATest(int i, MPI_Win win, int primary, int *srcbuf, int srcbufsize, int *getbuf,
             int getbufsize)
{
    int j, k;
    int *source = srcbuf;
    assert(srcbufsize == RMA_SIZE);

    for (j = 0; j < srcbufsize; j++)
        source[j] = -j;

    switch (i) {
        case 0:        /* Single short put (1 word at OFFSET_1) */
            source[0] = PUT_VAL;
            MPI_Put(source, 1, MPI_INT, primary, OFFSET_1, 1, MPI_INT, win);
            break;
        case 1:        /* Single short accumulate (1 word of value 17 at OFFSET_2) */
            source[0] = ACC_VAL;
            MPI_Accumulate(source, 1, MPI_INT, primary, OFFSET_2, 1, MPI_INT, MPI_SUM, win);
            break;
        case 2:        /* Single short get (1 word at OFFSET_3) */
            getbuf[0] = -1;
            MPI_Get(getbuf, 1, MPI_INT, primary, OFFSET_3, 1, MPI_INT, win);
            break;
        case 3:        /* Datatype single put (strided put) */
            for (j = 0; j < veccount; j++) {
                source[j * stride] = PUT_VAL + j;
            }
            MPI_Put(source, 1, vectype, primary, OFFSET_1, 1, vectype, win);
            break;
        case 4:        /* Datatype single accumulate (strided acc) */
            for (j = 0; j < veccount; j++) {
                source[j * stride] = ACC_VAL + j;
            }
            MPI_Accumulate(source, 1, vectype, primary, OFFSET_2, 1, vectype, MPI_SUM, win);
            break;
        case 5:        /* Datatype single get (strided get) */
            for (j = 0; j < veccount; j++) {
                getbuf[j] = -j;
            }
            MPI_Get(getbuf, 1, vectype, primary, OFFSET_3, 1, vectype, win);
            break;
        case 6:        /* a few small puts (like strided put, but 1 word at a time) */
            for (j = 0; j < veccount; j++) {
                source[j * stride] = PUT_VAL + j;
            }
            for (j = 0; j < veccount; j++) {
                MPI_Put(source + j * stride, 1, MPI_INT, primary,
                        OFFSET_1 + j * stride, 1, MPI_INT, win);
            }
            break;
        case 7:        /* a few small accumulates (like strided acc, but 1 word at a time) */
            for (j = 0; j < veccount; j++) {
                source[j * stride] = ACC_VAL + j;
            }
            for (j = 0; j < veccount; j++) {
                MPI_Accumulate(source + j * stride, 1, MPI_INT, primary,
                               OFFSET_2 + j * stride, 1, MPI_INT, MPI_SUM, win);
            }
            break;
        case 8:        /* a few small gets (like strided get, but 1 word at a time) */
            for (j = 0; j < veccount; j++) {
                getbuf[j * stride] = -j;
            }
            for (j = 0; j < veccount; j++) {
                MPI_Get(getbuf + j * stride, 1, MPI_INT, primary,
                        OFFSET_3 + j * stride, 1, MPI_INT, win);
            }
            break;
        case 9:        /* Single long put (OFFSET_1) */
            for (j = 0; j < longcount; j++)
                source[j] = j;
            MPI_Put(source, longcount, MPI_INT, primary, OFFSET_1, longcount, MPI_INT, win);
            break;
        case 10:       /* Single long accumulate (OFFSET_2) */
            for (j = 0; j < longcount; j++)
                source[j] = j;
            MPI_Accumulate(source, longcount, MPI_INT, primary,
                           OFFSET_2, longcount, MPI_INT, MPI_SUM, win);
            break;
        case 11:       /* Single long get (OFFSET_3) */
            for (j = 0; j < longcount; j++)
                getbuf[j] = -j;
            MPI_Get(getbuf, longcount, MPI_INT, primary, OFFSET_3, longcount, MPI_INT, win);
            break;
        case 12:       /* a few long puts (start at OFFSET_1, medcount) */
            for (j = 0; j < mednum; j++) {
                for (k = 0; k < medcount; k++) {
                    source[j * medcount + k] = j * 2 * medcount + k;
                }
                MPI_Put(source + j * medcount, medcount, MPI_INT, primary,
                        OFFSET_1 + j * 2 * medcount, medcount, MPI_INT, win);
            }
            break;
        case 13:       /* a few long accumulates (start at OFFSET_2, medcount) */
            for (j = 0; j < mednum; j++) {
                for (k = 0; k < medcount; k++) {
                    source[j * medcount + k] = ACC_VAL + j * 2 * medcount + k;
                }
                MPI_Accumulate(source + j * medcount, medcount, MPI_INT, primary,
                               OFFSET_2 + j * 2 * medcount, medcount, MPI_INT, MPI_SUM, win);
            }
            break;
        case 14:       /* a few long gets (start at OFFSET_3, medcount) */
            for (j = 0; j < mednum; j++) {
                for (k = 0; k < medcount; k++) {
                    getbuf[j * medcount + k] = -(j * medcount + k);
                }
                MPI_Get(getbuf + j * medcount, medcount, MPI_INT, primary,
                        OFFSET_3 + j * 2 * medcount, medcount, MPI_INT, win);
            }
            break;
    }
}

int RMACheck(int i, int *buf, MPI_Aint bufsize)
{
    int j, k;
    int errs = 0;

    switch (i) {
        case 0:        /* Single short put (1 word at OFFSET_1) */
            if (buf[OFFSET_1] != PUT_VAL) {
                errs++;
                printf("case 0: value is %d should be %d\n", buf[OFFSET_1], PUT_VAL);
            }
            break;
        case 1:        /* Single short accumulate (1 word of value 17 at OFFSET_2) */
            if (buf[OFFSET_2] != ACC_VAL + OFFSET_2) {
                errs++;
                printf("case 1: value is %d should be %d\n", buf[OFFSET_2], ACC_VAL + OFFSET_2);
            }
            break;
        case 2:        /* Single short get (1 word at OFFSET_3) */
            /* See RMACheckGet */
            break;
        case 3:        /* Datatype single put (strided put) */
        case 6:        /* a few small puts (like strided put, but 1 word at a time) */
            for (j = 0; j < veccount; j++) {
                int pos = j * stride + OFFSET_1;
                if (buf[pos] != PUT_VAL + j) {
                    errs++;
                    printf("case %d: buf[%d] is %d should be %d\n", i, pos, buf[pos], PUT_VAL + j);
                }
            }
            break;
        case 4:        /* Datatype single accumulate (strided acc) */
        case 7:        /* a few small accumulates (like strided acc, but 1 word at a time) */
            for (j = 0; j < veccount; j++) {
                int pos = j * stride + OFFSET_2;
                if (buf[pos] != ACC_VAL + j + OFFSET_2 + j * stride) {
                    errs++;
                    printf("case %d: buf[%d] is %d should be %d\n", i, pos, buf[pos],
                           ACC_VAL + j + OFFSET_2 + j * stride);
                }
            }
            break;
        case 5:        /* Datatype single get (strided get) */
        case 8:        /* a few small gets (like strided get, but 1 word at a time) */
            /* See RMACheckGet */
            break;
        case 9:        /* Single long put (OFFSET_1) */
            for (j = 0; j < longcount; j++) {
                if (buf[OFFSET_1 + j] != j) {
                    errs++;
                    printf("case 9: value is %d should be %d\n", buf[OFFSET_1 + j], OFFSET_1 + j);
                }
            }
            break;
        case 10:       /* Single long accumulate (OFFSET_2) */
            for (j = 0; j < longcount; j++) {
                if (buf[OFFSET_2 + j] != OFFSET_2 + j + j) {
                    errs++;
                    printf("case 10: value is %d should be %d\n", buf[OFFSET_2 + j],
                           OFFSET_2 + j + j);
                }
            }
            break;
        case 11:       /* Single long get (OFFSET_3) */
            /* See RMACheckGet */
            break;
        case 12:       /* a few long puts (start at OFFSET_1, medcount) */
            for (j = 0; j < mednum; j++) {
                for (k = 0; k < medcount; k++) {
                    if (buf[OFFSET_1 + j * 2 * medcount + k] != j * 2 * medcount + k) {
                        errs++;
                        printf("case 12: value is %d should be %d\n",
                               buf[OFFSET_1 + j * 2 * medcount + k], j * 2 * medcount + k);
                    }
                }
            }
            break;
        case 13:       /* a few long accumulates (start at OFFSET_2, medcount) */
            for (j = 0; j < mednum; j++) {
                for (k = 0; k < medcount; k++) {
                    if (buf[OFFSET_2 + j * 2 * medcount + k] !=
                        OFFSET_2 + 2 * j * 2 * medcount + 2 * k + ACC_VAL) {
                        errs++;
                        printf("case 13: value is %d should be %d\n",
                               buf[OFFSET_2 + j * 2 * medcount + k],
                               OFFSET_2 + 2 * j * 2 * medcount + k + ACC_VAL);
                    }
                }
            }
            break;
        case 14:       /* a few long gets (start at OFFSET_3, medcount) */
            /* See RMACheckGet */
            break;
        default:
            fprintf(stderr, "Unrecognized case %d\n", i);
            errs++;
            break;
    }
    return errs;
}

int RMACheckGet(int i, MPI_Win win, int *getbuf, MPI_Aint getsize)
{
    int errs = 0;
    int j, k;

    /* */
    switch (i) {
        case 0:        /* Single short put (1 word at OFFSET_1) */
            break;
        case 1:        /* Single short accumulate (1 word of value 17 at OFFSET_2) */
            break;
        case 2:        /* Single short get (1 word at OFFSET_3) */
            if (getbuf[0] != OFFSET_3) {
                errs++;
                printf("case 2: value is %d should be %d\n", getbuf[0], OFFSET_3);
            }
            break;
        case 3:        /* Datatype single put (strided put) */
            break;
        case 4:        /* Datatype single accumulate (strided acc) */
            break;
        case 5:        /* Datatype single get (strided get) */
        case 8:        /* a few small gets (like strided get, but 1 word at a time) */
            for (j = 0; j < veccount; j++) {
                if (getbuf[j * stride] != OFFSET_3 + j * stride) {
                    errs++;
                    printf("case %d: value is %d should be %d\n", i,
                           getbuf[j * stride], OFFSET_3 + j * stride);
                }
            }

            break;
        case 6:        /* a few small puts (like strided put, but 1 word at a time) */
            break;
        case 7:        /* a few small accumulates (like strided acc, but 1 word at a time) */
            break;
        case 9:        /* Single long put (OFFSET_1) */
            break;
        case 10:       /* Single long accumulate (OFFSET_2) */
            break;
        case 11:       /* Single long get (OFFSET_3) */
            for (j = 0; j < longcount; j++) {
                if (getbuf[j] != OFFSET_3 + j) {
                    errs++;
                    printf("case 11: value is %d should be %d\n", getbuf[j], OFFSET_3 + j);
                }
            }
            break;
        case 12:       /* a few long puts (start at OFFSET_1, medcount) */
            break;
        case 13:       /* a few long accumulates (start at OFFSET_2, medcount) */
            break;
        case 14:       /* a few long gets (start at OFFSET_3, medcount) */
            for (j = 0; j < mednum; j++) {
                for (k = 0; k < medcount; k++) {
                    if (getbuf[j * medcount + k] != OFFSET_3 + j * 2 * medcount + k) {
                        errs++;
                        printf("case 14: buf[%d] value is %d should be %d\n",
                               j * medcount + k,
                               getbuf[j * medcount + k], OFFSET_3 + j * 2 * medcount + k);
                    }
                }
            }
            break;
        default:
            fprintf(stderr, "Unrecognized case %d\n", i);
            errs++;
            break;
    }
    return errs;
}


void RMATestInit(int i, int *buf, MPI_Aint bufsize)
{
    int j;
    for (j = 0; j < bufsize; j++) {
        buf[j] = j;
    }
}