matlab call lapack,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
matlab call lapack,MATLAB中调用LAPACK 和BLAS函数相关推荐
- java怎么调用存储函数_java中调用存储过程或存储函数的方法
java中调用存储过程或存储函数的方法 1.调用存储过程:CallableStatement clstmt = null;try {clstmt = conn.prepareCall("{c ...
- java 执行oracle 存储过程_oracle--在java中调用存储过程和存储函数
在java中调用存储过程和存储函数 存储过程: 查询某个员工的姓名 月薪 职位: create or replace procedure queryempinfo(eno in number, pen ...
- 如何在matlab sfunction 函数中调用自己写的函数?
自己编写了一个s函数,有几个参数引用了自己写的几个函数,在脚本中可以正确运行,但在写成s函数,进行 simulink 仿真的时候,已知提示"too many input auguments& ...
- 【Matlab】在Java中调用matlab函数
考虑到计算的各种情况,有些用Java代码实现的计算难免会显得不够高效.而利用MATLAB写好相应的计算函数,然后打包成jar包供Java调用,在某些情况下会更加方便.现在就来说一下如何实现这一过程: ...
- 在全局中调用类的静态成员函数
在全局中直接调用类的静态成函数会产生重复声明错误. 例如: FooClass::FooStaticFunction(); 如果在全局中间接调用类的静态成员函数则不会产生错误,但是会产生多余的全局变量. ...
- [转载] 扩展Python之在Python中调用C编写的函数模块
参考链接: 如何在Python中调用C函数 目录 编写Python扩展1. 创建应用代码2. 根据样板编写封装代码2.1 包含Python头文件2.2 为每一个模块函数添加形如PyObject* *M ...
- 在一个PHP中调用另一个php函数,php调用函数 php如何调用函数?
php如何调用函数? 1.递推 2.回归递推: 递推为正向的推导,即从前向后的分析问题,寻找递推的条件. 1-3求和为例 sum(1) = 1 0 sum(2) = 2 1 sum(3) = 3 2 ...
- matlab怎么在c 中调用,在C中调用Matlab (转)
C语音常用库和函数 #include //设定插入点 #include //字符处理 #include //定义错误码 # ... ASP.NET 5系列教程 (一):领读新特性 近期微软发布了ASP ...
- python类中调用另一个程序函数_Python:如何在另一个类中调用函数
我被一些python脚本困住了 https://python4kids.brendanscott.com/2014/12/02/hooking-up-the-sunfish-chess-engine- ...
最新文章
- 我共享的资源,有四个上了首页的排行榜。
- html页面跳转IP,JS获取访客IP进行自动跳转
- [USACO17JAN]Promotion Counting 题解
- 为什么声明性编码使您成为更好的程序员
- java spring包_java 自定义加载器,加载spring包,动态加载实现,jar包隔离,tomcat加载webapp方式...
- No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK? 问题
- linux下安装xz命令
- jq使用教程06_数据更新日志
- Mysql的master,slave的配置
- 2017值得一瞥的JavaScript相关技术趋势
- rotate list java_Rotate List | Java最短代码实现
- 钉钉云课堂sign计算方式
- 编程小白的第一本Python入门书学习笔记
- 手术导航系统原理简介、主要工作及应用
- 网易交互设计师微专业C2 设计需求分析与方案选择
- 课堂作业:首尾相连求最大子数组
- ICCV 2017:训练GAN的16个技巧,2400+星(PPT)
- 眼底图像血管增强与分割--(5)基于Hessian矩阵的Frangi滤波算法
- css与mdx,mdx、mdd及css三者关系以及欧路中应如何安装css
- 论文中公式后标点的使用
热门文章
- 计算机技术转让增值税,技术转让及开发的增值税优惠政策梳理
- 南大软件分析 前5节笔记
- [免费软件]淘宝宝贝排名查询
- 红包雨实现(优化版)
- 程序员公司要全员降薪了,我该留下还是走呢
- net::ERR_CONNECTION_REST与XMLHttpRequest:网络错误 0x2ee2, 由于出现错误 00002ee2 而导致此项操作无法完成。
- CANoe学习记录(三)软硬件在线采集
- Ubuntu 20.04 系统分区
- 一不小心就侵权,这拯救打工人的6个小工具!
- 计算机 创新方法举例,列举列举五种创新的方法并加以举例说明