-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmultitask.cpp
More file actions
100 lines (84 loc) · 2.52 KB
/
multitask.cpp
File metadata and controls
100 lines (84 loc) · 2.52 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
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
#include <iostream>
#include <omp.h>
#include <math.h>
const double PI = 3.141592653589793238463;
using namespace std;
int num_threads, upper_bound;
bool isPrime(int num) {
for (int i=2; i<num; i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
/**
* Gets an array of Prime numbers with size `num`
* Eg:- prime_table(100) - Will generate first 100 prime numbers from [2, 3, ...]
*/
int* prime_table(int num) {
int* primes = new int[num]; // Allocate memory for table
int prime_number = 2, i = 0;
while (i < num) {
if (isPrime(prime_number)) {
primes[i] = prime_number;
i++;
}
prime_number++;
}
return primes;
}
/**
* Computes sine table
* Taken from - https://people.sc.fsu.edu/~jburkardt/c_src/multitask_openmp/multitask_openmp.c
* PS: Not sure what this gives as output, some Expensive operation!!
* Do rise an Issue at https://github.com/nowke/hpc_lab/issues/new if you know the answer!
*/
double* sine_table(int num) {
double* sines = new double[num];
double a;
#pragma omp parallel for private(a) num_threads(num_threads)
for (int i=0; i<num; i++) {
sines[i] = 0.0;
for (int j=0; j <= i; j++) {
a = j * PI / (num - 1);
sines[i] += sin(a);
}
}
return sines;
}
int main(int argc, char* argv[]) {
if (argc < 3) {
cout << "Usage: ./multitask.o upper_bound num_threads" << endl;
return 1;
}
upper_bound = atoi(argv[1]);
num_threads = atoi(argv[2]);
int* primes;
double* sines;
// Time Variables
double start_time = omp_get_wtime();
double task1_start, task2_start, task1_end, task2_end;
#pragma omp parallel sections num_threads(num_threads)
{
#pragma omp section
{
task1_start = omp_get_wtime();
primes = prime_table(upper_bound);
task1_end = omp_get_wtime();
}
#pragma omp section
{
task2_start = omp_get_wtime();
sines = sine_table(upper_bound);
task2_end = omp_get_wtime();
}
}
double end_time = omp_get_wtime();
cout << "Largest Prime: " << primes[upper_bound - 1] << endl;
cout << "Largest Sine: " << sines[upper_bound - 1] << endl;
cout << "Total Time: " << end_time - start_time << "s" << endl;
cout << "Task 1 Time: " << task1_end - task1_start << "s" << endl;
cout << "Task 2 Time: " << task2_end - task2_start << "s" << endl;
return 0;
}