forked from faasm/faasm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreads_dist.cpp
More file actions
53 lines (43 loc) · 1.15 KB
/
Copy paththreads_dist.cpp
File metadata and controls
53 lines (43 loc) · 1.15 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
#include <faasm/faasm.h>
#include <faasm/array.h>
#include <pthread.h>
#include <faasm/compare.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;
}
FAASM_MAIN_FUNC() {
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;
}