Saturday, January 12, 2008

More mac and linux -- parallelization

My company just bought a bunch of 8-core workstations -- it is high time to work on programs that take advantage of a modern computing environment.


I had written a renderer that used pthread and semaphores to partition and then manage the job, so I thought I'd see if I could do the same thing on my mac.  Unfortunately, I had used unnamed semaphores (sem_init(...)) on my Linux boxes, and the program failed (all threads appeared to be finished immediately) on the Leopard box.


Turns out that when you examine the error return, sem_init() has an entry point and prototype in the library and header file respectively, but it's actually "Not Implemented".  So, I switched over to named semaphores, and it all works perfectly.

In particular, on my 8-core box, I was getting some 6x speedup.


// start the threads
for(thread_id = 0; thread_id < n_proc; thread_id++) {
char sem_name[64];
printf("Starting thread %d\n", thread_id);
sprintf(sem_name, "sem_%d\n", thread_id);
if((semaphore[thread_id] = sem_open(sem_name, O_CREAT, 0777, 0)) == SEM_FAILED) {
printf("sem_open failed\n");
perror("sem_open error: ");
return -1;
}
ret = pthread_create(&thread[thread_id], NULL, (void *)median_scanlines, (void *)thread_id);
}

// wait for all threads to finish -- when thread is done, it calls sem_post(), then sem_wait below will unblock
for(thread_id = 0; thread_id < n_proc; thread_id++) {
printf("Waiting for thread %d to finish\n", thread_id);
sem_wait(semaphore[thread_id]);
}


4 Comments:

Blogger wrosecrans said...

It's an interesting post, but the (pre) tag for the code makes it so the lines don't wrap, and they run under the navigation stuff on the right. Makes it a little annoying to read.

I look forward to finding this useful, just as soon as I find the cash for a new Mac. ;)

Oh, and you offered "gdiff" to the word in the previous post, but you don't mention what it actually does, or why we would email you to ask for it...

11:58 PM  
Blogger Unknown said...

Have you looked at using OpenMP?

12:26 PM  
Blogger Zita said...

Very interesting topic you have here.. Great code.. we will implement this and try out.. Thanks for all the efforts you have made for crafting this great article.. Ecommerce website developers

12:41 AM  
Blogger Unknown said...

Great post! I am actually getting ready to go across, this post is very informative. By the way, Get mothers day poems
mothers day poems from daughter
funny mothers day quotes

2:09 AM  

Post a Comment

<< Home