Im writing a program that multiplies two matrices using a variable number of threads and then compares execution time for each run. The user specifies the maximum number of threads to use and then the program does the multiplication with 1 thread, again with 2, 3, 4....up to max_threads (we don't have to worry about max_threads being more than 8). So whats the best way to create the threads for each run? Here's my best shot in the dark.
EDIT: I have to use 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);
}
}
The code for multiplyMatrices is below.
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);
}