Multiply matrices using threads

  • Thread starter Thread starter cs23
  • Start date Start date
  • Tags Tags
    Matrices Threads
Click For Summary
SUMMARY

The forum discussion centers on a C program that multiplies two matrices using threads, specifically matrices A (18x16) and B (16x18). The user encounters segmentation faults due to incorrect parameter passing in the pthread_create function. The compute_c_ij function requires an integer argument to specify the row index, which is currently being passed as NULL. Correcting this by passing the appropriate index will resolve the segmentation fault issue.

PREREQUISITES
  • C programming language
  • Understanding of multithreading with pthreads
  • Matrix multiplication concepts
  • Debugging techniques for C programs
NEXT STEPS
  • Review pthreads documentation for correct parameter passing
  • Learn about matrix multiplication algorithms
  • Explore debugging tools for C, such as gdb
  • Investigate thread synchronization techniques in C
USEFUL FOR

This discussion is beneficial for C programmers, students learning multithreading, and developers working on performance optimization in matrix computations.

cs23
Messages
64
Reaction score
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
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/.
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
1
Views
4K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 1 ·
Replies
1
Views
8K
  • · Replies 3 ·
Replies
3
Views
6K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K