Sie sind auf Seite 1von 16

Parallelism

• Parallelism is a way to execute multiple tasks at the same time, such as


creating and displaying two sine waves at different frequencies by using
two different loops.

• A challenge in programming parallel tasks is passing data among multiple


loops without creating a data dependency.

• For example, if you pass the data using a wire, the loops are no longer
parallel.
Sharing Data:
• In the multiple sine wave example, if you want to share a single stop button
between the loops.

• The Boolean control is a data input to both loops, therefore the Loop
Control terminal is read only once, before either While Loop begins
executing.

• If False is passed to the loops, the While Loops run indefinitely. Turning
off the switch does not stop the VI because the switch is not read during the
iteration of either loop. (Incorrect Method)
Sharing Data:
• Move the Loop Control terminal inside Loop 1 so that it is read in each
iteration of Loop 1.
• Although Loop 1 terminates properly, Loop 2 can not execute until it
receives all its data inputs.
• Loop 1 does not pass data out of the loop until the loop stops, so Loop 2
must wait for the final value of the Loop Control, available only after Loop
1 finishes.
• Therefore, the loops do not execute in parallel. Also, Loop 2 executes for
only one iteration (Incorrect Method).
Sharing Data: Solution 1
• If you could read the stop button from a file, you would no longer have a
dataflow dependency between the loops, as each loop can independently
access the file.

• However, reading and writing to files can be time consuming, at least in


processor time.
Variables: Solution 2
• Variables enable you to perform normal dataflow by
passing data from one place to another without
connecting the two places with a wire.

• Therefore, you can create block diagrams that have


simultaneous operations.

• To make the two loops run in parallel, remove the


wire, pass data between the subVIs using a technique
called variable.
Variables
• In LabVIEW, variables are block diagram elements that
allow you to access or store data in another location.

• Variables can be of the following types.

– Local: store data in front panel controls and indicator

– Global: Global variable and process shared variables store


data in special repositories than you can access from
multiple VIs

– Functional Global: stores data in while loop shift registers.


Variables
• Using Variables in a Single VI
• Local variables transfer data within a single
VI.
Creating Local Variables
• Right-click an existing front panel object or block diagram terminal and select
Create>>Local Variable from the shortcut menu to create a local variable.

• A local variable icon for the object appears on the block diagram.

• You also can select a local variable from the Functions palette and place it on the
block diagram.

• To associate this local variable with a control or indicator, right-click the local
variable node and select >> Select Item from the shortcut menu. The expanded
shortcut menu lists all the front panel objects that have owned labels.
Reading and Writing to Variables
• After you create a local or global variable, you can read data
from a variable or write data to it.

• By default, a new variable receives (write) data.

• Right-click the variable and select Change To Read from the


shortcut menu to configure the variable to behave as a control.

• To change the variable to receive data from the block diagram


rather than provide data, right-click the variable and select
Change To Write from the shortcut menu.
Global Variable
• You also can use variables to access and pass data among several VIs that
run simultaneously.
• For example, suppose you have two VIs running simultaneously. Each VI
contains a While Loop .
• The first VI contains a Boolean control to terminate both VIs.

• Use a global variable to terminate both loops with a single Boolean control.
Creating Global Variables
• For every global variable, LabVIEW creates a special global VI, which has a front
panel but no block diagram.

• Add controls and indicators to this front panel to define the data types of the global
variables it contains. This front panel is a container, which can be accessed by
several VIs.

1. Select a global variable from the Functions palette and place it on the block
diagram.

2. Double-click the global variable node to display the front panel of the global VI.
Creating Global Variables
3. Place controls and indicators on this front panel the same way you do on
a standard front panel.

4. Save it an name.vi and return to the block diagram of the original VI.

5. Select the object in the global VI that you want to access. Right-click the
global variable node and select a front panel object from the Select Item
shortcut menu. The shortcut menu lists all the front panel objects in the
global VI that have owned labels.
Creating Global Variables
If you want to use this global variable in other VIs.
• Select Select a VI from the Functions palette.

• Select the global VI you saved before and put it in the


diagram panel.

• Right-click the global variable to associate the global


variable with the data from another front panel object.
Initializing Variables
• Verify that the local and global variables contain
known data values before the VI runs. Otherwise, the
variables might contain data that cause the VI to
behave incorrectly.

• If you do not initialize the variable before the VI


reads the variable for the first time, the variable
contains the default value of the associated front
panel object.
Wrong

Das könnte Ihnen auch gefallen