Multiply matrices using threads

In summary, the individual is getting segmentation faults when trying to run their program, possibly due to incorrect parameters being passed into the pthread_create function. An example in Spanish may be helpful in understanding how to correctly use this function.
  • #1
cs23
66
0

Homework Statement


In my main function i am filling 2 matrices. Matrix A is 18x16 and MatrixB is 16x18. Then i am multiplying them in my thread function using an array of threads. However, i am getting segmentations faults when trying to run the program.

Code:
#include<stdio.h>
#include<pthread.h>
 
    int mata[18][16];
    int matb[16][18];
    int matc[18][18];
 
void* compute_c_ij(void* arg)
{
    int k;
    int n= *((int*)arg);
 
    for(k=0;k<16;++k)
    {
        matc[n][n] += mata[n][k] + matb[k][n];
    }
 
        pthread_exit(NULL);
}
 
int main()
{
 
    pthread_t thr[18];
 
 
    int n,m;
 
    int i,j;
 
    for(i=0;i<18;++i)
        for(j=0;j<16;++j)
            {
            mata[i][j] = (i+1) +(j+1);
            }
 
    for (i=0; i<16; ++i)
        for(j=0;j<18;++j)
            {
            matb[i][j]= (i+1)+(2*(j+1));
            }
 
 
    for (i=0;i<18 ; ++i)
     {
     pthread_create(&thr[i],NULL,compute_c_ij,NULL);
     }
 
     for (i=0 ; i<18 ; ++i)
     {
      pthread_join(thr[i],NULL);
      printf("%d\n",matc[i][i]);
     }
 
     pthread_exit(NULL);
}

Homework Statement


Homework Equations


The Attempt at a Solution

 
Last edited:
Physics news on Phys.org
  • #2
Just an educated guess, but I don't think you are passing the right parameters into pthread_create. Your start function, compute_c_ij takes a parameter, and this is what should go in the 4th parameter of pthread_create (where you have NULL).

I found this example, which might be helpful, although much of it is in Spanish - http://www.dreamincode.net/forums/topic/53953-pthreadh/.
 

1. What are threads and why are they used for multiplying matrices?

Threads are units of execution within a program that can run concurrently with other threads. They are used for multiplying matrices because they allow the program to divide the task of multiplying the matrices into smaller, more manageable parts that can be executed simultaneously, resulting in faster processing times.

2. How do I create and manage threads for multiplying matrices?

To create and manage threads, you will need to use a threading library in your programming language of choice. This library will provide functions and methods for creating, starting, and joining threads. You will also need to carefully design and implement your algorithm to properly manage the threads and ensure that each thread is working on the correct part of the matrix multiplication.

3. Are there any limitations to using threads for multiplying matrices?

While using threads can greatly improve the speed of matrix multiplication, there are some limitations to consider. Firstly, the speedup achieved by using threads will depend on the size and complexity of the matrices being multiplied. Additionally, if not properly managed, threads can lead to issues such as race conditions and deadlocks. It is important to carefully design and test your algorithm to avoid these limitations.

4. Can I use multiple processors or cores to further improve the speed of multiplying matrices with threads?

Yes, you can use multiple processors or cores to further improve the speed of matrix multiplication with threads. Each processor or core can handle a different thread, allowing for even more parallel processing. However, this will also depend on the capabilities of your hardware and the implementation of your algorithm.

5. How can I measure the performance of multiplying matrices with threads?

To measure the performance of multiplying matrices with threads, you can use various metrics such as execution time, speedup, and efficiency. Execution time refers to the time it takes for the program to complete the matrix multiplication task. Speedup is a measure of how much faster the program is with threads compared to without threads. Efficiency measures how well the threads are utilized and is calculated by dividing the speedup by the number of threads used.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
996
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
0
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
7K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
Back
Top