MATLAB中调用LAPACK 和BLAS函数

一、代码matrixMultiply.c 从自带例子中拷贝过来:    

>> copyfile(fullfile(matlabroot,'extern','examples','refbook','matrixMultiply.c'),'.')>> ls mat*
>> ls mat*
matlab.mat  matrixMultiply.c  mattest.mat

>> fileattrib('matrixMultiply.c','+w')

 cat matrixMultiply.c 

/*=========================================================
 * matrixMultiply.c - Example for illustrating how to use 
 * BLAS within a C MEX-file. matrixMultiply calls the 
 * BLAS function dgemm.
 *
 * C = matrixMultiply(A,B) computes the product of A*B,
 *     where A, B, and C are matrices.
 *
 * This is a MEX-file for MATLAB.
 * Copyright 2009-2010 The MathWorks, Inc.
 *=======================================================*/

#if !defined(_WIN32)
#define dgemm dgemm_
#endif

#include "mex.h"
#include "blas.h"

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    double *A, *B, *C; /* pointers to input & output matrices*/
    size_t m,n,p;      /* matrix dimensions */
    /* form of op(A) & op(B) to use in matrix multiplication */
    char *chn = "N";
    /* scalar values to use in dgemm */
    double one = 1.0, zero = 0.0;

    A = mxGetPr(prhs[0]); /* first input matrix */
    B = mxGetPr(prhs[1]); /* second input matrix */
    /* dimensions of input matrices */
    m = mxGetM(prhs[0]);  
    p = mxGetN(prhs[0]);
    n = mxGetN(prhs[1]);

    if (p != mxGetM(prhs[1])) {
        mexErrMsgIdAndTxt("MATLAB:matrixMultiply:matchdims",
                "Inner dimensions of matrix multiply do not match.");
    }

    /* create output matrix C */
    plhs[0] = mxCreateDoubleMatrix(m, n, mxREAL);
    C = mxGetPr(plhs[0]);

    /* Pass arguments to Fortran by reference */
    dgemm(chn, chn, &m, &n, &p, &one, A, &m, B, &p, &zero, C, &m);
}

二、编译:

