Intel® oneAPI Math Kernel Library
Ask questions and share information with other developers who use Intel® Math Kernel Library.

Use Intel MKL libraries with Matlab 2012b

Jeena_J_
Beginner
1,408 Views

         

Hi all, 

       I am working with a Matlab code and needs to handle large sparse systems. I have to take perform Mathematical operations like 'inverse', eigen value computation etc on these sparse systems. While using the inbuilt Matlab functions like 'inv()' and 'eig()', I ended up in getting memory issues and the program takes so much time to finish the analysis. This forces me to consider incorporating Intel MKL library functions to my code. I couldn't find a proper help to do so. I have seen certain procedures described for Matlab R2010a version http://software.intel.com/en-us/articles/using-intel-mkl-with-matlab

But I couldn't find any information for using MKL with Matlab R2012b. Can anyone help me with this regard ?

0 Kudos
20 Replies
mecej4
Honored Contributor III
1,398 Views

More details on the problems that you are working on would help. How big is 'large'? Do you want the dominant eigenvalues or all of them? Secondly, before considering writing MEX files to do the job, investigate using the sparse-matrix routines (those have the prefix "sp-") in Matlab. Thirdly, when one mentions that a matrix inverse is needed it can be taken as a sign of inexperience in linear algebra, because almost always explicit computation of matrix inverses is neither necessary nor efficient or accurate.

0 Kudos
Jeena_J_
Beginner
1,398 Views

Hi, 

Thanks for the comments

The systems are atleat 5000 x 50000 matrices.... I donn't need the complete eigen vectors, but needs only few (between 6-15)....

While taking the eigen values , I have two matrices.. the first one is a full matrix and the second one is a sparse singular matrix.... both have a size more than 5000 x 5000 ... If I use the 'sparse' function in matlab, I have to use 'eigs' to compute the eigen vectors and mode shapes.... but 'eigs' function can't compute the eigen vectors when matrix is singular ..... and my second matrix may or may not be singular ....

0 Kudos
Ying_H_Intel
Employee
1,398 Views

Hi Jeena,

As i understand, there are two questions,

1. choose suitable lapack functions to perform operation on the sparse matrix.

Not exaxt sure if you can call them in Matlab directly.  Matlab should have blas and lapack function supported.  So what you need to do  is to choose suitable function.   you may consult mathworks for the functions.

2. Regarding use MKL in Matlab.

The paper http://software.intel.com/en-us/articles/using-intel-mkl-with-matlab.  mentioned, Matlab have employied MKL BLAS in itself.  So you may check if it is ture by command

> > version - lapack

>> version - blas

 (sorry, i haven't matlab so can't test).

If both  show MKL verstion. then actually, matlab have used MKL blas and lapack..  You don't need to find a way to use MKL with matlab.

If you'd like try, then the mathworks article http://www.mathworks.com/help/matlab/matlab_external/calling-lapack-and-blas-functions-from-mex-files.html

and the article   http://software.intel.com/en-us/articles/intel-math-kernel-library-intel-mkl-for-windows-using-intel-mkl-in-matlab-executable-mex-files

