LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Speeding up MATLAB scripts that return large arrays

I have a MATLAB script that runs in a subVI and returns a 512x512 array, which I pass on to another subVI in my main program that uses it to draw a pixmap.  Unfortunately, the code is a little bit too slow right now.

So, I was wondering: Is LabVIEW being slowed down because it is re-allocating this 512x512 array every time I run this subVI?  If it is, perhaps I could persuade it to keep using the same array?  After all, once the picture has been displayed, I don't need the data anymore.  I was thinking I could do something like this:

Or would this be even worse because it passes the whole array back to MATLAB?  (Or is data transmission from MATLAB to LabVIEW so fast that I shouldn't be worrying about it at all?)

Am I thinking about this the right way, or is there something in particular about this implementation that I should be aware of?  I seem to recall trying something similar once that didn't work as well.
0 Kudos
Message 1 of 12
(4,451 Views)
I've found that having to initialize variables in MATLAB is kind of slow, but if you can initialize all MATLAB variables earlier, you computations will go faster.  Once a variable is defined in the MATLAB script window, that variable is always available for any other MATLAB script calls while the vi is open.

In my application, I used a MATLAB script window to initialize my variables in MATLAB to there allocated sizes.   I then used another MATLAB script window to pass the the data into the script window and assign it to the defined variables from the previous MATLAB script window.  Lastly, I used another MATLAB window to perform the operations on the data and read it out.


Randall Pursley
0 Kudos
Message 2 of 12
(4,433 Views)

rpursley8 wrote: I've found that having to initialize variables in MATLAB is kind of slow, but if you can initialize all MATLAB variables earlier, you computations will go faster.  Once a variable is defined in the MATLAB script window, that variable is always available for any other MATLAB script calls while the vi is open.
Yes, so since my script executes repeatedly and uses the same variable names each time, MATLAB at least will not be re-initializing the variables each time the script is run. But does LabVIEW directly access the area of memory where MATLAB stores the array? If not, is there some way to prevent LabVIEW from re-initializing a copy of the array every time?

Message Edited by kehander on 12-14-2007 09:27 AM
0 Kudos
Message 3 of 12
(4,421 Views)

Hi,
I would recommend reading the LabVIEW help.  There is a great section about memory usage in VIs.  Here is the online link for LabVIEW.  Basically you need to initialize the array before the loop starts to iterate but that is covered in the help.

 
Eric A.
National Instruments
Distributed I/O Product Support Engineer
0 Kudos
Message 4 of 12
(4,406 Views)
Yes, I'm aware of the dangers of dynamically resizing an array in a loop.  But that is not what I am doing here!  I am always using a 512x512 array; my question is whether LabVIEW is reallocating this array every time my MATLAB script is called.  And as I said, I'm thinking that passing the array back to MATLAB might also take time.


Message Edited by kehander on 12-14-2007 04:51 PM
0 Kudos
Message 5 of 12
(4,403 Views)
One thing to keep in mind is that the Matlab (TM) and LabVIEW have completely separate memory spaces. They literally can't share data directly by passing pointers back and forth. For instance, both applications might have memory addresses 0xABCDEFFF that point to completely different data. This isolation is actually a very good thing. It means one application that screws up can't usually hurt the other applications running.

The Matlab Scriptnode uses an ActiveX interface to send commands and data from LabVIEW. Generally these ActiveX interfaces use TCP to transmit the data, even though the applications are on the same computer. So if you pass a 512x512 array from LabVIEW to Matlab, there has to be a copy made (at least the first time). The same goes for when you receive the return value from the Scriptnode. Basically, there are going to be at least two copies of the big array in memory, one for each application.

Do you only need to pass this data from LabVIEW to Matlab once? What's the workflow like? Is the data changing on the LabVIEW side as well? An alternative you might try would be to save the data to file and load it from Matlab. Then at least you could read in the data in smaller chunks to do the processing and vice-versa with LabVIEW. Just a thought.


Message Edited by Jarrod S. on 12-14-2007 05:57 PM
Jarrod S.
National Instruments
0 Kudos
Message 6 of 12
(4,391 Views)
Ah, interesting. I guess it would be a little bit too much to ask for shared memory space.

The script in question is grabbing data from a camera and is executing several times per second. (Yes, it would be nice to read the data into LabVIEW directly from the camera, but I've tried that and it wasn't working very well.) Disk-writing is thus unlikely to make things any faster. I am doing some operations on the array in LabVIEW, but it does occur to me that I could speed things up by performing the operations in MATLAB instead.

Actually, the data in question is originally an array of uint16 in MATLAB and ultimately ends up as uint8 in LabVIEW. Maybe if I typecast it to double and then re-cast it in LabVIEW the data transfer will be faster? Or would typecasting in LabVIEW take too long?

Message Edited by kehander on 12-15-2007 12:05 AM
0 Kudos
Message 7 of 12
(4,381 Views)
I did some more testing, and typecasting does indeed seem to take so long as to completely erase the benefits of passing an array 1/8th of the original size.

So I'm still wondering: if LabVIEW is in fact re-allocating a 512x512 array every time I pass data back from my MATLAB script, can I stop that from happening?
0 Kudos
Message 8 of 12
(4,351 Views)
Hi kehander,
 
The array data may be rewritten each time, but since the array size is not changing separate memory should not be allocated on each iteration. 
 
You said that reading data directly into LabVIEW didn't seem to be working very well.  What exactly were the results when you tried that?
Jennifer R.
National Instruments
Applications Engineer
0 Kudos
Message 9 of 12
(4,281 Views)
I would not try to use MATLAB and LabVIEW together in the same application, but I know it's tempting. You're better off writing native code in either development platform/language. As someone has written already, to run the MATLAB code, first LV has to open MATLAB and then process the code, that in it of itself is a slow process. Think native and your apps speed should increase considerably. That's been my experience anyway. Good luck!
0 Kudos
Message 10 of 12
(4,262 Views)