SAM Example 2

From SoOS

Jump to: navigation, search

Contents

[edit] Description

This example shows how to end and retrieve information to/from worker threads.

[edit] Code

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sam.h>
  4.  
  5. #define N 4
  6. #define ARRAY_SIZE 64
  7.  
  8. struct datast
  9. {
  10.   int *A;
  11. };
  12.  
  13. void * worker_routine(void *args)
  14. {
  15.   SAM_t sam_context; /* Declare a SAM context variable */
  16.   void *myargs; /* Pointer to where the arguments are stored */
  17.   int *myArray; /* Pointer to the Array of the data */
  18.   struct datast *data_arg; /* Pointer to the arguments of the function */
  19.   int total; /* Store the variable of the calculation */
  20.   int *result; /* Pointer to the result (total) of the calculation */
  21.   int i;
  22.  
  23.  
  24.   /* Start the execution of this worker thread */
  25.   /* Extract the SAM context and the arguments */
  26.   SAM_Execution_start(args, &sam_context, &myargs);
  27.  
  28.   /* Print the SAM worker ID of this worker thread  */
  29.   printf("SAM worker id: %d\n", (int)SAM_My_workid(sam_context));
  30.  
  31.   /* Extract the arguments of the function */
  32.   data_arg = (struct datast *) myargs;
  33.   myArray = data_arg->A;
  34.  
  35.   total = 0;
  36.  
  37.  
  38.   /* Make some calculations... */
  39.   for(i=0; i<ARRAY_SIZE; i++)
  40.     {
  41.       total += myArray[i];
  42.     }
  43.  
  44.   total = total * (int)SAM_My_workid(sam_context);
  45.  
  46.   /* Finish the execution of this worker trhead */
  47.   SAM_Execution_finish(sam_context);
  48.  
  49.   /* Reserve memory for the result and return it */
  50.   result = (int *) malloc(sizeof(int));
  51.   *result = total;
  52.  
  53.   return result;
  54. }
  55.  
  56. int main(void)
  57. {
  58.   SAM_t sam_context; /* Declare a SAM context variable */
  59.   SAM_Workerid_t th[N]; /* For storing the worker threads ID's  */
  60.   int *result; /* Gets the result of the worker thread */
  61.   int *myArray; /* Pointer to the array of the data */
  62.   struct datast *data_arg; /* Pointer to the worker threads arguments */
  63.   int i;
  64.  
  65.  
  66.   myArray = (int *) malloc(ARRAY_SIZE *sizeof(int));
  67.   data_arg = (struct datast *) malloc(sizeof(struct datast));
  68.  
  69.  
  70.   /* Set some data into the Array */
  71.   for(i=0; i<ARRAY_SIZE; i++)
  72.     {
  73.       myArray[i] = i;
  74.     }
  75.  
  76.  
  77.   /* Initialize the SAM context */
  78.   SAM_Init(&sam_context);
  79.  
  80.   /* Setup the arguments we are going to send to the worker threads */
  81.   data_arg->A = myArray;
  82.  
  83.   for(i=0;i<N;i++)
  84.     {
  85.       /* Create the worker threads running worker_routine  */
  86.       th[i] = SAM_Execution_create(sam_context, *worker_routine, (void *)data_arg);
  87.     }
  88.  
  89.  
  90.   for(i=0;i<N;i++)
  91.     {
  92.       /* Join the worker threads and get the result */
  93.       result = (int *)SAM_Execution_join(sam_context, th[i]);
  94.  
  95.       /* Print the result */
  96.       printf("RES %d: %d\n", i, *result);
  97.  
  98.       /* Do not forget to free the memory reserved to store the result */
  99.       free(result);
  100.     }
  101.  
  102.  
  103.   /* Destroy the SAM context */
  104.   SAM_Destroy(&sam_context);
  105.  
  106.  
  107.   /* Free the memory */
  108.   free(myArray);
  109.   free(data_arg);
  110.  
  111.   return 0;
  112. }

[edit] Explanation

This example shows how to start worker threads, send information to them and retrieve a result.

In line 81 a struct that holds the arguments needed by the worker threads is set up, and then in line 86, with the call to function SAM_Execution_create(), the worker thread is created and the pointer to the struct with the arguments is sent. The worker thread gets the pointer to the struct with the arguments with the execution of the function SAM_Execution_start() in the line 26.

The result of the worker thread calculations is sent in the return of its main function (line 53), but first the memory for keeping the result must be reserved (lines 50 and 51). The main execution thread retrieves the result from each working thread by executing the function SAM_Execution_join() (line 93). If we reserved memory for storing the result, we must remember to free it (line 99).

[edit] Compile Command

gcc example2.c -lsam -lpthread -o example2

[edit] Download