LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Help with improving a hologram generation vi

Hello All,
 
I'm quite new to this Labview programming having slowly been teaching myself over the past few months. I've just started a physics PhD and am developing a few programs to generate phase-only holograms (kinoforms) to run on an optics system I have constructed. The physics works fine and the results from the program do what i want them to.
 
However, the problem comes when I wish to improve my program. Ideally I would like to run it real time which intially posed me a few problems which I 'fixed' and it does now run in "real time". Unfortunately some of the programs run unexpectedly slow (some of them I expect to as they use 2D Fourier Transforms). The particular program in question does not use any fourier transforms and is thus the simplest one I use and hence I would expect it to run rather quickly but it doesn't.
 
I'm not sure why this is, it could be because of the large arrays I am using, at times 768 x 768 x N, and their use in For Loops. Or it could be something really obvious and stupid that a naive programmer such as myself has done wrong.
 
If anyone could take a look at my program and suggest any improvements, mostly to do with speed, I would be very grateful. Or indeed any criticism at all would be great. Below is a small list of instructions on how to run the program.
 
I've attached all the VIs as a Library "Kinoform Generation.llb" (this is also a first for me so here's hoping it works!)
 
Step 1. Load and Run "Gratings And Lenses.vi"
Step 2. Choose the size of Hologram you would like to generate on the dialog popup (256 works reasonbly fast but that really only gets used for testing the program and I ned to have ones 768 pixels in size)
Step 3. Choose where you would like the resulting hologram image window to popup on the dialog popup - I have it pop up on a secondary microdisplay in my laboratory - but you can also display it on screen by chosing appropriate pixel coordinates
Step 4. Push Start to start the VI and then click any of the number controls on the front panel to alter the desired pattern.
 
Please don't hesitate to ask me anything if I've made things unclear.
 
I hope someone can help, any advice and criticism would be greatly appreciated - I'm wanting to learn.
 
Thanks in advance
 
Daniel Burnham
 
P.S. If I get this one sorted out I'll no doubt see if someone can help me improve my more complicated ones that use Fourier Transforms in iterative loops! They really did cause me a headache!
0 Kudos
Message 1 of 6
(3,227 Views)
Dan,
 
I have great news for you.  You have 1 big but very easy "bug" to fix issue.  You are experienceing something that we all get bite by once, and then you never use them again.
 
Global Variables.
 
Globals variables are by far the slowest and worst way to pass data around in an application.  There are very few times to ever use them.  Each time you read them copies of data are made and there are also sorts of processing etc that is involved.
 
So in your applicaiton with settings of 768,0,0 the global variable was called 28323849 times.  That constituted 21.8 seconds of my execution time.
 
Whats worse is that the values never change in this application.  Take a look at my modified GLA.vi  All I did was move the globals out of the for loops.  There is no need to do the same processing over and over.
 
With that change the global was called 13 times, and cost me less then microseconds.  The GLA.vi went from taking me 55437.5 milliseconds to taking 5890.6 milliseconds.
 
I realize you just sent a subset of your code so there may be something I am missing.  If you can actually remove the globals completely and wire the data into the subVI you will most likely see even faster executions.  In addition if you are doing things in loops see if it is really necassary to be in the loop.  For example in your inner loop you take 2 PI / (Laser wavelength * .4).  The result of that math should allways be the same so why calculate it millions of times?
 
Let me know if you have more questions.
0 Kudos
Message 2 of 6
(3,201 Views)

Evan,

Thank you so much! Sounds like this will sort out allsorts of speed issues. A little problem though - The vi you attached won't run as I only have labview 7.1.1  😞 Is it to possible to save it to run on 7.1.1?

Thanks again

Daniel Burnham

0 Kudos
Message 3 of 6
(3,197 Views)

D'oh 

Here you go...

0 Kudos
Message 4 of 6
(3,194 Views)
Evan,
 
Just wanted to say thanks again. I implemented the things you recommended and now my program is running about 25 times faster! Which is awesome!
 
Thanks
 
Dan Burnham
0 Kudos
Message 5 of 6
(3,179 Views)
One small thing to point out is that pressing "Stop" causes the algorithm to run one last time because of the way you have your event structure configured. You should make that a separate event.

Also, Evan's response in moving the global variable outside of the loop is an excellent suggestion. However, with all due respect to Evan's comment, because of your experience with this VI please don't fall into the "global variables are evil" camp. I am not suggesting nor do I believe that Evan is in that camp, but for a relatively new LabVIEW programmer you may take some comments as gospel. Like any other programming construct they have their use and misuse. Learning how to program is as much learning when and how something should be used as much as it is learning the syntax of a language. And for everybody else, please, I don't want to start a flame war over this. I'm just trying to provide a cautionary point of view.

0 Kudos
Message 6 of 6
(3,163 Views)