Logo AND Algorithmique Numérique Distribuée

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