SAM Example 2
From SoOS
Contents |
[edit] Description
This example shows how to end and retrieve information to/from worker threads.
[edit] Code
#include <stdio.h>
#include <stdlib.h>
#include <sam.h>
#define N 4
#define ARRAY_SIZE 64
struct datast
{
int *A;
};
void * worker_routine(void *args)
{
SAM_t sam_context; /* Declare a SAM context variable */
void *myargs; /* Pointer to where the arguments are stored */
int *myArray; /* Pointer to the Array of the data */
struct datast *data_arg; /* Pointer to the arguments of the function */
int total; /* Store the variable of the calculation */
int *result; /* Pointer to the result (total) of the calculation */
int i;
/* Start the execution of this worker thread */
/* Extract the SAM context and the arguments */
SAM_Execution_start(args, &sam_context, &myargs);
/* Print the SAM worker ID of this worker thread */
printf("SAM worker id: %d\n", (int)SAM_My_workid(sam_context));
/* Extract the arguments of the function */
data_arg = (struct datast *) myargs;
myArray = data_arg->A;
total = 0;
/* Make some calculations... */
for(i=0; i<ARRAY_SIZE; i++)
{
total += myArray[i];
}
total = total * (int)SAM_My_workid(sam_context);
/* Finish the execution of this worker trhead */
SAM_Execution_finish(sam_context);
/* Reserve memory for the result and return it */
result = (int *) malloc(sizeof(int));
*result = total;
return result;
}
int main(void)
{
SAM_t sam_context; /* Declare a SAM context variable */
SAM_Workerid_t th[N]; /* For storing the worker threads ID's */
int *result; /* Gets the result of the worker thread */
int *myArray; /* Pointer to the array of the data */
struct datast *data_arg; /* Pointer to the worker threads arguments */
int i;
myArray = (int *) malloc(ARRAY_SIZE *sizeof(int));
data_arg = (struct datast *) malloc(sizeof(struct datast));
/* Set some data into the Array */
for(i=0; i<ARRAY_SIZE; i++)
{
myArray[i] = i;
}
/* Initialize the SAM context */
SAM_Init(&sam_context);
/* Setup the arguments we are going to send to the worker threads */
data_arg->A = myArray;
for(i=0;i<N;i++)
{
/* Create the worker threads running worker_routine */
th[i] = SAM_Execution_create(sam_context, *worker_routine, (void *)data_arg);
}
for(i=0;i<N;i++)
{
/* Join the worker threads and get the result */
result = (int *)SAM_Execution_join(sam_context, th[i]);
/* Print the result */
printf("RES %d: %d\n", i, *result);
/* Do not forget to free the memory reserved to store the result */
free(result);
}
/* Destroy the SAM context */
SAM_Destroy(&sam_context);
/* Free the memory */
free(myArray);
free(data_arg);
return 0;
}
[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