michalenko
michalenko

Reputation: 37

Multi-thread program in C - takes longer to run than single thread

In my program I'm calculating the min value in an array. I do this with no threads and with threads. However, I noticed that as I increase the number of threads the program takes longer to run (I'm using timeval to calculate this). This doesn't make much sense to me and this is also my first time trying to program with threads. Any ideas on what is causing this?

#include <stdlib.h>
#include<stdio.h>
#include<pthread.h>
#include<sys/time.h>

struct timeval start_time, stop_time;

#define N_items 100;

int num_threads = 1;

int start[100];
int x[4000];
int min[100];
pthread_t tid[100];

void *thread(void *arg)
{
    int i, s;
    double my_min;

    s = *(int *) arg;

    my_min = x[s];
    for(i=s+num_threads; i<4000; i+= num_threads){
        if(x[i]<my_min)
            my_min = x[i];
    }

    min[s] = my_min;
    return NULL;

}




int main(){
    int i;
    int my_min;
    int elapsed;

    //Create array of size 4000 with random numbers
    for (i=0; i<4000; i++){
        x[i] = random()%100+1;  
    }


    gettimeofday(&start_time, NULL);

    //Create threads
    for(i = 0; i<num_threads; i++){
        start[i] = i;
        if ( pthread_create(&tid[i], NULL, thread, (void *)&start[i]) ) {
            printf("Can't create thread\n");
        }

    }

    for(i = 0; i<num_threads; i++){
        pthread_join(tid[i], NULL);
    }


    my_min = min[0];
    for(i = 0; i<num_threads; i++) {
        if(min[i]<my_min)
            my_min = min[i];
    }

    gettimeofday(&stop_time, NULL);

    printf("min is: %d\n", my_min);

    elapsed = (stop_time.tv_sec*1000000+ stop_time.tv_usec) -
              (start_time.tv_sec*1000000+ start_time.tv_usec);

    printf("Elapsed time is: %d\n", elapsed );

}

Upvotes: 0

Views: 126

Answers (2)

Zan Lynx
Zan Lynx

Reputation: 54373

Another thing you are doing wrong is that i += num_threads. Using CPU cache effectively is very important and what you are doing there is wasting it.

You want to use solid sequential access blocks instead of jumping all over the place. Maybe try (untested):

int count = 4000;
int chunk = count / num_threads;
int start = s * chunk;
int end = start + chunk;
for(int i = start; i < end; ++i)

Upvotes: 1

David Schwartz
David Schwartz

Reputation: 182885

There's no point in creating and destroying a thread just to do 4,000 quick operations. You're measuring overhead, not work.

Upvotes: 2

Related Questions