2011-10-24 10:02:02 +02:00

135 lines
2.8 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <omp.h>
int *matA = 0;
int *matB = 0;
int *matResult = 0;
int matricesize;
double *matAdouble = 0;
double *matBdouble = 0;
double *matResultDouble = 0;
/*
* validate commandline arguments, set global variables as required or inform user of usage and quit the program
*/
void validateArguments(int argc, const char * argv[])
{
if (argc != 3)
{
printf("two parameters expected: filename mode\n");
exit(1);
}
if (strcmp(argv[2],"assembler") != 0 && strcmp(argv[2],"c_row") != 0 && strcmp(argv[2],"c_double") != 0 && strcmp(argv[2],"c_column") != 0) {
printf("Second parameter is invalid.\n");
exit(1);
}
}
/*
* read a matrice file into memory
*/
void readMatricefile(const char *file, char bDouble)
{
// open file
FILE * pFile;
char line[16];
pFile = fopen(file, "r");
if (pFile == NULL)
{
printf("Accessing input file failed\n");
exit(1);
}
// Determine matrice size
fgets(line, 16, pFile);
matricesize = atoi(line);
// Allocate memory
int size = matricesize*matricesize;
if (bDouble)
{
// Double required
matAdouble = (double*) malloc(size * sizeof(double));
matBdouble = (double*) malloc(size * sizeof(double));
matResultDouble = (double*) malloc(size * sizeof(double));
if (matAdouble == NULL || matBdouble == NULL || matResultDouble == NULL)
{
printf("Error allocating memory.\n");
exit(1);
}
// continue reading
int i = 0;
while (fgets(line, 16, pFile) != NULL && i < 2*size)
{
if (i < size)
matAdouble[i] = strtod(line, NULL);
else
matBdouble[i - size] = strtod(line, NULL);
i++;
}
}
else
{
// Integers required
matA = (int*) malloc(size * sizeof(int));
matB = (int*) malloc(size * sizeof(int));
matResult = (int*) malloc(size * sizeof(int));
if (matA == NULL || matB == NULL || matResult == NULL)
{
printf("Error allocating memory.\n");
exit(1);
}
// continue reading
int i = 0;
while (fgets(line, 16, pFile) != NULL && i < 2*size)
{
if (i < size)
matA[i] = atoi(line);
else
matB[i - size] = atoi(line);
i++;
}
}
// done
fclose(pFile);
}
int main (int argc, const char * argv[])
{
// check we got valid arguments, else quit
validateArguments(argc, argv);
// read input matrice file
readMatricefile(argv[1], (strcmp(argv[2],"c_double") == 0) );
// ---- insert your code here ----
// clear memory
if ((strcmp(argv[2],"c_double") != 0))
{
free(matA);
free(matB);
free(matResult);
}
else
{
free(matAdouble);
free(matBdouble);
free(matResultDouble);
}
return 0;
}