Multiply matrices using threads

  • Thread starter Thread starter cs23
  • Start date Start date
  • Tags Tags
    Matrices Threads
Join the discussion
Registration is free. Start your own thread to ask a follow-up.
1 reply · 2K views
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