-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
67 lines (59 loc) · 1.99 KB
/
main.cpp
File metadata and controls
67 lines (59 loc) · 1.99 KB
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 <iostream>
#include <thread>
#include <vector>
#include <future>
#include <numeric>
#include <exception>
#ifdef TBB_FOUND
#include <tbb/parallel_for.h>
#include <tbb/blocked_range.h>
#include <tbb/global_control.h>
#endif
int main() {
try {
std::cout << "=== Parallelization Library Verification ===\n";
// Test std::thread
std::cout << "[std::thread] Launching 4 threads...\n";
std::vector<std::thread> threads;
for (int i = 0; i < 4; ++i) {
threads.emplace_back([i] {
std::cout << " Thread " << i << " is working\n";
});
}
for (auto& t : threads) t.join();
std::cout << "[std::thread] OK\n";
// Test std::async
std::cout << "[std::async] Launching 2 tasks...\n";
auto f1 = std::async(std::launch::async, [] { return 21; });
auto f2 = std::async(std::launch::async, [] { return 21; });
if ((f1.get() + f2.get()) != 42)
throw std::runtime_error("std::async failed");
std::cout << "[std::async] OK\n";
#ifdef TBB_FOUND
// Test oneTBB
std::cout << "[oneTBB] Parallel for over vector...\n";
const int size = 1000;
std::vector<int> data(size, 1);
std::atomic<int> total = 0;
tbb::parallel_for(
tbb::blocked_range<int>(0, size),
[&](tbb::blocked_range<int> r) {
int local_sum = 0;
for (int i = r.begin(); i < r.end(); ++i)
local_sum += data[i];
total += local_sum;
}
);
if (total != size)
throw std::runtime_error("oneTBB parallel sum failed");
std::cout << "[oneTBB] OK\n";
#else
std::cout << "[oneTBB] Not available.\n";
#endif
std::cout << "=== All available parallel libraries verified successfully ===\n";
return 0;
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << '\n';
return 1;
}
}