-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMethods.h
More file actions
120 lines (113 loc) · 3.18 KB
/
Methods.h
File metadata and controls
120 lines (113 loc) · 3.18 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include "SearchingAlgorithms.h"
#include <omp.h>
#include <time.h>
void SerialProgramming(int arr[], int key, int n, int choice2){
int choice;
struct Args threadArgs;
threadArgs.key = key;
threadArgs.arr=(int *)malloc(sizeof(int)*n);
memcpy(threadArgs.arr, arr, n*sizeof(int));
threadArgs.choice = choice2;
threadArgs.keyFound = -1;
choice = 1; int i = 1;
for (i=1; i<=3; i++){
switch(i){
case 1:
{
threadArgs.start = 0;
threadArgs.end = n;
clock_t begin = clock();
LinearSearchUsingPthreads((void*)&threadArgs);
clock_t end = clock();
double time_spent = (double)(end-begin)/CLOCKS_PER_SEC;
timespent[0] = time_spent;
}
case 2:
{
threadArgs.start = 0;
threadArgs.end = n;
clock_t begin = clock();
BinarySearchUsingPthreads((void*)&threadArgs);
clock_t end = clock();
double time_spent = (double)(end-begin)/CLOCKS_PER_SEC;
timespent[1] = time_spent;
}
case 3:
{
threadArgs.start = 0;
threadArgs.end = n;
clock_t begin = clock();
TernarySearchUsingPthreads((void*)&threadArgs);
clock_t end = clock();
double time_spent = (double)(end-begin)/CLOCKS_PER_SEC;
timespent[2] = time_spent;
}
}
}
}
void* LinearSearchUsingOMP(void *receivedArgs, int n){
struct Args *struct_ptr = (struct Args*) receivedArgs;
int j;
struct_ptr->start=0;
struct_ptr->end=n;
clock_t begin = clock();
#pragma omp parallel for
for(j=struct_ptr->start; j<struct_ptr->end; j++){
if(struct_ptr->key == struct_ptr->arr[j]){
struct_ptr->keyFound = j;
}
}
clock_t end = clock();
double time_spent = (double)(end-begin)/CLOCKS_PER_SEC;
timespent[10] = time_spent;
return 0;
}
void OMPProg(int arr[], int key, int n, int choice){
int tid, numt;
struct Args threadArgs;
threadArgs.arr=(int *)malloc(sizeof(int)*n);
memcpy(threadArgs.arr, arr, n*sizeof(int));
threadArgs.key = key;
threadArgs.choice = choice;
threadArgs.keyFound = -1;
int choice2=2;
int from, to;
int i=0,j=0,noOfThreads=0;
LinearSearchUsingOMP((void*)&threadArgs, n);
for(j=0; j<3; j++){
noOfThreads=(j==0 ? 1 : j==1 ? 2 : j==2 ? 4 : 4);
for(i=2; i<=4; i++){
clock_t begin = clock();
#pragma omp parallel default(shared) private(tid) num_threads(noOfThreads)
{
tid = omp_get_thread_num();
numt = omp_get_num_threads();
threadArgs.start = (n/numt) * tid;
threadArgs.end = (n/numt) * (tid + 1);
switch(choice2){
case 2:
{
BinarySearchUsingPthreads((void*)&threadArgs);
break;
}
case 3:
{
TernarySearchUsingPthreads((void*)&threadArgs);
break;
}
default:
{
break;
}
}
}
choice2++;
}
choice2=2;
}
if(threadArgs.keyFound != -1){
printf("Key Exists at %d", threadArgs.keyFound);
} else {
printf("-1");
}
}