Go to Post CD, you are crazy!!! - Tottanka [more]
Home
Go Back   Chief Delphi > Technical > Programming > NI LabVIEW
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 23-03-2011, 22:18
siggy2xc siggy2xc is offline
Registered User
AKA: Tyler Siegrist
FRC #1756 (Argos)
Team Role: Programmer
 
Join Date: Mar 2010
Rookie Year: 2009
Location: peoria
Posts: 70
siggy2xc is an unknown quantity at this point
More local variables.

I would like to organize code I have written into multiple vi's but doing so would require me to make input and output local variables for each sub vi. I remember reading somewhere that you should avoid local and global variables because they will slow down your code. Is the slowdown at all significant or is there a better way to do this?
Reply With Quote
  #2   Spotlight this post!  
Unread 24-03-2011, 01:20
ratdude747's Avatar
ratdude747 ratdude747 is offline
Official Scorekeeper
AKA: Larry Bolan
no team
 
Join Date: Feb 2009
Rookie Year: 2008
Location: Madison, IN
Posts: 1,064
ratdude747 has a reputation beyond reputeratdude747 has a reputation beyond reputeratdude747 has a reputation beyond reputeratdude747 has a reputation beyond reputeratdude747 has a reputation beyond reputeratdude747 has a reputation beyond reputeratdude747 has a reputation beyond reputeratdude747 has a reputation beyond reputeratdude747 has a reputation beyond reputeratdude747 has a reputation beyond reputeratdude747 has a reputation beyond repute
Re: More local variables.

did you try clustering the variables and then making the cluster global? that may simplify things, especially if a lot of variables go between the same .vi's.
__________________
Dean's List Semi-finalist 2010
1747 Harrison Boiler Robotics 2008-2010, 2783 Engineers of Tomorrow 2011, Event Volunteer 2012-current

DISCLAIMER: Any opinions/comments posted are solely my personal opinion and does not reflect the views/opinions of FIRST, IndianaFIRST, or any other organization.
Reply With Quote
  #3   Spotlight this post!  
Unread 24-03-2011, 07:58
Greg McKaskle Greg McKaskle is offline
Registered User
FRC #2468 (Team NI & Appreciate)
 
Join Date: Apr 2008
Rookie Year: 2008
Location: Austin, TX
Posts: 4,751
Greg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond repute
Re: More local variables.

How do the subVIs run? If one calls another calls another, like much of WPILib, then the typical approach is to pass the data in and out using a parameter. This is what happens if you select some code and use Edit>>Create SubVI from Selection.

If the VIs are going to loop forever, or for a long time, similar to how Periodic tasks or Vision does, then you need another communication mechanism. Globals are the one I'd start with. Be careful to write to a given global in only one loop and read it in the others, or you will start to see race conditions.

Greg McKaskle
Reply With Quote
  #4   Spotlight this post!  
Unread 25-03-2011, 15:39
linkhyrule5 linkhyrule5 is offline
Registered User
FRC #1072
 
Join Date: Oct 2009
Location: California
Posts: 12
linkhyrule5 is an unknown quantity at this point
Re: More local variables.

Actually, I would warn against using Globals in LabVIEW. LabVIEW will actually avoid race conditions by automatically pausing the program to allow everything that needs to write to it to do so before anything can read. This can cause massive slowdowns, as well as otherwise inexplicable pauses in the code.

If you really need to communicate across VIs, make a VI whose only purpose is to serve and receive a value, as follows:

Code:
Indicator - Control
Reply With Quote
  #5   Spotlight this post!  
Unread 26-03-2011, 09:18
Greg McKaskle Greg McKaskle is offline
Registered User
FRC #2468 (Team NI & Appreciate)
 
Join Date: Apr 2008
Rookie Year: 2008
Location: Austin, TX
Posts: 4,751
Greg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond repute
Re: More local variables.

Quote:
Originally Posted by linkhyrule5 View Post
Actually, I would warn against using Globals in LabVIEW. LabVIEW will actually avoid race conditions by automatically pausing the program to allow everything that needs to write to it to do so before anything can read. This can cause massive slowdowns, as well as otherwise inexplicable pauses in the code.

If you really need to communicate across VIs, make a VI whose only purpose is to serve and receive a value, as follows:

Code:
Indicator - Control
Perhaps I can clarify a bit. Data updates in LV are always atomic. They are protected so that competing writes will not corrupt the result, but they do not avoid race conditions -- using the definition that NI teaches in its classes.

As an example, lets say that some loops are playing a game. Each loop is assigned a color from the rainbow, and they update a global with the color whenever they like. They don't coordinate their updates, but since the writes are atomic, the color in the global will always be a legal color -- "red", "orange", "yellow", ..., "violet". You will never see odd combinations such as "blulet" or "violow". To do this, LV must make the writes atomic using a lock or mutex. It adds some overhead to the value update, but it is certainly not a massive slowdown.

