Compile the C-file in MATLAB - matlab

Description
I got a Mathematica Symbolic Toolbox for MATLAB--Version 2.0 here
Then I using the documentation it cotained to compile in MATLAB enviroment
Installation steps:
1)Go to your Mathematica directory and locate the file mathlink.h in
E:\math\Mathematica\5.\AddOns\MathLink\DeveloperKit\Windows\CompilerAdditions\mldev32\include'
and also the file ml32i1m.lib in
E:\math\Mathematica\5.\AddOns\MathLink\DeveloperKit\Windows\CompilerAdditions\mldev32\lib. Copy both files to a predetermined directory (we will refer to this directory as C:\xxx).
2) Copy the content of compressed file math.tar into C:\xxx.
3) Open Matlab command window and execute mex –setup. Choose “Microsoft Visual C/C++ version 6.0 in C:\Program Files\Microsoft Visual Studio”. This tells Matlab that it needs to use a C compiler (as opposed to Fortran compiler). You’ll need Microsoft Visual C/C++ installed. Do not choose the “Lcc C version 2.4 in C:\MATLAB6P1\sys\lcc” option.
4) Open Matlab command window and run mathrun.m. This program will compile the C-file math.c.
The file I got shown as belew:
Then I do step by step
(1) Find the mathlink.h and ml32i1m.lib in the following path
D:\WolframResearch\Mathematica\8.0\SystemFiles\Links\MathLink\DeveloperKit\Windows\CompilerAdditions\mldev32\include
D:\Wolfram Research\Mathematica\8.0\SystemFiles\Links\MathLink\DeveloperKit\Windows\CompilerAdditions\mldev32\lib
(2)Copy the content of compressed file math.zip into C:\XXX
(3) Compile in MATLAB
mex -setup
(4)last setp
addpath C:\XXX
run mathrun.m
I don't know why?
Update
The matlab code in mathrun.m
addpath C:\XXX;
% adds the directory C:\XXX to the list of directories which Matlab "sees" (referred to as paths)
mlpath='C:\XXX' % The directory where mathlink.h is
mllib='C:\XXX\ml32i1m.lib' % The library ml32i1m.lib
% make command
command=sprintf('mex -D__STDC __ -I % s % s % s', mlpath, 'math.c', mllib);
% compile
eval(command)

Seems like the path is not correctly passed to mex, so it cannot find math.c. Comment the original line:
%command=sprintf('mex -D__STDC __ -I % s % s % s', mlpath, 'math.c', mllib);
and add this one instead:
command=sprintf('mex -D__STDC __ -I%s %s %s', mlpath, 'math.c', mllib);
because the mex documentation specifies that there should be no space between the -I switch and the input path. To be really on the safe side, you can even write:
command=sprintf('mex -D__STDC __ -I%s %s %s', mlpath, fullfile(mlpath,'math.c'), mllib);

Related

Using LOADLIBRARY to load 'visa32.dll' and 'visa.h' in 64-bit Matlab R2017b

