Code Examples - 5.2 English - 57404

AOCL User Guide (57404)

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

Basic GEMM Example:

#include "aocl_dlp.h"
#include <stdlib.h>

int main() {
    // Matrix dimensions
    int m = 128, n = 128, k = 128;

    // Allocate matrices
    float *a = (float*)aligned_alloc(64, m * k * sizeof(float));
    float *b = (float*)aligned_alloc(64, k * n * sizeof(float));
    float *c = (float*)aligned_alloc(64, m * n * sizeof(float));

    // Initialize matrices (implementation not shown)
    // ...

    // Perform GEMM: C = A * B
    aocl_gemm_f32f32f32of32(
        'R', 'N', 'N',           // Row-major, no transpose
        m, n, k,                 // Dimensions
        1.0f, a, k, 'N',        // A matrix
        b, n, 'N',              // B matrix
        0.0f, c, n,             // C matrix
        NULL                    // No post-operations
    );

    // Clean up
    free(a); free(b); free(c);
    return 0;
}

GEMM with Post-Operations Example:

#include "aocl_dlp.h"
#include <stdlib.h>
#include <string.h>

int main() {
    // Matrix dimensions
    int m = 128, n = 64, k = 128;

    // Allocate matrices
    float *a = (float*)malloc(m * k * sizeof(float));
    float *b = (float*)malloc(k * n * sizeof(float));
    float *c = (float*)malloc(m * n * sizeof(float));
    float *bias = (float*)malloc(n * sizeof(float));

    // Initialize matrices (implementation not shown)
    // ...

    // Allocate and configure post-operations metadata
    dlp_metadata_t* metadata = (dlp_metadata_t*)malloc(sizeof(dlp_metadata_t));
    memset(metadata, 0, sizeof(dlp_metadata_t));

    // Set up sequence: bias + ReLU
    metadata->seq_length = 2;
    metadata->seq_vector = (DLP_POST_OP_TYPE*)malloc(2 * sizeof(DLP_POST_OP_TYPE));
    metadata->seq_vector[0] = BIAS;
    metadata->seq_vector[1] = ELTWISE;

    // Configure bias
    metadata->bias = (dlp_post_op_bias*)malloc(sizeof(dlp_post_op_bias));
    metadata->bias->bias = bias;
    metadata->bias->stor_type = DLP_F32;

    // Configure ReLU
    metadata->eltwise = (dlp_post_op_eltwise*)malloc(sizeof(dlp_post_op_eltwise));
    metadata->eltwise->sf = NULL;
    metadata->eltwise->algo.alpha = NULL;
    metadata->eltwise->algo.beta = NULL;
    metadata->eltwise->algo.algo_type = RELU;

    // Perform fused GEMM with bias and ReLU: C = ReLU(A * B + bias)
    aocl_gemm_f32f32f32of32(
        'R', 'N', 'N', m, n, k,
        1.0f, a, k, 'N',
        b, n, 'N',
        0.0f, c, n,
        metadata  // Apply post-operations
    );

    // Clean up
    free(metadata->seq_vector);
    free(metadata->bias);
    free(metadata->eltwise);
    free(metadata);
    free(a); free(b); free(c); free(bias);

    return 0;
}