Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8202219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T07:06:08+00:00 2026-06-07T07:06:08+00:00

Hello, I have created a multithreaded application for multiplying two matrices using pthreads,but to

  • 0

Hello,

I have created a multithreaded application for multiplying two matrices using pthreads,but to my surprise the multithreaded program is taking much time than my expectation.

I dnt know where is the problem in my code,the code snippet is given below::

#include "pthreads.h"
#include "cv.h"
#include "cxcore.h"

CvMat * matA;       /* first matrix */
CvMat * matB;       /* second matrix */
CvMat * matRes;     /* result matrix */

int size_x_a; /* this variable will be used for the first  dimension */
int size_y_a; /* this variable will be used for the second dimension */

int size_x_b,size_y_b;
int size_x_res;
int size_y_res;

struct v {
  int i; /* row */
  int j; /* column */
};


void *printThreadID(void *threadid)
{
/*long id = (long) threadid;
//printf("Thread ID: %ld\n", id);

arrZ[id] = arrX[id] + arrY[id];

pthread_exit(NULL);*/
return 0;
}

int main()
{
/* assigining the values of sizes */
size_x_a = 200;
size_y_a = 200;
size_x_b = 200;
size_y_b = 200;

/* resultant matrix dimensions */
size_x_res = size_x_a;
size_y_res = size_y_b;

matA = cvCreateMat(size_x_a,size_y_a,CV_64FC1);
matB = cvCreateMat(size_x_b,size_y_b,CV_64FC1);
matRes = cvCreateMat(size_x_res,size_y_res,CV_64FC1);

pthread_t thread1;
pthread_t thread2;
pthread_t multThread[200][200];

int res1;
int res2;
int mulRes;
/*******************************************************************************/ 

/*Creating a thread*/
res1 = pthread_create(&thread1,NULL,initializeA,(void*)matA);
if(res1!=0)
{
    perror("thread creation of thread1 failed");
    exit(EXIT_FAILURE);
}


/*Creating a thread*/
res2 = pthread_create(&thread2,NULL,initializeB,(void*)matB);

if(res2!=0)
{
    perror("thread creation of thread2 failed");
    exit(EXIT_FAILURE);
}


pthread_join(thread1,NULL);
pthread_join(thread2,NULL);

/*Multiplication of matrices*/
for(int i=0;i<size_x_a;i++)
    {
  for(int j=0;j<size_y_b;j++)
      {
      struct v * data = (struct v*)malloc(sizeof(struct v));
      data->i = i;
      data->j = j;

mulRes = pthread_create(&multThread[i][j],NULL,multiplication,  (void*)data);
       }
    }

for(int i=0;i<size_x_a;i++)
{
for(int j=0;j<size_y_b;j++)
    {
    pthread_join(multThread[i][j],NULL);    
    }
}


for(int i =0;i<size_x_a;i++)
{
    for(int j = 0;j<size_y_a;j++)
    {
        printf("%f ",cvmGet(matA,i,j));
    }
}
return 0;
}

void * multiplication(void * param)
{
struct v * data = (struct v *)param;
double sum =0;
for(int k=0;k<size_x_a;k++)
    sum += cvmGet(matA,data->i,k) * cvmGet(matB,k,data->j); 

cvmSet(matRes,data->i,data->j,sum);
pthread_exit(0);

return 0;
}

void * initializeA(void * arg)
{
CvMat * matA  = (CvMat*)arg;
//matA = (CvMat*)malloc(size_x_a * sizeof(CvMat *));

/*initialiazing random values*/
for (int i = 0; i < size_x_a; i++) 
{
 for (int j = 0; j < size_y_a; j++) 
 {
    cvmSet(matA,i,j,size_y_a + j); /* just some unique number for each element */
 }
}
return 0;
}

void * initializeB(void * arg)
{
CvMat* matB  = (CvMat*)arg;
//matB = (CvMat*)malloc(size_x_b * sizeof(CvMat *));

/*initialiazing random values*/
for (int i = 0; i < size_x_b; i++) 
{
  for (int j = 0; j < size_y_b; j++) 
  {
    cvmSet(matB,i,j,size_y_b + j); /* just some unique number for each element */
  }
}
return 0;
}

void * initializeRes(void * arg)
{
CvMat * res  = (CvMat*)arg;
//res = (CvMat*)malloc(size_x_res * sizeof(CvMat *));

/* for matrix matRes, allocate storage for an array of ints */
for (int i = 0; i < size_x_res; i++) 
{
    for (int j = 0; j < size_y_res; j++) 
    {
        cvmSet(matRes,i,j,0);
    }
}
return 0;
}

I am doing this multithreading for the first time.
Kindly help me with this,any suggestion or correction will be very helpful.

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T07:06:11+00:00Added an answer on June 7, 2026 at 7:06 am

    You’re creating ALOT of threads, which will involve lots of context switches. If each thread is doing pure calculations, and wont involve any sort of waiting (like networking, sockets, etc) there is no reason why threading will be faster than not threaded. Unless of course you are on a multi CPU/core machine, then you should create one thread per core. With this sort of processing, more threads than cores will just slow it down.

    What you could do is divide the work-set into tasks that can be enqueued, and have worker threads (one/CPU core) that will pull the tasks off of a common worker queue. This is a standard producer/consumer problem.

    Here is some generic info about the producer/consumer problem.

    Its been a long time since Ive done matrix multiplication, so bear with me 🙂 It appears that you could divide the following into separate tasks:

    /*Multiplication of matrices*/
    for(int i=0;i<size_x_a;i++)
        {
      for(int j=0;j<size_y_b;j++)
          {
          struct v * data = (struct v*)malloc(sizeof(struct v));
          data->i = i;
          data->j = j;
    
          /* Instead of creating a thread, create a task and put it on the queue
           * mulRes = pthread_create(&multThread[i][j],NULL,multiplication,  (void*)data);
           */
    
          /* Im not going to implement the queue here, since there are several available
           * But remember that the queue access MUST be mutex protected. */
          enqueue_task(data);
           }
        }
    

    Previously, you will have to have created what is called the thread-pool (the worker threads, one per CPU core), whose worker function will try to pull off the queue and execute the work. There are ways to do this with pthread conditional variables, whereby the threads are blocked/waiting on the cond var if the queue is empty, and once the queue is populated, then the cond var is signalled, thus releasing the threads so they can start working.

    If this is not a logical division of work, and you cant find one, then perhaps this problem is not suitable for multi-threading.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a simple hello world application in xcode using the command line
Hello I have created an application using qt and I managed to save some
Hello i have created a sample application in android using eclipse IDE my code
I have created a simple Hello World Application using Flash Builder Burrito SDK. I
I have created my first application for android Hello android. With using Eclipse, ADT
hello i have created one windows application in c# .net and its working fine
Hello i am using NSOperationQueue to download images in the background. I have created
Hello I have created and application which contains a gridview that is populated by
I have created a very basic Hello world application where I am trying to
I have created a sample Hello World program in android it was working fine

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.