With Matlab R2015b on 32-bit Windows 7, LOADLIBRARY was working fine:
library = 'C:\Windows\System32\visa32.dll';
headerFile = 'C:\Program Files\IVI Foundation\VISA\WinNT\include\visa_Matlab.h';
loadlibrary(library, headerFile, 'alias','visa32');
and I can use functions in 'visa32.dll', such as,
defaultRm = 0;
[err, defaultRm] = calllib('visa32', 'viOpenDefaultRM', defaultRm);
vi = 0;
InstrAddr = 'GPIB0::29::INSTR';
pInstrAddr = libpointer('int8Ptr', [int8( InstrAddr ) 0] );
[err, tempVar, vi] = calllib('visa32', 'viOpen', defaultRm, pInstrAddr, 0, 0, vi);
to open instrument's communication port...
But after I have upgraded to 64-bit Windows 7 and Matlab R2017b, LOADLIBRARY won't work any more, even I have use 62-bit versions of 'visa32.dll' and 'visa.h'. There are two problems:
(1) Firstly, Matlab complaints about not have the right compiler installed and can't even run LOADLIBRAY. Follow their online instruction to install the MinGW-w64 Compiler addon. This problem seems to have revolved.
(2) LOADLIBRAY seems to be able to run, but then it has the following error:
Failed to parse type '( fastcall )) viOpenDefaultRM ( ViPSession vi' original input '( fastcall )) viOpenDefaultRM ( ViPSession vi '
Found on line 217 of input from line 93 of file C:\Program Files (x86)\IVI Foundation\VISA\WinNT\Include\visa.h
Error parsing argument for function attribute function may be invalid.
...
I wonder if somebody could help please. Many thanks in advance!
The advantages of using VISA library in MATLAB for instrument control are: direct (i.e. faster) access to ports, be able to use GTL (go-to-local) and handles communication error better, etc. Use LOADLIBRARY to load the VISA library and CALLLIB to access the functions in the loaded library, e.g.
loadlibrary('visa32.dll','visa.h','alias','visa32');
[err, defaultRm] = calllib('visa32', 'viOpenDefaultRM', defaultRm);
However, limitations in LOADLIBRARY have caused a lot of trouble, and it works differently for different Windows architecture. Here is the solution that has been tested on 32- and 64-bit Windows:
  1. C++ compiler in MATLAB:
    a. This only affects 64-bit MATLAB;
    b. A MATLAB add-on called ‘MinGW-w64 Compiler’ must be installed:
      i. this is relatively easy for R2017b, just use MATLAB’s Add-Ons Manager;
      ii. for MATLAB version prior to R2017b, it is very difficult:
       Download and install 'MinGW-w64' from:
       tdm-gcc.tdragon.net/download
       sourceforge.net/projects/tdm-gcc/postdownload?source=dlp
       sourceforge.net/projects/mingw/files/
       Then configure MATLAB as described in:
       de.mathworks.com/help/matlab/matlab_external/compiling-c-mex-files-with-mingw.html
       de.mathworks.com/help/matlab/matlab_external/install-mingw-support-package.html
       de.mathworks.com/matlabcentral/answers/313298-i-already-have-mingw-on-my-computer-how-do-i-configure-it-to-work-with-matlab
  2. VISA library file:
    a. Different DLL file for different Windows architecture;
    b. The library file is available after the installation of NI-488.2 driver (the latest version to date is 'NI4882_1760f0.exe';
    c. For 32-bit Windows and 32-bit MATLAB:
      i. Library file: ‘visa32.dll’
      ii. Location: ‘C:\Windows\System32\’
    d. For 64-bit Windows and 64-bit MATLAB:
      i. Library file: ‘visa64.dll’
      ii. Location: ‘C:\Windows\System32\’
      iii. Notes:
       1. According to NI’s website, the 64-bit VISA DLL is ‘visa32.dll’ that resides in 'C:\Windows\SysWOW64. This is not right;
       2. Alternatively, a 64-bit version of ‘visa32.dll’, which is smaller than ‘visa64.dll’ installed by NI-488.2 driver, can be downloaded from: https://www.azdll.net/files/visa32-dll.html.
  3. Header files:
    a. Two headers files are needed to load the VISA library: ‘visa.h’ and ‘visatype.h’;
    b. Both are available after the installation of NI-488.2 driver;
    c. For 32-bit Windows, both files are in:
‘C:\Program Files\IVI Foundation\VISA\Win64\include’
    d. For 64-bit Windows, both files are in:
‘C:\Program Files (x86)\IVI Foundation\VISA\WinNT\include’
    e. ‘visatype.h’ can be used as it is;
    f. ‘visa.h’ must be modified for MATLAB’s LOADLIBRARY. Changes made on ‘visa.h’ are:
      i. Add the following at the beginning of the header file:
#ifdef _WIN64
#define __fastcall
#endif
      ii. Mask out 'viInstallHandler' and 'viUninstallHandler', as they are not commonly used;
      iii. Remove ', ...' from the following functions as it’s not supported by LOADLIBRARY (https://de.mathworks.com/matlabcentral/answers/103180-why-do-i-get-warnings-when-loading-a-dll-with-loadlibrary-in-matlab-7-13-r2011b):
        viPrintf, viSPrintf, viScanf, viSScanf, viQueryf
      iv. Replace 'ViVAList' with 'ViConstBuf' in the following functions:
        viVPrintf, viVSPrintf, viVScanf, viVSScanf, viVQueryf
      v. Replace the 'viVxiServantResponse' function with the following:
#ifndef _WIN64
ViStatus _VI_FUNC viVxiServantResponse(ViSession vi, ViInt16 mode, ViUInt32 resp);
#endif
      g. The modified ‘visa.h’, renamed as ‘visa_Matlab.h’, can be downloaded from visa_Matlab.h.
Example:
% ‘visa32.dl’ or ‘visa64.dll’, ‘visa_Matlab.h’ and ‘visatype.h’ must be
% available in the local directory
archstr = computer('arch');
if ~isempty(strfind(archstr, 'PCWIN64')) || ~isempty(strfind(archstr, 'win64'))
dllName = 'visa64';
else
dllName = 'visa32';
end
headerFileName = 'visa_Matlab.h';
loadlibrary ([dllName '.dll'], 'visa_Matlab.h','alias',dllName);
visaFunctions = libfunctions(dllName,'-full');

Setting the output directory of mex files in the compiler command

My Project has the following structure:
MainFolder:
>>InitToolbox.m //Here addpaths are executed
>>Compile.m //Here mex compilations calls are made
AlgorithmsFolder //MATLAB code
UtilitiesFolder //MATLAB code
MexFolder // C++ files
>>test1.cpp
>>test2.cu
Whever I run (either in compile.m or directly in the command line) the following compiler call:
mex -v -largeArrayDims ./MexFolder/test1.cpp ./MexFolder/test2.cu
The output test1.mexw64 is saved in MainFolder.
Is there any way to modify the compiler call to create the .mexw64 file either on the original location of the files or in an specific user defined location?
You want to specify the output directory using the outdir option for mex
mex -v -largeArrayDims ./MexFolder/test1.cpp ./MexFolder/test2.cu -outdir output_directory
"output_directory" above can be any path you want.
It can also be a variable, but that would require you to update the way you're calling mex
outputFolder = 'path/to/folder';
mex('-v', '-largeArrayDims', 'MexFolder/test1.cpp', ...
'MexFolder/test2.cu', '-outputdir', outputFolder);

Matlab Distributed Server parfor can't find mex opencv files

everyone!
I'm trying to parallelize an algorithm that uses mex files from mexopencv (KNearest.m, KNearest_.mexw32).
The program is based vlfeat (vlsift.mex32) + mexopencv (KNearest.m and KNearest_.mexw32).I classify descriptors obtained from images.
All the code is located on the fileshare
\\ LAB-07 \ untitled \ DISTRIB \ (this is the program code)
\\ LAB-07 \ untitled \ + cv (mexopencv)
When I run the program with matlabpool close everything works well.
Then I make matlabpool open (2 computers on 2 cores each. ultimately 4 worker, but now I use for testing only 2 workers on the computer and run the program which)
PathDependencises from fileshare -> \LAB-07\untitled\DISTRIB\ , \LAB-07\untitled+cv
Before parfor loop I train classifier on the local machine
classifiers = cv.KNearest
classifiers.train(Descriptors',Labels','MaxK',1)
Then run parfor
descr=vlsift(img);
PredictClasses = classifiers.predict(descr');
Error
Error in ==> KNearest>KNearest.find_nearest at 173
Invalid MEX-file '\\LAB-07\untitled\+cv\private\KNearest_.mexw32':
The specified module could not be found.
That is KNearest.m finds, but no KNearest_.mexw32. Because KNearest_.mexw32 located in private folder, I changed the code KNearest.m (everywhere where it appeal KNearest_ () changed to cv.KNearest_ (). Example: this.id = сv.KNearest_ ()) and placed in a folder with KNearest_.mexw32 KNearest.m. As a result, get the same error
Immediately after matlabpool open file search on workers
pctRunOnAll which ('KNearest.m')
'KNearest.m' not found.
'KNearest.m' not found.
'KNearest.m' not found.
pctRunOnAll which ('KNearest_.mexw32')
'KNearest_.mexw32' not found.
'KNearest_.mexw32' not found.
'KNearest_.mexw32' not found.
after cd \LAB-07\untitled+cv
pctRunOnAll which ('KNearest.m')
\\LAB-07\untitled\+cv\KNearest.m
\\LAB-07\untitled\+cv\KNearest.m % cv.KNearest constructor
\\LAB-07\untitled\+cv\KNearest.m
>> pctRunOnAll which ('KNearest_.mexw32')
\\LAB-07\untitled\+cv\KNearest_.mexw32
\\LAB-07\untitled\+cv\KNearest_.mexw32
\\LAB-07\untitled\+cv\KNearest_.mexw32
I ran and FileDependecies, but the same result.
I do not know this is related or not, I display during the execution of the program classifiers
after training and before parfor
classifiers =
cv.KNearest handle
Package: cv
Properties:
id: 5
MaxK: 1
VarCount: 128
SampleCount: 9162
IsRegression: 0
Methods, Events, Superclasses
Within parfor before classifiers.predict
classifiers =
cv.KNearest handle
Package: cv
Properties:
id: 5
I tested the file cvtColor.mexw32. I left in a folder only 2 files cvtColor.mexw32 and vl_sift
parfor i=1:2
im1=imread('Copy_of_start40.png');
im_vl = im2single(rgb2gray(im1));
desc=vl_sift(im_vl);
im1 = cvtColor(im1,'RGB2GRAY');
end
The same error, and vl_sift work, cvtColor no...
If the worker machines can see the code in your shared filesystem, you should not need FileDependencies or PathDependencies at all. It looks like you're using Windows. It seems to me that the most likely problem is file permissions. MDCS workers running under a jobmanager on Windows by default run not using your own account (they run using the "LocalSystem" account I think), and so may well simply not have access to files on a shared filesystem. You could try making sure your code is world-readable.
Otherwise, you can add the files to the pool by using something like
matlabpool('addfiledependencies', {'\\LAB-07\untitled\+cv'})
Note that MATLAB interprets directories with a + in as defining "packages", not sure if this is intentional in your case.
EDIT
Ah, re-reading your original post, and your comments below - I suspect the problem is that the workers cannot see the libraries on which your MEX file depends. (That's what the "Invalid MEX-file" message is indicating). You could use http://www.dependencywalker.com/ to work out what are the dependencies of your MEX file, and make sure they're available on the workers (I think they need to be on %PATH%, or in the current directory).
Edric thanks. There was a problem in the PATH for parfor. With http://www.dependencywalker.com/ looked missing files and put them in a folder +cv. Only this method works in parfor.
But predict in parfor gives an error
PredictClasses = classifiers.predict(descr');
??? Error using ==> parallel_function at 598
Error in ==> KNearest>KNearest.find_nearest at 173
Unexpected Standard exception from MEX file.
What() is:..\..\..\src\opencv\modules\ml\src\knearest.cpp:365: error: (-2) The search
tree must be constructed first using train method
I solved this problem by calling each time within parfor train
classifiers = cv.KNearest
classifiers.train(Descriptors',Labels','MaxK',1)
But it's an ugly solution :)

compilemex in Statistical Pattern Recognition Toolbox in Matlab

I am new to Statistical Pattern Recognition Toolbox in matlab.
I am trying to use the oaasvm function in this toolbox. Before using this I need to run the compilemex.m. But when I run it I get the following error
>> compilemex
Compiling MEX files of STPRtool...
mex -O -IC:\Program Files\MATLAB\R2013a\toolbox\stprtool\kernels -outdir C:\Program Files\MATLAB\R2013a\toolbox\stprtool\kernels C:\Program Files\MATLAB\R2013a\toolbox\stprtool\kernels\kernel.c C:\Program Files\MATLAB\R2013a\toolbox\stprtool\kernels\kernel_fun.c
Error using mex (line 206)
The destination directory "C:\Program" cannot be found.
Error in compilemex (line 113)
eval(mexstr);
Can anyone tell how shall I proceed to run the oaasvm code?
As explained by Schorsch in the comments, you need to add quotes around path strings to handle spaces in them.
The fix is easy, edit the file compilemex.m, look for the following section (line 102), and make the below changes:
% -- Compile functions -----------------------------
for i=1:length(fun),
mexstr = ['mex -O -I''' translate(fun(i).include,root) ...
''' -outdir ''' translate(fun(i).outdir, root) ''' '];
for j=1:length(fun(i).source),
mexstr = [mexstr '''' translate(char(fun(i).source(j)),root) ''' '];
end
fprintf('%s\n',mexstr);
eval(mexstr);
end
Basically I've added single quotes (escaped by singe quote) around path strings. The result is that the commands executed will be of the form:
mex -O -I'C:\Documents and Settings\Amro\Desktop\stprtool\kernels'
-outdir 'C:\Documents and Settings\Amro\Desktop\stprtool\kernels'
'C:\Documents and Settings\Amro\Desktop\stprtool\kernels\kernel.c'
'C:\Documents and Settings\Amro\Desktop\stprtool\kernels\kernel_fun.c'
Note that you dont have to place this package inside the MATLAB installation. You can place the extracted folder anywhere on your system, as long as you add it to the MATLAB path addpath

finding error in command line

I am trying to run some kind of programm using command line, but I got an error.
The command line is:
quantisnp2.exe --outdir D:\output\ --config "C:\Program files\QuantiSNP\params.dat" --levels "C:\Program files\QuantiSNP\levels.dat" --sampleid CNV1 --gender female --emiters 10 --Lsettings 2000000 --doXcorrect --genotypes --gcdir D:\gc\ --input-files C:\Program files\CNV1.txt
QuantiSNP:Single-file mode input find.
QuantiSNP:Processing file: C:|Program
QuantiSNP:Local CG content directory specified. Local CG content correction will be used.
??? Error using ==>textread at 167
File not found.
Error in ==> quantisnp2 at 293
The first thing I'd be looking at is the unquoted C:\Program files\CNV1.txt at the end of the command (all your other ones are quoted).
There's a good chance that's being treated as two arguments, C:\Program and files\CNV1.txt.
You may also want to check the spelling of emiters, I'm pretty certain the correct English word would be emitters though, of course, this could be a case of the QuantiSNP developers not knowing how to spell :-)