#include
#include
#include
#include
Copyright By PowCoder代写 加微信 powcoder
#define BILLION 1000000000.0
#define MAX_LINE_LENGTH 25000
#define BLUR_SIZE 2
int main (int argc, char *argv[])
// Check console errors
if( argc != 6)
printf(“USE LIKE THIS: convolution_serial n_row n_col mat_input.csv mat_output.csv time.csv\n”);
return EXIT_FAILURE;
// Get dims
int n_row = strtol(argv[1], NULL, 10);
int n_col = strtol(argv[2], NULL, 10);
// Get input/output files
FILE* inputFile1 = fopen(argv[3], “r”);
if (inputFile1 == NULL){
printf(“Could not open file %s”,argv[2]);
return EXIT_FAILURE;
FILE* outputFile = fopen(argv[4], “w”);
FILE* timeFile = fopen(argv[5], “w”);
// Matrices to use
int* filterMatrix_h = (int*)malloc(5 * 5 * sizeof(int));
int* inputMatrix_h = (int *) malloc(n_row*n_col*sizeof(int));
int *outputMatrix_h = (int *) malloc(n_row*n_col*sizeof(int));
int *outputMatrix2_h = (int *) malloc(n_row*n_col*sizeof(int));
// read the data from the file
int row_count = 0;
char line[MAX_LINE_LENGTH] = {0};
while (fgets(line, MAX_LINE_LENGTH, inputFile1))
if (line[strlen(line) – 1] != ‘\n’) printf(“\n”);
char *token;
const char s[2] = “,”;
token = strtok(line, s);
int i_col = 0;
while (token != NULL)
inputMatrix_h[row_count*n_col + i_col] = strtol(token, NULL,10 );
token = strtok (NULL, s);
row_count++;
// Filling filter
// 1 0 0 0 1
// 0 1 0 1 0
// 0 0 1 0 0
// 0 1 0 1 0
// 1 0 0 0 1
for(int i = 0; i< 5; i++)
for(int j = 0; j< 5; j++)
filterMatrix_h[i*5+j]=0;
filterMatrix_h[0*5+0]=1;
filterMatrix_h[1*5+1]=1;
filterMatrix_h[2*5+2]=1;
filterMatrix_h[3*5+3]=1;
filterMatrix_h[4*5+4]=1;
filterMatrix_h[4*5+0]=1;
filterMatrix_h[3*5+1]=1;
filterMatrix_h[1*5+3]=1;
filterMatrix_h[0*5+4]=1;
fclose(inputFile1);
// --------------------------------------------------------------------------- //
// ------ Algorithm Start ---------------------------------------------------- //
struct timespec start, end;
clock_gettime(CLOCK_REALTIME, &start);
// Performing convolution
// Take a look at slides about the blurring example
for(int i=0; i
max = outputMatrix_h[curRow*n_col + curCol];
outputMatrix2_h[i*n_col+j] = max;
clock_gettime(CLOCK_REALTIME, &end);
double time_spent2 = (end.tv_sec – start.tv_sec) +
(end.tv_nsec – start.tv_nsec) / BILLION;
// ————————————————————————— //
// —— Algorithm End —————————————————— //
// Save output matrix as csv file
for (int i = 0; i