The MacView

Virtual Instrumentation from a Mac perspective

Name: Marc
Location: Austin, Texas, United States

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

Tuesday, August 12, 2008

Squeezing Parallelism Out of Your Diagrams with Merge Errors.vi







There are two different ways to run your code on multiple cores in LabVIEW, static and dynamic. We've talked about the dynamic case before, getting a reentrant reference to a VI in a loop and running it. Now I'd like to talk about some simpler methods of squeezing parallelism out of your code.

When I code, I find that I usually, for convenience, chain everything together with error clusters. So I would have one long string of actions, all tied together with an error cluster wire. It's convenient, but cuts down on LabVIEW's ability to run code in parallel. I have come to heavily use the Merge Errors.vi to help me run more code in parallel.

Merge Errors.vi has three error clusters and an array of error clusters as input, and one error cluster as output. So you fork your incoming error wire to each of the sub-VIs that can run in parallel, and then take their output and run them into Merge Errors.vi. If you have more than 3 error clusters to merge, you can also drop a build array node for the remainder.

When ordering is important, make sure the error you want to return to the user is in the first terminal. For instance the code shown below is a "destroy" VI. It must destroy the data, regardless of any errors coming in. This is like the close file primitive. You may have gotten an error while writing to the file, but you still want to close the file. However, we want to return any errors that were passed in to us, even if the destroy resulted in an error.

The bottom line is, use the Merge Errors.vi liberally in your code to handle multiple errors instead of chaining all error clusters and preventing sub-VIs from running in parallel.

Labels: , , ,

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


Friday, August 08, 2008

Simple Preferences VIs

NIWeek was very informative. I got to talk to a lot of customers who use Macs and got a lot of feedback.

I was talking to Travis Pape (pronounced poppy) about his really cool iBike app (LabVIEW built app for Windows and Mac that allows access to iBike computer). We were talking about the difficulties he was having with preference files, and I suggested he simplify the code.

I wrote two VIs that access the OS preference mechanism. They allow you to store key/value pairs for your application. I've posted these VIs at:

http://decibel.ni.com/content/docs/DOC-1832

Another example of using vi.lib/Platform/CFString.llb and CallLibraryFunction nodes. Took about 20 minutes to look up the functions, wire it all up, and then use Polish VIs to add the documentation.

Labels: , , , , , , ,

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


Monday, April 21, 2008

Its The Little Things

First off, talking of little things, my girls are now a year old. For those of you who have twins know, this is about when things start to get easier. The fog lifts and you start to get a life back. I am getting close to completing my work on the next version of LabVIEW. That means I should have more time to post here.

Its the little touches that make the Mac OS a pleasure to use. Since I joined the LabVIEW team back in the LabVIEW 7.0 days, I have tried to add a little special Mac something to the release. Unfortunately, due to several circumstances, I will not be able to do that for the next release of LabVIEW.

Stephen Covey, in his book The 7 Habits of Highly Effective People talks about sharpening the saw. In other words, stop every once in a while and evaluate your efficiency and take some time to become more efficient. We are doing that with the Mac build of LabVIEW, which means the next release will not have the bells and whistles I would like to have in it.

To lament this, I wanted to publicly remind myself of the cool little Mac features I have been able to get into LabVIEW since 7.0. I hope there are also some things in here that you didn't know, and can now be more productive in LabVIEW. (Warning: These features are currently Mac only. Using LabVIEW on Windows may get frustrating after getting used to these features on a Mac.)

Go To LabVIEW Folder

I was fixing a problem with the file dialog box, when I started to get annoyed with trying to find the LabVIEW folder of the LabVIEW I was currently running (I have several versions of LabVIEW scattered all over my hard drive). So I added a button to the Open File dialog (if it treats LLBs as folders) that will allow you to go directly to the currently running LabVIEW's folder.

Horizontal Scrolling

