-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlock-contention.c
67 lines (42 loc) · 939 Bytes
/
lock-contention.c
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
#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
*/