Aristos Queue Posted March 30, 2007 Report Share Posted March 30, 2007 Almost every time I see a someone write an LV2-style global (you know, an uninitialized shift register on a loop that is guaranteed to run exactly once, used to store data for retrieval on a later call), it is written with a While Loop, with a constant wired to the Stop terminal. I've always written mine with a For Loop with a constant 1 wired to the N terminal. It seemed more intuitive, and for some reason I felt LV might optimize it better, since normally the contents of a While Loop cannot be optimized based on number of times it'll execute, but a For Loop always can. So I asked Jeff K (father of LV, deep knowledge of diagram optimization). He says that we can indeed constant-fold a While Loop with a constant wired to the Stop terminal. It's the only time we can constant fold the contents of a while loop, but we can do it. He says that there's no advantage to using one over the other. It answered a nagging question in my mind. I thought others might be interested as well. Quote Link to comment
Michael Aivaliotis Posted March 30, 2007 Report Share Posted March 30, 2007 I honestly never thought of using a for loop. I always used a while loop since the beginning of time. What's even more curious is that of all the LabVIEW programmers I've interacted with, not even once has anyone brought this point up. I'm not trying to suggest that your implementation is invalid or any less worthy. I'm just shocked that this is the first time I've come across this. Learn something new everyday, I guess. Quote Link to comment
Jim Kring Posted March 30, 2007 Report Share Posted March 30, 2007 I prefer not to use loops, at all -- just use a unowned (floating) feedback node. http://forums.lavag.org/index.php?act=attach&type=post&id=5331 http://forums.lavag.org/index.php?act=attach&type=post&id=5332 Quote Link to comment
Tomi Maila Posted March 30, 2007 Report Share Posted March 30, 2007 QUOTE(Aristos Queue @ Mar 29 2007, 05:30 AM) So I asked Jeff K (father of LV, deep knowledge of diagram optimization). He says that we can indeed constant-fold a While Loop with a constant wired to the Stop terminal. It's the only time we can constant fold the contents of a while loop, but we can do it. He says that there's no advantage to using one over the other. I tested this particular issue a month back while I was working on a piece of code that would use in everywhere. The result was that while loop indeed is faster than for loop for this particular task. I didn't figure out why, after all I don't have deep knowledge of diagram optimization like Jeff K does. Tomi Quote Link to comment
LAVA 1.0 Content Posted March 30, 2007 Report Share Posted March 30, 2007 I usually use while loops... Question to AQ: I have a Functional Global "Array of Cluster" always 50 Elements. Why is the "write" faster than the "read"? Quote Link to comment
Götz Becker Posted March 30, 2007 Report Share Posted March 30, 2007 The feedback node concept is nice, but I wonder why I see a warning sign. I always have warnings on by default. Strange thing in this case is, that the error/warning dialog shows a warning for the VI but no details about it given. http://forums.lavag.org/index.php?act=attach&type=post&id=5333 Quote Link to comment
Jon Sweeney Posted March 30, 2007 Report Share Posted March 30, 2007 QUOTE(Jim Kring @ Mar 29 2007, 12:56 AM) I prefer not to use loops, at all -- just use a unowned (floating) feedback node. James, Interesting use of the feedback loop. This use doesn't seem to be covered in the LabVIEW documentation at all. Could you elaborate slightly as to what is going on? Thank you. Jon Quote Link to comment
Grampa_of_Oliva_n_Eden Posted March 30, 2007 Report Share Posted March 30, 2007 I do not remeber which verion of LV it was but I benchmarked the performance of the For vs While thinking that I could pick up some speed with the For loop. To my suprise the While loop won. Another point (that I was planning to cover in a future Nugget) is that AE do not have to limit themselves to a single iteration. Example: In order to "shoe-horn" an app into a FP 2000 I needed to be very careful with my memory usage. By using an AE that could call itself, all of the analysis could be performed with the data residing in the SR. It sorta works like a "state machine" with multiple entry states. SInce the actual proccessing required depended on the data, I could not tell before the AE was called, how many steps would execute. So... Using a while loop allowed me to adapt without replacing the while with a for. Just my thoughts, Ben Quote Link to comment
jpdrolet Posted March 30, 2007 Report Share Posted March 30, 2007 QUOTE(Jim Kring @ Mar 29 2007, 01:56 AM) I prefer not to use loops, at all -- just use a unowned (floating) feedback node.http://forums.lavag.org/index.php?act=attach&type=post&id=5331 http://forums.lavag.org/index.php?act=attach&type=post&id=5332 Hmmm... Just looking at the scrollbars there is a hint that the diagram is larger than what is shown... Anything hidden in the 5th dimension, Jim? Quote Link to comment
Grampa_of_Oliva_n_Eden Posted March 30, 2007 Report Share Posted March 30, 2007 QUOTE(martin@aerodynamics @ Mar 29 2007, 03:47 AM) Why is the "write" faster than the "read"? These Q are generally best answered by looking at the code and examining the buffer re-use. Are both the Control an Indicator on the root of the diagram? Ben Quote Link to comment
Aristos Queue Posted March 30, 2007 Author Report Share Posted March 30, 2007 QUOTE(Tomi Maila @ Mar 29 2007, 03:28 AM) I tested this particular issue a month back while I was working on a piece of code that would use in everywhere. The result was that while loop indeed is faster than for loop for this particular task. Do you have the VIs that you used for the benchmarking? These would be useful to investigate. Quote Link to comment
Jim Kring Posted March 30, 2007 Report Share Posted March 30, 2007 QUOTE(jpdrolet @ Mar 29 2007, 05:37 AM) Hmmm... Just looking at the scrollbars there is a hint that the diagram is larger than what is shown...Anything hidden in the 5th dimension, Jim? Quote Link to comment
Mike Ashe Posted March 30, 2007 Report Share Posted March 30, 2007 I did one with a FOR loop once, quite a while back. The application was a sort of Tag list, which was rather large, but the individual reads and writes were allowed to be sparse arrays. The actual read or write variable was a cluster of two arrays, one for indexes and one for the data. This allowed for non-contiguous get-set of data chunks. The FOR made this a little simpler, although it could have been written easily enough with a While. Quote Link to comment
Tomi Maila Posted March 30, 2007 Report Share Posted March 30, 2007 QUOTE(Aristos Queue @ Mar 29 2007, 04:36 PM) Do you have the VIs that you used for the benchmarking? These would be useful to investigate. I didn't find the old tests but I quickly wrote new ones. They are attached. Feedback node was fastest, while loop second and for loop last. Tomi Quote Link to comment
orko Posted March 30, 2007 Report Share Posted March 30, 2007 QUOTE(Tomi Maila @ Mar 29 2007, 08:38 AM) Feedback node was fastest, while loop second and for loop last. Now *that* is interesting... Seeming that this is the first time I've heard of the feedback node method for LV2 globals, the question I have is are there any drawbacks or perhaps other advantages to using it other than speed? And what of the warning dialog that beckerg noticed? The feedback node is something that was covered way back in the Basics course, but I honestly never really found a practical use for it (yet). This seems like it might be useful, but I want to make sure that there isn't something that I should watch out for. Thanks! Quote Link to comment
Aristos Queue Posted March 30, 2007 Author Report Share Posted March 30, 2007 Well, my little question -- and this groups' response -- has churned up some concern within R&D. I'm passing the feedback back. As far as code inspection goes, there should be no performance difference between the two loops. Regarding the free-range feedback node, opinions are divided about whether there should be any difference there or not ... Quote Link to comment
eaolson Posted March 30, 2007 Report Share Posted March 30, 2007 QUOTE(Tomi Maila @ Mar 29 2007, 10:38 AM) I didn't find the old tests but I quickly wrote new ones. They are attached. Feedback node was fastest, while loop second and for loop last. The feedback method has four buffer allocations inside the outermost for loop. The for method has five and the while method has six. Could that have something to do with it? I'm not sure how constant folding will affect those or exactly what happens when a loop element (i or N) is left unwired. Quote Link to comment
Grampa_of_Oliva_n_Eden Posted March 30, 2007 Report Share Posted March 30, 2007 QUOTE(Tomi Maila @ Mar 29 2007, 10:38 AM) I didn't find the old tests but I quickly wrote new ones. They are attached. Feedback node was fastest, while loop second and for loop last.Tomi Tomi, I am not sure (yet) if I trust these tests in the context of an action engine. Ben Quote Link to comment
Tomi Maila Posted March 30, 2007 Report Share Posted March 30, 2007 QUOTE(Ben @ Mar 29 2007, 08:24 PM) I am not sure (yet) if I trust these tests in the context of an action engine. That's very wise of you as I made a mistake. The feedback node of mine doesn't work without the outer loop. To work it would need to have a loop. Jim, how do you create this feedback node of yours? It seems to be inside a hidden while loop. And it seems the hidden while loop is set up so that it goes on forever. At least for me. Did you really create this with LV 8.20 or did you happen to use something non-public? Tomi EDIT: The updated feedback node test is attached. It's still fastest but not by so clear marigin. Quote Link to comment
eaolson Posted March 30, 2007 Report Share Posted March 30, 2007 QUOTE(Tomi Maila @ Mar 29 2007, 12:42 PM) EDIT: The updated feedback node test is attached. It's still fastest but not by so clear marigin. There seems to be a constant folding bug with your updated VI. The output is always 0. I think this is a known issue. I don't know if this will affect the speed of operation or not. You also don't have the same First Call? or case structure as in the other examples. When I add them in, it seems to be quite a bit slower than the other two methods. (The difference between the While and the For methods is only about 1% for me.) Quote Link to comment
Tomi Maila Posted March 30, 2007 Report Share Posted March 30, 2007 QUOTE(eaolson @ Mar 29 2007, 09:34 PM) You also don't have the same First Call? or case structure as in the other examples. When I add them in, it seems to be quite a bit slower than the other two methods. (The difference between the While and the For methods is only about 1% for me.) First call is not needed as feedback node can be initialized in the edge of the loop. Quote Link to comment
Jim Kring Posted March 30, 2007 Report Share Posted March 30, 2007 QUOTE(Tomi Maila @ Mar 29 2007, 10:42 AM) Jim, how do you create this feedback node of yours? It seems to be inside a hidden while loop. And it seems the hidden while loop is set up so that it goes on forever. At least for me. Did you really create this with LV 8.20 or did you happen to use something non-public? I am under NDA not to say anything until this Sunday. Quote Link to comment
eaolson Posted March 30, 2007 Report Share Posted March 30, 2007 QUOTE(Tomi Maila @ Mar 29 2007, 01:41 PM) First call is not needed as feedback node can be initialized in the edge of the loop. So can a shift register. The initialization happens on each call, which makes an initialized feedback node not so useful for an LV2 global. I was just trying to point out that comparing a feedback-method global without First Call? and a while-method global with it is like comparing apples to oranges. Execution times, 10^8 iterations: (YMMV) feedback-method global with initialization only: 1272 ms feedback-method global with initialization and First Call?: 4693 ms for-method global: 4600 ms while-method global: 4200 ms Quote Link to comment
Yair Posted March 30, 2007 Report Share Posted March 30, 2007 Stephen, I tried it before a couple of times, but I went back to the while loop. The main reasons - It's clearer to most developers (including me). Since a for loop's number of iterations can be determined by any number of things wired into the loop, I often wouldn't "see" a small constant wired into the N terminal. While loops have a single termination point and the T constant is commonplace. As Ben suggested, sometimes you would want an unknown number of iterations. Quote Link to comment
Tomi Maila Posted March 30, 2007 Report Share Posted March 30, 2007 QUOTE(eaolson @ Mar 29 2007, 10:10 PM) So can a shift register. The initialization happens on each call, which makes an initialized feedback node not so useful for an LV2 global. I was just trying to point out that comparing a feedback-method global without First Call? and a while-method global with it is like comparing apples to oranges. You must be right. I've not been using feedback nodes so I misunderstood the meaning of initialization node, I guess. Tomi Quote Link to comment
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.