Intel® FPGA SDK for OpenCL™ Pro Edition: Best Practices Guide

ID 683521
Date 6/21/2022
Public

A newer version of this document is available. Customers should click here to go to the newest version.

Document Table of Contents

4.4. Allocating Aligned Memory

When allocating host-side memories that are used to transfer data to and from the FPGA,the memory must be at least 64-byte aligned.

Aligning the host-side memories allows direct memory access (DMA) transfers to occur to and from the FPGA and improves buffer transfer efficiency.

Attention: Depending on how the host-side memory is used, Intel® recommends to allocate more strict alignment. For example, if the allocated memory is used to create a buffer using the CL_MEM_USE_HOST_PTR flag, the memory should also be properly aligned to the data types used to access the buffer in kernels. For more information about the alignment requirements of host-side memory, refer to section C.3 of the OpenCL Specification version 1.2.

To set up aligned memory allocations, add the following source code to your host program:

  • For Windows:
    #define AOCL_ALIGNMENT 64
    #include <malloc.h>
    void *ptr = _aligned_malloc (size, AOCL_ALIGNMENT);

    To free up an aligned memory block, include the function call _aligned_free(ptr);

  • For Linux:
    #define AOCL_ALIGNMENT 64
    #include <stdlib.h>
    void *ptr = NULL;
    posix_memalign (&ptr, AOCL_ALIGNMENT, size);

    To free up an aligned memory block, include the function call free(ptr);