-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy paththreads_dist.cpp
55 lines (45 loc) · 1.16 KB
/
threads_dist.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
#include <faasm/array.h>
#include <faasm/compare.h>
#include <faasm/faasm.h>
#include <pthread.h>
#define N_THREADS 4
#define ARRAY_KEY "shared_array"
void* threadIncrement(void* voidArgs)
{
auto threadNo = *((int*)voidArgs);
faasm::AsyncArray<int> distArray(ARRAY_KEY, N_THREADS);
// Do the update
distArray.pullLazy();
distArray[threadNo] = threadNo;
distArray.push();
return nullptr;
}
int main(int argc, char* argv[])
{
faasm::AsyncArray<int> distArray(ARRAY_KEY, N_THREADS);
distArray.zero();
// Set up arguments
pthread_t threads[N_THREADS];
int threadArgs[N_THREADS];
int expected[N_THREADS];
for (int t = 0; t < N_THREADS; t++) {
threadArgs[t] = t;
expected[t] = t;
}
// Spawn threads
for (int t = 0; t < N_THREADS; t++) {
pthread_create(&threads[t], nullptr, threadIncrement, &threadArgs[t]);
}
// Join threads
for (auto& t : threads) {
if (pthread_join(t, nullptr)) {
return 1;
};
}
// Check
distArray.pull();
if (!faasm::compareArrays(distArray.data(), expected, N_THREADS)) {
return 1;
}
return 0;
}