2

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);
}
5
  • I'd suggest looking into OpenMP myself. And when you run, make sure the matrices are nice and big or you won't see much improvement at all. Commented Nov 1, 2012 at 8:01
  • 2
    why are you computing i*val? You realize you multiply i by an address, right? Commented Nov 1, 2012 at 8:01
  • 2
    So what is the problem with this code? Is is not working? Are you getting errors? Commented Nov 1, 2012 at 8:03
  • Cant use OpenMP. I'm doing i*val so that each thread only handles certain rows making it parallelizable (4 threads -> thread 0 does rows 0,4,8,12... thread 1 does rows 1,5,9,13....). Dunno if there are problems with the code as I haven't compiled yet. Im just asking if this is the correct way to do something like this. Commented Nov 1, 2012 at 8:09
  • Sounds like a better question for codereview.stackexchange.com Commented Nov 1, 2012 at 13:00

2 Answers 2

4

It is C++ use std::thread + std::bind:

std::vector<std::thread > thread_pool;
thread_pool.reserve(h);
void* someData;
for(int i = 0; i < h; i++)
{   
    thread_pool.push_back(std::thread(std::bind(multiplyMatrices, someData)));
}
Sign up to request clarification or add additional context in comments.

3 Comments

Since std::thread is (to my knowledge) a move-only resource management type anyway, where is the need for a std::unique_ptr and dynamic memory allocation here?
By the way, there's no need for std::move here, since temporaries are rvalues anyway.
std::bind is not needed here and you could even use emplace_back directly
0

The biggest problem I see with your code is how you're passing the data to the thread function. The data should be passed as a pointer. The following should work better:

for (int h=2; h <= max_threads; h++)
{
    for(int i = 0; i < h; i++)
    {   
        // Notice Im passing a pointer to i here.
        // Since i may go out of scope, and its value could change before the
        // thread is started and multiplyMatrices() is called, this could be
        // risky. Consider using an array/vector defined before these for
        // loops to avoid this problem.
        pthread_create(thr_id[i],NULL, multiplyMatrices, &i);
        ...

void* multiplyMatrices(void* valPtr)
{    
    int val = *((int*) valPtr);
    for(int i = 0; i < n; i = i*val)
    {
       ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.