Intel® oneAPI Math Kernel Library
Ask questions and share information with other developers who use Intel® Math Kernel Library.
7032 Discussions

Using Intel MKL with R (Similar to MATLAB Implementation)

Maraki
Beginner
836 Views

Hello,

I've been using the Intel MKL library in MATLAB successfully to optimize a time-consuming subfunction. To do this, I set up the compiler and flags in MATLAB using the following commands:

 

mex -setup
C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2020\windows

mex -v -g example.c -I"C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2020\windows\mkl\include" -L"C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2020\windows\mkl\lib\intel64" -lmkl_intel_lp64 -lmkl_core -lmkl_sequential -lmkl_cd

 

Now, I'm looking to achieve the same optimization in R, but I'm unsure of how to set up the Intel MKL library and compiler in R for a similar purpose. I've installed Intel oneAPI and  I've followed the following steps to set up my environment:

 

# Path to the Intel oneAPI toolkit 
oneapi_dir <- "C:/Program Files (x86)/Intel/oneAPI"
mkl_include_dir <- file.path(oneapi_dir, "mkl", "latest", "include")
mkl_lib_dir <- file.path(oneapi_dir, "mkl", "latest", "lib", "intel64")

# C code file
c_code_file <- "C:\\Users\\Desktop\\example.c"

#
output_executable <- "my_program"

# Define the compilation command with the output executable name
compile_command <- paste(
"icc -o", output_executable,
c_code_file,
paste("-I", mkl_include_dir, collapse = " "),
paste("-L", mkl_lib_dir, collapse = " "),
"-lmkl_intel_lp64 -lmkl_core -lmkl_sequential -lmkl_cdft_core -lmkl_intel_thread -qopenmp"
)

# Compile the C code
system(compile_command)

 

However, when I run this script in R I get this

 

system(compile_command)
[1] 127

 

The c-code is: 

#include <stdio.h>
#include <mkl.h>

int main() {
    // Define vector size and variables
    int n = 1000;
    double *x, *y;
    double result;

    // Allocate memory for the vectors
    x = (double*)malloc(n * sizeof(double));
    y = (double*)malloc(n * sizeof(double));

    // Initialize the vectors with some data (you can modify this)
    for (int i = 0; i < n; i++) {
        x[i] = i + 1.0;
        y[i] = 2.0 * (i + 1.0);
    }

    // Calculate the dot product of the vectors using MKL
    result = cblas_ddot(n, x, 1, y, 1);

    // Print the result
    printf("Dot product: %lf\n", result);

    // Free allocated memory
    free(x);
    free(y);

    return 0;
}

Could someone please guide me on the equivalent steps to perform this in R? I'm particularly interested in how to configure R to use the Intel compiler and link to the MKL library.

Thank you in advance for your help!

Best regards,

0 Kudos
1 Solution
ShanmukhS_Intel
Moderator
755 Views

Hi Maraki,

 

Thanks for posting in Intel communities. Thanks for elaborating on your issue.

 

We have tried running the shared code and below is the result. 

Dot product: 667667000.000000

 

Please refer to the below links for compiling R code with MKL, extended support, and other linking-related details.

 

How to use the BLAS and LAPACK libraries within Intel® oneAPI Math Kernel Library (oneMKL) to improve the performance of R. 

https://www.intel.com/content/www/us/en/developer/articles/technical/using-onemkl-with-r.html

 

Other important data which might be useful to you:

 

Building R with the Intel® Math Kernel Library (Intel® MKL) BLAS and LAPACK to improve the performance of those parts of R that rely on matrix computations. 

https://www.intel.com/content/www/us/en/developer/articles/technical/extending-r-with-intel-mkl.html

 

Linking Intel MKL to R

https://www.intel.com/content/www/us/en/developer/articles/technical/quick-linking-intel-mkl-blas-lapack-to-r.html

 

Could you please get back to us with the MKL version being used and your findings for the code being used by you? 

 

Best Regards,

Shanmukh.SS

 

 

 

View solution in original post

0 Kudos
3 Replies
ShanmukhS_Intel
Moderator
756 Views

Hi Maraki,

 

Thanks for posting in Intel communities. Thanks for elaborating on your issue.

 

We have tried running the shared code and below is the result. 

Dot product: 667667000.000000

 

Please refer to the below links for compiling R code with MKL, extended support, and other linking-related details.

 

How to use the BLAS and LAPACK libraries within Intel® oneAPI Math Kernel Library (oneMKL) to improve the performance of R. 

https://www.intel.com/content/www/us/en/developer/articles/technical/using-onemkl-with-r.html

 

Other important data which might be useful to you:

 

Building R with the Intel® Math Kernel Library (Intel® MKL) BLAS and LAPACK to improve the performance of those parts of R that rely on matrix computations. 

https://www.intel.com/content/www/us/en/developer/articles/technical/extending-r-with-intel-mkl.html

 

Linking Intel MKL to R

https://www.intel.com/content/www/us/en/developer/articles/technical/quick-linking-intel-mkl-blas-lapack-to-r.html

 

Could you please get back to us with the MKL version being used and your findings for the code being used by you? 

 

Best Regards,

Shanmukh.SS

 

 

 

0 Kudos
ShanmukhS_Intel
Moderator
693 Views

Hi Maraki,


A gentle reminder:

Has the information provided helped? Could you please get back to us if you have any updates on your issue?


Best Regards,

Shanmukh.SS


0 Kudos
ShanmukhS_Intel
Moderator
644 Views

Hi Maraki,


A gentle reminder:

Has the information provided helped? Could you please get back to us if you have any updates on your issue?


Best Regards,

Shanmukh.SS


0 Kudos
Reply