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

how to pass number of threads into LAPACK routines

david47
Beginner
502 Views

I have a Fortran code comprised of many subroutines that use the MKL LAPACK routines.  Some of the LAPACK calls are within an OpenMP loop and some are outside of the OpenMP loop.   I would like to run LAPACK outside of the parallelized loops using all of the threads available however inside the parallelized loop I want (and have to) call the LAPACK routine with only 1 thread.

How do I set up my code to do this?  In Visual Studio, Properties -> Fortran -> Libraries -> Use Intel Math Kernel Library, if I set this to Sequential then the call to LAPACK will work properly inside the parallel loop but will not run with all of the threads outside of the loop.  If I set the MKL to Parallel then this will not run properly inside the loop.

Is there a way to pass in the number of threads into the LAPACK routines during runtime?

Thank you so much for your help!

Sincerely,

David

 

0 Kudos
4 Replies
Gennady_F_Intel
Moderator
482 Views

yes, it is possible by using mkl_set_num_threads() routines before the specific mkl's routine.

the pseudo-code will look like as follows:

mkl_set_num_threads(1)

getrf(*,*,*....)

int numthr=mkl_get_max_threads();

mkl_set_num_threads(numthr);

getrf(.....);


david47
Beginner
468 Views

Thank you so much for your reply Gennady!

So, just to clarify, is the following pseudo code correct,

N = mkl_get_max_threads()  OR N = OMP_get_max_threads()

call mkl_set_num_threads(N)

call getrf(...)

 

call mkl_set_num_threads(1)

call OMP_set_num_threads(N)

!$OMP PARALLEL ...

    do i = 1,M

        call getrf(...)

    end do

!$OMP END PARALLEL DO

 

1) Are the MKL and OMP routines independent from each other.  Do the MKL commands control the threading of each MKL algorithm and the OMP commands control the thread count for the loops?

2) Do I call the mkl_set_num_threads once before entering the OMP loop or does it have to be called each time within the parallelized loop? 

3) Can I use the OpenMP or MKL command to get the number of threads but then apply 2) above to tell the code how to use them?

 

So I guess that I always compile with, Properties -> Fortran -> Libraries -> Use Intel Math Kernel Library set to Parallel and then use the commands above as "switches" to run in sequential (Nthread = 1) and parallel (Nthread = N)?

 

Thanks you so much again for your help.  This will be extremely useful in my computational heavy calculations!!

 

Sincerely,

David 

 

Gennady_F_Intel
Moderator
403 Views

David,


1) MKL uses OpenMP (libiomp5*.*) library as a one of backend threading runtime, but if you call call mkl_set_num_threads(1) then MKL will execute the single thread computation. You may check it by enabling the verbose mode (e.x – set/export MKL_VERBOSE=1) to see how many threads will be used by mkl’s routines.

 

2)Yes, You could call mkl_set_num_thread once before OMP loop.

 

3) yes you can call MKL functions many times to get/set differen number of threads

 

And Yes, in the case if you are working from within the MVSV IDE, you could set Properties -> Fortran -> Libraries -> Use Intel Math Kernel Library set to Parallel

And then there OpenMP threaded mkl libs will be linked with your application. 


-Gennady


Gennady_F_Intel
Moderator
380 Views

This issue has been resolved and we will no longer respond to this thread. If you require additional assistance from Intel, please start a new thread. Any further interaction in this thread will be considered community only. 



Reply