我一直在使用pthreads,但已经意识到如果我使用1个线程或者如果我将任务分成N / 1的N / N,我的代码将独立地花费相同的时间。举例来说,我将代码缩减到了这个例子:
#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;
}
代码错误或我的系统上有些东西不允许并行处理。我正在运行Ubuntu 11.10 x86_64-linux-gnu,gcc 4.6,英特尔®至强(R)CPU E5620 @ 2.40GHz×4
感谢您的任何建议!
编辑: 鉴于答案我已经意识到(1)progress_timer计时器不允许我测量“实际”时间的差异和(2)我在“功能”中给出的任务似乎不足以让我的机器给出不同的1或3个线程的时间(这是奇数,在两种情况下我都会得到10秒左右......)。我试图分配内存并使其更重,是的,我看到了不同。虽然我的其他代码更复杂,但它仍然很有可能运行+ - 同时使用1或3个线程。谢谢!
答案 0 :(得分:2)
这是预期的。您正在测量CPU时间,而不是测量时间。
time ./test 1
WITH THREADS
2.55 s
real 0m1.387s
user 0m2.556s
sys 0m0.008s
实时小于用户时间,与您的测量时间相同。实时是您的挂钟显示,用户和系统是在用户和内核模式下花费的CPU时间 由所有CPU组合。
time ./test 0
NO THREADS
2.56 s
real 0m2.578s
user 0m2.560s
sys 0m0.008s
您的测量时间,实时和用户时间几乎相同。
答案 1 :(得分:1)
罪魁祸首似乎是 progress_timer 或者更理解它。
尝试用此替换main()。这告诉程序没有按照 progress_timer 报告的时间,可能会报告总系统时间?
#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;
}