Paralle programming baby steps

Member
Posts: 321
Joined: 2004.10
Post: #1
I’ve been trying to delve into parallel programming for a long time. I think I understand the basics but I never seem to make the leap into actual code. Working from a bottom up approach I thought of the most trivial, simplistic and agonizingly useless example possible. Oh, and let’s pretend I’ve got a Mac Pro so we’ve got plenty of cores.

int x = 1;
int y = 1;
int z = 1;

while( x < 1000000 )
{
x++;
}
while( y < 1000000 )
{
y++;
}
while( z < 1000000 )
{
z++;
}

Since there are no interdependencies between the three loops, using parallel programming techniques, shouldn’t I get a near 2/3rds reduction in running time?

What are people’s experiences an advice or recommendations?

I’d like to stay with C/C++ and neither is an inherently concurrent language so I guess this will require the use of an API or library. I’ve glanced at POSIX Threads and gotten scared. I’ve used SDL for basic input before and see SDL_threads. So I guess my question is, does anybody have experience using SDL_threads and would this be trivial to code. Oh heck, anybody want to post actual working code so I could use it as a spring board?

Thanks in advance.
Quote this message in a reply
Moderator
Posts: 3,572
Joined: 2003.06
Post: #2
Well, the only way to get parallel processing happening is to use more than one processor at the same time, and the only way that will ever happen is to use multiple threads of execution on the same task (or some other sort of vector processing). I'd say off-hand, if you can't "get" POSIX threads, then forget it, because that's about as easy as it gets IMHO.
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #3
I've never had issues with SDL threads, thought I only use them for streaming music. They are ever so slightly simpler than POSIX threads in that they are cross platform and have fewer synchronization primitives to learn.

All thread libraries work the same way at a basic level. You create a new thread and give it a function to run. Wrapping access to shared resources is also pretty easy, though also easy to do wrong. Like solving memory leaks or memory corruption issues, synchronization issues can be very hard to find.

The other hard part about threading is making sure you aren't making performance worse. Synchronization can be very expensive, and adding multithreading where it's not needed can make a program slower and needlessly more complicated.

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #4
If you can't figure out how to use pthread_create and pthread_join from the man pages then multithreading is *way* too hard for you. Starting threads is not difficult; avoiding deadlocks and race conditions is difficult.
Quote this message in a reply
Member
Posts: 321
Joined: 2004.10
Post: #5
I wrote my first stupid parallel program using SDL_threads. It was rather thrilling to see the CPU meter spike to 100% for both cores.

I was kinda peeking at POSIX pthreads and when I compare my SDL invocation

Code:
SDL_Thread *id;
ThreadData *td = new ThreadData;
td->name = "****";
td->i = y;
td->max = 9999999999;

id = SDL_CreateThread( letsCount, td );

with pthreads, but there seems to be a whole lot of other stuff going on that SDL can't handle.

Code:
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg);


Should I stay with SDL_threads for now or make the jump to pthreads now? I'm concerned that SDL_threads is a toy that will just teach me a lot of bad habits that I'll have to break in the future.

thanks for any advice,
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #6
I don't think there is anything wrong with SDL threads. You are correct that there are less features, but that's also what makes them simpler. If you aren't doing anything terribly complicated, the basic functionality of SDL will be fine and a little easier to learn/use.

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Very First Steps . . . freeradical426 10 4,123 Jun 13, 2005 01:23 PM
Last Post: MacFiend