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

Integrating a C Subfunction for Optimizing R Code Performance>

Fio
Beginner
1,172 Views

I have an existing R script that performs various operations, and I'm looking to create a C sub function to optimize a specific part of it. My goal is to integrate this C sub function within R Desktop for improved performance and efficiency. 

For example, let's assume that I want to sum the elements obtained by multiplying the vector x and y. In R, I can achieve this with the following code:

 

 

# R code
x <- c(1, 2, 3)
y <- c(4, 5, 6)
z <- sum(x * y)   

 

 

The same operation using oneMKL can be performed like this:

#include <stdio.h>
#include <mkl.h>
int main() {
    double x[] = {1.0, 2.0, 3.0};
    double y[] = {4.0, 5.0, 6.0};
    double z;
    // Perform vector multiplication and summation using Intel MKL
    z = cblas_ddot(3, x, 1, y, 1);
    printf("Result: %lf\n", z);
    return 0;
}

 

 

After compiling the .c function using CMD, I get:

C:\Program Files (x86)\Intel\oneAPI>cd C:/Users/Administrator/Desktop
C:\Users\Administrator\Desktop>icx -o example example.c /Qmkl /MD
Intel(R) oneAPI DPC++/C++ Compiler for applications running on Intel(R) 64, Version 2023.1.0 Build 20230320
Copyright (C) 1985-2023 Intel Corporation. All rights reserved.

C:\Users\Administrator\Desktop>example.exe
Result: 32.000000

 

What I want to do is replace the 4th line of the Rscript code with a .c function. I'm facing two problems:

1) Convert the C Function for General Inputs:

I don't know if I have defined the function correctly.

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

void multiplyAndSum(double *x, double *y, int length, double *result) {
    *result = cblas_ddot(length, x, 1, y, 1);
}

2) Calling the C Function in R:

I don't know how to call the .c function in step 1 within R. Is there a way to compile the function and run it explicitly within R Desktop, without using the command prompt (cmd)

Please, I understand that my question may be quite basic, but my knowledge is limited. I would greatly appreciate a step-by-step answer, ideally.

0 Kudos
5 Replies
IntelSupport
Community Manager
1,070 Views

Hi,


Thanks for posting in Intel communities.


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


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


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



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


Regards,

Jilani


0 Kudos
Fio
Beginner
1,057 Views

Thank you very much for the help and the links.

If I'm not mistaken, the first and third links provide instructions for Linux, while the second one is for Windows. However, I'm sincerely sorry, but I'm having trouble understanding what I need to do. I apologize deeply, but so far, I've been working with MATLAB and Intel Parallel Studio, and compiling with them was easier for me. I've invested a lot of time into this, but I'm still struggling. Could you please provide more detailed instructions? I would greatly appreciate it.

My environment variables are displayed in the image below. Perhaps this information could be helpful for you to assist me.

Screenshot 2023-09-14 141741.png

 Thank you very much.

 

0 Kudos
IntelSupport
Community Manager
894 Views

Hi,


Thanks for sharing your requirement details. We are looking into your issue internally. We will get back to you soon with an update.


Regards,

Jilani


0 Kudos
IntelSupport
Community Manager
790 Views

Hi Fio,

 

Apologies for delay in the response.

 

We regret to say that we will not be able to support you on these R related queries, since we aren’t “R” experts. We’d only be able to support issues only related to Intel products.  

 

If you have any query on Intel products, then please raise a new thread in the appropriate forum and we’d be happy to support you. Thank you.

 

Regards,

Jilani

 

0 Kudos
IntelSupport
Community Manager
723 Views

Hi Fio,



This thread will no longer be monitored by Intel. If you need further assistance, please post a new thread. We are going ahead and closing this thread.



Regards,

Jilani


0 Kudos
Reply