![](/img/trans.png)
[英]OpenMP: Share single-threaded and multi-threaded implementations of the same algorithm
[英]Single-threaded and multi-threaded code taking the same time
我一直在使用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個線程。 謝謝!
這是預料之中的。 您正在測量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
您的測量時間,實時和用戶時間幾乎相同。
罪魁禍首似乎是進步或者理解它。
嘗試用此替換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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.