Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
change some tests to avoid useless global variables
[simgrid.git] / teshsuite / smpi / mpich3-test / rma / mutex_bench.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *
4  *  (C) 2013 by Argonne National Laboratory.
5  *      See COPYRIGHT in top-level directory.
6  */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <assert.h>
11
12 #include <mpi.h>
13 #include "mpitest.h"
14 #include "mcs-mutex.h"
15
16 #define NUM_ITER    1000
17 #define NUM_MUTEXES 1
18
19 const int verbose = 0;
20 double delay_ctr = 0.0;
21
22 int main(int argc, char **argv)
23 {
24     int rank, nproc, i;
25     double t_mcs_mtx;
26     MPI_Comm mtx_comm;
27     MCS_Mutex mcs_mtx;
28
29     MPI_Init(&argc, &argv);
30
31     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
32     MPI_Comm_size(MPI_COMM_WORLD, &nproc);
33
34 #ifdef USE_WIN_SHARED
35     MPI_Comm_split_type(MPI_COMM_WORLD, MPI_COMM_TYPE_SHARED, rank, MPI_INFO_NULL, &mtx_comm);
36 #else
37     mtx_comm = MPI_COMM_WORLD;
38 #endif
39
40     MCS_Mutex_create(0, mtx_comm, &mcs_mtx);
41
42     MPI_Barrier(MPI_COMM_WORLD);
43     t_mcs_mtx = MPI_Wtime();
44
45     for (i = 0; i < NUM_ITER; i++) {
46         /* Combining trylock and lock here is helpful for testing because it makes
47          * CAS and Fetch-and-op contend for the tail pointer. */
48 #ifdef USE_CONTIGUOUS_RANK
49         if (rank < nproc / 2) {
50 #else
51         if (rank % 2) {
52 #endif
53             int success = 0;
54             while (!success) {
55                 MCS_Mutex_trylock(mcs_mtx, &success);
56             }
57         }
58         else {
59             MCS_Mutex_lock(mcs_mtx);
60         }
61         MCS_Mutex_unlock(mcs_mtx);
62     }
63
64     MPI_Barrier(MPI_COMM_WORLD);
65     t_mcs_mtx = MPI_Wtime() - t_mcs_mtx;
66
67     MCS_Mutex_free(&mcs_mtx);
68
69     if (rank == 0) {
70         if (verbose) {
71             printf("Nproc %d, MCS Mtx = %f us\n", nproc, t_mcs_mtx / NUM_ITER * 1.0e6);
72         }
73     }
74
75     if (mtx_comm != MPI_COMM_WORLD)
76         MPI_Comm_free(&mtx_comm);
77
78     MTest_Finalize(0);
79     MPI_Finalize();
80
81     return 0;
82 }