简体   繁体   中英

Single-threaded and multi-threaded code taking the same time

Ive been using pthreads but have realized that my code is taking the same amount of time independently if i use 1 thread or if i separate the task into 1/N for N threads. To exemplify i reduced my code to this example:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

#include <boost/progress.hpp>

#define SIZEEXEC 200000000

using namespace boost;
using std::cout;
using std::endl;

typedef struct t_d{
   int  intArg;
} Thread_data;

void* function(void *threadarg)
{
    Thread_data *my_data= (Thread_data *) threadarg; 
    int size= my_data->intArg;

    int i=0;
    unsigned rand_state = 0;
    for(i=0; i<size; i++) rand_r(&rand_state);
    return 0;
}

void withOutThreads(void)
{
    Thread_data* t1= new Thread_data();
    t1->intArg= SIZEEXEC/3;
    function((void *) t1);

    Thread_data* t2= new Thread_data();
    t2->intArg= SIZEEXEC/3;
    function((void *) t2);

    Thread_data* t3= new Thread_data();
    t3->intArg= SIZEEXEC/3;
    function((void *) t3);    
}

void withThreads(void)
{
    pthread_t* h1 = new pthread_t;
    pthread_t* h2 = new pthread_t;
    pthread_t* h3 = new pthread_t;
    pthread_attr_t* atr = new pthread_attr_t;

    pthread_attr_init(atr);    
    pthread_attr_setscope(atr,PTHREAD_SCOPE_SYSTEM);

    Thread_data* t1= new Thread_data();
    t1->intArg= SIZEEXEC/3;
    pthread_create(h1,atr,function,(void *) t1);

    Thread_data* t2= new Thread_data();
    t2->intArg= SIZEEXEC/3;
    pthread_create(h2,atr,function,(void *) t2);

    Thread_data* t3= new Thread_data();
    t3->intArg= SIZEEXEC/3;
    pthread_create(h3,atr,function,(void *) t3);

    pthread_join(*h1,0);
    pthread_join(*h2,0);
    pthread_join(*h3,0);
    pthread_attr_destroy(atr);
    delete h1;
    delete h2;
    delete h3;
    delete atr;
}

int main(int argc, char *argv[])
{
    bool multThread= bool(atoi(argv[1]));

    if(!multThread){
        cout << "NO THREADS" << endl;
        progress_timer timer;
        withOutThreads();
    }
    else {
        cout << "WITH THREADS" << endl;
        progress_timer timer;
        withThreads();
    }

    return 0;
}

Either the code is wrong or there is something on my system not allowing for parallel processing. I'm running on Ubuntu 11.10 x86_64-linux-gnu, gcc 4.6, Intel® Xeon(R) CPU E5620 @ 2.40GHz × 4

Thanks for any advice!

EDIT: Given the answers i have realized that (1) progress_timer timer did not allow me to measure differences in "real" time and (2) that the task i am giving in "function" does not seem to be enough for my machine to give different times with 1 or 3 threads (which is odd, i get around 10 seconds in both cases...). I have tried to allocate memory and make it heavier and yes, i see a difference. Although my other code is more complex, there is a good chance it still runs +- the same time with 1 or 3 threads. Thanks!

This is expected. You are measuring CPU time, not wall time.

time ./test 1
WITH THREADS
2.55 s


real    0m1.387s
user    0m2.556s
sys     0m0.008s

Real time is less than user time, which is identical to your measured time. Real time is what your wall clock shows, user and sys are CPU time spent in user and kernel mode by all CPUs combined .

time ./test 0
NO THREADS
2.56 s


real    0m2.578s
user    0m2.560s
sys     0m0.008s

Your measured time, real time and user time are all virtually the same.

The culprit seems to be progress_timer or rather understanding of it.

Try replacing main() with this. This tells the program doesn't take time as reported by progress_timer , maybe it reports total system time?

#include <sys/time.h>
void PrintTime() {
    struct timeval tv;

    if(!gettimeofday(&tv,NULL))
        cout << "Sec=" << tv.tv_sec << " usec=" << tv.tv_usec << endl ;

}

int main(int argc, char *argv[])
{
    bool multThread= bool(atoi(argv[1]));

    PrintTime();
    if(!multThread){
        cout << "NO THREADS" << endl;
        progress_timer timer;
        withOutThreads();
    }
    else {
        cout << "WITH THREADS" << endl;
        progress_timer timer;
        withThreads();
    }
    PrintTime();

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM