I have a mex(MATLAB version of C ) function that I want to convert to C . It's very similar except for the main() function. But one of the sub-functions is running considerably slower when compiled with C vs mex.
I compile my mex code via:
mex -v COMPFLAGS='$COMPFLAGS /std:c 11' COPTIMFLAGS="-O3" CFLAGS="$CFLAGS -fopenmp" rcp_adp.cpp -I/usr/include ini.c -I/usr/include cpp/INIReader.cpp -larmadillo -lopenblas -lgomp -lfftw3_omp -lfftw3 -lm -lpthread
and I use this for C compile:
g --verbose -std=c 11 rcp_main.cpp -I/usr/include ini.c -I/usr/include cpp/INIReader.cpp -O3 -larmadillo -llapack -lopenblas -lgomp -lfftw3_omp -lfftw3 -lm -fopenmp -lpthread
I cannot post the sub-function but it is using armadillo functions and a parallel for loop for signal processing. The timing difference goes from 0.5sec to 25sec.
The for loops are called like so:
#pragma omp parallel for num_threads(max_threads) /*do processing in parallel threads*/
for(int ii=0;ii<Nsp;ii ){
}
I'm at a loss for what could be causing the timing difference. Any help would be greatly appreciated.
EDIT: I just wanted to clarify, this is the exact code running in both instances. Its a header file written in C . The only difference is that one version is compile inside MATLAB using the mex command, that is linked with g while the other version is just with g .
So my working theory is that when using MEX/G /MATLAB to compile, MATLAB is forcing the compiler to use MATLABs versions of LAPACK and BLAS vs the linked openblas and lapack.I plan on testing this tomorrow afternoon with a simplified version of the code and while update with results.
EDIT: Sorry, forgot to mention I am using Ubuntu 20.04. I haven't run my test yet, but I have looked further into openblas. I see that there is openblas-openmp, how can I link to that instead of the standard openblas?
CodePudding user response:
On Windows, the CFLAGS
environment variable is not used. You are not passing the -fopenmp
flag to the compiler. I am also not sure about the COPTIMFLAGS
flag, you might not be passing the -O3
flag either.
On Windows, use the COMPFLAGS
environment variable to pass flags to the compiler. On Linux and macOS, use CFLAGS
for the C compiler and CXXFLAGS
for the C compiler. This is described here in the documentation.
If you run mex
from within MATLAB, do
mex -v COMPFLAGS='$COMPFLAGS -std=c 11 -O3 -fopenmp' rcp_adp.cpp -I/usr/include ini.c -I/usr/include cpp/INIReader.cpp -larmadillo -lopenblas -lgomp -lfftw3_omp -lfftw3 -lm -lpthread
If you run mex
from the Windows command prompt, use "
instead of '
.
CodePudding user response:
So fixed the issue. It seems like MEX/MATLAB was compiling with mkl blas libraries while g was compiling with standard openblas. I install openblas-openmp
sudo apt install libopenblas-openmp-dev
then I updated where link was point to, so that it pointed to the openmp version (repeat for lapack):
sudo update-alternatives --config libopenblas.so.0-x86_64-linux-gnu
And the timing discrepancies disappeared.