# Comment this Program

• 10-04-2004
kishorepalle
Comment this Program
Hello Guys, Please do take some time to comment on this program. Please discuss the pro's & cons of the logic...

---Kishore

/************************************************** ******
*
*
************************************************** ******
*
* element in the resulting array. Each of the child threads is passed
* a pointer to a structure that contains the element indices and
* pointers to starting and resulting arrays.
*
* The master thread joins to each thread, prints out the result and
* exits.
*/
Code:

```#include <stdlib.h> #include <stdio.h> #include <errno.h> #include <pthread.h> #define MIN_REQ_SSIZE 81920 #define ARRAY_SIZE 100 typedef int matrix_t[ARRAY_SIZE][ARRAY_SIZE]; typedef struct {   int      id;   int      size;   int      Arow;   int      Bcol;   matrix_t  *MA, *MB, *MC; } package_t; matrix_t MA,MB,MC; /* * Routine to multiply a row by a column and place element in * resulting matrix. */ void mult(int size,           int row,           int column,           matrix_t MA,           matrix_t MB,           matrix_t MC) {   int position;   MC[row][column] = 0;   for(position = 0; position < size; position++) {     MC[row][column] = MC[row][column] +       ( MA[row][position]  * MB[position][column] ) ;   } } /*  * Routine to start off a worker thread.  */ void *mult_worker(void *arg) {   package_t *p=(package_t *)arg;   printf("MATRIX THREAD %d: processing A row %d, B col %d\n",         p->id, p->Arow, p->Bcol );   mult(p->size, p->Arow, p->Bcol, *(p->MA), *(p->MB), *(p->MC));   free(p);     printf("MATRIX THREAD %d: complete\n", p->id);   return(NULL); } /* * Main(): allocates matrix, assigns values, then * creates threads to process rows and columns. */ extern int main(int argc, char **argv) {   int      size, row, column, num_threads, i;   pthread_t *threads;      /* threads holds the thread ids of all                               threads created, so that the                               main thread can join with them. */   package_t *p;            /* argument list to pass to each thread. */     unsigned long thread_stack_size;   pthread_attr_t *pthread_attr_p, pthread_custom_attr;   /* Currently size hardwired to ARRAY_SIZE size */   size = ARRAY_SIZE;   /* one thread will be created for each element of the matrix. */   threads = (pthread_t *)malloc(size*size*sizeof(pthread_t));     /* Fill in matrix values, currently values are hardwired */   for (row = 0; row < size; row++) {     for (column = 0; column < size; column++) {       MA[row][column] = 1;     }   }   for (row = 0; row < size; row++) {     for (column = 0; column < size; column++) {       MB[row][column] = row + column + 1;     }   }   printf("MATRIX MAIN THREAD: The A array is is;\n");   for(row = 0; row < size; row ++) {     for (column = 0; column < size; column++) {       printf("%5d ",MA[row][column]);     }     printf("\n");   }   printf("MATRIX MAIN THREAD: The B array is is;\n");   for(row = 0; row < size; row ++) {     for (column = 0; column < size; column++) {       printf("%5d ",MB[row][column]);     }     printf("\n");   }    /* This is not required for the program as the peers stack will not be,     to big. Its just here to show how to check and set pthread attributes.   */   pthread_attr_init(&pthread_custom_attr); #ifdef _POSIX_THREAD_ATTR_STACKSIZE   pthread_attr_getstacksize(&pthread_custom_attr, &thread_stack_size);   if (thread_stack_size < MIN_REQ_SSIZE) {     pthread_attr_setstacksize(&pthread_custom_attr, (long)MIN_REQ_SSIZE);   } #endif   /* Process Matrix, by row, column, Create a thread to process     each element in the resulting matrix*/   num_threads = 0;   for(row = 0; row < size; row++) {     for (column = 0; column < size; column++) {       p = (package_t *)malloc(sizeof(package_t));       p->id = num_threads;       p->size = size;       p->Arow = row;       p->Bcol = column;       (p->MA) = &MA;       (p->MB) = &MB;       (p->MC) = &MC;             pthread_create(&threads[num_threads],                     &pthread_custom_attr,                     mult_worker,                     (void *) p);       printf("MATRIX MAIN THREAD: thread %d created\n", num_threads);       num_threads++;           }   }     /* Synchronize on the completion of the element in each thread. */   for (i = 0; i < (size*size); i++) {     pthread_join(threads[i], NULL);     printf("MATRIX MAIN THREAD: child %d has joined\n", i);   }     /* Print results */   printf("MATRIX MAIN THREAD: The resulting matrix C is;\n");   for(row = 0; row < size; row ++) {     for (column = 0; column < size; column++) {       printf("%5d ",MC[row][column]);     }     printf("\n");   }   return 0; }```
• 10-04-2004
XSquared
Con #1: lack of code tags.
• 10-04-2004
kishorepalle
Hey Thanx for your XSquared... Can you be more specific about the missing LACK OF CODE TAGS
• 10-04-2004
XSquared
• 10-04-2004
itsme86
It's in the big announcement at the top of the topics list that says to READ FIRST IF POSTING CODE
• 10-04-2004
Brain Cell
hehe , do it before Salem see's you ;)
• 10-04-2004
Salem
Too late
• 10-04-2004
kishorepalle
Sorry about the mixed up code .
I tried to do it... But I couldn't...When I copy and paste the code it come like that only.
• 10-04-2004
Salem
Click on the edit button of your original post and put &#91;code]&#91;/code] around your code.
I could, but I'm not going to.
• 10-04-2004
kishorepalle
I did it... Thanx it worked... Well I guess i learned a new thing today...

So, is it OK now if I ask you to post your comments on this program. Like your opinion...Pros & Cons like performance issues and e.tc. and pls give it a overall rating as well. :)
• 10-05-2004
kermit
Um, other than the fact that it does not compile on my machine.. what sort of comments would you like? ;)
• 10-05-2004
Salem
1. There is no need to cast the result of malloc in C.
2. 10000 threads - are you crazy?
Do you realise how much time is going to be spent thrashing away between threads rather than actually getting any work done?
3. There is no free(threads) at the end.

In fact, all your other casts of void* to some other type can be removed as well.

> matrix_t MA,MB,MC;
These should not be global.

> extern int main
extern is not needed here

> printf("MATRIX MAIN THREAD: The A array is is;\n");
You have the same code to print each matrix - make it a function and just call it 3 times.