From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW MathScript RT Module

cancel
Showing results for 
Search instead for 
Did you mean: 

how improve MathScript speed & how much fast in 8.2

Hi

Right now I'm working on data acquisition and process.
For processing the data, I'm using the MathScript.
However whenever I run the vi, it's extremely slow because of the MathScript.

Like one of this forum's answer, I did use multiple Mathscripts and try to put
an unnecessary calculation outside MathScript, but still it took a lot of
time to run the vi.

Is there any tip or method improve speed of MathScript?

Beside,  I heard that the MathScript in the labview 8.2 has a significant speed improvements.
So, how much faster, compare the labview 8.0? 
Maybe it's a stupid quesiton, but I really need to know before I try labview 8.2.

Thank you in advance.

Have a good night.
Best regards
aggie
0 Kudos
Message 1 of 20
(12,104 Views)
Hello,

MathScript is implemented on top of LabVIEW.  MathScript is also a typeless language while LabVIEW is strictly-typed.  The combination of these two factors make MathScript run slower than native LabVIEW code.  However, there are some steps to take in order to improve the speed of MathScript.  You mentioned two of them: Moving calculations outside of MathScript and breaking larger scripts into multiple smaller ones.  Another thing to look for is if the MathScript node is inside a for or while loop.  Passing variables into and out of the MathScript node during each iteration of a loop is very expensive.  It is better to use a MathScript global variable to store variables that are only updated in the MathScript node.  But perhaps the best method to improve the speed of the MathScript node is to vectorize your code.  This means taking operations that are performed in a loop and instead performing them on the entire matrix at once.  For example, if you want to implement a clipping algorithm that clips values of a matrix greater than one, the following code will work:
for i=1:length(A)
    if (A(i) > 1)
        A(i) = 1;
    end
end


However, an approach that will run substantially faster is to use matrix indexing to do the work for you.  The same operation can be performed in one line:
A(A > 1) = 1;
Any time you see code that is iterating over elements of a matrix, ask yourself if it's possible to vectorize the code.

The speed of MathScript has been improved in LabVIEW 8.2.  The most notable difference is in the compile speed.  This means the MathScript Window is more responsive and that your wait is shorter when running a VI after changing the MathScript node.  A run-time speed improvement was made for matrix indexing (hence another reason to try to vectorize your code).  These two speed improvements can be quantified as follows:
  • Up to 300% compile time speedup
  • Average of 500% speedup for indexing with vectorized operations
The indexing operations that realize the greatest improvement:
Writing operations such as
A(:, 1:3) = B;        % Replacing consecutive (or nonconsecutive) columns with a matrix
A(1:3, : ) = B;       % Replacing consecutive (or nonconsecutive) rows with a matrix
A([1 3], [2 4]) = B;  % Replacing elements (nonconsecutive rows and columns) with a matrix


Reading operations such as
B = A(1:2:end);       % Reading elements (consecutive or nonconsecutive)

Grant M.
Staff Software Engineer | LabVIEW Math & Signal Processing | National Instruments
Message 2 of 20
(12,068 Views)

I am facing a similar speed issue with MathScript nodes. I have written a client application using two normal while loops - one producer loop reads the TCP port and writes to a queue at about 150Hz, the other consumer loop reads the queue and does some heavy going matrix calculations within one giant mathscript node, ideally within a ms or two.

 

The producer / consumer arrangment give me the buffer I need, because there is no handshaking with the server(!) So my mathscript nodes have to sit within loops and receive data a frame at a time. It might be a pain to do, but would normal wired code in a sub.vi be faster than mathscript?

 

 

0 Kudos
Message 3 of 20
(10,479 Views)

grahamwebb wrote:

... but would normal wired code in a sub.vi be faster than mathscript?


Yes!

 

What kind of operations are you doing? How big are the matrices?

0 Kudos
Message 4 of 20
(10,472 Views)

Good! .. I am using 3x3 matrices for linked-segment modelling - addition, subtraction, transpose, multiplication, nothing over the top, lots of indexing and manipulating vector components. I've currently got 350 lines of Mathscript that I'd like to run at 120Hz. It will take a while to wire! so I wanted to check first what sort of speed increase to expect.   

 

 

0 Kudos
Message 5 of 20
(10,468 Views)
aarrgh!! What is the point of Mathscript if it is too slow to actually write code on?! I now 7 seperate nodes each with 20-40 lines of code, and the damn thing still freezes every time I move something or write something. I'm better off using a text editor and trying to guess my way through the debugging.
0 Kudos
Message 6 of 20
(10,240 Views)

Hello,

 

The MathScript R&D team is continuing to work on improving the edit-time responsiveness of MathScript Nodes. According to the MathScript RT Module roadmap shown at NI Week 2009, we have currently scheduled some further improvements in this area for the 2009 SP1 release, targeted roughly 6-8 months from now. (Although, this is just the expected roadmap and things are subject to change.)

 

We would really like to make sure that we address your issues with these changes. If you are able to send us your application, we could make sure that our optimizations fix the issues that you're seeing. If you can't share your code for some reason, we would still appreciate any further details that you can give us. Are all the MathScript Nodes on the same VI or are they on different VIs? Do any of the MathScript Nodes have particularly complicated expressions or call deeply-nested user-defined functions? What kinds of actions are you performing when LabVIEW hangs? If you wait long enough, does LabVIEW become responsive again or do you have to restart? Any other details that you can give us would help us isolate the cause of the issues you're seeing.

 

With your help, I hope we can make the editing experience acceptable in the next release of LabVIEW.

 

Thanks!

jattas

LabVIEW MathScript R&D

0 Kudos
Message 7 of 20
(10,215 Views)

jattas,

 

If you could provide an NI email address I'd be happy to send you a section of the code and discuss the problems further.

 

The nodes are all on one vi for development purposes, but will be 'subvi'd' later. They are performing simple matrix calculations. LabVIEW doesn't respond for a minute or so but then recovers. The fact the whole program doesn't respond is a pain because it isn't possible to work on other vi's while I wait.

 

 

Graham

0 Kudos
Message 8 of 20
(10,193 Views)

Hello Graham,

 

I sent an email to the address that NI has on file for you. Please let me know if you do not receive it so I can contact you by other means.

 

Thanks,

jattas

0 Kudos
Message 9 of 20
(10,167 Views)

Hi jattas, I haven't received an email. I have added a University email address to my profile that may be worth trying.

 

 

0 Kudos
Message 10 of 20
(10,102 Views)