顯示具有 matlab 標籤的文章。 顯示所有文章
顯示具有 matlab 標籤的文章。 顯示所有文章

2011年3月20日 星期日

cuda under matlab mex

最近需要用到cuda來提升方法的速度,,,因為用得環境是matlab, 所以就透過mex來連結cuda與matlab.
事實上, matlab 2010b以經可以直接在matlab底下撰寫gpu的kernel function了(參考), 所以如果您有capability >= 1.3 以上的顯示卡, 就不需要透過mex file來當做matlab與cuda的溝通橋樑了.
如果您需要mex的話, 請參考底下.

環境: win7 + cuda 3.1 + matlab 2010b.
先從底下這個網址下載好心人寫好的setup_cuda_mex.m, 接著在matlab底下直接run 該.m檔.
注意:
 1. win 7在C槽底下有permission的限制, 所以要將產生的cuda_mex.pl放到matlab/bin底下
 2. x64的問題該聯結底下有好心人回答.

正常來說這樣應該就可以順利產生.mexw64檔案了.

FAQ1. cuda底下並不支援double-precison的加速效果, 可以參考這篇文章.
如果真的有double-precision的需求, 可在 setup_cuda_mex.m中 'compflags' 底下加上
' --arch sm_13' 就有支援double.
FAQ2. launched timeout問題:
如果在測試的過程中有出現"launched timeout"等訊息的話, 可以參考這篇文章,
如果不是register 或share memory用得過多的話, 就是kernel func花了太多時間在執行上.
所以有個方法可以設定kernel func的launched time參考.(不是很建議, 因為該時間內您的螢幕將會沒有反應XD)
cmd->regedit-> enter
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ GraphicsDrivers
TdrLevel = yourtimeoutinsec
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ GraphicsDrivers \ DCI\
Timeout = yourtimeoutinsec

然後reboot your computer, then it is done.

2010年8月3日 星期二

在matlab環境編譯mex + opencv library

由於工作的需要, 所以需要將別人所寫好的mex funtion重編一次;;;但是該mex function裡面有用到opencv的lib, 所以在編譯mex的時候, 需要將opencv的lib也一起加到matlab的環境中.

系統環境: win 7
matalb: matalb 2010a
c++ compiler: visual studio 2008

首先先設定matlab所選定的compiler:

1. matlab command: mex -setup
(C:\Users\xxx\AppData\Roaming\MathWorks\MATLAB\R2010a\mexopts.bat 為預設的mex option file)

Please choose your compiler for building external interface (MEX) files:
Would you like mex to locate installed compilers [y]/n? y
Select a compiler:
[1] Microsoft Visual C++ 2008 SP1 in C:\Program Files (x86)\Microsoft Visual Studio 9.0
[0] None
Compiler: 1
Please verify your choices:
Compiler: Microsoft Visual C++ 2008 SP1
Location: C:\Program Files (x86)\Microsoft Visual Studio 9.0
Are these correct [y]/n? y
***************************************************************************
Warning: MEX-files generated using Microsoft Visual C++ 2008 require
that Microsoft Visual Studio 2008 run-time libraries be
available on the computer they are run on.
If you plan to redistribute your MEX-files to other MATLAB
users, be sure that they have the run-time libraries.
***************************************************************************

Trying to update options file: C:\Users\xxx\AppData\Roaming\MathWorks\MATLAB\R2010a\mexopts.bat
From template: C:\PROGRA~1\MATLAB\R2010a\bin\win64\mexopts\msvc90opts.bat
Done . . .
**************************************************************************

2. 修改mexopt.bat
由於opencv所放置的路徑為: C:\Program Files\OpenCV-2.1.0\vs2008
所以修改mexopts.bat檔, 請參考.
主要修改的有:
set OPENCVDIR=C:\Program Files\OpenCV-2.1.0\vs2008
set MATLAB=%MATLAB%
set VSINSTALLDIR=C:\Program Files (x86)\Microsoft Visual Studio 9.0
set VCINSTALLDIR=%VSINSTALLDIR%\VC
rem In this case, LINKERDIR is being used to specify the location of the SDK
set LINKERDIR=C:\Program Files\\Microsoft SDKs\Windows\v6.0A\
set PATH=%VCINSTALLDIR%\bin\amd64;%LINKERDIR%\bin\x64;%VCINSTALLDIR%\BIN;%VSINSTALLDIR%\Common7\Tools;%VSINSTALLDIR%\Common7\Tools\bin;%MATLAB_BIN%;%PATH%
set INCLUDE=%LINKERDIR%\include;%VCINSTALLDIR%\ATLMFC\INCLUDE;%VCINSTALLDIR%\INCLUDE;%VCINSTALLDIR%\VCPackages;%INCLUDE%;%OPENCVDIR%\include\opencv
set LIB=%LINKERDIR%\LIB\x64;%VCINSTALLDIR%\LIB\amd64;%VCINSTALLDIR%\ATLMFC\LIB\amd64;%MATLAB%\extern\lib\win64;%LIB%;%OPENCVDIR%\lib;%OPENCVDIR%\bin
...略...
set LINKFLAGS=/dll /export:%ENTRYPOINT% /LIBPATH:"%LIBLOC%" libmx.lib libmex.lib libmat.lib /MACHINE:X64 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /incremental:NO /implib:"%LIB_NAME%.x" /MAP:"%OUTDIR%%MEX_NAME%%MEX_EXT%.map" cv210.lib HighGUI210.lib cxcore210.lib

3. build mex: mex -c xxx.cpp