可変数のスレッドを使用して2つの行列を乗算し、実行ごとに実行時間を比較するプログラムを作成しています。ユーザーが使用するスレッドの最大数を指定すると、プログラムは1スレッド、2、3、4 ....最大max_threadsで乗算を実行します(max_threadsが8を超えることを心配する必要はありません)。 。では、実行ごとにスレッドを作成するための最良の方法は何ですか?これが暗闇の中での私のベストショットです。
編集:pthreadを使用する必要があります。
//Ive already called multiplyMatrices for the single thread run. Start with 2 threads.
for (int h=2; h <= max_threads; h++)
{
for(int i = 0; i < h; i++)
{
pthread_create(thr_id[i],NULL, multiplyMatrices, i);
}
for(int i = 0; i < h; i++)
{
pthread_join(thr_id[i],NULL);
}
}
multiplyMatricesのコードは以下のとおりです。
void* multiplyMatrices(void* val)
{
for(int i = 0; i < n; i = i*val)
{
for(int j = 0; j < p; j++)
{
c[i][j] = 0;
for(int k = 0; k < m; k++)
{
c[i][j] += matrix_A[i][k] * matrix_B[k][j];
}
}
val++;
}
pthread_exit(0);
}