Lock Contention

In [1]:
!rm -Rf tmp
!mkdir -p tmp
In [2]:
%%writefile tmp/lock-contention.c

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <omp.h>
#include <math.h>
#include "timing.h"


int main()
{
  const int n = 10*1024*1024/omp_get_max_threads();

  /*
  puts("write");
  for (int i = 0; i<n; ++i)
    a[i] = i;
    */

  omp_lock_t my_lock;

  omp_init_lock(&my_lock);

  double x = 0;

  timestamp_type t1;
  get_timestamp(&t1);

#pragma omp parallel default(none) shared(my_lock) reduction(+:x)
  {
    x = 12;
    for (int ntrips = 0; ntrips < n; ++ntrips)
    {
#define DO \
      omp_set_lock(&my_lock); \
      omp_unset_lock(&my_lock);

      DO DO DO DO DO     DO DO DO DO DO
    }
  }

  timestamp_type t2;
  get_timestamp(&t2);

  double elapsed = timestamp_diff_in_seconds(t1, t2);
  printf("lock cycle rate: %g per s\n", n*10/elapsed);

  omp_destroy_lock(&my_lock);

  return x;
}









        /*
#define DO2 \
      x = sin(x);

      DO2 DO2 DO2 DO2 DO2  DO2 DO2 DO2 DO2 DO2
      */
Writing tmp/lock-contention.c
In [1]:
!cd tmp; gcc -std=gnu99 -fopenmp -lrt -I.. -olock-contention lock-contention.c
In [3]:
!OMP_NUM_THREADS=1 ./tmp/lock-contention
lock cycle rate: 7.93316e+07 per s
In [4]:
!OMP_NUM_THREADS=2 ./tmp/lock-contention
lock cycle rate: 8.48677e+06 per s
In [5]:
!OMP_NUM_THREADS=3 ./tmp/lock-contention
lock cycle rate: 4.9341e+06 per s
In [ ]: