-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.cpp
108 lines (77 loc) · 2.25 KB
/
test.cpp
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
101
102
103
104
105
106
107
108
#include <iostream>
#include <cilk/cilk.h>
#ifdef USE_CILK
#include <cilk/cilk_api.h>
#endif
#include <pthread.h>
size_t NUM_ITERATIONS = 100000000;
// Don't optimize this fuction.
__attribute__((weak, noinline, optnone)) void loopFunction(size_t i, size_t* arr, pthread_t pid) {
// Print the pthread and worker ID for the last iteration.
if (i == NUM_ITERATIONS - 1) {
std::cout << "Pthread ID for i = " << i << ":\t\t" << pthread_self() << "\n";
#ifdef USE_CILK
std::cout << "WorkerID for i = " << i << ":\t\t" <<__cilkrts_get_worker_number() << "\n";
#endif
}
// Check that the pthread id in the loop is always the same as the one before the loop.
if (!pthread_equal(pthread_self(), pid)) {
std::cout << "NOT EQUAL (pid = " << pthread_self() << ")\n";
exit(-1);
}
// Do something.
arr[i] = i;
}
void* something(void* arg) {
size_t* arr = new size_t[NUM_ITERATIONS];
auto pthread_id = pthread_self();
std::cout << "Pthread ID before loop:\t\t" << pthread_self() << "\n";
#ifdef USE_CILK
std::cout << "Worker ID before loop:\t\t" <<__cilkrts_get_worker_number() << "\n";
#endif
// #pragma cilk grainsize 1
cilk_for ( size_t i = 0; i < NUM_ITERATIONS; ++i) {
loopFunction(i, arr, pthread_id);
}
std::cout << "Pthread ID after loop:\t\t" << pthread_self() << "\n";
#ifdef USE_CILK
std::cout << "Worker ID after loop:\t\t" <<__cilkrts_get_worker_number() << "\n";
#endif
std::cout << arr[NUM_ITERATIONS - 1] << "\n";
delete [] arr;
return nullptr;
}
int fib(int n, pthread_t pid) {
if (!pthread_equal(pthread_self(), pid)) {
std::cout << "NOT EQUAL\n";
exit(-1);
}
if (n < 2)
return n;
int x = cilk_spawn fib(n-1, pid);
int y = fib(n-2, pid);
cilk_sync;
return x + y;
}
void* fibHandler(void* arg) {
int n = *(int*)arg;
fib(n, pthread_self());
return nullptr;
}
int main(int argc, char** argv) {
int numThreads = 1;
if (argc > 1) {
numThreads = atoi(argv[1]);
}
pthread_t* threads = new pthread_t[numThreads];
int n = 20;
for (size_t i = 0; i < numThreads; ++i) {
pthread_create(threads + i, nullptr, &something, nullptr);
// Uncomment for fib.
//pthread_create(threads + i, nullptr, &fibHandler, &n);
}
for (size_t i = 0; i < numThreads; ++i) {
pthread_join(threads[i], nullptr);
}
delete[] threads;
}