我正在求解2D離散域(LX,LY)上的4個等式。這4個eqns需要解決1000個時間步驟。每個公式將需要(i,j)位置處的參數a,b和c。 我在main()中動態地創建了a_m,b_m和c_m,我將它們的地址傳遞給每個線程。多個pthreads被多次調用;比串行代碼慢
我創建了4個函數eq1,eq2,eq3,eq4。
我爲每個公式創建LX * LY線程數。 每個線程ID將重新表示域LX,LY中的唯一(i,j)。 因此,每個線程只能在向量a_m,b_m,c_m中的特定數據位置(i,j)上工作。 然後,我在main()中依次調用4個eqn。 這4個方程應該按照bcoz的順序求解,參數a,b和c在每個方程(i,j)上被更新。
我的程序運行速度比串行計算慢。你能否提出一些優化技巧來加快速度。
我想創建4個線程,每個方程一個,並解決每個線程的LX * LY時間不會提供太多的加速。
# define N LX*LY
struct thread_data
{
int thread_id, t;
double *a, *b, *c ;
};
struct thread_data thread_data_array[N];
//Function declaration
void *eqn1(void *threadarg)
{
//implementation of eqn1
pthread_exit(NULL);
return 0;
}
void *eqn2(void *threadarg)
{
//implementation of eqn2
pthread_exit(NULL);
return 0;
}
void *eqn3(void *threadarg)
{
//implementation of eqn3
pthread_barrier_wait (&barrier);
pthread_exit(NULL);
return 0;
}
void *eqn4(void *threadarg)
{
//implementation of eqn4
pthread_exit(NULL);
return 0;
}
//所有的主要
void * main(void *)
{
pthread_t threadid[N];
//dynamically create a_m, b_m and c_m of size N each
for(time=0; time<10000; time++)
{
for(i=0; i<N; i++)
{
//thread_data_array[i] is initialized
// *a,*b,*c in thread_data_array[i] will save address of a_m,b_m,c_m created dynamically in main()
pthread_create(threadid[i], eq1, (void *) &thread_data_array[i]);
}
for(i=0; i<N; i++)
{
//thread_data_array[i] is initialized
// *a,*b,*c in thread_data_array[i] will save address of a_m,b_m,c_m created dynamically in main()
pthread_create(threadid[i], eq2, (void *) &thread_data_array[i]);
}
for(i=0; i<N; i++)
{
//thread_data_array[i] is initialized
// *a,*b,*c in thread_data_array[i] will save address of a_m,b_m,c_m created dynamically in main()
pthread_create(threadid[i], eq3, (void *) &thread_data_array[i]);
}
for(i=0; i<N; i++)
{
//thread_data_array[i] is initialized
// *a,*b,*c in thread_data_array[i] will save address of a_m,b_m,c_m created dynamically in main()
pthread_create(threadid[i], eq4, (void *) &thread_data_array[i]);
}
for(j=0;j<N; j++) {
pthread_join(threadid[j], NULL);
}
}
free(a_m); free(b_m); free(c_m);
}
你知道每個連續的for循環都會破壞等待來自先前循環的剛剛啓動的,現在孤立的線程的任何機會,對吧?即只有最後一組是等待的,並且毫無疑問與所有這些孤兒線直接競爭。 pthread_create的第一個參數是錯誤的,它應該是'threadid + i',而不是'threadid [i]',並且你的編譯器應該告訴你這一點。如果這沒有錯,我會驚訝。上面的 – WhozCraig 2014-10-08 18:23:09
是一個僞代碼,語法不準確。你是對的,它應該是&threadid [i]。 – user2415927 2014-10-08 18:52:12
發佈代碼並不代表你正在做的事。我們無法讀懂你的想法。我們所能讀到的只是你發佈的代碼和單詞。 – 2014-10-08 18:57:19