-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdenoiser_PTHREADS.c
241 lines (207 loc) · 5.88 KB
/
denoiser_PTHREADS.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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <pthread.h>
#include <math.h>
//#include <papi.h>
#include <string.h>
#define N 10000
#define THREADS 10
#define THREADSWORKER 10
#define TOTAL_ITERATIONS 50000
typedef struct fileinfo
{
char *file_name;
int start_index;
int end_index;
int id;
} fileinfo;
typedef struct threadinfo
{
int start_row;
int end_row;
int id;
} threadinfo;
int matrix[N][N];
int finalmatrix[N][N];
double beta, gammaValue;
void *thread(void *);
int gotospecificline(FILE *, int);
void matrixcopy(void *destmat, void *srcmat);
void *worker(void *);
int main(int argc, char **argv)
{
// long_long papi_time_start, papi_time_stop;
// papi_time_start = PAPI_get_real_usec();
int i, j;
char *file_name = argv[1];
char *file_name_output = argv[2];
beta = atof(argv[3]);
double pi = atof(argv[4]);
gammaValue = log((1 - pi) / pi) / 2;
pthread_t threads[THREADS];
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
for (i = 0; i < THREADS; i++)
{
fileinfo *finfo = (fileinfo *)malloc(sizeof(fileinfo));
finfo->file_name = file_name;
finfo->start_index = i * (N / THREADS);
finfo->end_index = finfo->start_index + (N / THREADS) - 1;
finfo->id = i;
if (pthread_create(&threads[i], NULL, thread, (void *)finfo) != 0)
{
printf("pthread_create failed!\n");
return EXIT_FAILURE;
}
}
for (i = 0; i < THREADS; i++)
{
pthread_join(threads[i], NULL);
}
matrixcopy(finalmatrix, matrix);
pthread_t threadsworker[THREADSWORKER];
for (i = 0; i < THREADSWORKER; i++)
{
threadinfo *tinfo = (threadinfo *)malloc(sizeof(threadinfo));
tinfo->start_row = i * (N / THREADSWORKER);
tinfo->end_row = tinfo->start_row + (N / THREADSWORKER) - 1;
tinfo->id = i;
if (pthread_create(&threadsworker[i], NULL, worker, (void *)tinfo) != 0)
{
printf("pthread_create failed!\n");
return EXIT_FAILURE;
}
}
for (i = 0; i < THREADSWORKER; i++)
{
pthread_join(threadsworker[i], NULL);
}
FILE *file = fopen(file_name_output, "w");
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
{
if (finalmatrix[i][j] < 0)
{
fprintf(file, " %d", finalmatrix[i][j]);
}
else
{
fprintf(file, " %d", finalmatrix[i][j]);
}
}
fprintf(file, "\n");
}
fclose(file);
// papi_time_stop = PAPI_get_real_usec();
// printf("Running time: %dus\n", papi_time_stop - papi_time_start);
pthread_exit(NULL);
}
int gotospecificline(FILE *file, int nextLine)
{
int i;
char *line = NULL;
size_t len = 0;
for (i = 0; i < nextLine; i++)
{
getline(&line, &len, file);
}
return 0;
}
void *thread(void *args)
{
int i, j;
char *line = NULL;
ssize_t c;
fileinfo *finfo = (fileinfo *)args;
FILE *file = fopen(finfo->file_name, "r");
size_t length = 0;
if (c = gotospecificline(file, finfo->start_index) != 0)
{
printf("Error fseek");
}
for (i = finfo->start_index; i <= finfo->end_index; i++)
{
getline(&line, &length, file);
char *tmp = NULL;
for (j = 0; j < N; j++)
{
char val[3];
val[0] = line[(j * 3)];
val[1] = line[(j * 3) + 1];
val[2] = line[(j * 3) + 2];
int x = atoi(val);
matrix[i][j] = x;
}
}
fclose(file);
}
void matrixcopy(void *destmat, void *srcmat)
{
memcpy(destmat, srcmat, N * N * sizeof(int));
}
int getrandom(int lower, int upper)
{
return (rand() % (upper - lower + 1)) + lower;
}
int adder(int rows, int columns, int rowCenter, int columnCenter)
{
int sum = 0;
int i, j;
for (i = rowCenter - 1; i <= rowCenter + 1; ++i)
{
if (i >= 0 && i < rows)
{ // if within row boundaries
for (j = columnCenter - 1; j <= columnCenter + 1; ++j)
{
if (j >= 0 && j < columns)
{ // if within column boundaries
if (i != rowCenter || j != columnCenter)
{ // skip center
sum += finalmatrix[i][j];
}
}
}
}
}
return sum;
};
double randomProbability()
{
return ((double)rand()) / RAND_MAX;
}
void *worker(void *arg)
{
int count = 0;
srand(time(NULL));
threadinfo *tinfo = (threadinfo *)arg;
int iterations = TOTAL_ITERATIONS;
while (iterations--)
{
if (iterations % 1000000 == 0)
{
// printf("thread %d started a new millionth iteration (iterations left: %d)\n", tinfo->id, iterations);
}
/* pick a random pixel */
int rowPosition = getrandom(tinfo->start_row, tinfo->end_row);
int columnPosition = rand() % N;
int rowCount = tinfo->end_row - tinfo->start_row + 1;
/* sum neighbour cells */
int sum = adder(N, N, rowPosition, columnPosition);
/* calculate delta_e */
double deltaE = -2 * gammaValue * matrix[rowPosition][columnPosition] * finalmatrix[rowPosition][columnPosition] - 2 * beta * finalmatrix[rowPosition][columnPosition] * sum;
if (iterations == 3000002)
{
//printf("Thread id: %d ha delta pari a %d, e ha log(randomProbability):%d\n", tinfo->id, deltaE, log(randomProbability()));
}
if (log(randomProbability()) <= deltaE)
{
// flip the pixel
//count ++;
finalmatrix[rowPosition][columnPosition] = -finalmatrix[rowPosition][columnPosition];
}
}
//printf("Thread id: %d ha fatto %d flip\n", tinfo->id, count);
}