#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
void test0(int x, int y, int a[x][y]) {
printf("writing to array i-j order\n");
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
a[i][j] = i+j;
}
}
}
void test1(int x, int y, int a[x][y]) {
printf("writing to array j-i order\n");
for (int j = 0; j < y; j++) {
for (int i = 0; i < x; i++) {
a[i][j] = i+j;
}
}
}
int main(int argc, char*argv[]) {
size_t x = atoi(argv[2]);
size_t y = atoi(argv[3]);
size_t n_bytes = x * y * sizeof (int);
printf("allocating a %d x %d int array = %.1lfGb\n", (int)x, (int)y, n_bytes/1000000000.0);
void *m = malloc(n_bytes);
if (m == NULL) {
fprintf(stderr, "malloc failed\n");
return 1;
}
if (atoi(argv[1])) {
test1(x, y, m);
} else {
test0(x, y, m);
}
return 0;
}