Changing MKL_NUM_THREADS at runtime

Changing MKL_NUM_THREADS at runtime

Hello,

I have an application that uses Windows threads on a high level for parallelization. MKL is called on each thread.

In certain critical sections of the program threads are executed serially (think mutex). In these critical sections MKL functions are also called.

If my machine has N processors, I create N Windows threads so that at a very high level I achieve the parallelization I want. At this point I would like to set MKL_NUM_THREADS to 1 as I have no more processor power left.

However, at the critical sections I would like to set MKL_NUM_THREADS to N.

My question: is there a way to continuosly change the value of this environment variable? I'm concerned about when a MKL function will actually check the value of the environment variable. If I use "putenv" this may not be good enough.

Thanks.

4 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

You can use the MKL function, mkl_set_num_threads() to set/modify the number of threads at run-time and this function has precedence overthe environment variable, MKL_NUM_THREADS.

You can refer to section 5 ("Manging Performance and Memory") of the MKL user guide located at, http://software.intel.com/en-us/articles/intel-math-kernel-library-documentation/ for more details and code examples.

Quoting mullervki

However, at the critical sections I would like to set MKL_NUM_THREADS to N.

I'm confused by this statement. Perhaps you don't mean critical section in the sense it normally has in the context of threading.
As Vamsi said, you should be able to call mkl_set_num_threads to set the number for the next MKL function call.

You probably misunderstand the role of environmental variables. They provide a means of communication with the runtime of the compiler, or with the operating system. The runtime or the OS may be written to query the existence and value of an environmental variable, and decide to allocate threads, allocate more memory, abort a process, etc. The hardware itself knows NOTHING about environmental variables.

Secondly, an environmental variable is not a semaphore. Your program inherits the environment that existed when the process was started. Subsequent changes to the environment are not communicated to your program.

> If my machine has N processors, I create N Windows threads so that at a
very high level I achieve the parallelization I want. At this point I
would like to set MKL_NUM_THREADS to 1 as I have no more processor power
left.

If all possible threads have been allocated, how can you do anything but wait for at least one thread to finish? If you terminate threads before they finished, would your results not be junk?

Why don't you create N-1 (or N-M) threads, instead?

Leave a Comment

Please sign in to add a comment. Not a member? Join today