Wednesday, February 8, 2012

POSIX threading API on Windows

When I was writing the Multicore book, I was struck by the commonality of the Windows [1][2] and POSIX threading APIs. The concepts were common, but the names (and parameters) of the function calls were different. It looked like a relatively trivial task to write a shim to translate between the two. However, I had a book to write, so it languished until one evening last week when I actually got around to doing it.

The result is these two files winpthread.h and winpthread.cpp. I don't claim that these are a high fidelity port, there's plenty missing, but they should be sufficient to get a code working. In fact, there's very little complexity in the code, the only caveat is that condition variables were not introduced in Windows until Vista, and these require the use of critical sections rather than mutexes. The difference between mutexes and critical sections is that critical sections are local to a single processes whereas mutexes can be shared between processes.

Let me know if there's bugs, any improvements, or if the code is useful.

Friday, February 3, 2012

Using prtpicl to get cache sizes

If you are on a SPARC system you can get cache size information using the command fpversion, which is provided with Studio:

$ fpversion
 A SPARC-based CPU is available.
 Kernel says main memory's clock rate is 1012.0 MHz.

 Sun-4 floating-point controller version 0 found.
 An UltraSPARC chip is available.

 Use "-xtarget=sparc64vii -xcache=64/64/2:5120/256/10" code-generation option.

The cache parameters are output exactly as you would want to pass them into the compiler - for each cache it describes the size in KB, the line size in bytes, and the associativity.

fpversion doesn't exist on x86 systems. The next best thing is to use prtpicl to output system configuration information, and inspect that output for cache size. Here's the cache output for the same SPARC system using prtpicl.

$ prtpicl -v |grep cache
              :l1-icache-size    0x10000
              :l1-icache-line-size       0x40
              :l1-icache-associativity   0x2
              :l1-dcache-size    0x10000
              :l1-dcache-line-size       0x40
              :l1-dcache-associativity   0x2
              :l2-cache-size     0x500000
              :l2-cache-line-size        0x100
              :l2-cache-associativity    0xa