Get Processor Count

April 10, 2009

From time to time you may need to know at runtime how many processors (or more accurately processor cores) are available to your program.  For example,  you might want to determine how many threads to use with multi-threaded FFTW.

Darel Finley succinctly shows how to do this programmatically on Mac OS X.  The same code unchanged compiles on FreeBSD, and I’ve added code for Linux, and wrapped it in CPP conditionals to detect which code to use at compile time.  If compiled on something other than Mac OS X, FreeBSD or Linux, the value returned defaults to 1. So without further ado, here’s nproc.c:
/* Compile with: gcc -o nproc nproc.c */
#include <stdio.h>
#include <sys/param.h>
#include <sys/sysctl.h>

int CPUCount()
{
    int    count;
#if defined(__APPLE__) || defined(__FreeBSD__)
    size_t size = sizeof(int);
    if (sysctlbyname("hw.ncpu",&count,&size,NULL,0)!=0)
        return(1);
    else
        return(count);
#elif defined(__linux__)
    char *fname = "/proc/cpuinfo";
    char  input[256];
    FILE *ptr;
    if ((ptr = fopen(fname, "r")) == NULL) return(1);
    count = 0;
    while (fgets(input, 256, ptr))
        if (strncmp("processor", input, 9) == 0)
            count++;
    fclose(ptr);
    return((count>0)?count:1);
#else
    return(1);
#endif
}

int main(void)
{
    printf("CPU Count: %d\n", CPUCount());
    return(0);
}


Camera + Interwebz = Pwnage

May 12, 2008

Heh heh heh

Stolen Mac helps nab burglary suspects


Remove .mac Icon from iSync (take two)

May 12, 2008

Already out of date. I found a much better way to achieve the same goal, and wrote an application to do it.

See, I’m not a fan of online programming tutorials. You know the kind, how to write “Hello World” in the new AmazingWidgetLanguage. For many they may be just the thing, but for me, learning or improving my knowledge of a programming language goes much better if I set the bar a little higher than “Hello World“. So I used SyncScrubber to learn how to write a full-blown AppleScript application in Xcode, complete with multiple script files, global variables, all that good stuff. Overkill? Perhaps. But then I accomplished what I wanted to do.

All the gory details available here.


Remove .mac Icon from iSync

April 28, 2008

iSync 2.4 on OS X Tiger has that annoying .mac icon which can’t normally be removed. Yet if you click on it, it’s pretty much useless, isn’t it? Some nonsense about “You no longer synchronize information across computers with iSync”, yet the icon remains to taunt and annoy.

But, with a bit of hacking with Apple’s Interface Builder program you can hide the .mac icon.

All the gritty details are available here, and no, it’s not a Photoshop trick. Ok, ok, now I know this issue is a bit old well old by now, but I’m still running Tiger for work and that doesn’t look likely to change any time soon. Some of you might be in a similar situation.

Comments welcome.


Apple Mail Unable to Find Responses

April 25, 2008

There I was, scanning dozens of “you’re awesome” type emails, hammering out cool and minimalist responses like “k” and “l8r”. Looking for a witty piece of micro-literature I had sent earlier, I clicked on this thing in the message status column of the Inbox. Apple Mail told me to drop dead, in a typically humourless way:

Mail was unable to find your response to the message “Cyber Feen Rawks”. You may have deleted the message.

No, dipshit, You may have deleted the message.

But no, it was still in its Sent folder

After searching around for a few minutes I found several sites which hinted at the answer, which was to rebuild the Sent mailbox for each affected account. Hinted in a “Just the facts, Ma’am” and humour-free fashion I might add.

Anyway what worked for me was to click on each Sent folder, select Mailbox -> Rebuild, wait a few seconds, and all was well. You may also need to rebuild your Inbox folders in the same fashion if the problem persists.

But at the end of the day, your Mail will behave and you too will want to use the word “Rawks”. And for the detail-orientated among you, it’s a Macbook Pro (Intel) running OS X 10.4.11 and Mail 2.1.3, retrieving mail from a POP server.


Follow

Get every new post delivered to your Inbox.