-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark.c
More file actions
203 lines (180 loc) · 7.53 KB
/
benchmark.c
File metadata and controls
203 lines (180 loc) · 7.53 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
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <float.h>
#include <time.h>
#include <sys/time.h>
// If you want to modify the kernel, you can do it here! Don't worry about normalizing it, it will be done for you automatically.
float kernel[] = { 0, -1, 0,
-1, 4, -1,
0, -1, 0 };
// Range of the values to be tested.
//int minimum = 32, maximum = 401, step = 92;
int minimum = 400, maximum = 1201, step = 19;
//=============================================================================================
//========================DO NOT MODIFY ANYTHING BEYOND THIS POINT=============================
//=============================================================================================
// Reference function to be compared with your code.
int reference(float* in, float* out, int data_size_X, int data_size_Y,
float* kernel);
// Your function implemented in part1.c
int conv2D(float* in, float* out, int data_size_X, int data_size_Y,
float* kernel);
/*
The normalize assumes that the kernel is a 3 by 3 kernel.
*/
void normalize( float * kernel ) {
int sum = 0;
for (int i = 0; i < 9; i++ ) {
sum += kernel[i];
}
for (int i = 0; i < 9 && sum != 0; i++ ) {
kernel[i] /= sum;
}
}
int main( int argc, char ** argv ) {
// Normalize the kernel so that it produces correct colors.
normalize(kernel);
float *in_image, *out_image, *out_ref_image, *in_image1, *in_image2, *in_image3;
int real_image = 0, repeat = 0;
unsigned char info[54];
int size, color_table_size, color_size, x_dim, y_dim;
unsigned char * color_table;
if ( argc == 3 ) {
x_dim = atoi(argv[1]);
y_dim = atoi(argv[2]);
repeat = 1;
} else if ( argc == 2 ) {
real_image = 1;
repeat = 1;
FILE* f = fopen(argv[1], "rb");
int check = fread(info, sizeof(unsigned char), 54, f); // Read the 54-byte info header
color_table_size = *((int*)(info + 0x0a))- 54;
color_table = (unsigned char *) malloc(sizeof(unsigned char) * color_table_size);
check = fread(color_table, sizeof(unsigned char), color_table_size, f);
color_size = *((int*)(info + 0x1c));
if (*((int*)(info + 0x0e)) != 40 || (color_size != 8 && color_size != 32) ) {
printf( "Sorry this file format is not supported yet. Please use a different image!\nBMP pictures with 8 bits per pixel grayscale (may be different from seemingly grayscale pictures that actually uses 4 colors) and 32 bit per pixel color image.\n" );
return -1;
}
color_size /= 8;
x_dim = *((int*)(info + 0x12));
y_dim = *((int*)(info + 0x16));
x_dim = (x_dim > 0) ? x_dim : -1 * x_dim;
y_dim = (y_dim > 0) ? y_dim : -1 * y_dim;
size = x_dim * y_dim;
unsigned char data[size*color_size];
check = fread(data, sizeof(unsigned char), size * color_size, f);
fclose(f);
in_image = (float *) malloc(sizeof(float) * size);
if (color_size > 1) {
in_image1 = (float *) malloc(sizeof(float) * size);
in_image2 = (float *) malloc(sizeof(float) * size);
in_image3 = (float *) malloc(sizeof(float) * size);
}
out_image = (float *) malloc(sizeof(float) * size);
out_ref_image = (float *) malloc(sizeof(float) * size);
for (int i = 0; i < size; i++) {
in_image[i] = ((float) data[i*color_size])/255;
if (color_size > 1) {
in_image1[i] = ((float) data[i*color_size+1]/255);
in_image2[i] = ((float) data[i*color_size+2]/255);
in_image3[i] = ((float) data[i*color_size+3]/255);
}
}
}
double total_Gflop_s = 0;
double total_iterations = 0;
for ( int counter = 0; counter < 10 && (repeat || (!counter)); counter++ ) {
for ( int x = minimum; x < maximum; x+=step ) {
for ( int y = minimum; y < maximum; y+=step ) {
if (repeat) {
x = x_dim;
y = y_dim;
maximum = ( x_dim > y_dim ) ? y_dim-1 : x_dim-1;
}
if (!real_image) {
in_image = (float*) malloc( x * y * sizeof(float) );
out_image = (float*) malloc( x * y * sizeof(float) );
out_ref_image = (float*) malloc( x * y * sizeof(float) );
for( int i = 0; i < x * y; i++ ) in_image[i] = 2 * drand48() - 1;
}
// Clear the memory of reference and output to be used to check for correctness.
memset( out_ref_image, 0, sizeof(float) * x * y );
reference( in_image, out_ref_image, x, y, kernel );
memset( out_image, 0, sizeof(float) * x * y );
conv2D( in_image, out_image, x, y, kernel );
// Subtract out the difference to figure out how large it is. If the difference is large enough, then error out.
for ( int i = 0; i < x * y; i++ ) {
float temp = out_image[i] - out_ref_image[i];
// if (counter ==0 && x == minimum && y == minimum)
// printf("Ref: %f, Out: %f\n", out_ref_image[i], out_image[i]);
if ( temp * temp > 0.0001 ) {
printf( "FAILURE: error in convolution calculation exceeds an acceptable margin.\n" );
printf( "Expected: %f, received: %f, with image size x = %d, y = %d at array location: %d\n", out_ref_image[i], out_image[i], x, y, i );
return -1;
}
}
double Gflop_s, seconds = -1.0;
for( int n_iterations = 1; seconds < 0.1; n_iterations *= 2) {
/* warm-up */
conv2D( in_image, out_image, x, y, kernel);
/* measure time */
struct timeval start, end;
gettimeofday( &start, NULL );
for( int i = 0; i < n_iterations; i++ )
conv2D( in_image, out_image, x, y, kernel);
gettimeofday( &end, NULL );
seconds = (end.tv_sec - start.tv_sec) + 1.0e-6 * (end.tv_usec - start.tv_usec);
/* compute Gflop/s rate */
Gflop_s = 2e-9 * n_iterations * 2 * x * y * 9 / seconds;
}
printf( "Dimemsions: x = %d, y = %d \t Performance: %g Gflop/s\n", x, y, Gflop_s );
total_Gflop_s += Gflop_s;
total_iterations++;
if (!real_image) {
free(in_image);
free(out_image);
free(out_ref_image);
}
} // End of y loop
} // End of x loop
} // End of counter loop
double average = total_Gflop_s / total_iterations;
printf( "Average Gflop/s = %g\n", average );
if (real_image) {
unsigned char data[size*color_size];
float out_image1[size], out_image2[size];
memset( out_image, 0, sizeof(float) * size );
memset( out_image1, 0, sizeof(float) * size );
memset( out_image2, 0, sizeof(float) * size );
conv2D( in_image, out_image, x_dim, y_dim, kernel );
if (color_size > 1) {
conv2D( in_image1, out_image1, x_dim, y_dim, kernel);
conv2D( in_image2, out_image2, x_dim, y_dim, kernel);
}
for (int i = 0; i < size; i++) {
data[i*color_size] = (unsigned char) (out_image[i] * 255);
if (color_size > 1) {
data[i*color_size+1] = (unsigned char) (out_image1[i] * 255);
data[i*color_size+2] = (unsigned char) (out_image2[i] * 255);
data[i*color_size+3] = (unsigned char) (in_image3[i] * 255);
}
}
FILE* f = fopen("out_img.bmp", "wb");
fwrite(info, sizeof(unsigned char), 54, f);
fwrite(color_table, sizeof(unsigned char), color_table_size, f);
fwrite(data, sizeof(unsigned char), size*color_size, f);
fclose(f);
free(in_image);
free(out_image);
free(out_ref_image);
free(color_table);
if (color_size > 1) {
free(in_image1);
free(in_image2);
free(in_image3);
}
} // End of real image storing
return 0;
} // End of main