Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I need to use mkl_malloc for a Intel_mkl program?

Tags:

intel-mkl

I'm learning to write program with Intel mkl, in the sample codes 'mkl_malloc' and 'mkl_free' are used, and I tried to replace them with 'malloc' and 'free', the program still worked. So, do I need to use the mkl's version?

like image 696
cloudygoose Avatar asked Oct 19 '25 23:10

cloudygoose


1 Answers

You don't strictly need to use the MKL memory functions. Your code will run without errors if you do so. However, for best performance and reproducible results, you should supply the MKL library with data aligned on 64-byte boundaries, and the MKL memory functions are a convenient way to do this.

Here's a reference to performance:

To improve performance of your application that calls Intel MKL, align your arrays on 64-byte boundaries and ensure that the leading dimensions of the arrays are divisible by 64.

And a reference to reproducibility:

One of the ways Intel MKL gets good performance is through use of new instructions made available with successive generations of Intel® processors. Some of these instructions make computation more efficient by performing the same floating point operation on multiple floating point numbers at once. The way some of these instructions are loaded however depends on how the data is situated in memory. If in one run of the program, the data happens to be aligned along a 16-byte boundary, then the first 2 double precision numbers in the array would be grouped together, while in the next run, if the array is offset from that memory boundary, then the 2nd and 3rd double precision numbers are grouped together. This difference in order can cause different results when running the same program two times consecutively with all settings remaining identical.

The differences with unaligned data (if any) can depend on the particular CPU you are using, and I believe that the impact is not as great on recent processors as it was in previous generations. Even so, it's probably still a good idea to align your data if at all possible.

like image 146
rhashimoto Avatar answered Oct 22 '25 04:10

rhashimoto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!