To show that, write the program to play the game on your laptop. Seven loops running full speed in parallel, each updating a variable with their color and the loop iteration they are on. I wrote mine to each update 1000 times. No race conditions or corruptions ... yet.

Change the game so that the loops append their values, and you will start to see race conditions as shown in the second png. If you look carefully at the resulting data stream, you'll notice that yellow skips number 309. Or rather, that the orange loop and yellow loop had a race. They both read the buffer, both updated, their local copy of the buffer, and then both wrote their string to the variable. In doing so, the yellow buffer that contained 309 was overwritten with the one from orange that was old. There is evidence of several other races in that portion of the buffer. You also get to see how the OS scheduler chooses to swap out threads. This laptop is a Core 2 Duo and you will see lots of interleaving, allowing more races.

To get rid of the race, you get rid of the read-modify-write access patterns that can interleave. In this case, the easiest way to do this is to build a subVI that accumulates the buffer. Each loop will pass in only the value to add "color #", and since subVI are also atomic, no values are lost -- ever. Again, this is a bit more overhead, but not massive. Making the subVI is a bit more complex than just a control wired to an indicator, but if you look in tutorials or examples, we refer to them as either "functional globals", or sometimes as LV 2 style. The name refers to when this was this was the only way to get global data. LV used to be ever more functional and didn't have global or local variables.

Back to the topic, it is really the access pattern that causes race conditions, and they aren't necessarily bad. Just be aware when you are allowing them to happen. If you have only one writer to a global or other storage within your program there are no races.

Greg McKaskle
Attached Thumbnails
Click image for larger version

Name:	Screen shot 2011-03-26 at 7.33.25 AM.png
Views:	19
Size:	25.2 KB
ID:	10485  Click image for larger version

Name:	Screen shot 2011-03-26 at 7.40.38 AM.png
Views:	16
Size:	53.2 KB
ID:	10486  
Reply With Quote
  #6   Spotlight this post!  
Unread 26-03-2011, 15:10
siggy2xc siggy2xc is offline
Registered User
AKA: Tyler Siegrist
FRC #1756 (Argos)
Team Role: Programmer
 
Join Date: Mar 2010
Rookie Year: 2009
Location: peoria
Posts: 70
siggy2xc is an unknown quantity at this point
Re: More local variables.

in my program i have locals being written and read from through out a sequence structure, but never the same local within the same frame. Would this ever cause race conditions?
Reply With Quote
  #7   Spotlight this post!  
Unread 26-03-2011, 19:47
Vikesrock's Avatar
Vikesrock Vikesrock is offline
Team 2175 Founder
AKA: Kevin O'Connor
no team
Team Role: Engineer
 
Join Date: Mar 2006
Rookie Year: 2007
Location: Manchester, NH
Posts: 3,305
Vikesrock has a reputation beyond reputeVikesrock has a reputation beyond reputeVikesrock has a reputation beyond reputeVikesrock has a reputation beyond reputeVikesrock has a reputation beyond reputeVikesrock has a reputation beyond reputeVikesrock has a reputation beyond reputeVikesrock has a reputation beyond reputeVikesrock has a reputation beyond reputeVikesrock has a reputation beyond reputeVikesrock has a reputation beyond repute
Send a message via AIM to Vikesrock Send a message via MSN to Vikesrock Send a message via Yahoo to Vikesrock
Re: More local variables.

Quote:
Originally Posted by siggy2xc View Post
in my program i have locals being written and read from through out a sequence structure, but never the same local within the same frame. Would this ever cause race conditions?
Stupid question, but why not just pass the value(s) through with a wire?

If you only accessing the variable(s) from inside the sequence structure and never inside the same frame then you should not have a problem with race conditions.
__________________


2007 Wisconsin Regional Highest Rookie Seed & Regional Finalists (Thanks 930 & 2039)
2008 MN Regional Semifinalists (Thanks 2472 & 1756)
2009 Northstar Regional Semifinalists (Thanks 171 & 525)
Reply With Quote
  #8   Spotlight this post!  
Unread 27-03-2011, 01:20
siggy2xc siggy2xc is offline
Registered User
AKA: Tyler Siegrist
FRC #1756 (Argos)
Team Role: Programmer
 
Join Date: Mar 2010
Rookie Year: 2009
Location: peoria
Posts: 70
siggy2xc is an unknown quantity at this point
Re: More local variables.

My program records data to a file, then write that file to a local so that there is no slowdown or lag from reading. There are multiple cases and sequence structures depending on what button you press (probably about 7 or 8 different ones) so wires would make the code very messy and the whole point I've gone into this is to organize my 7 or 8 different options into separate VIs.
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 09:28.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi