-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.c
193 lines (175 loc) · 6.68 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#include <math.h>
#include <string.h>
#include <unistd.h>
#include "processing_img.h"
#include "send_wrappers.h"
#include "recv_wrappers.h"
#include "initializations.h"
MPI_Comm CARTESIAN_COMM;
extern MPI_Datatype mpi_block, mpi_block_img;
int* process_img_wrapper(int* block, int block_width, int block_height, int filter_rounds, int convergence_option,
int convergence_rounds, int rank, int img_width, int img_height);
unsigned char* convert_img(int* image, int img_height, int img_width);
/* Main arguments:
* 1. the image to be processed path/<filename>
* 2. the width of the image
* 3. the height of the image
* 4. number of times to apply the filter on the image
* 5. is black and white (0/1)
* 6. convergence (0/1)
* 7. number of rounds to check for convergence
*/
int main(int argc, char *argv[])
{
if ( argc < 7 )
{
printf("Wrong number of parameters given.\n");
printf("Usage: %s <img_filename> <width> <height> <number of repetitions"
"> <isBW image (0/1)> <withConvergence(0/1)> <number of rounds to "
"check for convergence [optional]>\n", argv[0]);
return 1;
}
int convergence_option = atoi(argv[6]);
if ( convergence_option == 1 && argc != 8 )
{
printf("Wrong number of parameters given.\n");
printf("Usage: %s <img_filename> <width> <height> <number of repetitions"
"> <isBW image (0/1)> <withConvergence(0/1)> <number of rounds to "
"check for convergence [optional]>\n", argv[0]);
return 1;
}
char* filename = argv[1];
int img_width = atoi(argv[2]);
int img_height = atoi(argv[3]);
int filter_rounds = atoi(argv[4]);
int isBW = atoi(argv[5]);
int convergence_rounds = -1;
if ( convergence_option == 1 )
convergence_rounds = atoi(argv[7]);
int numprocs, rank;
// Read image and send it
int *block, **rgb_block;
if ( isBW )
block = *(initalization_phase(filename, img_width, img_height, 1));
else
rgb_block = initalization_phase(filename, img_width, img_height, 0);
MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
int block_width = img_width / sqrt(numprocs) + 2;
int block_height = img_height / sqrt(numprocs) + 2;
// Create new communicator (of Cartesian topology)
int dims[2] = {(int) sqrt(numprocs), (int) sqrt(numprocs)};
int cyclic[2] = {1, 1};
MPI_Cart_create(MPI_COMM_WORLD, 2, dims, cyclic, 1, &CARTESIAN_COMM);
int coords[2];
memset(coords, '\0', sizeof(coords));
if (CARTESIAN_COMM != MPI_COMM_NULL) {
MPI_Cart_coords(CARTESIAN_COMM, rank, 2, coords);
MPI_Cart_rank(CARTESIAN_COMM, coords, &rank);
}
else {
printf("Could not properly set CARTESIAN_COMM_WORLD\n");
MPI_Finalize();
return EXIT_FAILURE;
}
MPI_Barrier(CARTESIAN_COMM);
double start_time = MPI_Wtime();
if ( isBW )
{
block = process_img(block, block_width, block_height, filter_rounds, convergence_option, convergence_rounds);
int* image = NULL;
if (rank != 0) {
MPI_Gather(block + block_width + 1, 1, mpi_block, image, 1, mpi_block_img, 0, CARTESIAN_COMM);
}
else {
image = malloc(img_width * img_height * sizeof(int));
MPI_Gather(block + block_width + 1, 1, mpi_block, image, 1, mpi_block_img, 0, CARTESIAN_COMM);
double end_time = MPI_Wtime();
printf("%f\n", end_time - start_time);
// write BW image as raw file
FILE* output = fopen("output.raw", "wb");
unsigned char* img_buffer = malloc(img_height * img_width *
sizeof(unsigned char));
int i;
for (i = 0; i < img_height * img_width; i++) {
img_buffer[i] = (unsigned char) image[i];
}
fwrite(img_buffer, sizeof(char), img_height * img_width, output);
fclose(output);
free(img_buffer);
free(image);
}
free(block);
}
else
{
int** rgb_img_buffer = malloc( 3 * sizeof(int*) );
int i;
for ( i = 0; i < 3; i++)
{
rgb_img_buffer[i] = process_img_wrapper(rgb_block[i], block_width, block_height, filter_rounds, convergence_option,
convergence_rounds, rank, img_width, img_height);
}
if (rank == 0)
{
int i, j;
double end_time = MPI_Wtime();
printf("%f\n", end_time - start_time);
unsigned char** image_buffer = malloc( 3 * sizeof(unsigned char*));
for ( i = 0; i < 3; i++)
{
image_buffer[i] = convert_img(rgb_img_buffer[i], img_height, img_width);
free(rgb_img_buffer[i]);
}
FILE* output = fopen("/tmp/output.raw", "wb");
unsigned char* image = malloc( 3 * img_height * img_width * sizeof(unsigned char));
for ( i = 0 , j = 0; i < img_height * img_width; i++ , j += 3 )
{
image[j] = image_buffer[0][i];
image[j+1] = image_buffer[1][i];
image[j+2] = image_buffer[2][i];
}
fwrite(image, sizeof(char), 3 * img_height * img_width, output);
fclose(output);
for ( i = 0; i < 3; i++)
{
free(image_buffer[i]);
}
free(image_buffer);
free(image);
}
free(rgb_img_buffer);
}
MPI_Finalize();
return 0;
}
int* process_img_wrapper(int* block, int block_width, int block_height, int filter_rounds, int convergence_option,
int convergence_rounds, int rank, int img_width, int img_height)
{
block = process_img(block, block_width, block_height, filter_rounds, convergence_option, convergence_rounds);
int* image = NULL;
if (rank != 0) {
MPI_Gather(block + block_width + 1, 1, mpi_block, image, 1, mpi_block_img, 0, CARTESIAN_COMM);
free(block);
return NULL;
}
else {
image = malloc(img_width * img_height * sizeof(int));
MPI_Gather(block + block_width + 1, 1, mpi_block, image, 1, mpi_block_img, 0, CARTESIAN_COMM);
free(block);
return image;
}
}
unsigned char* convert_img(int* image, int img_height, int img_width)
{
unsigned char* img_buffer = malloc(img_height * img_width *
sizeof(unsigned char));
int i;
for (i = 0; i < img_height * img_width; i++) {
img_buffer[i] = (unsigned char) image[i];
}
return img_buffer;
}