Logo AND Algorithmique Numérique Distribuée

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