>> mex -v matrixMultiply.c -lmwblas
详细模式已开。
... 正在查找编译器 'gcc-4.9'...
... 正在执行命令 'which gcc-4.9'...是('/usr/bin/gcc-4.9')。
... 正在执行命令 'gcc-4.9 -print-file-name=libstdc++.so'...是('/usr/lib/gcc/x86_64-linux-gnu/4.9/libstdc++.so')。
找到已安装的编译器 'gcc-4.9'。
Options file details
-------------------------------------------------------------------
	Compiler location: /usr/bin/gcc-4.9
	Options file: /home/mymotif/.matlab/R2017a/mex_C_glnxa64.xml
	CMDLINE2 : /usr/bin/gcc-4.9 -pthread -Wl,--no-undefined -Wl,-rpath-link,/opt/local/MATLAB/R2017a/bin/glnxa64 -shared  -O -Wl,--version-script,"/opt/local/MATLAB/R2017a/extern/lib/glnxa64/c_exportsmexfileversion.map" /tmp/mex_7030238084799_6443/matrixMultiply.o /tmp/mex_7030238084799_6443/c_mexapi_version.o   -lmwblas   -L"/opt/local/MATLAB/R2017a/bin/glnxa64" -lmx -lmex -lmat -lm -lstdc++ -o matrixMultiply.mexa64
	CC : /usr/bin/gcc-4.9
	DEFINES : -DTARGET_API_VERSION=700  -DUSE_MEX_CMD   -D_GNU_SOURCE -DMATLAB_MEX_FILE 
	MATLABMEX : -DMATLAB_MEX_FILE 
	CFLAGS : -fexceptions -fPIC -fno-omit-frame-pointer -pthread
	INCLUDE : -I"/opt/local/MATLAB/R2017a/extern/include" -I"/opt/local/MATLAB/R2017a/simulink/include"
	COPTIMFLAGS : -O -DNDEBUG
	CDEBUGFLAGS : -g
	LD : /usr/bin/gcc-4.9
	LDFLAGS : -pthread -Wl,--no-undefined -Wl,-rpath-link,/opt/local/MATLAB/R2017a/bin/glnxa64
	LDTYPE : -shared 
	FUNCTIONMAP : "/opt/local/MATLAB/R2017a/extern/lib/glnxa64/mexFunction.map"
	VERSIONMAP : "/opt/local/MATLAB/R2017a/extern/lib/glnxa64/c_exportsmexfileversion.map"
	LINKEXPORT : -Wl,--version-script,"/opt/local/MATLAB/R2017a/extern/lib/glnxa64/mexFunction.map"
	LINKEXPORTVER : -Wl,--version-script,"/opt/local/MATLAB/R2017a/extern/lib/glnxa64/c_exportsmexfileversion.map"
	LINKLIBS : -lmwblas   -L"/opt/local/MATLAB/R2017a/bin/glnxa64" -lmx -lmex -lmat -lm -lstdc++
	LDOPTIMFLAGS : -O
	LDDEBUGFLAGS : -g
	MWCPPLIB : "/opt/local/MATLAB/R2017a/sys/os/glnxa64/libstdc++.so.6"
	OBJEXT : .o
	LDEXT : .mexa64
	SETENV : CC="/usr/bin/gcc-4.9"
                CXX="g++"
                CFLAGS="-fexceptions -fPIC -fno-omit-frame-pointer -pthread -DTARGET_API_VERSION=700  -DUSE_MEX_CMD   -D_GNU_SOURCE -DMATLAB_MEX_FILE "
                CXXFLAGS="-fexceptions -fPIC -fno-omit-frame-pointer -pthread -std=c++11 -DTARGET_API_VERSION=700  -DUSE_MEX_CMD   -D_GNU_SOURCE -DMATLAB_MEX_FILE "
                COPTIMFLAGS="-O -DNDEBUG"
                CXXOPTIMFLAGS="-O -DNDEBUG"
                CDEBUGFLAGS="-g"
                CXXDEBUGFLAGS="-g"
                LD="/usr/bin/gcc-4.9"
                LDXX="g++"
                LDFLAGS="-pthread -Wl,--no-undefined -Wl,-rpath-link,/opt/local/MATLAB/R2017a/bin/glnxa64 -shared  -lmwblas   -L"/opt/local/MATLAB/R2017a/bin/glnxa64" -lmx -lmex -lmat -lm -lstdc++ -Wl,--version-script,"/opt/local/MATLAB/R2017a/extern/lib/glnxa64/mexFunction.map""
                LDDEBUGFLAGS="-g"
	GCC : /usr/bin/gcc-4.9
	CPPLIBS : /usr/lib/gcc/x86_64-linux-gnu/4.9/libstdc++.so
	MATLABROOT : /opt/local/MATLAB/R2017a
	ARCH : glnxa64
	SRC : /home/mymotif/matlab_workplace/matrixMultiply.c;/opt/local/MATLAB/R2017a/extern/version/c_mexapi_version.c
	OBJ : /tmp/mex_7030238084799_6443/matrixMultiply.o;/tmp/mex_7030238084799_6443/c_mexapi_version.o
	OBJS : /tmp/mex_7030238084799_6443/matrixMultiply.o /tmp/mex_7030238084799_6443/c_mexapi_version.o 
	SRCROOT : /home/mymotif/matlab_workplace/matrixMultiply
	DEF : /tmp/mex_7030238084799_6443/matrixMultiply.def
	EXP : matrixMultiply.exp
	LIB : matrixMultiply.lib
	EXE : matrixMultiply.mexa64
	ILK : matrixMultiply.ilk
	MANIFEST : matrixMultiply.mexa64.manifest
	TEMPNAME : matrixMultiply
	EXEDIR : 
	EXENAME : matrixMultiply
	OPTIM : -O -DNDEBUG
	LINKOPTIM : -O
	CMDLINE1_0 : /usr/bin/gcc-4.9 -c -DTARGET_API_VERSION=700  -DUSE_MEX_CMD   -D_GNU_SOURCE -DMATLAB_MEX_FILE  -I"/opt/local/MATLAB/R2017a/extern/include" -I"/opt/local/MATLAB/R2017a/simulink/include" -fexceptions -fPIC -fno-omit-frame-pointer -pthread -O -DNDEBUG /home/mymotif/matlab_workplace/matrixMultiply.c -o /tmp/mex_7030238084799_6443/matrixMultiply.o
	CMDLINE1_1 : /usr/bin/gcc-4.9 -c -DTARGET_API_VERSION=700  -DUSE_MEX_CMD   -D_GNU_SOURCE -DMATLAB_MEX_FILE  -I"/opt/local/MATLAB/R2017a/extern/include" -I"/opt/local/MATLAB/R2017a/simulink/include" -fexceptions -fPIC -fno-omit-frame-pointer -pthread -O -DNDEBUG /opt/local/MATLAB/R2017a/extern/version/c_mexapi_version.c -o /tmp/mex_7030238084799_6443/c_mexapi_version.o
