简体   繁体   中英

MEX code in a MATLAB Wrapper

I have the following code:

for i=1:N,
    some_mex_file();
end

My MEX file does the following:

  1. Declares an object, of a class I defined, that has 2 large memory blocks, ie, 32x2048x2 of type double.
  2. Processes the data in this object.
  3. Destroys the object.

I am wondering if it takes more time when I call a MEX file in a loop that allocates large memory blocks for its object. I was thinking of migrating to C++ so that I can declare the object only once and just reset its memory space so that it can be used again and again without new declaration. Is this going to make a difference or going to be a worthless effort? In other words, does it take more time to allocate a memory in MEX file than to declare it once and reuse it?

So, the usual advice here applies: Profile your code (both in Matlab and using a C/C++ profiler) , or at least stop it in a debugger several times to see where it's spending its time. Stop "wondering" about where it's spending its time, and actually measure where it's spending its time.

However, I have run into problems like this, where allocating/deallocating memory in the MEX function is the major performance sink. You should verify this, however, by profiling (or stopping the code in a debugger).

The easiest solution to this kind of performance problem is twofold:

  1. Move the loop into the MEX function. Call the MEX function with an iteration count, and let your fast C/C++ code actually perform the loop. This eliminates the cost of calling from Matlab into your MEX function (which can be substantial for large N), and facilitates the second optimization:

  2. Have your MEX function cache its allocation/deallocation, which is much, much easier (and safer) to do if you move the loop into the MEX function. This can be done several ways, but the easiest is to just allocate the space once (outside the loop), and deallocate it once the loop is done.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM