File: tstaticfor.cpp

package info (click to toggle)
wsclean 3.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 8,036 kB
  • sloc: cpp: 71,902; python: 9,239; ansic: 230; makefile: 175; sh: 172
file content (100 lines) | stat: -rw-r--r-- 2,884 bytes parent folder | download | duplicates (3)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <aocommon/staticfor.h>

#include <mutex>

#include <unistd.h>  // for sleep

#include <boost/test/unit_test.hpp>

using aocommon::StaticFor;

BOOST_AUTO_TEST_SUITE(staticfor)

BOOST_AUTO_TEST_CASE(construct) {
  StaticFor<size_t> sFor(4);
  BOOST_CHECK_EQUAL(sFor.NThreads(), 4);
}

BOOST_AUTO_TEST_CASE(run) {
  StaticFor<size_t> loop(4);
  std::mutex mutex;
  std::vector<size_t> counts(10, 0);
  loop.Run(0, 10, [&](size_t a, size_t b) {
    for (size_t iter = a; iter != b; ++iter) {
      std::unique_lock<std::mutex> lock(mutex);
      counts[iter]++;
    }
  });

  std::vector<size_t> ref(10, 1);
  BOOST_CHECK_EQUAL_COLLECTIONS(counts.begin(), counts.end(), ref.begin(),
                                ref.end());
}

BOOST_AUTO_TEST_CASE(single_threaded) {
  StaticFor<size_t> loop(1);
  std::vector<size_t> counts(10, 0);
  loop.Run(0, 10, [&](size_t a, size_t b) {
    for (size_t iter = a; iter != b; ++iter) {
      counts[iter]++;
    }
  });

  std::vector<size_t> ref(10, 1);
  BOOST_CHECK_EQUAL_COLLECTIONS(counts.begin(), counts.end(), ref.begin(),
                                ref.end());
}

BOOST_AUTO_TEST_CASE(resume_run) {
  std::vector<size_t> counts(20, 0);
  std::mutex mutex;
  StaticFor<size_t> loop(40);
  loop.Run(0, 10, [&](size_t a, size_t b) {
    for (size_t iter = a; iter != b; ++iter) {
      std::unique_lock<std::mutex> lock(mutex);
      counts[iter]++;
    }
  });
  std::vector<size_t> ref(20, 0);
  std::fill(ref.begin(), ref.begin() + 10, 1);
  BOOST_CHECK_EQUAL_COLLECTIONS(counts.begin(), counts.end(), ref.begin(),
                                ref.end());

  loop.Run(10, 20, [&](size_t a, size_t b) {
    for (size_t iter = a; iter != b; ++iter) {
      std::unique_lock<std::mutex> lock(mutex);
      counts[iter]++;
    }
  });
  ref = std::vector<size_t>(20, 1);
  BOOST_CHECK_EQUAL_COLLECTIONS(counts.begin(), counts.end(), ref.begin(),
                                ref.end());
}

BOOST_AUTO_TEST_CASE(run_with_thread_id) {
  StaticFor<size_t> loop(4);
  std::mutex mutex;
  std::vector<size_t> counts(10, 0);
  std::vector<size_t> threads(4, 0);
  loop.Run(0, 10, [&](size_t a, size_t b, size_t t) {
    for (size_t iter = a; iter != b; ++iter) {
      std::unique_lock<std::mutex> lock(mutex);
      counts[iter]++;
    }
    std::unique_lock<std::mutex> lock(mutex);
    threads[t]++;
  });

  std::vector<size_t> ref(10, 1);
  BOOST_CHECK_EQUAL_COLLECTIONS(counts.begin(), counts.end(), ref.begin(),
                                ref.end());

  // Not all threads might actually be used, because if one thread
  // finishes before a second thread is starting, the first
  // thread is used to perform the next loop.
  // Therefore all we can check is whether there weren't more than
  // 4 blocks:
  for (size_t i = 0; i != threads.size(); ++i) BOOST_CHECK_LT(threads[i], 5);
}

BOOST_AUTO_TEST_SUITE_END()