]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - dense-linear-algebra-libraries/linalg.git/blob - examples/matmpy/main.c
Test matmpy on ARM and DSP. (hard coded to test on ARM for now, due to
[dense-linear-algebra-libraries/linalg.git] / examples / matmpy / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <math.h>
4 #include <time.h>
6 #include "cblas.h"
8 /* Number of elements in matrix to display */
10 /* For profiling */
11 #define tick()  clock_gettime(CLOCK_MONOTONIC, &t0);
12 #define tock() (clock_gettime(CLOCK_MONOTONIC, &t1), \
13                         t1.tv_sec - t0.tv_sec + (t1.tv_nsec - t0.tv_nsec) / 1e9)
14 #define fout stdout
17 double *A, *B, *C;
18 int m, n, k;
19 double alpha, beta;
20 struct timespec t0, t1;
21 double secs = 0.0;
23 static void report_flops(double secs, int m, int n, int k, int N)
24 {
25   fprintf(fout,"Total time for %d tests: %8.6fs, %5.3f Gflops\n",
26 //          N, secs, (float)N*m*n*(2*k-1) / (secs * 1e6));
27           N, secs, (float)N*2*m*n*k / (secs * 1e9));
28 }
30 double matrix_mult(void) {
31     int i,j;
32     for (i = 0; i < (m*k); i++) {
33         A[i] = (double)rand()/RAND_MAX;
34     }
36     for (i = 0; i < (k*n); i++) {
37         B[i] = (double)rand()/RAND_MAX;
38     }
40     for (i = 0; i < (m*n); i++) {
41         C[i] = 0.0;
42     }
44     tick();
45     cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, m, n, k, alpha, A, k, B, n, beta, C, n);
46     secs += tock();
48     /* We do a simplistic checksum across a subset of the result matrix */
49     double checksum = 0.0;
50     for (i=0; i<m; i++) 
51       for (j=0; j<n; j++) 
52         checksum += C[j+i*n];
53     return checksum;
54 }
56 int main()
57 {
58     int t;
59     double checksum;
60     char *ti_cblas_offload_env;
61     int numtests = 10;
63     /* configuration */
64     m = k = n = 1000;
65     alpha = 0.7; 
66         beta  = 1.3; 
68     /* allocate the matrices */
69     A = (double *)__malloc_ddr( m*k*sizeof( double ) );
70     B = (double *)__malloc_ddr( k*n*sizeof( double ) );
71     C = (double *)__malloc_ddr( m*n*sizeof( double ) );
72     if (A == NULL || B == NULL || C == NULL) {
73       printf( "\nERROR: Can't allocate memory for matrices. Aborting... \n\n");
74       __free_ddr(A);
75       __free_ddr(B);
76       __free_ddr(C);
77       return 1;
78     }
79         
80     srand(123456789);
82     /* Force BLAS execution on ARM due to insufficient MSMC memory. 
83        This will be removed later.   */
84     putenv("TI_CBLAS_OFFLOAD=000");
86     /* Check the environment variable that controls offloading */
87     ti_cblas_offload_env = getenv("TI_CBLAS_OFFLOAD");
88     if(ti_cblas_offload_env == NULL) {
89       printf("Environment variable TI_CBLAS_OFFLOAD is not defined. ");
90       printf("Use default offloading configuration:\n");
91           printf("\tBLAS level 1: running on ARM.\n");
92           printf("\tBLAS level 2: running on ARM.\n");
93           printf("\tBLAS level 3: running on ARM or DSP based on matrix sizes.\n");
94     }
95     else {
96       printf("TI_CBLAS_OFFLOAD is defined as %s\n", ti_cblas_offload_env);
97     }
99     printf ("A(%ix%i) X B(%ix%i) => C(%ix%i)\n", m, k, k, n, m, n);
101     printf("Warming caches (by doing a single matrix-multiply)..\n");
102     checksum = matrix_mult();
104     /* reset secs, so we can now begin the real timing */
105     secs = 0;
107     printf("Now doing %d tests after warming caches\n", numtests);
108     for (t=0; t<numtests; t++)
109       checksum += matrix_mult();
110     report_flops(secs, m, n, k, numtests);
112     printf("Result CHECKSUM: %16.4f\n", checksum);
114     __free_ddr(A);
115     __free_ddr(B);
116     __free_ddr(C);
118     return 0;