Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » Parallel Processing ProgramsRSS Feeds

Program to multiply two matrices using thread

Posted By: Easy Tutor     Category: C Programming     Views: 14688

Write a program to multiply two matrices using thread.

Code for Program to multiply two matrices using thread in C Programming

# include <stdio.h>
# include <pthread.h>

int MAT1[10][10];
int MAT2[10][10];
int MAT3[10][10];

int r1,c1,r2,c2;

void *thread_Multiply_Matrix(void *);

int main()
{
    pthread_t tid;    
    int iCount,jCount,kCount;
    
    printf("Enter Number of Rows For Matrix 1 :");
    scanf("%d",&r1);
    
    printf("Enter Number of Columns For Matrix 1 :");
    scanf("%d",&c1);
    
    for(iCount=0;iCount<r1;iCount++)
    {
        for(jCount=0;jCount<c1;jCount++)
        {
            printf("Enter Mat1[%d][%d] :",iCount,jCount);
            scanf("%d",&MAT1[iCount][jCount]);
        }
    }
    
    printf("\n");
    
    printf("Enter Numer of Rows For Matrix 2 :");
    scanf("%d",&r2);
    
    printf("Enter Number of Columns For Matrix 2 :");
    scanf("%d",&c2);
    
    for(iCount=0;iCount<r2;iCount++)
    {
        for(jCount=0;jCount<c2;jCount++)
        {
            printf("Enter Mat2[%d][%d] :",iCount,jCount);
            scanf("%d",&MAT2[iCount][jCount]);
        }
    }
    
    if(c1!=r2)
    {
        printf("Multipication of Matrix not Possible !!!");
    }
    else
    {
        for(iCount=0;iCount<r1;iCount=iCount+2)
        {
            for(jCount=0;jCount<c2;jCount=jCount+2)
            {
                MAT3[iCount][jCount]=0;
            }
        }
        
        pthread_create(&tid,NULL,thread_Multiply_Matrix,NULL);
        
        for(iCount=0;iCount<r1;iCount=iCount+2)
        {
            for(jCount=0;jCount<c2;jCount++)
            {
                for(kCount=0;kCount<c1;kCount++)
                {
                    MAT3[iCount][jCount]+=MAT1[iCount][kCount] * MAT2[kCount][jCount];
                }
            }
        }
        
        pthread_join(tid,NULL);
    }
    
    printf("\n Matrix 1 \n");
    
    for(iCount=0;iCount<r1;iCount++)
    {
        for(jCount=0;jCount<c1;jCount++)
        {
            printf("%d \t",MAT1[iCount][jCount]);
        }
        printf("\n");
    }    
    
    printf("\n Matrix 2 \n");
    
    for(iCount=0;iCount<r2;iCount++)
    {
        for(jCount=0;jCount<c2;jCount++)
        {
            printf("%d \t",MAT2[iCount][jCount]);
        }
        printf("\n");
    }    
    
    printf("\n Multipication of Matrix ...\n");
    
    for(iCount=0;iCount<r1;iCount++)
    {
        for(jCount=0;jCount<c2;jCount++)
        {
            printf("%d \t",MAT3[iCount][jCount]);
        }
        printf("\n");
    }    
    return 0;
}

void *thread_Multiply_Matrix(void *para)
{
    int iCount,jCount,kCount;
    for(iCount=1;iCount<r1;iCount=iCount+2)
        {
            for(jCount=0;jCount<c2;jCount++)
            {
                for(kCount=0;kCount<c1;kCount++)
                {                    
                    MAT3[iCount][jCount]+=MAT1[iCount][kCount] * MAT2[kCount][jCount];
                }
            }
        }
        
    printf("thread finished ...");
    pthread_exit(NULL);
}
  
Share: 


Didn't find what you were looking for? Find more on Program to multiply two matrices using thread Or get search suggestion and latest updates.

Easy Tutor
Easy Tutor author of Program to multiply two matrices using thread is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
Tashik Javed from United States Comment on: May 13
hey it displays an error

tashik@ubuntu:~$ gcc -o task prog.c
/tmp/ccrD6sb7.o: In function `main':
prog.c:(.text+0x22f): undefined reference to `pthread_create'
prog.c:(.text+0x304): undefined reference to `pthread_join'
collect2: ld returned 1 exit status


kindly help me

Isah Salisu from Turkey Comment on: Dec 24
please help me with a parallel program code for the multiplication of two 5x5 matrices by using drived data types.Choose 6 nodes in the run time and each row of the resultant matrix should be calculated by one of the nodes. Create your matrixes only in the master node and use drived data types in your program.

View All Comments