Using CBLAS API in C - 5.2 English - 57404

AOCL User Guide (57404)

Document ID
57404
Release Date
2025-12-29
Version
5.2 English

This section contains an example application written in C code using the CBLAS API for DGEMM. The following process takes place during the execution of the code:

  1. The CBLAS Layout option is used to choose row-major layout which is consistent with C.

  2. The function arguments are passed by value.

  3. cblas.h is included as a header. A sample command to compile it and link with the AOCL-BLAS library is also shown in the following code:

    // File: CBLAS_DGEMM_usage.c
    // Example code to demonstrate CBLAS DGEMM usage
    
    #include <stdio.h>
    #include "cblas.h"
    #define DIM 2
    
    int main() {
    
       double a[DIM * DIM] = { 1.0, 2.0, 3.0, 4.0 };
       double b[DIM * DIM] = { 5.0, 6.0, 7.0, 8.0 };
       double c[DIM * DIM];
       int I, J, M, N, K, lda, ldb, ldc; double alpha, beta;
    
       M = DIM;
       N = M; K = M;
       lda = M; ldb = K; ldc = M;
       alpha = 1.0;
       beta = 0.0;
    
       printf("a = \\n");
       for ( I = 0; I < M; I ++ ) {
         for ( J = 0; J < K; J ++ ) {
           printf("%f\t", a[I * K + J]);
         }
         printf("\n");
       }
    
       printf("b = \\n");
       for ( I = 0; I < K; I ++ ) {
         for ( J = 0; J < N; J ++ ) {
           printf("%f\t", b[I * N + J]);
         }
         printf("\n");
       }
    
       cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, M, N, K,
       alpha, a, lda, b, ldb, beta, c, ldc);
    
       printf("c = \\n");
       for ( I = 0; I < M; I ++ ) {
         for ( J = 0; J < N; J ++ ) {
           printf("%f\t", c[I * N + J]);
         }
         printf("\n");
       }
    
       return 0;
    }
    

Note

To get the CBLAS API with AOCL-BLAS, you must provide the flag --enable-cblas to the configure command while building the AOCL-BLAS library. Alternatively, use the -DENABLE_CBLAS=ON option to the CMake command.

A sample compilation command with a GCC compiler for the code above is as follows:

$ gcc CBLAS_DGEMM_usage.c -I/path/to/include/aocl-blas/ \
  /path/to/libblis.a -lpthread -lm