为什么用c写的大型c 实现矩阵相乘乘功能的mex比matlab慢了20倍

温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!&&|&&
人生一年又一年,只要每年都有所积累,有所成长,都有那么一次自己认为满意的花开时刻就好。即使一时不顺,也要敞开胸怀。生命的荣枯并不是简单的重复,一时的得失不是成败的尺度。花开不是荣耀,而是一个美丽的结束,花谢也不是耻辱,而是一个低调的开始。
LOFTER精选
网易考拉推荐
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
: Microsoft Visual C++ .NET 2003
: Microsoft Visual C/C++ 6.0
: Borland C++Builder 6
: Intel, Compaq, or Digital Visual Fortran for Fortran MEX-files
Note: The LCC compiler shipped with MATLAB does not include a
debugger. You can use MEXPRINTF to print status messages to the screen, but will
not have access to breakpoints and controlled execution as with other compilers.
These instructions assume familiarity with your debugger.& For
instructions on how to use your debugger once the MEX-file is loaded, refer to
your debugger documentation.
Section 23: Debugging on Linux/UNIX/Mac You can find general Linux/UNIX/Mac debugging information here:
In addition, the MathWorks Support site contains step-by-step
instructions for the following debuggers:
: GDB for C
: GDB for Fortran
:&GDB and&DBX on numerous UNIX platforms. This includes platforms supported by
past versions of MATLAB.
:&GDB on Solaris
: Xcode on Mac
:&GDB for Fortran MEX-files&on Linux/UNIX/Mac These instructions assume familiarity with your debugger.& For
instructions on how to use your debugger once the MEX-file is loaded, refer to
your debugger documentation. Useful GDB commands
file Read a source file in
step Move forward one line in debugging
where, whereami&& Show the call stack, show the current location
list Show the current line and following source file contents
print Display the value of a variable
stop in, stop at Stop in a particular function, stop at a particular line
help Show the help
what Tell what a thing is, give the function prototype,
Section 24: Using other debuggers If using another debugger, start MATLAB with the -D flag, specifying your
matlab -Dgdb The GNU debugger
matlab -Dddd A graphical front end to GDB
matlab -D"workshop -D &path to matlab.exe&"& The Sun Workshop debugger Section 25: C++ MEX-file Overview MEX-files can be written in C++. See mexcpp.cpp in the
$MATLABROOT/extern/examples/mex directory (where
$MATLABROOT is the MATLAB root directory on your machine, as returned
by typing matlabroot at the MATLAB Command Prompt.)
Section 26: Tips for C++ MEX-files Here are some tips to keep in mind if you decide to use C++ in your
For an example MEX-file, see:
&MATLAB&/extern/examples/mex/mexcpp.cppThe
extension .cpp is unambiguous and generally recognized by C++ compilers. Other
possible extensions include .C, .cc, and .cxx.
Using cout will not work as expected in C++ MEX-files. This is because cout
is expecting to use a display that is not MATLAB. To workaround this problem,
If you run your MEX-file in MATLAB and you do not receive the expected
output, make sure that you have a C++ flush() function call in your
Section 27: Compiling C++ MEX-filesUse mex -setupto select a C++ compiler at the MATLAB command
prompt. The LCC compiler shipped with MATLAB is a C-only compiler, and cannot be
used with C++. Next, invoke MEX as:
mex mexcpp.cpp Special instructions for MATLAB 6.1 (R12.1) and earlier on
UNIX: In order to compile this example, you should copy the options
file for C++ MEX-files (&MATLAB&/bin/cxxopts.sh) to your current
directory. Then, invoke MEX as:mex mexcpp.cpp -f cxxopts.shThis is not required for
MATLAB 6.5 (R13) and later on UNIX, where a C++ compiler can be selected using
mex -setup as shown above.
Section 28: If Linking fails
Retry compiling withyprime.c Try runningmex -setupagain
Check that you are using a supported compiler. A list of supported compilers
can be found in the .
Verify that the code is correct C code
If you are getting linking, unresolved external, or undefined symbols
errors, follow these steps:
Find the name of the symbol
It is a MathWorks symbol? Does it begin with "_mx", "_mex", "_eng","_mat",
"_mlf", or "_mcl"?
If it is a MathWorks symbol, ensure you are linking aganist the correct
Section 29: If loading fails
Dependency Walker is a free tool that can help identify missing DLL-files,
one common cause of MEX-files that fail to load on Windows. Solution
explains how to use Dependency Walker to diagnose this issue.
Ensure the MEX-file is a 32- or 64-bit shared library as appropriate for
your platform. In particular, Windows MEX-files cannot depend on 16-bit
libraries.
Ensure &MATLAB&/extern/lib/$ARCH is included in $LD_LIBRARY_PATH
Messages from ld.so usually indicate a problem with the library
On Windows, ensure that the directories of all necessary external DLL-files
are in the PATH variable
Section 30: If running fails - segmentation violations: Memory is grouped into blocks, or segments. A key function of every operating
system is to keep track of which processes own the various memory segments. A
process can only access memory which it owns. This prevents critical data from
being overwritten and maintains security. A segmentation violation occurs when a process attempts to access memory
which it does not own. This typically happens when a user tries to write past
the end of an array, access dynamically allocated data that has previously been
freed, or de-reference a NULL pointer. If you encounter a problem like this, debug the MEX-file using the steps in
the .&Table of MEX Examples Source code for the MEX examples shown in the following table are in
subfolders of matlabroot/extern/examples. For more
information, see
or , or enter the example name in the Help browser search field. MEX Examples
Example Name
Example Subfolder
Description
refbook Fill mxArray using mxGetPr
refbook Fill mxArray with non-double values
refbook Fill mxArray using mxSetPr to dynamically
allocate memory
mex Multiply a scalar times 1xN matrix
mex Illustrate memory requirements of large
refbook Pass complex data
refbook Use of Fortran %VAL
refbook Handle FORTRAN complex return type for function called from
a C MEX-file
refbook Use unsigned 16-bit integers
mex Identify data type of input variable
refbook Use N-dimensional arrays
refbook Populate a sparse matrix
refbook Call a LAPACK function
refbook Call a LAPACK function with complex numbers
refbook Call a BLAS function
refbook Pass matrices in Fortran
refbook Pass non-double matrices in Fortran
mex Register an exit function to close a data file
mex Call built-in MATLABdisp function
mex Illustrate some C++ language features in a
mex Use mexEvalString to assign variables in
mex How to use mexfunction
mex Use mexGet and mexSet to change
Color property of a graphics handle
mex Use mexGetVariable and mexPutVariable to
track counters in the MEX-file and in the MATLAB global workspace
mex How to lock and unlock a MEX-file
mex Control MATLAB response to errors in
mexCallMATLAB
mx Demonstrate MATLAB 1-based matrix indexing versus C 0-based
mx Create 2-D cell array
mx Create 2-D string array
mx Create MATLAB structure from C structure
mx Read MATLABeps value
mx Read inf value
mx Display number of nonzero elements in a sparse matrix and
maximum number of nonzero elements it can store
mx Check if array is member of specified class
mx Check for NaN and infinite values
mx Check if workspace variable is logical or global
mx Allocate memory to copy a MATLAB string to a C
mx Reshape an array
mx Reallocate memory for sparse matrix and reset values of
pr, pi, ir and nzmax
refbook Pass C character matrix from FORTRAN to MATLAB
refbook Manipulate structures and cell arrays
refbook Copy MATLAB string data to and from C-style string
refbook Create mxArray and pass to MATLABsin and
plot functions
refbook Demonstrate common workflow of MEX-file
refbook Use LAPACK for symmetric indefinite factorization
refbook Pass multiple parameters
refbook Solve simple 3 body orbit problem
阅读(4356)|
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
历史上的今天
在LOFTER的更多文章
loftPermalink:'',
id:'fks_082',
blogTitle:'什么是Mex文件?Using MEX-Files to Call C/C++ Programs',
blogAbstract:'Mex文件其实就是用C语言写好的函数,编译后能被MATLAB调用。编译完成后,即可像m文件一样适用mex。Using MEX-Files to Call C/C++ and Fortran
ProgramsIntroduction
{list a as x}
{if x.moveFrom=='wap'}
{elseif x.moveFrom=='iphone'}
{elseif x.moveFrom=='android'}
{elseif x.moveFrom=='mobile'}
${a.selfIntro|escape}{if great260}${suplement}{/if}
{list a as x}
推荐过这篇日志的人:
{list a as x}
{if !!b&&b.length>0}
他们还推荐了:
{list b as y}
转载记录:
{list d as x}
{list a as x}
{list a as x}
{list a as x}
{list a as x}
{if x_index>4}{break}{/if}
${fn2(x.publishTime,'yyyy-MM-dd HH:mm:ss')}
{list a as x}
{if !!(blogDetail.preBlogPermalink)}
{if !!(blogDetail.nextBlogPermalink)}
{list a as x}
{if defined('newslist')&&newslist.length>0}
{list newslist as x}
{if x_index>7}{break}{/if}
{list a as x}
{var first_option =}
{list x.voteDetailList as voteToOption}
{if voteToOption==1}
{if first_option==false},{/if}&&“${b[voteToOption_index]}”&&
{if (x.role!="-1") },“我是${c[x.role]}”&&{/if}
&&&&&&&&${fn1(x.voteTime)}
{if x.userName==''}{/if}
网易公司版权所有&&
{list x.l as y}
{if defined('wl')}
{list wl as x}{/list}matlab用mex编译cpp文件
mex -setup选择编译器mex XXXXX.cpp &把这个cpp文件编译成库,生成的文件,32位系统中是mexw32,64位系统中是mexw64。都是matlab专用,不是dllmex X1.cpp X2.cpp X3.cpp 生成的库名字叫X1mex -O X1.cpp &大写O选项,优化编译,跑得嗷嗷快,以前都没发现呀32位windows系统,matlab给我们赠送了编译器lcc,不用自己装。64位系统下 还要自己找,麻烦
在isomap里边有个dijkstra算法的cpp文件要编译,readme里边告诉我们要用mex -O dijkstra.cpp
但是在64位系统里边是不行滴,一定要用mex -O -largeArrayDims dijkstra.cpp
另外irs和jcs也不能用int * 类型,为了保证兼容性必须要用mwIndex *
都是因为稀疏矩阵的原因,稀疏矩阵在32位系统和64位系统下的下标类型是不一样滴
分享这篇日志的人也喜欢
你好可爱哟
被屏蔽的新人
热门日志推荐
人人最热标签
北京千橡网景科技发展有限公司:
文网文[号··京公网安备号·甲测资字
文化部监督电子邮箱:wlwh@··
文明办网文明上网举报电话: 举报邮箱:&&&&&&&&&&&&
请输入手机号,完成注册
请输入验证码
密码必须由6-20个字符组成
下载人人客户端
品评校花校草,体验校园广场你似乎来到了没有知识存在的荒原...
来源链接是否正确?用户、话题或问题是否存在?         2222小 型 微 型 计 算 机 系 统
成动态连接库的方法,如下:
       2003年
换掉该部分MATLAB代码,会大大提高程序的运行效率(众所周知,MATLAB程序中的循环语句执行速度比较慢).获得MEX文件的方法主要有两种:
①mex命令将用mcc编译生成的MEX原代码编译连接为MEX文件.例如:
mexmyfile.c
输出结束生成myfile.dll.mex的具体使用可参见MATLAB帮助.
②将原有的C程序进行一定的改写或直接用C语言来编写,然后用mex命令编译连接.MATLAB为此提供了一组编写MEX文件的函数:mexFunction,完成MEX文件和MATLAB的接口功能,实现数据的通信和控制权转换;mexGetArray,从MATLAB中读出矩阵;mexPutArray,向MATLAB中写入矩阵;mexGetArrayPtr,取得MATLAB工作空间中的矩阵指针;mexE2valString,执行字符串所代表的MATLAB命令;mexCallMatlab,调用MATLAB的指令、M函数和其它MEX文件;mexErrMsgTxt,处理异常情况.此外,MATLAB还带有许多用于进行数据交换的API指令,它们都以mx开头,这在前文已经用到过了.MEX文件的具体编写方法和在MATLAB中的调用方法请参见〔3〕.4.2.2 生成独立的C/C++应用程序.
使用mcc命令编译时,++可执行程序的原代码C/++,这样的应用程序可以脱离MATLAB.:
mbuildmyfile.cpp
执行结束生成myfile.exe文件,mbuild的详细使用方法可参见MATLAB帮助文件.另外,在执行编译连接过程中会用到以下几个库文件:MATLAB的M文件数学库(libmmfile.dll),MATLAB编译程序库(libmcc.dll),MATLAB内嵌数学库(lib2matlb.dll),MATLAB应用程序接口库(libmx.dll),MATLAB工具库(libut.dll)和ANSIC数学库.
注意:①将生成的C++原代码编译连接为独立的应用程序时,除了会用到以上几个库以外,还需要有MATLABC++MATH库.
②所有的MATLAB动态连接库均位于&matlab&\bin目录,在发布或运行应用程序时,必须确保上述几个库文件位于应用程序所在的目录.如果系统中未装有MATLAB,就需自己将它们复制到相应位置.
③如果要在VC的工程中使用由M文件生成的应用程序,除了通过mbuild命令进行编译连接后再加入工程外,还可以将原代码加入到工程中,在IDE(集成开发环境)下编译连接,我们将在下面讲述这种方法.4.2.3 生成独立的C/C++动态连接库.
通过mbuild命令编译连接生成C/C++动态连接库的方法与生成独立应用程序的方法一样,这里不再赘述,我们只就有关如何使用IDE进行编译连接的问题进行讨论(另外我们也可以用集成开发环境编译生成MEX文件),假定我们现在使用的是VC集成开发环境,MATLAB的安装目录为D:\Mat2lab.在此之前,我们补充说明前文所提到的通过mcc命令生
①mcc2m2Wlib:funlib2Tlink:libfun.m生成C动态连接库
②mcc2p2Wlib:funlib2Tlink:libfun.m生成C++动态连接库
③mcc2Wlib:mylib2LC2t2Tlink:lib2hmyfile.m生成具有特定计算功能的C动态连接库
其中,funlib和mylib是编程者自定的名字,fun.m和my2file.m是M文件名.
对IDE设置的主要步骤如下:
①在VC集成开发环境中选择Tool|Options,进入Options对话框,选择Directories标签,然后在Showdirectoriesfor中选中Includefiles,并在Directories栏中增加指向MATLAB外部接口的目录:D:\Matlab\Extern\Include及D:\Matlab\Extern\Inlude\cpp.
,会用到DLL的引入库文件),所以,可在DOS方式下运行命令:
def:文件名.def/out:文件名.lib/machine:ix86
例如:lib/def:libmx.def/out:libmx.lib./machine.ix86,其中libmx.def是由MATLAB提供的,libmx.lib是执行该命令所生成的引入库文件.最后把得到的这些引入库文件复制到D:\Matlab\extern\Include目录下.
③在VC环境中选择菜单Project|Settings,并选中C/C++项,设置Category栏中选项为Preprocessor,然后在Preproces2sordefinition栏中内容后加上MSVC,MSWIND.
④紧接着在第三步出现的对话框中设置Category栏中选项为CodeGeneration,随后在出现的UseRuntimeLibrary列表框中选择MultithreadedDLL.
另外还要注意的是,在编译连接时应相应的加上4个头文件matlab.h,matrix.h,mcc.h,math.h.
从上文我们可以看出,无论是用命令行方式,还是用IDE进行MATLAB和VC的接口连接都十分的不方便.比如在IDE中,用户必须得自己动手建立
MSVC工程,加入适当的库文件(.lib)和路径,而且当加入工程的M文件被改动时,重新编译C/C++文件是十分困难的事.正因为如此,MathWorks公司针对VisualStudio开发环境提供了add2in工具条.通过add2in可以大大地方便用户在IDE环境中使用M文件,其主要功能如:
①轻松地将M文件加入到一个已存在的VC工程中;②快捷地建立基于M文件或MEX文件的新VC工程;③只需要调试M文件,而无需调试由MATLAB编译器编译生成的C/C++文件,大大降低了调试的难度;
④可以在调试中使用可视化矩阵查看器(VisualMatrixViewer)检查矩阵数据;
⑤将整个工程打包发布应用程序.而当你在MATLAB环境下运行mbuild2setup或mex2setup命令并选择VC5.0或VC6.0作为缺省的编译器时,add2in会
China Academic Journal Electronic Publishing House. All rights reserved.
12期       葛昭攀等:基于MATLAB进行混合编程的接口技术实现  2223
自动加载到你的系统中.但必须注意的是,由于MATLABadd2
in是在MATLAB环境之外运行,所以在运行了mbuild2setup或
mex2setup命令后,还要运行命令“cd(prefdir);”才
接口仅涉及到MATLAB与一些程序开发软件之间的接口.事实上,MATLAB还可以通过ODBC与oracle、access等数据库结
合,而且MATLAB除了能处理一些常用的数据外,更增添了处理空间数据的功能(利用MATLABMapping工具箱),因此将VC/VB、MATLAB、数据库以及一些地理信息软件结合起来就可以开发出功能强大的智能空间决策支持系统,对于这一点我们另文讨论.
Reference:
图2 add2in浮动工具条Fig.2 Add2infloatingbar
找到用户的MATLAB路径.这之后,就可以在MSVC中配置
add2in了(以Windows2000为例),选择Tools|Customize菜单,在Add2insandMacroFiles页下选中MatlabAdd2in复选框,然后add2in浮动工具条就出现了(如图2),而且当你每次启动MSVC时add2in都会自动加载.由于使用Add2in较为简便,读者可通过点击add2in的Help图标了解其具体使用方法.
当然,有了Add2in以后并不是说前文所提到的一些方法就没用了,这是因为只有当我们十分清楚地了解了MATAdd2助,从而更好的地使用这种工具.
1ZhangYi2hua.MasteryofMATLAB〔M〕.Beijing:TsinghuaUniversityPress,1999.(InChinese)
2WangGuo2yintr.InsideVC++〔M〕.Beijing:TsinghuaUniversityPress,1996.(InChinese)
3ShiDan,SangNong.StudyinHybridprogrammingbetweenmatlabandVC++anditsapplicationinimageprocessing〔J〕.ComputerEngineer2ing,):35~38.(In)
4DuanXiao2jun,DuXiao2y,2yun.TransferringthefunctionofATD〔J〕.ComputerAndInforma2,(1)~(InChinese)
guide〔M〕.TheMathWorks,Inc,1999.
1张宜华.精通MATLAB5〔M〕.北京:清华大学出版社,1999.2王国印.VisualC++技术内幕〔M〕.北京:清华大学出版社,1996.
3石丹,桑农.Matlab与VC++混合编程的研究及其在图象处理
MATLAB的功能和特点使其在数值分析方面具有无可比
中的应用〔J〕.计算机工程,):35~38.
4段晓君,杜小勇,易东云.将Matlab函数转换为VB可用的DLL
拟的优势,而通过对MATLAB接口的研究将其与VC、VB等开
发软件结合起来,则既可以大大降低程序员开发复杂算法的难度,又可以保证所开发系统中算法的高效、稳定,因此对我们的科研或实践都大有益处.但需要说明一点,本文所讲述的
〔J〕.电脑与信息技术,~47.
China Academic Journal Electronic Publishing House. All rights reserved.
看过本文章的还看过。。。
第24卷第12期2003年12月 小型微型计算机系统 mini—mlcr0systems v24 no.12 dec.2003 基于matlab进行混合编程的接口技术实现葛昭攀,吴兵(华东师范大学城市与.........
第24卷第12期2003年12月 小型微型计算机系统 mini—mlcr0systems v24 no.12 dec.2003 基于matlab进行混合编程的接口技术实现葛昭攀,吴兵(华东师范大学城市与.........
如要投诉违规内容,请到百度文库投诉中心;如要提出功能问题或意见建议,请点击此处进行反馈。 基于matlab进行混合编程的接口技术实现 matlab与java混合编程技术matlab与.........
基于matlab与java的混合编程研究及实现_互联网_it计算机_专业资料。java和matlab...的接口, 把事先 存放的文件数据信息写入到 matlab 环境中, matlab 对数据进行.........
化技术、matrixvb技术、动态数据交换di)e技术、通过编写m文件实现接口连接技术。...基于matlab进行混合编程... 5页 2下载券 2017 baidu 使用百度前必读 | 文库.........
为了缩短永磁同步电动机的研制周期,本文开发了基于vb与matlab混合编程的永磁同步电动机电磁设计软件.提出了vb与matlab混合编程的实现方法及其接口技术,实现了输入数据.........
基于matlab与.net平台接口... 3页 20财富值 visualc...matlab与c#混合编程 35页 免费 浅谈c#与matlab的混合...( aamasan ) d t r l g 技术 来 实现 托 .........
1 vc++与matlab混合编程的实现方法 matlab与c语言的接口技术[1pmatlab与 vc混合编程技术,它是通过应用程序接口(api)瞪1实现的。一般而言,matlab与vc混合编程的.........
matlab与fortran程序的接口技术_计算机软件及应用_it计算机_专业资料。matlab2002年第17卷第2期(总第59期) 电力学报journalofelectricpower vol-17no.22002 (sum..........
(2007)专辑一0186一03 基于matcom的matlab和vc++的混合编程吴晟,刘爱国(江西鞲西供电公司市场营销部,江西新余338000) 摘要:目前实现matlab和vc++的接口技术很多,.........
2 基于 matlab 编译器的 vc 与 matlab 混合编程实现层次聚类分析 1 创造 ...[2] 陈永超 , 李汉强 .matlab0 与 vc0 的接口技术 [j]. 电脑知识与.........
2008年第24卷第12期 matlab与fortran接口技术赵晓艾摘要:介绍了动态链接函数库mex文件,mat文件及matlab引擎的调用原理,利用matlab和fortran接口技 术实现fortran程序.........
通过动态链接库实现vb和... 3页 免费 vb与matlab混合编程探讨 5页 免费m...与vta65 b混合编程中的接口技术研究t eefe hilerimalb.ad hbipormihitrctcnc.........
? 在此,我们以matlab和vc++的混合编程为例,介绍matlab的接口技术 matlab与vc+...打开关闭一个matlab对话、向matlab发送命令字符串、 实现vc++和matlab的数据.........
matlab与 fortran语言混合编程的接口技术_专业资料。阐述了在matlab环境下,调用fortran语言的原理,并通过一实例说明如何实现matlab,fortran两种语言的混合编程.今日.........
自动化、com组件、dde、dll等接口技术实现lab. view与matlabsimulink混合编程的...编写撵本程序 基于activex自动诧技术懿并发方式可l;l更搬灵 活缝对matlab进行.........
种实现matlab与vc混台编程的新的接口技术,说明了该...环境要求:安装matlab6,matlab编译器组,混合编程的有...舒全英.宋星原.罗鹏 基于matlab c—mex及优化工具.........
matlab与+fortran语言混合编程的接口技术_计算机软件及...一窭倒说明如何实现ma山b,fom柚两种语言的混合编程...选用基于邻域 特征对cr和mr不同模态的医学图像进行.........
matlab与c++接口与混合编程小结_电子电路_工程科技_...否则只画一个图出来 这是我问他们的技术支持搞...基于matlab进行混合编程... 6页 免费 matlab与cvisua.........
分别为基于数据 共享、基于源码转换和库引用、基于对matlab计算引擎的控制、应用labview...的程序控制技术或程序控制接口来实现两者的混合编程, 如对 matlab 计算引擎.........
l-nsvc 2基于maflab编译器的vc与matlab混合编程实现层次聚类分析1创造m...matlab7基础与提高 2005 陈永超;李汉强 matlab0与vc0的接口技术 2006(.........
■ 热门推荐

我要回帖

更多关于 c语言两个矩阵相乘 的文章

 

随机推荐