Showing posts with label arrays labview. Show all posts
Showing posts with label arrays labview. Show all posts

Wednesday, August 18, 2010

Taking advantage of mutliple CPUs

There has been a lot of talk recently about how difficult it is in C/C++ to write good code that efficiently takes advantage of multiple cores/CPUs. That is one of the inherent benefits of LabVIEW, it will automatically split things out onto multiple cores/CPUs if it can figure out that it can.

In the NI-Week keynote, they demonstrated a block diagram with four for-loops executing simultaneously. That is one way to do it. It is simple and gets the job done. However, sometimes you need something that is a little more flexible. To do this, you can use Queues and Open VI by Reference.



This block diagram shows a VI I wrote that generalizes and simplifies the task of running a VI multiple times simultaneously on different sets of data. It takes a path to the VI to run several copies of, two queue references, the names of the queue controls on the VI and the number of copies to start running.



Here is an example of the worker VI. All it takes is two queues, the Data In Queue and the Data Out Queue. It takes a unit of work from the Data In Queue, works on it and puts the results in the Data Out Queue. You may recognize the work being done as the for loop work done in the NI-Week Keynote.

There are, of course, some caveats to great performance and working as expected.

1. Each unit of work must be independent. It may not rely on any other unit of work.

2. There must be no references to uninitialized shift registers, global VIs or references to things that are not protected against being called from multiple threads.

3. The worker VI (and as many sub VIs as possible) must be Reeantrant (File -> VI Properties -> Execution).

4. The Units of work must be fairly hefty. You must have significantly more work done in the VI than the overhead of passing things around in queues.

Using the VI in the keynote on my Mac Pro (4 core), I got 3.6x faster than single core. When I replaced it with the VIs shown above doing the work, I got to 4.9x.

Also of note: on the Mac, you can determine the number of cores in the machine by running the VI at vi.lib/Platform/Miscellaneous.llb/MPProcessors.vi.

Programing LabView - Arrays of Clusters

From the last example, place an array container next to the cluster control and then drag the entire cluster into the array container:

Expand the array so you can see multiple elements by left click / dragging the bottom of the array container:


Type in some information. Note that you can provide different information for each cluster element of the array.


Here is an example in code of how you would use many of the pieces learned so far in these tutorials to actually do something with an array of clusters:

This example autoindexes an element of the array of clusters for each iteration of the for loop. Each element coming out of the autoindexing tunnel is a cluster that is then unbundled by name. In this example the string type only is sent to an outgoing autoindexing tunnel which creates a string array. The indicator at the right shows an array of strings. You could process the data in each cluster element any way you want with code in the for loop. This is one of many programming patterns that you will see in LabView.
Related Posts Plugin for WordPress, Blogger...

Popular Projects

My Blog List

Give support

Give support
Encourage Me through Comments & Followers

Followers