File: mutex_bench.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 (78 lines) | stat: -rw-r--r-- 1,714 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
/*
 * Copyright (C) by Argonne National Laboratory
 *     See COPYRIGHT in top-level directory
 */

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#include <mpi.h>
#include "mpitest.h"
#include "mcs_mutex.h"

#define NUM_ITER    1000
#define NUM_MUTEXES 1

const int verbose = 0;
double delay_ctr = 0.0;

int main(int argc, char **argv)
{
    int rank, nproc, i;
    double t_mcs_mtx;
    MPI_Comm mtx_comm;
    MCS_Mutex mcs_mtx;

    MTest_Init(&argc, &argv);

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

#ifdef USE_WIN_SHARED
    MPI_Comm_split_type(MPI_COMM_WORLD, MPI_COMM_TYPE_SHARED, rank, MPI_INFO_NULL, &mtx_comm);
#else
    mtx_comm = MPI_COMM_WORLD;
#endif

    MCS_Mutex_create(0, mtx_comm, &mcs_mtx);

    MPI_Barrier(MPI_COMM_WORLD);
    t_mcs_mtx = MPI_Wtime();

    for (i = 0; i < NUM_ITER; i++) {
        /* Combining trylock and lock here is helpful for testing because it makes
         * CAS and Fetch-and-op contend for the tail pointer. */
#ifdef USE_CONTIGUOUS_RANK
        if (rank < nproc / 2) {
#else
        if (rank % 2) {
#endif
            int success = 0;
            while (!success) {
                MCS_Mutex_trylock(mcs_mtx, &success);
            }
        } else {
            MCS_Mutex_lock(mcs_mtx);
        }
        MCS_Mutex_unlock(mcs_mtx);
    }

    MPI_Barrier(MPI_COMM_WORLD);
    t_mcs_mtx = MPI_Wtime() - t_mcs_mtx;

    MCS_Mutex_free(&mcs_mtx);

    if (rank == 0) {
        if (verbose) {
            printf("Nproc %d, MCS Mtx = %f us\n", nproc, t_mcs_mtx / NUM_ITER * 1.0e6);
        }
    }

    if (mtx_comm != MPI_COMM_WORLD)
        MPI_Comm_free(&mtx_comm);

    MTest_Finalize(0);

    return 0;
}