1. ## 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>

#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);

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;
main thread can join with them. */
package_t *p;             /* argument list to pass to each thread. */

/* Currently size hardwired to ARRAY_SIZE size */
size = ARRAY_SIZE;

/* one thread will be created for each element of the matrix. */

/* 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.
*/
}
#endif

/* Process Matrix, by row, column, Create a thread to process
each element in the resulting matrix*/
for(row = 0; row < size; row++) {
for (column = 0; column < size; column++) {
p = (package_t *)malloc(sizeof(package_t));
p->size = size;
p->Arow = row;
p->Bcol = column;
(p->MA) = &MA;
(p->MB) = &MB;
(p->MC) = &MC;

mult_worker,
(void *) p);

}
}

/* Synchronize on the completion of the element in each thread. */
for (i = 0; i < (size*size); i++) {
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;
}```

2. Con #1: lack of code tags.

3. Hey Thanx for your XSquared... Can you be more specific about the missing LACK OF CODE TAGS

4. It's in the big announcement at the top of the topics list that says to READ FIRST IF POSTING CODE
<< !! Posting Code? Read this First !! >>

5. hehe , do it before Salem see's you

6. Too late

7. 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.

8. 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.

9. 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. Um, other than the fact that it does not compile on my machine.. what sort of comments would you like?

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.