Question 8A Sequential part - 16 marks
This question considers a 2D grid of floating point numbers with dimensions width and height. Find
the position of this grid that has the highest magnitude average. This function computes the sum of the neighbouring values in the 2D grid (NorthWest, North, NorthEast, East, SouthEast, South, SouthWest, West) plus the centre and divides this by 9. Neighbours outside the dimensions of the array are treated as value zero. The grid is given as a 1D array which stores the 2D grid positions row-major order. NULL is returned in found x or found y if there is an error. The error can arise if the input array is NULL, dimensions are <= 0, or found x or found y are NULL values themselves.
The 1D array sequence: 1, 2, 3, 4, 5, 6, 7, 8, 9 represents the grid:
1 2 3
4 5 6
7 8 9
Write the code needed for the following function prototype to work. You may write additional helper
functions if needed.
// pre: w > 0 h > 0
// returns the position of the highest magnitude average of the array
// in variables found_x and found_y
void get_hma(float *array , int w, int h, int *found_x , int *found_y)
Question 8B Parallel part - 7 marks
Consider a parallelisation of this problem. Describe in words how you would use 2 or more threads to
gain a speedup > 1.
In your answer, you should explain if the approach is task parallel, data parallel, or both and also provide a drawing of a task-dependency graph that supports your description.
Question 8C Parallel part - 7 marks
Write a parallel solution using exactly 4 threads to gain a speedup > 1. Write your solution with notation that is similar to pthreads. thread create(), thread join(), lock(mutex), unlock(mutex). sem wait(s), sem post(s), sem init(s, value).