-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathparallelthreshold.c
More file actions
129 lines (105 loc) · 2.41 KB
/
parallelthreshold.c
File metadata and controls
129 lines (105 loc) · 2.41 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
121
122
123
124
125
126
127
128
129
/*
Author:Featherfruit on Github
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include<sys/time.h>
#include<omp.h>
int main(int argc, char *argv[])
{
if( argc != 7 )
{
printf("Invaild number of Arguments entered \n");
return -1;
}
int r, c,num_thread;
float p;
r = atoi(argv[1]);
c = atoi(argv[2]);
p = atof(argv[4]);
num_thread = atoi(argv[6]);
srand( time(NULL) );
struct timeval start, end;
gettimeofday(&start, NULL);
int **matrix = (int**)malloc(r*sizeof(int*));
for(int i = 0; i < r; i++)
matrix[i] = malloc(sizeof(int)*c);
int **binaryimage = (int**)malloc(r*sizeof(int*));
for(int i = 0; i < r; i++)
binaryimage[i] = malloc(sizeof(int)*c);
void writeToFile(FILE*, int**, int, int);
int pixelPercentageCompare(int **,int , int ,float, int , int,int);
FILE *fp = fopen(argv[3],"r");
if (fp == NULL)
return 0;
for(int i = 0; i < r; i++)
{
for(int j = 0; j < c; j++){
fscanf(fp, "%d", &matrix[i][j]);
}
}
fclose(fp);
int i,j;
omp_set_dynamic(0);
omp_set_num_threads(num_thread);
#pragma omp parallel for collapse(2) shared(binaryimage) private(i,j)
for(i = 0; i < r; i++)
{
for(j = 0; j < c; j++)
{
int b = pixelPercentageCompare(matrix,r,c, p,i,j,num_thread);
if(b == 1)
{
binaryimage[i][j] = 1;
}
else if (b == 0)
{
binaryimage[i][j] = 0;
}
}
}
FILE *outFPtr = fopen(argv[5], "w");
writeToFile(outFPtr,binaryimage, r, c);
for (int i = 0; i < r; i++)
free(matrix[i]);
free(matrix);
for (int i = 0; i < r; i++)
free(binaryimage[i]);
free(binaryimage);
//end of execution
gettimeofday(&end, NULL);
//execution time calculation
float exec_time = ((end.tv_sec * 1000000 + end.tv_usec) - (start.tv_sec * 1000000 + start.tv_usec));
exec_time /= 1000000;
printf("Time taken to execute: %fs\n\n", exec_time);
}
int pixelPercentageCompare(int **matrix,int r, int c, float p, int i, int j,int num_thread)
{
int count=0;
for(int a = 0; a < r; a++)
{
for(int b = 0; b < c; b++)
{
if(matrix[i][j] > matrix[a][b])
{
count++;
}
}
}
float k = ((count*100)/(r*c));
//printf("k: %lf i: %d j: %d\n",k,i,j);
if(k < p) return 1;
else return 0;
}
//function 2 write to file
void writeToFile(FILE *outFPtr, int **matrix, int r, int c)
{
for(int i = 0; i < r; i++)
{
for(int j = 0; j < c; j++)
fprintf(outFPtr, "%d ", matrix[i][j]);
fprintf(outFPtr, "\n");
}
}