-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpi_faasm.cpp
More file actions
54 lines (42 loc) · 1.31 KB
/
Copy pathpi_faasm.cpp
File metadata and controls
54 lines (42 loc) · 1.31 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
#include <cstdio>
#include <omp.h>
#include <random>
#include <faasm/input.h>
#include <faasm/shared_mem.h>
#define CHUNK_SIZE 100000
#define PI 3.14159
unsigned long genSeed()
{
int threadNum = omp_get_thread_num();
return threadNum * threadNum * 77 - 22 * threadNum + 1927;
}
int main(int argc, char** argv)
{
std::string inputData = faasm::getStringInput("4");
int nWorkers = std::stoi(inputData);
long result = 0;
int nTotal = nWorkers * CHUNK_SIZE;
FAASM_REDUCE(result, FAASM_TYPE_LONG, FAASM_OP_SUM)
#pragma omp parallel num_threads(nWorkers) default(none) shared(nTotal) \
reduction(+ : result)
{
// Different seed per thread
std::uniform_real_distribution<double> unif(0, 1);
std::mt19937_64 generator(genSeed());
#pragma omp for
for (int i = 0; i < nTotal; i++) {
double x = unif(generator);
double y = unif(generator);
if (x * x + y * y <= 1.0) {
result++;
}
}
}
float pi = 4 * ((float)result / (nTotal));
float error = abs(PI - pi);
std::string output = "Pi estimate: " + std::to_string(pi) + " (error " +
std::to_string(error) + ")\n";
printf("%s", output.c_str());
faasm::setStringOutput(output.c_str());
return 0;
}