1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
#include <math.h>           // for nan()
#include <stddef.h>         // for size_t, ptrdiff_t
#include <stdio.h>          // for printf
#include <stdlib.h>         // for malloc(), free()
#include <sys/times.h>      // needed for walltime()
#include <unistd.h>         // needed for walltime()

//-- Function for benchmarking and testing -------------------------------------

double
walltime()
{
   struct tms    ts;
   static double ClockTick=0.0;

   if (ClockTick==0.0) {
        ClockTick = 1.0 / ((double) sysconf(_SC_CLK_TCK));
   }
   return ((double) times(&ts)) * ClockTick;
}

//------------------------------------------------------------------------------

void
initGeMatrix(size_t m, size_t n,
             double *A,
             ptrdiff_t incRowA, ptrdiff_t incColA)
{
    for (size_t i=0; i<m; ++i) {
        for (size_t j=0; j<n; ++j) {
            A[i*incRowA + j*incColA] = i*n + j +1;
        }
    }
}

int
main()
{
    printf("#%5s %5s %10s %10s\n", "m", "n", "t (colm)", "t (rowm)");

    for (size_t m=100, n=100; m<=7000 && n<=7000; m+=100, n+=100) {

        printf(" %5ld %5ld ", (long)m, (long)n);


        // bench initialization for row major matrix
        {

            ptrdiff_t incRowA = n;
            ptrdiff_t incColA = 1;

            double *A = malloc(m*n*sizeof(double));
            if (!A) {
                abort();
            }

            double t  = walltime();
            initGeMatrix(m, n, A, incRowA, incColA);
            t = walltime() - t;

            printf("%10.2lf ", t);

            free(A);
        }

        // bench initialization for col major matrix
        {

            ptrdiff_t incRowA = 1;
            ptrdiff_t incColA = m;

            double *A = malloc(m*n*sizeof(double));
            if (!A) {
                abort();
            }

            double t  = walltime();
            initGeMatrix(m, n, A, incRowA, incColA);
            t = walltime() - t;

            printf("%10.2lf ", t);

            free(A);
        }

        printf("\n");

    }

}