-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprocessing_img.c
200 lines (185 loc) · 5.82 KB
/
processing_img.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
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <mpi.h>
#include <string.h>
#ifdef OMP //if defined during compile, try to use OpenMP
#ifdef _OPENMP
#include <omp.h>
#endif
#endif //OMP
#include "processing_img.h"
#include "send_wrappers.h"
#include "recv_wrappers.h"
#define filter_sum 1
//int filter[9] = { 1, 2, 1, 2, 4, 2, 1, 2, 1 };
//int filter[9] = {0,1,0,1,-4,1,0,1,0}; // Edge detect
int filter[9] = {0,-1,0,-1,5,-1,0,-1,0};
//int filter[9] = {0,0,0,-1,1,0,0,0,0};
//int filter[9] = {0,0,0,0,1,0,0,0,0};
// Forward declarations
int calculate_filtered_pixel(int pixel_idx, int* src_array, int width,
int height, int* filter);
int compare_blocks(int* first_array, int* second_array, int block_width,
int block_height);
int* create_random_array(int width, int height)
{
int array_size = width * height;
int* array = malloc(array_size * sizeof(int));
int i;
srand(time(NULL));
for (i = 0; i < array_size; i++)
{
int shade = rand() % 255;
array[i] = shade;
// array[i] = i;
}
return array;
}
int* process_img(int* block, int block_width, int block_height, int rep_num, int cnv_option, int cnv_rounds) {
int i, rank, proc_num;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &proc_num);
MPI_Request* requests_send, *requests_recv;
int* tmp_block = malloc(block_width * block_height * sizeof(int));
memset(tmp_block, '\0', block_width * block_height * sizeof(int));
for (i = 1; i <= rep_num; i++) {
// Send outer
requests_send = send_data(block, rank, proc_num, block_width, block_height);
// Recv foreign
requests_recv = recv_data(block, rank, proc_num, block_width, block_height);
// Process our (inner) block
compute_inner_values(block, tmp_block, block_width, block_height, filter);
// Wait for our foreign
wait_on_recv(requests_recv);
// Process our (outer) block
compute_outer_values(block, tmp_block, block_width, block_height, filter);
//check for convergence
if ( cnv_option && i % cnv_rounds == 0 )
{
int* cnv_buffer;
int convergence = compare_blocks(block, tmp_block, block_width, block_height);
if (rank == 0)
{
cnv_buffer = malloc(proc_num * sizeof(int));
MPI_Gather(&convergence, 1, MPI_INT, cnv_buffer, 1, MPI_INT, 0, MPI_COMM_WORLD);
int cnv_sum = 0, j;
for ( j = 0; j < proc_num; j++)
cnv_sum += cnv_buffer[j];
MPI_Bcast( &cnv_sum, 1, MPI_INT, 0, MPI_COMM_WORLD);
if (cnv_sum > 0)
break;
}
else
{
int cnv_sum = 0;
MPI_Gather(&convergence, 1, MPI_INT, cnv_buffer, 1, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Bcast( &cnv_sum, 1, MPI_INT, 0, MPI_COMM_WORLD);
if (cnv_sum > 0)
break;
}
}
// Wait on send of our outer
wait_on_send(requests_send);
int* tmp = tmp_block;
tmp_block = block;
block = tmp;
}
free(tmp_block);
return block;
}
void compute_inner_values(int* src_array, int* dest_array,
int width, int height, int* filter)
{
int i, row_number = 3;
int array_size = width * height;
#ifdef OMP
#ifdef _OPENMP
#pragma omp parallel for num_threads(4)
#endif
#endif // OMP
for (i = 2 * width + 2; i < array_size - 2 * width - 2; i++)
{
if( i % (row_number * width - 2) == 0)
{
//to skip elements of the first and last column
i += 3;
row_number++;
continue;
}
dest_array[i] = calculate_filtered_pixel(i, src_array, width, height, filter);
}
}
void compute_outer_values(int* src_array, int* dest_array,
int width, int height, int* filter)
{
int i;
int array_size = width * height;
// Compute outer lines
for (i = width + 1; i < array_size - width - 1; i++)
{
// Go to last line
if (i % (2 * width - 1) == 0) {
i = array_size - 2 * width;
continue;
}
dest_array[i] =
calculate_filtered_pixel(i, src_array, width, height, filter);
}
// Compute outer columns
for (i = 2 * width + 1; i < array_size - 3 * width + 3; i += width)
{
dest_array[i] =
calculate_filtered_pixel(i, src_array, width, height, filter);
int right_idx = i + width - 3;
dest_array[right_idx] =
calculate_filtered_pixel(right_idx, src_array, width, height, filter);
}
}
int calculate_filtered_pixel(int pixel_idx, int* src_array, int width,
int height, int* filter)
{
int sum = 0;
int j, z = 0;
int coef = - 1;
for (j = pixel_idx + coef * width - 1;
j <= pixel_idx + width + 1; j++ )
{
if ((j % width) == (pixel_idx + 2) % width)
{
coef++;
j = pixel_idx + coef * width - 2;
continue;
}
sum += (src_array[j] * filter[z]) / filter_sum;
z++;
}
if (sum > 255)
return 255;
else
return sum;
}
void print_array(int* arr, int width, int height)
{
int i;
for (i = 0; i< width * height;i++)
{
if((i != 0) && (i % width == 0))
printf("\n");
printf("%4d ", arr[i]);
}
putchar('\n');
}
// return 0 if blocks are equal
// return 1 if blocks are diferent
int compare_blocks(int* first_array, int* second_array, int block_width, int block_height)
{
int i;
for ( i = block_width + 1; i < block_height * block_width - block_width - 1; i++)
{
if (first_array[i] != second_array[i])
return 1;
}
return 0;
}