CUDA: Timer

To measure the performance of CUDA code it is better to use events provided by the CUDA Runtime API rather than using CPU-based timers like clock or high-resolution timers.

As a simple illustration, consider that we want to measure the time taken for the execution of a kernel named fooKernel. First, we create CUDA events to handle the begin and end times of the kernel execution:

cudaEvent_t beginEvent;
cudaEvent_t endEvent;

cudaEventCreate( &beginEvent );
cudaEventCreate( &endEvent );

We then record the begin and end times of the kernel execution:

cudaEventRecord( beginEvent, 0 );
fooKernel<<< x, y >>>( z, w );
cudaEventRecord( endEvent, 0 );

Finally, we wait for the completion of the recording of the end event in the CUDA stream. After that, we compute the time elapsed between the recorded begin and end times to obtain the kernel execution time:

cudaEventSynchronize( endEvent );

float timeValue;
cudaEventElapsedTime( &timeValue, beginEvent, endEvent );

cout << "Time: " << timeValue << endl;

The begin and end events can be reused any number of times in the program. When we do not need them anymore, they should be destroyed:

cudaEventDestroy( beginEvent );
cudaEventDestroy( endEvent );

A simple CudaTimer class that implements this usage can be seen here.

Tried with: CUDA 3.2

LEd and MikTeX 2.9

The LEd (LaTeX Editor) was last released in 2009 and supported MikTeX 2.8 back then. To use it with MikTeX 2.9, open ConfigurationOptions and change these settings:

  • Set TeX executables option to C:\Program Files\MiKTeX 2.9\miktex\bin (32-bit Windows) or C:\Program Files (x86)\MiKTeX 2.9\miktex\bin (64-bit Windows).
  • Leave the TeX distribution option at MikTeX 2.8

Tried with: LEd 0.53

MikTeX: Installers

MikTeX can be installed in many ways, only one of which I have found to be easy in the long term.

There are 2 kinds of MikTeX installers:

  • Basic Installer: Installs the basic files. Whenever any TeX file you try to compile needs an unavailable package, it will need to be installed from the Internet by using the MikTeX Package Manager.
  • Net Installer: This single installer is used to achieve two separate tasks:
    1. To download the files required for a complete MikTeX system.
    2. To install either a basic or full MikTeX system using the setup files downloaded in the above step.

I used to use the Basic Installer, but have come to loathe it! :-( Installing packages when required is a pain since that needs Administrator privileges. Also, the MikTeX package website is sometimes offline or slow and you will be unable to install the oh-so-urgently-needed package you want.

Since, both Internet speed and disk space is cheap these days, I recommend running the Net Installer to download the entire MikTeX archive from a nearby mirror. Run the Net Installer a second time to install a complete MikTeX system from your downloaded MikTeX archive.

Tried with: MikTeX 2.9

Visual Studio: Call Stack Text

It is useful to be able to grab the text of the call stack displayed in the Call Stack window. This is required for recording the call stack on certain errors or searching on Google or for posting it to forums.

To grab the text of the call stack, right-click in the Call Stack window and choose Select All. This will select all the calls on the stack. Right-click and choose Copy to copy their text. You can paste this call stack text anywhere you want.

Alternatively, just use the keyboard shortcuts: Ctrl-A (for Select All) and Ctrl-C (for Copy).

Tried with: Visual Studio 2010

Thrust: Remove Duplicates in Multiple Vectors

With the magical thrust::zip_iterator duplicates in multiple vectors can be easily removed and the vectors can be trimmed in Thrust.

Consider two vectors, one of key values and the other holding their values. There can be many values associated with each key. The keys are sorted and the values associated with each key are also sorted. Finding duplicates in these vectors boils down to finding duplicate pairs and removing them. Here is how to achieve this easily using thrust::unique and thrust::zip_iterator:

typedef thrust::device_vector< int >                IntVector;
typedef IntVector::iterator                         IntIterator;
typedef thrust::tuple< IntIterator, IntIterator >   IntIteratorTuple;
typedef thrust::zip_iterator< IntIteratorTuple >    ZipIterator;

IntVector keyVector;
IntVector valVector;

// Remove duplicate pairs
ZipIterator newEnd = thrust::unique( thrust::make_zip_iterator( thrust::make_tuple( keyVector.begin(), valVector.begin() ) ),
                                     thrust::make_zip_iterator( thrust::make_tuple( keyVector.end(), valVector.end() ) ) );

IntIteratorTuple endTuple = newEnd.get_iterator_tuple();

// Trim the vectors
keyVector.erase( thrust::get<0>( endTuple ), keyVector.end() );
valVector.erase( thrust::get<1>( endTuple ), valVector.end() );

Tried with: Thrust 1.3 and CUDA 3.2

Firefox: Double-Click to Only Highlight Word

Problem

If you double-click anywhere on a word of text in any application on Windows, it will highlight that word and any following whitespace characters upto the beginning of the next word. Supposedly, Mac behaves the same way too.

The reason for this behaviour seems to be to aid in copy-paste of a word inside a document. It also helps if one wants to delete a word, since the following whitespace is also nicely removed.

However, I find this behaviour quite anachronistic and irritating today. Especially when I am editing text on Firefox and want to apply formatting on the highlighted word either in rich text mode or HTML mode.

Solution

Thankfully, there is an obscure feature hidden in Firefox to make it highlight only the word on a double-click and ignore the following whitespace. Open about:config and set layout.word_select.eat_space_to_next_word to false.

Reference

Tried with: Firefox 4

Vim: Delete All Lines of a File

Problem

To delete all the lines of a file that is open in Vim.

Solution

  1. Type gg to move the cursor to the first line of the file, if it is not already there.
  2. Type dG to delete all the lines.

Deleting all the lines of a file is not a frequently used operation. So, I need a command that is easy to recall, even though it is not used much. I find dG easy to remember since I frequently use dd to delete a line, and also gg and G, which move the cursor to the first and last line of the file respectively.

Tried with: Vim 7.3

Foobar2000: Pause on Lock

I sometimes listen to music using headphones while working at the computer. A longstanding dream of mine has been to see the music playback being intelligent to my presence. I wish the music would pause whenever I removed the headphones or left the computer. Also, the music would play from where it had paused when I return and put the headphones back on. This requires some sensory capability in the headphones or in the computer, so it is not possible right now without extra equipment like a headphone sensor or the Kinect.

It is however possible to pause the music whenever I lock the computer and play from where it had paused when I return and unlock the computer! Not my dream exactly, but I will take what I can get! :-)

I listen to music using Foobar2000 and this can be achieved in it by using the Pause on Lock component. Download it from here and install it. To enable the feature in Foobar2000, choose PlaybackPause on Lock.

Tried with: Pause on Lock 0.5 and Foobar2000 1.0