I'm a newbie to parallel programming. I'm trying to use pthreads to parallelize a program I'm coding. However it seems that the parallel version of my program takes the same amount of time as serial version even though i force the threads to run on different cores. I created a simple test program based on examples found on the web to see if I could create a parallel program but alas the simple program suffers from the same problem. The parallel and serial versions takes the same amount of time. Please find attached the simple example program i created and hardward info. Can anyone make any suggestions?
while (i <n * 200000) {
i++;
res += sqrt(i);
}
return res;
}
void *thread_func(void *param){
int *cpu_proc;
cpu_proc = (int *) param;
cpu_set_t cpuset;
if((*cpu_proc) == 0){
CPU_SET(0,&cpuset);
//bind process to processor 0
if (pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset) <0) {
perror("pthread_setaffinity_np");
}
//waste some time so the work is visible with "top"
printf("result: %f\n", waste_time(2000));
}
else{
CPU_SET(2,&cpuset);
//process switches to processor 1 now
if (pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset) <0) {
perror("pthread_setaffinity_np");
}
//waste some more time to see the processor switch
printf("result: %f\n", waste_time(2000));
}
}
int main(int argc, char *argv[]){
clock_t s_time, e_time;
double total_time;
int proc_0 = 0;
int proc_1 = 1;
pthread_t threads[2];
I think it is because clock() isn't doing what you want. (See the man page).
If I change clock() to time(0), then the above program runs in approx 3 seconds on my machine,
but if I put the first pthread_join() between the two pthread_create() calls to simulate serial execution,
it runs in approx 6 seconds.
well, threads aren't neccessarily any better.
2 cores won't make much diff.
there's all sorts of clever shuffling going on in cache memory between cores,
mutexes, context switches, thread switching,
your program doesn't do anything. If it did your bottleneck would be in
using limited resources, memory buss, disks, disk controllers, network cards.
I've done experiments with file encryption on my dual core AMD.
2 threads were about the same as none (i.e no pthreads), above 2 threads it slowed down.
still it's good fun.