-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprime_omp.c
68 lines (53 loc) · 1.46 KB
/
prime_omp.c
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
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
int *myArray;
int *myPrime;
int primeCount = 0;
void isPrime(int number){
int b;
int tid = omp_get_thread_num();
printf("Checking array item %d, value of %d on thread %d\n", number, myArray[number], tid);
for(b = 2; b <= myArray[number]; b++){
if(myArray[number] % b == 0){
break;
}
}
//int *isprime = (int *) malloc(sizeof(int));
if(b == myArray[number]){
myPrime[number]= myArray[number];
primeCount++;
}else{
myPrime[number] = 0;
}
}
int main(int argc, char* argv[]){
int size = atoi(argv[3]) - atoi(argv[2]);
int start = atoi(argv[2]);
int iT = atoi(argv[1]);
printf("Array size will be %d\n", size);
myArray = calloc(size, sizeof(int));
myPrime = calloc(size, sizeof(int));
for (int i = 0; i < size; i++){
myArray[i] = start;
start++;
}
/*for (int i = 0; i < size; i++){
printf("Array item %d has a value of %d\n", i, myArray[i]);
}*/
omp_set_num_threads(iT);
#pragma omp parallel
{
# pragma omp for
for (int i = 0; i < size; i++){
isPrime(i);
}
}
for (int i = 0; i < size; i++){
if (myPrime[i] != 0){
printf("Number %d is a Prime Number\n", myPrime[i]);
}
}
printf("Total Prime Numbers found is %d\n", primeCount);
}
//gcc prime_omp.c -o prime_omp -fopenmp