Information

Author(s) Erik-Jan Lingen, Matthias Möller
Deadline No deadline
Submission limit No limitation

Sign in

[Threads] Exercise 7 : Reduction

This material is part of the course Practical Introduction to Parallel Programming and meant for educational purposes only.


Task

Implement a struct reductiont that can be used to perform a global reduction operation – like a global sum – across multiple threads.

That is, this data type should enable multiple threads to provide a local value (of type double) and obtain the global reduction of all local values.

You must implement the following functions: reduction_init, reduction_destroy`, ``reduction_sum.

/* red INOUT A pointer to the reduction variable to be initialised.                */
/* nt  IN    The number of threads that will participate in a reduction operation. */

void reduction_init ( reduction_t* red, int nt );

The function reduction_init` initialises a reduction variable. That is, it initialises the members of the struct reduction_t; more about this later.

It must be called before a reduction operation can be performed.

The parameter nt specifies the number of threads that will participate in a reduction operation. This will be the number of threads calling the function reduction_sum.

/* red INOUT A pointer to the reduction variable to be destroyed.                  */

void reduction_destroy ( reduction_t* red );

The function reduction_destroy destroys a reduction variable.

It must be called when a reduction variable is no longer needed. Each call to reduction_init must be paired with a call to reduction_destroy.

/* red IN A pointer to a reduction variable.                                       */
/* val IN The local value to be summed.                                            */

double reduction_sum ( reduction_t* red, double val );

The function reduction_sum performs a global sum across all threads.

The parameter val is the local value to be added to the global sum. The return value is the global sum.

This function waits until it has been called by the number of threads indicated by the nt parameter of the reduction_init function.


The struct reduction_t must contain at least a mutex, a condition variable and a double storing the result of the reduction operation (global sum in this case).

You need more members; that is for you to figure out.

Test the program by spawning multiple threads and by performing at least four global reduction operations in succession. Check whether the computed results are correct.


Question 1: Reduction
Question 2: Command Line Argument

Specify the command line arguments that should be passed to your program when it is run. Arguments must be given in quotes and separated by commas. Leave this filed empty if you do not want to specify command line arguments.

Example: "1","int","arg=2" is interpreted as three arguments 1, int, and arg=2.