LabVIEW supported the scroll wheel on the mouse on Windows before it was supported on the Mac. One day I got very annoyed that the scroll wheel did nothing in LabVIEW on the Mac, so I added scroll wheel support. Now that only brought us on parity with Windows, so I went a step further and added horizontal scrolling and fast scrolling. When the Mighty Mouse came out, LabVIEW worked in all directions. However, if you do not have a Mighty Mouse, you can scroll horizontally by holding down the shift key and using the scroll wheel. Fast Scrolling support is enabled (horizontal and vertical) by holding down the option key. It will double the scroll speed.

LabVIEW Version on Dock Icon

Note: This appears not to be compatible with Leopard. I don't know that this will be fixed in time for the next release.

If you set "BadgeAppInDock: True" in your LabVIEW preferences file, the version of LabVIEW will show up on the LabVIEW icon in the dock after you launch (you may have to click on it for it to show up). I never got it working quite right (and apparently it doesn't work for Leopard) so we left it in behind a preference setting. The version number on the icon should survive LabVIEW quit, however, it will be cleared after the Dock is restarted.

Flexible Preference File Format

The "style" of preference files for LabVIEW varies between the platforms. For instance, Windows has a "key=value" format and the Mac has a "key: value" format. I made the Mac preferences file format much more flexible, so line endings don't matter (DOS, Mac Classic or UNIX line endings will work), and equals (=) and colon (:) will work. By default, LabVIEW on Mac will write out the "key: value" format, but can read either format.


Command Line Path Strings

This isn't really a Mac specific feature, its more of a Mac necessitated workaround. I added two VIs at /AdvancedString: Command Line String To Path.vi and Path To Command Line String.vi. These VIs look and behave very similar to String to Path and Path to String primitives, and are drop in replacements for them. In fact, on every other platform, they behave exactly the same as their primitive counterparts. On the Mac, however, they convert between Mac classic paths (colon separated) and UNIX paths (slash separated).

The diagram shown here can convert symlinks into the path they represent (the Get File Info primitive does this a lot better). Notice the pink on the Path/String conversion bullets. They are the AdvancedString versions.

Use these VIs whenever you are interacting with the command line. This will make your VIs more portable among platforms.

I know I am forgetting a few other features, but these are the ones that have remained in my memory after my year of no sleep.

Labels: , , ,

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


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.


Tuesday, June 26, 2007

What ZFS support means

There have been a lot of ramblings about ZFS support in Leopard. Many people have wondered what the big deal is, and how it would help the average Mac user. Personally, I was just waiting for Steve Jobs to say the words "boot ZFS" and my life would have been compelte (well, not really, but it would have been pretty cool).

First, a little history (as I remember it, so it may not be completely accurate, but its close enough). The first filesystem (the way files are stored on disk) on the Macintosh was MFS (Multiple File System). MFS was very short lived (I have never actually used it, just heard about it). It did not have hierarchical file system (no folders). HFS (Hierarchical File System) replaced MFS pretty quickly. HFS was written when most people still booted off of 3.5" floppy disks (1.44 MB max) and computer RAM was measured in kB not GB. Then Apple upgraded HFS to HFSPlus. It handled much bigger drives, supported Unicode better and was just overall a better, more modern filesystem. The last little tweak Apple did to HFSPlus was to add journalling support (the filesystem better handled unexpected power outages).

The Sun develops an incredible filesystem, ZFS (Zetabyte File System).


  1. Adding more disk space is easy

    Right now, there are one or more logical disks for every physical disk. Most people are familiar with Macintosh HD which is a logical disk (Macintosh HD on the desktop) and a physical disk (say, a Western Digital inside their Mac). You can partition a physical disk into multiple logical disks. I have done this with a 250 GB external firewire drive. I have a different Mac OS version on each of (now 5) partitions, or logical disks.

    ZFS takes that trend in the reverse direction. You can have multiple physical disks "pooled" together into one logical disk. Imagine the following: you are running out of disk space, so you buy a new, much bigger hard drive. You install it and format it, and now you have two options: (1) migrate everything to the new drive, or (2) do some UNIX command lines to hobble the new disk into the filesystem on the old disk (sym-links, moving the home directory, etc). Neither of these is very clean, and both make things feel like the system is fragile.

    If your main hard drive had been formatted ZFS, you could do the following instead: Tell the OS to add the new drive to the "pool" of drive space available. That's it. The logical disk, Macintosh HD on your desktop, would now have the full storage capacity of both physical disks in your system. You can add as many drives as you can connect to your machine.

  2. Speed benefits of RAID, but in a simpler package


    The idea of "pooling" physical disks together for the logical disk has another benefit that is similar to the speed concepts in RAID. When you write data, you split the data between multiple disks. Then when you read it, both disks work as fast as they can to get their piece of the data requested. This is similar in concept to multiple CPUs (cores) in Macs today. If you can have two or more things sharing the load, you can make it faster.

  3. Failing disks can be detected sooner


    The problem with using multiple disks, is that your risk of disk failure goes up dramatically. Instead of a 5% that one disk will fail, you have a 10% chance that one of the two disks will fail (ok, I don't remember my statistics, or at least didn't want to think too hard about it, but you get the idea). Some RAID schemes solve this problem by storing just enough shared data on the disks, that if one drive goes down, it can continue to give you correct data (although slower) until you replace the drive.

    ZFS has a RAID mode, but you have to have all the disks be the same size. Standard ZFS has a cool feature though. Every block of data that is written, has a checksum written with it. Every time a block is read, it checks to make sure that checksum is correct. As soon as it finds a bad block, you can tell it to remove the bad disk from the "pool" (which will copy the data onto the remaining disks) and then replace the bad disk. You get very early detection of a failing disk, and should lose less data.

  4. Compression is built into the filesystem


    Back in the dark days Back when I used a PC and DOS, there was a cool program called Stacker (later Microsoft had a very similar feature). It allows you to reformat a disk and use compression to get more disk space. ZFS brings this back.

    With ZFS, you can turn compression on or off at any moment. While it is on, any data written to disk will be compressed. When off, data is written in raw, uncompressed format. When reading, it will read whatever format was written, compressed or uncompressed.

    You may thing that adding compression would slow down the filesystem, but it actually speeds it up. Processor and memory speeds have been growing at a much faster pace than disk speeds. So the little bit of time it takes to compress/uncompress the data is nothing compared to the time it takes the disk to read or write the data.

  5. Entire "Disk" revision history available


    ZFS has a feature called snapshots. This allows you to create a special "directory" (really a file) that is a snapshot of the filesystem at that moment in time. Think of it as a live, whole system Time Machine, without the external disk.

    This snapshot "directory" takes next to no time to create (there's nothing to copy), and takes up very minimal space. Basically, whenever you modify a file, the old one is kept in the snapshot. Any files that have not been changed since the snapshot are shared between the two. Its kind of like a whole filesystem diff.

    Imagine working on a project, and you get to a cross-road. A decision on direction is needed. You choose what you think is the best direction, but want to get back to the point your at in your project just in case. The code may not be to a point that you can put it in source code control, so you right click on the folder and select Create an Archive and make a zip file of the "snapshot". If it is a large project, you go to lunch and come back just as it finishes archiving it. You continue on and then realize that it is not the direction you really wanted to go. You unzip your snapshot zip file (again, going to lunch), and only then realize that you missed some files in another folder.

    With ZFS, just take regular snapshots of the entire filesystem. They are quick, small and capture the state of the entire filesystem.

    NOTE: Snapshots are not a replacement for backups. If your system gets fried, you lose your snapshots and your data. Backups, like with Time Machine, are extremely valuable and everyone should have a backup strategy.



So what is Apple's plan with ZFS? Nobody but Apple (maybe even Steve Jobs) really knows, but here is what we do know. Leopard has had some limited support for ZFS. They are stated that Leopard has "read only" support for ZFS (at least the beta the just handed out at WWDC has read only ZFS). MacRumors has posted that Apple is giving developers a beta of read-write ZFS as a separate download.

What I am looking forward to is when Apple replaces Journaled HFSPlus with ZFS as the default filesystem. That means they still need a non-beta read-write filesystem that you can boot Mac OS X off of (booting of ZFS is a fairly new feature on any OS).

So I hope the clamor for ZFS grows and Apple listens. I love HFSPlus, but I have a feeling I would love ZFS a whole lot more.

Labels: , , , , , , ,

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


Friday, June 15, 2007

The Importance of Documentation

Sorry for the long delay in posting. Between my three-year-old twin boys, three-month-old twin girls and working hard on getting the next version of LabVIEW for the Mac in great shape, I've been a bit busy.

I've noticed recently what a difference it makes having polished VIs. By polished, I mean well documented, consistent connector panes and a general overall consistency. With all the dialogs to get at the various information and set documentation on controls/indicators and VIs it can be a bit time consuming and difficult to make sure everything is consistent, but it is worth it.

In 8.2.1, you not longer use Command-H to show/hide the contextual help window (which is the system Hide LabVIEW command), you use the Help key (or if you have a MacBook, Command-Shift-H). The contextual help window is your guide to understanding code on the block diagram.

So the places to document your VI are:

1. File -> VI Properties -> Documentation

Set the VI description to some helpful text on what the VI does and how to use it. Also create an HTML file for further documentation. You can have just one HTML file and use anchors <a name="test"> for each VI. The Help tag is the name of the anchor. (NOTE: as of 8.2.1, you must manually escape any spaces in the anchors/Help tags, for instance <a name="test me"> would have a help tag of "test%20me").

While you are in VI Properties, visit the following pages also:

- Protection: make sure the password protection/locking is how you want it.
- Execution: make sure Allow debugging and Enable automatic error handling are turned off (if desired).
- Window Appearance: Make sure the window title is what you want it to be

2. Right-click (control-click if you don't have a mighty mouse) on each control/indicator that is in the connector pane (and possibly others) and select Properties -> Documentation and make sure the description is meaningful, as well as a short description in the tip strip. Also Right-click on each control/indicator in the connector pane and make sure that its Required/Recommended/Optional is set correctly. Also make sure that all controls and indicators that you meant to have on the connector pane are there.

You probably want to visit similar controls/indicators on each VI, instead of visiting each control/indicator on a single VI. For instance, visit all the "error in (no error)" controls on all your VIs, to make sure they are named the same and have the same (or consistent) descriptions, then go on to "error out" and any other common data types. The idea is to make sure you are consistent.

3. Right-click on the following elements of your Project window and select Properties:

- Project Node (My Project.lvproj)
- Library Nodes (MyLib.lvlib)
- Class Nodes (MyClass.lvclass)
etc.

Go to the Documentation page (in the project case, its the Project Description) and fill in the information in a similar way.

Doing this has three benefits:

1. The next person who needs to modify the project (or you in 6 months) will have plenty of documentation on how it works and how to use it.

2. If you product is meant to be used by other LabVIEW developers (a library or class meant for other to use) will have an easier time learning how to use your code correctly.

3. While you document you code, you find things you missed. It forces you to think through how things actually work, and helps you find problems or missing elements.


To simplify the documentation of VIs (not other project items like classes and libraries), I have written a tool that walks through all the VIs in a project and allows you to see in one place all of the VI information and allows you to update it in one place.

As you can see by clicking on the image to the right, you can easily navigate through all VIs in a project, and all terminals in a VI. You can also connect/disconnect terminals, rename terminals, add documentation to terminals and VIs, set the window title, set debugging and auto error handling and see how it all fits together in one screen.

If you are interested in trying out this tool, you can email me at Marc dot Page at NI dot com, and I will reply with a zip file containing this tool.

Labels: , , , , , , ,

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


Friday, January 19, 2007

The Revolution WILL be televised

Apple Computer announced $1 billion profit on over $7 billion in revenue. IDC announced Apple's computer market share went up almost 30% year-on-year (now 4.7%). With all the buzz of the iPhone and iPod line, and Apple dropping the "Computer" from its name, some of us Mac fans are left to wonder what this all means.

The iPod went from zero to hero in no time, and they continue to grow. The holiday quarter 2006 saw 50% more iPods being sold than the same quarter 2005. With AppleTV, iPhone and iPod Apple gets a lot of buzz, but its the Macintosh that is most interesting here. I think there is a quiet revolution happening.

A popular book here at National Instruments is Crossing the Chasm. In this book, the author discusses a Technology Adoption Model (image at link shown here) that is very helpful in understanding future directions in technology.

The theory goes like this:

  • Innovators are always on the bleeding edge in accepting new technology
  • Early Adopters quickly follow the Innovators
  • If the Early Majority accepts the new technology, then Late Majority will shortly follow
  • There are always Laggards that are the last to accept the new technology


The Macintosh never really got past the Early Adopters phase. That is the "chasm" referred to in the book. The iPod showed that Apple could cross that chasm. With one win under their belt, I think the iPhone and AppleTV are the next tests of their new model. But what about the Macintosh.

Another technology adoption model is the Alpha Geek model. This model states that when a majority of Alpha Geeks adopt a technology, the rest public at large will eventually follow. I think the IBM PC is a good example of this.

In the mid 80's home computers became popular. You had the Atari, Commodore, TRS, IBM, Amiga and Apple camps. The Alpha Geeks we split among them all. So when people asked their Alpha Geek friend which computer to buy, the advice depended on who your Alpha Geek was. Then as the PC became more popular among Alpha Geeks, their advice started converging on the PC, thus a shift in the Alpha Geek preference multiplied the effect on the general population.

Ten years ago, if you were to poll Alpha Geeks you would have found that 90% to 95% of them used Microsoft Windows. Anecdotal evidence over the last 10 years leads me to believe that another shift in Alpha Geek preference is happening, towards the Macintosh.

I started to notice a change on Slashdot about the time Mac OS X was released. Slashdot ten years ago seemed to be more Linux oriented. Since then more and more posts were Apple related. Not only that, the posts became increasingly pro Apple.

When I started working for National Instruments in 1997, there were just a handful of people who I worked with who had a Mac at home, and they were very, what we called at the time, Apple Evangelistas. Within the last few years, I have noticed that the developers I work with are starting to buy Macs for home use. Even more surprising is that the developers who are buying Macs are not Mac zealots. They are people who have been working on Windows versions of LabVIEW or DAQ, and really don't have much exposure to the Mac other than casual exposure. In fact, just this morning, another developer who I never thought would buy a Mac came up to me and said, "I have a MacBook Pro in my cart at the Apple store, and before I submitted the order, I just wanted to ask you about one of the features."

The third anecdote comes from my personal history. I was an IBM PC guy in high school (DOS days). I had even (ignorantly) talked a friend into being anti-Mac (his family was trying to decide between Mac and PC at the time). Then I started working for the Navy. They had me working on a Macintosh, and I grew to love it. However, this made me a geek black sheep of sorts in my family. My father tried to convince me that a Sparc station (from Sun) would be a better alternative to Microsoft.

When I went off to college, my parents bought me a Compaq PC (sometimes I think it was for fear that I might buy a Mac at college). I used the computer for about a year and a half. It was a useful tool, and it was free (for me). Then I got married. I was frustrated that my new wife had such a difficult time using our home computer. We decided it was time to sell the PC and get a Mac. Since then, I've purchased 6 Macs, and got my mother-in-law 2 Macs.

I was still a geek black sheep in my family. My two brothers were Windows/Linux fans. None of the male members of my immediate family had anything good to say about the Macintosh. Well, at least until the Mac switched to Intel. I was floored when my father and two brothers, and different times, expressed that they would consider buying a Mac.

I think that in the next ten years, we are going to see a personal computer revolution, and it WILL be televised (at least web-cast).

Labels: , , , , ,

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