?
Solved

Matrix program using multi threading calculation

Posted on 2008-02-12
6
Medium Priority
?
2,509 Views
Last Modified: 2010-04-21
Hi, I was asked to write a program to calculate the matrix multiplication using the following pseudo codes
#include <pthread.h>
// global data declaration


 int dim;


void *multiply(struct matrix *in)
{
// local data declaration


// multiplication process
...
...


        pthread_exit((void *)0);



}


int main(int argc, char *argv[])
{

// local data e.g. thread id
        int i, j, no_elements;
       


        if (argc != 2) {
             // output error ... need to specify one argument !
                return(1);
        }


        dim=atoi(argv[1]);


        no_elements = 0;
        for (i=0; i<dim; i++)
            for (j=0; j<dim; j++){
                // initialise the data
                ...
                no_elements++;
             }


// create threads
        for (m=0; m<no_elements; m++)
           pthread_create(..., 0,  (void *)multiply, ....);


// join the threads
        for (m=0; m<no_elements; m++)
           pthread_join(..., NULL);


// printing of result


        pthread_exit((void *)0);
}
when run:
$./ matrix    3
"3" is the size of the matrix

The result output as below
180  84  168            118  102  135            60720  50448  67308
171  195  42     *        78  110  204   =       43620  44604  69333
 71  164  59             196  136  154            32734  33306  52127

From the structure of the code above, what sort of variables should i store in the stuct matrix??   And maybe the algorithm on writing the multiply function() using struct as parameter. Thanks
0
Comment
Question by:glitchized
  • 3
  • 3
6 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 20875767
>> what sort of variables should i store in the stuct matrix??

At the very least, you'll need the size (rows and cols) of the matrix, and the actual data elements.


>> And maybe the algorithm on writing the multiply function() using struct as parameter. Thanks

Here's the algorithm for matrix multiplication easily explained :

        http://en.wikipedia.org/wiki/Matrix_multiplication
0
 

Author Comment

by:glitchized
ID: 20881291
i understand the algorithm for the matrix, but is there any tutorial/sample code to see how to do the matrix multiplication?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20882788
The code will be exactly the same as the algorithm ... Just multiply-add each row of the first matrix with each column of the second matrix ... Use a nested loop for that ...
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 

Author Comment

by:glitchized
ID: 20890760
i actually weak in pointers
I gt error  "request for member 'row' in something not a struct or union"
                "request for member 'row' in something not a struct or union"
in line   "total += in.row[l] * in.col[l];"
   
   #include <pthread.h>
   /* this is only in the form of pseudo codes but NOT C/C++ program
      you need to fill in the necessary detail
   */

   int dim;
   pthread_mutex_t mutex;
   //global data declaration
   struct matrix
   {      
      
      int row[9];
      int col[9];
   };

   void *multiply(struct matrix *in)
   {
      int total = 0;
      int l;
      // local data declaration
      pthread_mutex_lock(&mutex);
      for (l = 0; l < dim; l++)
      {      
            
err            total += in.row[l] * in.col[l];
            
      }
      


 


      pthread_mutex_unlock(&mutex);
           pthread_exit((void *)0);



   }


    int main(int argc, char *argv[])
   {

   // local data e.g. thread id
        int i, j,k,x,m, no_elements;    
        if (argc != 2) {
             // output error ... need to specify one argument !
                return(1);
        }


        dim=atoi(argv[1]);
      struct matrix a[dim*dim];

        no_elements = 0;
      k=0;
        for (i=0; i<dim; i++) {
            for (j=0; j<dim; j++){
            k++;
            no_elements++;
            if (k % dim == 0)
                  k=0;
            for (x=0; x<dim; x++){
                      a[(dim*i)+j].row[x] = (dim*i)+x+1;
                  a[(dim*i)+j].col[x] = k+(dim*x);
                 
               
            }
             
             }

      }
      
      pthread_t tid[no_elements];

   // create threads
        for (m=0; m<no_elements; m++)
           pthread_create(&tid[m], 0,  (void *)multiply,&a[m]);


   // join the threads
        for (m=0; m<no_elements; m++)
           pthread_join(tid[m], NULL);


   // printing of result


      pthread_mutex_destroy(&mutex);
        pthread_exit((void *)0);
   }

i think it happens while using the struct variables in the function..
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 1500 total points
ID: 20891888
  struct matrix
   {      
     
      int row[9];
      int col[9];
   };

This does not represent a matrix ... It's just two arrays of 9 ints.


>> I gt error  "request for member 'row' in something not a struct or union"

That's because in is a pointer to struct matrix, not a struct matrix. For accessing fields you have to use -> instead of .


But, as I said : you haven't really defined a matrix ... Try either this :

        int matrix[9][9];

or :

        int **matrix;  /* <--- allocate memory !!! */



>> i actually weak in pointers

Then I suggest reading these tutorials :

        arrays : http://www.cplusplus.com/doc/tutorial/arrays.html
        pointers : http://www.cplusplus.com/doc/tutorial/pointers.html
        dynamic memory : http://www.cplusplus.com/doc/tutorial/dynamic.html
0
 

Author Closing Comment

by:glitchized
ID: 31430182
Thanks for the ans and tutorials... they help a lot!!!!
0

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Make the most of your online learning experience.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Simple Linear Regression

599 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question