1

I am writing an R package that ideally needs the vdCdfNormInv function from the Intel MKL.

How do I configure the NAMESPACE file and the makevar file?

There is a scalar version of this function from Rmath.h. However, if possible, the MKL version should have better performance.

Jian Cao
  • 19
  • 2
  • Did you already try something? If yes, what went wrong? Howewer, MKL being licensed by Intel, there is probably no way to make it CRAN-publishable, but I might be wrong – Chelmy88 Aug 15 '19 at 11:56
  • I only found that you could change the `PKG_LIBS` variable in the `Makevar` file to link the default BLAS and LAPACK libraries used by R. I do not know how to link to an external library like MKL. Thanks though, linking to MKL might be not allowed. – Jian Cao Aug 15 '19 at 12:59
  • Ok, did you check the links giver here: https://stackoverflow.com/questions/18570526/how-to-build-a-r-package-which-use-rcpp-with-external-c-libraries – Chelmy88 Aug 15 '19 at 13:08
  • 3
    if you don't get an answer here in a reasonable amount of time, you might try the `r-package-devel@r-project.org` mailing list ... Have you done some benchmarks of the Intel MKL version vs. `pnorm` in the Rmath library ... ? – Ben Bolker Aug 15 '19 at 13:09
  • 1
    Based on [this question](https://stackoverflow.com/questions/38090206/linking-intels-math-kernel-library-mkl-to-r-on-windows), I suppose you'd have to add MKL to `SystemRequirements` in the `DESCRIPTION` file, but I don't know what would be needed in the `Makevars` file – Alexis Aug 15 '19 at 17:40
  • 1
    See https://stackoverflow.com/q/48037641/8416610 for some possible linking flags. You should use a `configure` script to test for the presence of MKL. You can then use pre-processor macros as suggested by @thc to destinguish between MKL and no-MKL usage. – Ralf Stubner Aug 16 '19 at 07:55
  • @BenBolker, not specifically. It will be interesting to test out ) However, the `pnorm` from `Rmath` does not assume standard normal, which may cost more FLOPs. – Jian Cao Aug 16 '19 at 19:47
  • @Chelmy88, thanks. I checked the manual and the short paper on Rcpp but did not find examples on linking external library. – Jian Cao Aug 16 '19 at 19:50
  • @RalfStubner thanks. It seems easier if R is built with the MKL. It's a good point that I should distinguish between MKL-equipped environment and others. – Jian Cao Aug 16 '19 at 20:00

1 Answers1

4

Use C++ preprocessor macros. Here's am example:

double result;
#ifdef INTEL_MKL_VERSION 
result = vdCdfNormInv(...);
#else
result = vdCdfNormInv_generic(...);
#endif

Alternatively, you can just throw an error (std::runtime_error or Rcpp::stop) if INTEL_MKL_VERSION is not defined.

But I think if you submit a package to CRAN, you should take the effort to make it available to people without MKL, even if it is slower.

See also reference: Using Predefined Preprocessor Symbols for Intel® MKL Version-Dependent Compilation

thc
  • 9,527
  • 1
  • 24
  • 39