Matrix program using multi threading calculation

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
glitchizedAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Infinity08Commented:
>> 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
glitchizedAuthor Commented:
i understand the algorithm for the matrix, but is there any tutorial/sample code to see how to do the matrix multiplication?
0
Infinity08Commented:
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 Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

glitchizedAuthor Commented:
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
Infinity08Commented:
  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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
glitchizedAuthor Commented:
Thanks for the ans and tutorials... they help a lot!!!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.