Suppose matrix A and matrix B are saved in two dimension arrays. Write OpenMP pr
ID: 3889896 • Letter: S
Question
Suppose matrix A and matrix B are saved in two dimension arrays. Write OpenMP programs for A+B and A×B, respectively. Run them using a different number of threads, and find the speed-up rate.
Example 2: calculate the area under a curve using critical directive #include #include #include void Trap (double a, double b, int n, double* global_result_p) int main (int argc, char* argv[) double global-result = 0.0; /* Store result in global-result */ double a. b /* Left and right endpoints /*Total number of trapezoids thread_count thread-count = strtol(argv [1], NULL, 10); printf("Enter a, b, and n "): scanf("%lf %If %d", &a; , &b; , &n; ) ; pragma omp parallel numthreads (thread-count) Trap(a, b. n, &global;_result); printf("With n = %d trapezoids, our estimate ", n); printf("of the integral from %f to %f %. 14e ", a, b, global_result); return 0; mainExplanation / Answer
#include<stdio.h>
#include<omp.h>
/* Main Program */
main()
{
float *Array, *Check, serial_sum, sum, partialsum;
int array_size, i;
printf("Enter the size of the array ");
scanf("%d", &array_size);
if (array_size <= 0) {
printf("Array Size Should Be Of Positive Value ");
exit(1);
}
/* Dynamic Memory Allocation */
Array = (float *) malloc(sizeof(float) * array_size);
Check = (float *) malloc(sizeof(float) * array_size);
/* Array Elements Initialization */
for (i = 0; i < array_size; i++) {
Array[i] = i * 5;
Check[i] = Array[i];
}
printf("The Array Elements Are ");
for (i = 0; i < array_size; i++)
printf("Array[%d]=%f ", i, Array[i]);
sum = 0.0;
partialsum = 0.0;
/* OpenMP Parallel For Directive And Critical Section */
#pragma omp parallel for shared(sum)
for (i = 0; i < array_size; i++) {
#pragma omp critical
sum = sum + Array[i];
}
serial_sum = 0.0;
/* Serail Calculation */
for (i = 0; i < array_size; i++)
serial_sum = serial_sum + Check[i];
if (serial_sum == sum)
printf(" The Serial And Parallel Sums Are Equal ");
else {
printf(" The Serial And Parallel Sums Are UnEqual ");
exit(1);
}
/* Freeing Memory */
free(Check);
free(Array);
printf(" The SumOfElements Of The Array Using OpenMP Directives Is %f ", sum);
printf(" The SumOfElements Of The Array By Serial Calculation Is %f ", serial_sum);
}