should be helpful.  (but it may take time as you may run other configuration problems like the forum mentioned http://mdkey.org/?p=277 ) ,

So  if your computation task is independent and have other compiler like Intel compiler + microsoft Visual C. you can write C code directly call MKL function. (save your matrix to a file, then read them to C file)

Best Regards,

Ying

0 Kudos
Jeena_J_
Beginner
1,398 Views

Hi , 

                                  Thanks for the comments.. I types the 'version' command and it displayed the version of Intel MKL being used in my Matlab version. 

   Now if I need to call a lapack function , say the function to create the inverse of a matrix - do I need to create a mex file / C# file to call the function ??

Thanks, 

Jeena 

0 Kudos
SergeyKostrov
Valued Contributor II
1,398 Views
Jeena, >>>>...I ended up in getting memory issues and the program takes so much time to finish the analysis... >> >>The systems are atleat 5000 x 50000 matrices.... I donn't need the complete eigen vectors, but needs >>only few (between 6-15).... >> >>While taking the eigen values , I have two matrices.. the first one is a full matrix and the second one is >>a sparse singular matrix.... both have a size more than 5000 x 5000 ... On what platform did you have memory related issues? Is it 32-bit or 64-bit? Do you use Single-Precision or Double-Precision floating point types? In case of 5000 x 50000 matrices of Double-Precision floating point type on a 32-bit Windows platform you could have some memory related problems. However, you did not mention what errors you had and please provide these details.
0 Kudos
TimP
Honored Contributor III
1,398 Views

On such large problems, taking an inverse isn't a suitable way to solve a problem for which there is an alternative.  Even "inverse iteration" for eigenvalues doesn't actually take an inverse.  Of course, this is a fairly deep academic specialty for which others reading this list should be better qualified to make recommendations than I.  I note that the search string "eigenvalue inverse iteration sparse matrix" is insufficiently specific, but you should at least skim relevant Wikipedia articles to refine questions to be asked here.

0 Kudos
Jeena_J_
Beginner
1,398 Views

Hi, 

Thanks for the comments...

@Sergey Kostrov

I am working on Windows platform 32-bit OS with 2GB RAM.....and I am using the default data type in Matlab ( i didn't intentionaly change the data type) ... I can see each variable takes 8 bytes storage space ....

For large systems, if I use [C,D] = eig(A,B), it shows up 'out of memory by using eigs() ' ... even for a smaller problems, by looking through the  performance profile of Matlab , I can see 'eigs()' take 99% of the total computation time ... 

My main question here is how to use Intel MKL with Matlab ( as I may have even higher matrices and using 'eig()' within Matlab takes so much time for computation ) I have seen a procedure to link MKL with Matlab version R2010a...http://software.intel.com/en-us/articles/using-intel-mkl-with-matlab .... I wasn't sure if its the same procedure for linking MKL with Matlab R2012b , as I was getting some errors ... 

0 Kudos
Jeena_J_
Beginner
1,398 Views

@ TimP

I am not using 'inverse' anywhere in Matlab code, but I was just trying to explain the situtation why I needed to use Intel MKL routines...I just meant I might need to deal with large sparse as well as full matrices and thus using the inbuilt Matlab functions might make my program work slow and ends up in storage issues.... 

At some stage or other I need to use MKL library functions with Matlab and thus I was trying to call MKL library functions through my Matlab program ... http://software.intel.com/en-us/articles/using-intel-mkl-with-matlab .... This describes about making MKL use with Matlab start up...I was trying to follow the instructions described in this page... When I was trying to build the custom dll using 'nmake ia32' it started showing some syntax errors .... Not sure if I set wrong environment variables or something...I have typed the following in Intel MKL command prompt ...

C:\Program Files\Intel\Composer XE 2013.149\mkl\bin\ia32\mklvars_ia32.bat 

cd..

cd..

set MKLROOT="C:\Program Files\Intel\Composer XE 2013.149\mkl"

set INCLUDE="C:\Program Files\Intel\Composer XE 2013.149\mkl"\include

set LIB="C:\Program Files\Intel\Composer XE 2013.149\mkl"\lib\ia32

cd tools

cd builder

nmake ia32 

I tried to follow whats explained in the above mentioned page , but after 'nmake ia32' the following error came

makefile(92) : fatal error UI023 : syntax error in expression Stop

http://software.intel.com/en-us/articles/intel-mkl-111-getting-started#Environment    I could successfully complete the example described in this page for linking with a C code....   so I know MKL works in my computer ....

0 Kudos
mecej4
Honored Contributor III
1,398 Views

Matlab already comes with MKL libraries integrated into it. The Intel article that you cited above is about replacing those libraries with a newer version of MKL, and probably does not apply to your installation. Secondly, you are not likely to see the performance of Matlab functions such as eig() improve dramatically as a result of upgrading to a more recent version of the Matlab MKL DLLs. In other words, given a calculation where 10 percent of the code consumes 90 percent of the CPU cycles, tinkering with the other 90 percent of the code and making it faster is not worthwhile unless you are a perfectionist.

In fact, I question why you must remain tied to using Matlab for a high-performance task where using a compiled language such as C or Fortran together with MKL might work out to be a better solution.

0 Kudos
Jeena_J_
Beginner
1,398 Views

@ mecej4 

Thanks for the clarifications .... If Matlab has lapack and blas libraries inbuilt to it, then can I call a lapack function without writing a mex file ? For example, if I need to solve a system of equations , can I directly use  functions like 'DGESV' through Matlab scripts by properly giving the inputs for the function 'DGESV' .... or do I need to create the mex file to call the function always ?

So far I am creating a prototype model using Matlab scripts and test it for accuracy of results, computation speed and then on a later stage someone else will implement it in using C or Fortran (as I have a zero experience with C or Fortran )... 

0 Kudos
mecej4
Honored Contributor III
1,398 Views

You are complicating things far more than is reasonable by thinking of calling MKL routines through Matlab. Matlab gives you a well thought-out language and environment for performing mathematical work at a higher level than you can in C or Fortran. You express your algorithm in the Matlab language, and Matlab takes care of translating, as needed, your Matlab expressions to calls to MKL routines.

Given that you have no experience in C or Fortran, my advice is that for the time being you put aside any thoughts of using MKL from Matlab or writing .MEX files. The chances are quite high that .MEX files written without the necessary in-depth understanding would run more slowly than straightforward Matlab code, and therefore do little more than waste your time.

0 Kudos
SergeyKostrov
Valued Contributor II
1,398 Views
Jeena, I keep my response as short as possible and I marked ( in bold ) your the most important comments. >>>>...The systems are at least 5000 x 50000 matrices... >> >>I am working on Windows platform 32-bit OS with 2GB RAM... and I am using the default data >>type in Matlab ( i didn't intentionaly change the data type) ... I can see each variable takes 8 bytes storage space... >> >>For large systems, if I use [C,D] = eig(A,B), it shows up 'out of memory by using eigs() ' ... even for a smaller problems, >>by looking through the performance profile of Matlab , I can see 'eigs()' take 99% of the total computation time... 5,000 x 50,000 = 250,000,000 ( elements ) x 8 ( size of double-precision floating point type ) = 2,000,000,000 ~= 1.9GB 1.9GB is needed in the best case and memory limitation for a 32-bit application on the 32-bit Windows operating system is 2GB. So, you need to try all these calculations on a 64-bit Windows operating system with a 64-bit application and a 64-bit version of Matlab.
0 Kudos
SergeyKostrov
Valued Contributor II
1,398 Views
>>... I can see each variable takes 8 bytes storage space... Or, change the data type to single-precision ( sizeof( float) = 4 bytes ) and I expect ~0.95GB of memory will be needed.
0 Kudos
Jeena_J_
Beginner
1,398 Views

Thanks for the comments :)

I will incorporate all these modifications and see the memory requirements 

0 Kudos
SergeyKostrov
Valued Contributor II
1,398 Views
Try to use Windows Task Manager ( Processes property page ) in order to verify how much memory is allocated for your application. It would be nice to see a screenshot for the case when 'Out of Memory' message is displayed ( including the dialog of Windows Task Manager! ).
0 Kudos
Ying_H_Intel
Employee
1,398 Views

Hi Jeena,

No, you can't call lapack function direclty in matlab. As i learned, there are 3 ways to use MKL in matlab,

1. some of matlab function have called MKL function internally,

2. maybe some matlab plugin have provide such wrapper, you can call lapack funtion directly.

3. call lapack function and link mkl library yourself as the artice discrible on mathworks.  ( in that paper, it use libmwlapack, you can change it into mkl libray) 

So agree to Sergey and Mecej4.

As you mentioned MKL are in your matlab.   the steps in the article was about using new version version instead of old mkl version.  So if the mkl version is new enough, you don't need to do them again.

Second, (not 100% exact sure, but you can consult mathworks)  the matlab fuction eigs() itself have accelerated by MKL lapack functions, (when mkl are already there), so there is  less chance for you to call lapack function manually to improve the performance unless you have special matrix structure and use special lapack functions.  on the other hand, if you have decide to call lapack, then the better solution is to call it directly in C , otherwise you will run into all kind of problems before you make all things ready。  

third, your matrix is too big to 32bit platform, you may need 64bit platform and 64bt matlab.

Best Regards,

Ying

0 Kudos
SergeyKostrov
Valued Contributor II
1,398 Views
I'd like to make a small comment on: >>...As i learned, there are 3 ways to use MKL in matlab... A 4th way is as follows: Any C function, that is from MKL or IPP libraries, or Win32 API, could be called from Matlab using a custom developed ActiveX component.
0 Kudos
Ying_H_Intel
Employee
1,398 Views

Hi Jenna,

Any updates?

I guess, the 64bit matlab should be able to solve the out of memory problem.

As sergey mentioned, there are all kind of ways to use c function (mkl are c/fortran function in nature )  in Matlab  (or call matlab in C).  

There are much articles in mathwork regarding lapack in matlab. As i see, the eig() itself use lapack,

for example, http://www.mathworks.com/company/newsletters/articles/matlab-incorporates-lapack.html

 There are now 16 different code paths underlying the eig function, depending on whether there are one or two arguments, whether the arguments are real or complex, whether the problem is symmetric and whether the eigenvectors are requested.

and here is another simple way to cal build-in lapack in matlab in *.m.

http://stackoverflow.com/questions/6438551/calling-matlabs-built-in-lapack-blas-routines

// dgesvd.m

X = rand(4,3);
[m,n] = size(X);
C = lapack('dgesvd', 'A', 'A', m, n, X, m, zeros(n,1), ...
           zeros(m), m, zeros(n), n, zeros(5*m,1), 5*m, 0);
[s,U,VT] = C{[7,8,10]};
V = VT';

If you are working within Matlab, could you pleas try it and let us know how it works?

Best Regards,
Ying

0 Kudos
RANGU_R_
Beginner
1,398 Views

hi,

   Embedding Process
 i am dividing high frequency coefficients into 3*3block ,if center coefficient f3(i,j) and i am finding average of minimum and maximum coefficients value..................condition 1 and 2 for inserting bit 0 and condition 3 and 4 for inserting bit 1. for retriving this condition is not working  plz help me out.

if f3(i,j) >=avg(i,j)       ...................1

    f6(i,j)=f3(i,j);         

end
   if ((f3(i,j)<avg(i,j))            .....2
    f6(i,j)=avg(i,j)+1;

end
  
    

if f3(i,j) <avg(i,j)       ...................3
    f6(i,j)=f3(i,j);         

end
   if ((f3(i,j)>=avg(i,j))            .....4
    f6(i,j)=avg(i,j)+1;

end

for extraction

if f3(i,j) <avg(i,j)    then extract bit 0

if f3(i,j) >=avg(i,j)    then extract bit 1

 

 

 

thank u advance  ........

0 Kudos
Ying_H_Intel
Employee
1,273 Views

HI RANGU,

As i understand, your case is kind of comparison and assignment operation.  You can implement them either in Matblab or in C.  Is there any special reason for you to ask the question in mkl forum? do you want to implement them in C code, then call MKL function?

Best Regards,

Ying

 

Or if you'd like use some functions to do this, if your data type is 32floating number, you may use IPP function .

ippiThreshold_LTValGTVal_32f_C1R( pSrc, int srcStep, pDst, int dstStep, roiSize,  thresholdLT, valueLT, thresholdGT, valueGT);

for example,  if f3(i,j)<2, then f6(i,j)=1,  if f3(i, j)>6, then f6(i, j)=7. 

IppStatus threshold( void ) {
Ipp8u x[5*4];
IppiSize roi = {5,4};
int i;
for( i=0; i<5*4; ++i ) x = (Ipp8u)i;

// the x in the function as input and out.  you can call _C1R which support x as input, y as output
return ippiThreshold_LTValGTVal_8u_C1IR( x, 5, roi, 2,1,6,7 );

}
The destination image x contains:
01 01 02 03 04
05 06 07 07 07
07 07 07 07 07
07 07 07 07 07

 

0 Kudos
Reply