|
From: | François-Henry Rouet |
Subject: | Setting OMP_NUM_THREADS from octave |
Date: | Thu, 4 Jun 2015 21:28:44 +0200 |
Hi all,
I'm trying to understand how octave deals with multithreaded libraries. I use octave 4.0.0, built with gcc 5.1.0, and linked against multithreaded OpenBLAS 0.2.14 (built with USE_OPENMP=1), which provides BLAS routines (e.g., matrix multiply) and LAPACK routines (e.g. LU factorization). I wrote two mex functions to play with OMP_NUM_THREADS (wrappers to omp_[gs]et_num_threads): void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]) { #pragma omp parallel printf("OMP_NUM_THREADS=%d\n",omp_get_num_threads()); } void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]) { double *dn = mxGetPr(prhs[0]); omp_set_num_threads((int)(dn[0])); } Now here's the issue; in this example, I start octave with OMP_NUM_THREADS undefined. I set the number of threads to 2 with my mex function, run some BLAS/LAPACK functions, then I set the number of threads to 1, run some BLAS/LAPACK functions, then I set the number of threads to 2 again. >> omp_set_num_threads(2) >> omp_get_num_threads() OMP_NUM_THREADS=2 OMP_NUM_THREADS=2 >> n=6000; A=rand(n,n)+n*eye(n); tic; lu(A); toc; tic; A*A; toc; Elapsed time is 8.04974 seconds. Elapsed time is 16.3446 seconds. >> omp_set_num_threads(1) >> omp_get_num_threads() OMP_NUM_THREADS=1 >> n=6000; A=rand(n,n)+n*eye(n); tic; lu(A); toc; tic; A*A; toc; Elapsed time is 10.5778 seconds. Elapsed time is 30.6964 seconds. >> omp_set_num_threads(2) >> omp_get_num_threads() OMP_NUM_THREADS=2 OMP_NUM_THREADS=2 >> n=6000; A=rand(n,n)+n*eye(n); tic; lu(A); toc; tic; A*A; toc; Elapsed time is 10.5794 seconds. Elapsed time is 31.3423 seconds. Going from 2 to 1 thread seems to work. However, when i set the number of threads back to 2, I still get single-thread performance. Also, if I start octave with OMP_NUM_THREADS=1, I get single-threaded behavior regardless of my calls to omp_set_num_threads. Any explanation to this? I found some old messages in the matlab and octave archives but nothing really helpful. Also, I tried to use setenv/putenv to set OMP_NUM_THREADS and it doesn't seem to work at all. Thanks for your help! |
[Prev in Thread] | Current Thread | [Next in Thread] |