I'm trying to write a super simple C program of the vector multiply-add "axpy" algorithm for integer data types. The program output the execution time to measure the performance of a machine. The matrices are filled by random numbers.
int benchmark(void) {
int N; /* The matrix size, controlled by user input */
int r, c; /* Row and Column number */
int random; /* Random number to fill the matix */
int a = rand() % 20; /* Scale number to multiply x matrix */
printf("Enter the size(N*N) of the matrices(Maximum 1,000,000)\n");
scanf("%d", &N);
if (N > 1000000) {
fprintf(stderr, "Size of matrix is too large!\n");
return 0;
}
/* Initialize and fill the matrix x and y */
int xMatrix[N][N], yMatrix[N][N], resultMatrix[N][N];
/* Compute time */
clock_t t;
t = clock();
for (r = 0; r < N; r++) {
for (c = 0; c < N; c++) {
random = rand() % 100;
xMatrix[r][c] = a * random; /* Multiply matrix x with random value a */
}
}
for (r = 0; r < N; r++) {
for (c = 0; c < N; c++) {
int random = rand() % 100;
yMatrix[r][c] = random;
}
}
/* Add two matrix together */
for (r = 0; r < N; r++) {
for (c = 0; c < N; c++) {
resultMatrix[r][c] = xMatrix[r][c] + yMatrix[r][c];
}
}
t = clock() - t;
double timeTaken = ((double)t) / CLOCKS_PER_SEC;
printf("\n -> Total time : %f seconds\n", timeTaken);
printf("\n -> Vector length : %d", N * N);
}
User controls the size of the matrix.
The program works fine when the value of N
is less than 800
.