Error Macro Win

Still porting Linux code to Mac, I’ve been trying to keep a useful habit: using the #warning and #error macros. This new code is riddled with #ifdef’s checking whether or not we’re trying to build on a Mac, and using alternatives to the Linux-only system calls, but in parsing these large chunks of code, sometimes I forget what I’m doing. How horrible would it be to accidentally leave empty the Mac-only code block when it’s meant to actually do something.

So, whenever I open up one such block, I add a little macro:

#ifndef MAC
// ... The Linux-only code
// ... takes up
// ... a lot
// ... of space
#else
#error "Don't forget to implement it as Mac!"
#endif

At least I’ll always catch it at compile time, and when I fix/add the Mac-only code, then I can go ahead and remove that macro. It’s not that I’m forgetful, but I’ve shot myself in the foot so much at this point.

Tagged with:
 

I’ve been around the block a time or two (or more) with subversion, but until recently I had limited experience with git. Sure, every now and then I’ve used it to check out projects, but not for my personal use.

No longer. And as of right now, I don’t have any intention of using anything but git for personal development.

Last week, I held lecture for parallel programming and I talked about using subversion for versioning, and I began to suspect that something was horribly wrong. Questions started springing up – where does the repository live? Am I calling svnadmin on my own machine? Where do I check out the repository? Though there are answers to these questions, for many things, such a model just doesn’t make sense.

If I’m working on a project that I’m not syncing between several computers, but I just want to have different stable versions and to try different crazy ideas using branches. It mitigates the cost of reverting drastic changes to code.

Use Case: You’re showing someone your code, and want to show off a neat feature you’ve made, or a problem you’re encountering, something about the project, invariably you’ve run into a problem where it doesn’t compile at the moment. You type furiously, trying to find and undo the most recent changes, but to no avail – there is no hope of getting it to compile in the 5 minutes you have someone’s attention. Enter version control. Revert to the last working copy and victory is yours.

In fact, just today I was asked if I could pull up some code I had been working on to show to a professor. Unfortunately at the moment it wasn’t compiling but was able to switch versions in 20 seconds and show off some very recent work from earlier in the day, thus saving face.

Use Case: You’ve got some crazy idea for an implementation you’d like to try out, but are worried about reverting back all the massive changes you’ll have to make in the code. Worry not! Create a new branch and feel free to change your code in every way you can think of and not lose other branches under development.

Use Case: You’ve got multiple versions of code each implementing the same basic algorithm but with different mechanisms, techniques, etc. and have to turn it in as part of a project. Just archive the whole directory (in a tarball or zip) and the user who unpacks it has access to every version your code has been in. Each branch, each stage of development. And very light-weight to boot.

I can’t speak for others, but I will be using git for the foreseeable future as it’s incredibly easy to use and alleviates many of the problems I encounter regularly with development.

Tagged with:
 

OpenGLot – Scalar Fields

I’m working on OpenGLot (my OpenGL plotting library) for a class project, and two of the features I decided to implement were contour lines and scalar fields in 2D.

A few days ago I got decent-looking contour lines working and today I got scalar fields implemented with a fragment shader. The programmer using the library can specify a function in the form of a string and have it plotted as a scalar field. Quickly. Really quickly.

Modern graphics cards support shaders, which are programs that get run on the graphics card, and in parallel. This is great for algorithms that can be run in isolation (one pixel doesn’t need to know what the others are doing), which is the case here. OpenGLot generates a fragment shader that colors a single pixel based on the value of the function. Each of the dozens or hundreds of cores on a GPU runs the same code in parallel for their particular pixel.

Contouring for a sinusoidal function with the isovalue 0.

Contouring for a sinusoidal function with the isovalue 0.

Here's a first look at the results (taken moments after this first worked for me).  It's the same sinusoidal function, and I will be improving upon the color mapping in the coming hours.

Here's a first look at the results (taken moments after this first worked for me). It's the same sinusoidal function, and I will be improving upon the color mapping in the coming hours.

The graphing program I use, Grapher.app is rapidly showing its age. The results it gives for the same function (though using a much better coloring scheme) are either grainy or extremely slow (10 or more seconds). OpenGLot is generating these in less than 0.1 seconds. (Grapher.app implements its scalar fields on the CPU, so comparing times is a little like comparing apples and oranges. Still, responsiveness in this type of matter is important.)

The quick (and grainy) plot in Grapher.app

The quick (and grainy) plot in Grapher.app

The slow (but smooth) plot in Grapher.app

The slow (but smooth) plot in Grapher.app

Tagged with: