Intel® Math Kernel Library 11.3 Update 4 Developer Guide

Example of Data Alignment

Needs for best performance with Intel MKL or for reproducible results from run to run of Intel MKL functions require alignment of data arrays. The following example shows how to align an array on 64-byte boundaries. To do this, use mkl_malloc() in place of system provided memory allocators, as shown in the code example below.

Aligning Addresses on 64-byte Boundaries

            
            // ******* C language *******
            ...
            #include <stdlib.h>
            #include <mkl.h>
            ...
            void *darray;
            int workspace;    
            // Set value of alignment
            int alignment=64;
            ...
            // Allocate aligned workspace
            darray = mkl_malloc( sizeof(double)*workspace, alignment );
            ...
            // call the program using MKL
            mkl_app( darray );
            ...
            // Free workspace
            mkl_free( darray );
            

            ! ******* Fortran language *******
            ...
            ! Set value of alignment
            integer    alignment
            parameter (alignment=64)
            ...
            ! Declare Intel MKL routines
            #ifdef _IA32
            integer mkl_malloc
            #else
            integer*8 mkl_malloc
            #endif
            external mkl_malloc, mkl_free, mkl_app
            ...
            double precision darray
            pointer (p_wrk,darray(1))
            integer workspace
            ...
            ! Allocate aligned workspace
            p_wrk = mkl_malloc( %val(8*workspace), %val(alignment) )
            ...
            ! call the program using Intel MKL
            call mkl_app( darray )
            ...
            ! Free workspace
            call mkl_free(p_wrk)