souffle  2.0.2-371-g6315b36
parallel_utils_test.cpp
Go to the documentation of this file.
1 /*
2  * Souffle - A Datalog Compiler
3  * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved
4  * Licensed under the Universal Permissive License v 1.0 as shown at:
5  * - https://opensource.org/licenses/UPL
6  * - <souffle root>/licenses/SOUFFLE-UPL.txt
7  */
8 
9 /************************************************************************
10  *
11  * @file parallel_utils_test.cpp
12  *
13  * A test case testing the B-trees utilization as sets.
14  *
15  ***********************************************************************/
16 
17 #include "tests/test.h"
18 
20 #include <string>
21 
22 namespace souffle {
23 
24 namespace test {
25 
26 TEST(ParallelUtils, SpinLock) {
27  const int N = 1000000;
28 
29  SpinLock lock;
30 
31  volatile int c = 0;
32 
33 #pragma omp parallel for num_threads(4)
34  for (int i = 0; i < N; i++) {
35  lock.lock();
36  c++;
37  lock.unlock();
38  }
39 
40  EXPECT_EQ(N, c);
41 }
42 
43 TEST(ParallelUtils, ReadWriteLock) {
44  const int N = 1000000;
45  const int K = 10;
46 
47  ReadWriteLock lock;
48 
49  volatile int c = 0;
50 
51 #pragma omp parallel for num_threads(4)
52  for (int i = 0; i < N; i++) {
53  if (i % K == 0) { // 10% write probability
54  lock.start_write();
55  c++;
56  lock.end_write();
57  } else {
58  lock.start_read();
59  // nothing to do here ..
60  lock.end_read();
61  }
62  }
63 
64  EXPECT_EQ(N / K, c);
65 }
66 
67 TEST(ParallelUtils, OptimisticReadWriteLock) {
68  const int N = 1000000;
69  const int K = 10;
70 
71  OptimisticReadWriteLock lock;
72 
73  volatile int c = 0;
74 
75 #pragma omp parallel for num_threads(4)
76  for (int i = 0; i < N; i++) {
77  if (i % K == 0) { // 10% write probability
78  lock.start_write();
79  c++;
80  c++;
81  lock.end_write();
82  } else {
83  bool succ = false;
84  do {
85  auto lease = lock.start_read();
86  // nothing to do here ..
87  int x = c;
88  succ = lock.end_read(lease);
89  EXPECT_TRUE((x % 2 == 0) || !succ);
90  } while (!succ);
91  }
92  }
93 
94  EXPECT_EQ(2 * (N / K), c);
95 }
96 } // namespace test
97 } // end namespace souffle
EXPECT_TRUE
#define EXPECT_TRUE(a)
Definition: test.h:189
ParallelUtil.h
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: test.h:191
i
size_t i
Definition: json11.h:663
test.h
souffle
Definition: AggregateOp.h:25
souffle::test::TEST
TEST(EqRelTest, Scoping)
Definition: binary_relation_test.cpp:51