From 538c764d2b4b030503c4dd86b786f90dd6e12c4c Mon Sep 17 00:00:00 2001 From: Zhang Xianyi Date: Tue, 6 Nov 2012 18:21:46 +0800 Subject: [PATCH] Refs #153. Restore the original CPU affinity when calling openblas_set_num_threads(1). Please read the issue on github.com for the detail. --- driver/others/blas_server.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/driver/others/blas_server.c b/driver/others/blas_server.c index f16b827d3..c51e681a5 100644 --- a/driver/others/blas_server.c +++ b/driver/others/blas_server.c @@ -770,6 +770,19 @@ void goto_set_num_threads(int num_threads) { if (num_threads < 1) num_threads = blas_num_threads; +#ifndef NO_AFFINITY + if (num_threads == 1) { + if (blas_cpu_number == 1){ + //OpenBLAS is already single thread. + return; + }else{ + //From multi-threads to single thread + //Restore the original affinity mask + gotoblas_set_affinity(-1); + } + } +#endif + if (num_threads > MAX_CPU_NUMBER) num_threads = MAX_CPU_NUMBER; if (num_threads > blas_num_threads) { @@ -800,6 +813,13 @@ void goto_set_num_threads(int num_threads) { UNLOCK_COMMAND(&server_lock); } +#ifndef NO_AFFINITY + if(blas_cpu_number == 1 && num_threads > 1){ + //Restore the thread 0 affinity. + gotoblas_set_affinity(0); + } +#endif + blas_cpu_number = num_threads; #if defined(ARCH_MIPS64)