ceslobfer / GPUmatrix

GPUmatrix: An R package for algebra using GPU
32 stars 1 forks source link

OpenMP & Intel MKL support and control number of threads that are used when running in CPU mode? #6

Closed tomwenseleers closed 6 months ago

tomwenseleers commented 7 months ago

When running in CPU mode, is there any way to control whether or not matrix operations are multithreaded using OpenMP and if so how many threads to use, and whether or not Intel MKL is used? In torch there is the function backends_openmp_is_available() which for me returns TRUE and backends_mkl_is_available() which also returns TRUE. If these functions return TRUE does it mean OpenMP & Intel MKL are used by default for matrix operations & calculating an SVD or a Cholesky decomposition etc? If so, is there also any way to control the number of threads that are used? [It would be handy if both OpenMP & Intel MKL were supported when in CPU mode, as Microsoft R Open, which was compiled against Intel MKL is being phased out, so this would potentially seem like a good replacement, that would avoid the hassle of compiling R with the Intel MKL BLAS, which especially on Windows is a real pain]

ceslobfer commented 7 months ago

Hello there! Thank you for using our package. These are good questions I haven't asked myself yet, and we'll take them as implementation suggestions. GPUmatrix uses torch for R by default and uses all available CPU threads for all operations. According to the torch documentation there is a way to allocate a number of threads for torch operations "torch.set_num_threads(int)". I need to make sure this is available for torch in R or if I can somehow set it as an environment variable. As for MKL or OpenMP, I think it defaults to MKL, although I don't know if it can be selected by default with an internal Python variable. I will have a more detailed comment next week and will upload the changes if I find a way to implement them. Thanks and sorry for the delay in responding.

ceslobfer commented 6 months ago

Hi! I've been looking for information about torch's use of OpenMP and MKL parallelisation, and it seems that it actually uses both types at the same time. MKL is an optimised linear algebra library that uses OpenMP for parallelism within operations. Actually, to change the number of parallelisation threads within the function, you should use the torch_set_num_threads() function. It is not worth writing your own GPUmatrix function that does exactly the same thing, or creating an environment variable, because in R you cannot make recompilation pointers like in C. Thanks again for using our package.