-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSeqSearchOpenMp.c
57 lines (46 loc) · 1.12 KB
/
SeqSearchOpenMp.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
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
#include <semaphore.h>
#include "ArrayGenerator.h"
int n = 1000000;
int t = 10;
int array1[];
int seg_size;
sem_t wait;
int Seq_Search(int array[], int n, int target, int start)
{
int i = start;
int R = start + seg_size;
while(i <= R)
{
if (array[i] == target)
{
sem_wait(&wait);
printf("%d", target);
printf(" Was found at location ");
printf("%d", i);
sem_post(&wait);
//tried to use semaphore to fix the output format
//But the program still works.
break;
}
else{i++;}
}
printf("%d", target);
puts(" Was not found in the array");
}
int main()
{
int array1 = arrayGenerator(n);
seg_size = (n/t);
sem_init(&wait, 0, 1);
#pragma omp parallel num_threads(t)
//for(int i = 0; i < n; i++)
{
int T_id = omp_get_thread_num();
Seq_Search(array1, n , 10000, ((n/t)* T_id));
}
return 0;
}
main();