The MacView

Virtual Instrumentation from a Mac perspective

Thursday, August 16, 2007

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.

The views expressed on this website/weblog are mine alone and do not necessarily reflect the views of my employer.