Comment this Program

This is a discussion on Comment this Program within the C Programming forums, part of the General Programming Boards category; Hello Guys, Please do take some time to comment on this program. Please discuss the pro's & cons of the ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    16

    Comment this Program

    Hello Guys, Please do take some time to comment on this program. Please discuss the pro's & cons of the logic...

    Thanx in advance for commenting.

    ---Kishore



    /************************************************** ******
    *
    * "Using POSIX Threads: Programming with Pthreads"
    *
    ************************************************** ******
    * matrix_threads.c --
    *
    * A master thread spawns separate child threads to compute each
    * 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;
    }
    Last edited by kishorepalle; 10-04-2004 at 09:19 PM.

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Con #1: lack of code tags.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    16
    Hey Thanx for your XSquared... Can you be more specific about the missing LACK OF CODE TAGS

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    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 !! >>
    If you understand what you're doing, you're not learning anything.

  6. #6
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    hehe , do it before Salem see's you
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    Too late
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    May 2004
    Posts
    16
    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.

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Registered User
    Join Date
    May 2004
    Posts
    16
    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.

  11. #11
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Um, other than the fact that it does not compile on my machine.. what sort of comments would you like?

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    > threads = (pthread_t *)malloc(size*size*sizeof(pthread_t));
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 10:17 PM
  3. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  4. my server program auto shut down
    By hanhao in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-13-2004, 09:49 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21