Skip to content

Instantly share code, notes, and snippets.

@RSquaredSoftware
Created September 24, 2013 04:56
Show Gist options
  • Save RSquaredSoftware/6680544 to your computer and use it in GitHub Desktop.
Save RSquaredSoftware/6680544 to your computer and use it in GitHub Desktop.
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "sys/time.h"
#define TRIALS 1
#define EXPERIMENTS 4
#define MATRIX_SIZE 2048
// [!!] this _CAN_ break if doing something like max(0, ++i)
// due to double evaluation. Be super extra careful with
// this. At least know it"s here...
#define max(a, b) (((a) > (b)) ? (a) : (b))
short A[MATRIX_SIZE][MATRIX_SIZE],
B[MATRIX_SIZE][MATRIX_SIZE],
C[MATRIX_SIZE][MATRIX_SIZE];
int main(int argc, char* argv[])
{
// Initalize array A and B with "1"s
for (int i = 0; i < MATRIX_SIZE; ++i)
for (int k = 0; k < MATRIX_SIZE; ++k)
A[i][k] = B[i][k] = 1;
// Iterate through the block sizes
for (int block_size = 4; block_size <= (1 << EXPERIMENTS); block_size *= 2)
{
long average = 0;
printf("%d: ", block_size);
// Run TRIALS number of trials for each block size
for (int trial = 0; trial < TRIALS; ++trial)
{
memset(C, 0, sizeof(C[0][0] * MATRIX_SIZE * MATRIX_SIZE));
// Keep track of when we start doing work
struct timeval time_start;
gettimeofday(&time_start, NULL);
// Do the matrix multiplication task
for (int k = 0; k < MATRIX_SIZE; k += block_size) {
printf("%s", "a");
for (int j = 0; j < MATRIX_SIZE; j += block_size) {
printf("%s", "b");
for (int i = 0; i < MATRIX_SIZE; i++) {
printf("%s", "c");
for (int jj = j; jj < max(j + block_size, MATRIX_SIZE); jj++) {
printf("%s", "d");
for (int kk = k; kk < max(k + block_size, MATRIX_SIZE); kk++)
{
printf("%s", "e");
}
}
}
}
}
// C[i][jj] = C[i][jj] + A[i][kk] * B[kk][jj];
// Keep track of when we finish our work
struct timeval time_end;
gettimeofday(&time_end, NULL);
// Keep track of the time for averaging later
long execution_time = (long) (time_end.tv_usec - time_start.tv_usec);
printf("%lu,", execution_time);
}
printf("%lu\n", average / TRIALS);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment