Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Openmp- paralell Reduce 1 Reduce Question: Write a program that will generate a

ID: 3599224 • Letter: O

Question

Openmp- paralell Reduce

1 Reduce

Question: Write a program that will generate a random array of integers. Then it will compute the sum of the array in parallel using the OpenMP for loop construct. Measure the time computing the sum takes. Write the program so that you can control the length of the array, the number of thread and the scheduling policy. Output the value of the reduction to stdout and the time taken to compute it to stderr. Question: Plot a speedup chart for dierent numbers of thread (1, 2, 4, 8, 16), for arrays of sizes 108, with dierent scheduling policies (static, dynamic,1, dynamic,1000, dynamic,100000) using the bench script. Does the plot make sense? Why?

Explanation / Answer

the correct code look like this

void initMatrix(int** m, int H, int W)
{
#pragma omp parallel
{
    srand(int(time(NULL)) ^ omp_get_thread_num());
    #pragma omp for
    for (int i = 0; i < H; ++i)
      for (int j = 0; j < W; ++j)
        m[i][j] = rand()%15;
}
}

#include <iostream>
#include <ctime>
using namespace std;

//prototypes
void printFunc(int[]);
void fillFunc(int[]);

int main()
{
int random[25]; //0-24 is 25 remember array indeices
srand((unsigned)time(NULL));
fillFunc(random)
printFunc(random);
return 0;
}

void fillFunc(int[] arr)
{
        for (int i = 1; i < 25; i++)
        {
                    arr[i] = 1+ rand() % 10;
         }

}

void printFunc(int arr[])
{
          for (int i = 1; i < 25; i++)
          {
                    cout << arr[i];
         }
}