-------------------------------------------------------------------
使用 'gcc-4.9' 编译。
/usr/bin/gcc-4.9 -c -DTARGET_API_VERSION=700  -DUSE_MEX_CMD   -D_GNU_SOURCE -DMATLAB_MEX_FILE  -I"/opt/local/MATLAB/R2017a/extern/include" -I"/opt/local/MATLAB/R2017a/simulink/include" -fexceptions -fPIC -fno-omit-frame-pointer -pthread -O -DNDEBUG /home/mymotif/matlab_workplace/matrixMultiply.c -o /tmp/mex_7030238084799_6443/matrixMultiply.o
In file included from /home/mymotif/matlab_workplace/matrixMultiply.c:18:0:
/opt/local/MATLAB/R2017a/extern/include/blas.h:585:0: warning: "dgemm" redefined
 #define dgemm FORTRAN_WRAPPER(dgemm)
 ^
/home/mymotif/matlab_workplace/matrixMultiply.c:14:0: note: this is the location of the previous definition
 #define dgemm dgemm_
 ^

/usr/bin/gcc-4.9 -c -DTARGET_API_VERSION=700  -DUSE_MEX_CMD   -D_GNU_SOURCE -DMATLAB_MEX_FILE  -I"/opt/local/MATLAB/R2017a/extern/include" -I"/opt/local/MATLAB/R2017a/simulink/include" -fexceptions -fPIC -fno-omit-frame-pointer -pthread -O -DNDEBUG /opt/local/MATLAB/R2017a/extern/version/c_mexapi_version.c -o /tmp/mex_7030238084799_6443/c_mexapi_version.o
/usr/bin/gcc-4.9 -pthread -Wl,--no-undefined -Wl,-rpath-link,/opt/local/MATLAB/R2017a/bin/glnxa64 -shared  -O -Wl,--version-script,"/opt/local/MATLAB/R2017a/extern/lib/glnxa64/c_exportsmexfileversion.map" /tmp/mex_7030238084799_6443/matrixMultiply.o /tmp/mex_7030238084799_6443/c_mexapi_version.o   -lmwblas   -L"/opt/local/MATLAB/R2017a/bin/glnxa64" -lmx -lmex -lmat -lm -lstdc++ -o matrixMultiply.mexa64
MEX 已成功完成。

三、运行:

>> A = [1 3 5; 2 4 7];
>> B = [-5 8 11; 3 9 21; 4 0 8];
>> X = matrixMultiply(A,B)

X =

    24    35   114
    30    52   162

猜你喜欢

转载自my.oschina.net/u/2245781/blog/1825199