please help me with this and explain in details also in the first question modify them both please
trap1 and trap2:
Q1 Consider the below omp_trap1.c implantation, modify the code so that: i. The time used by
the parallel block is timed using the OpenMP function omp_get_wtime(). The syntax is double
omp_get_wtime ( void ) It returns the number of seconds that have elapsed since some time in
the past. ii. Now on a system with at least two cores, time the program with a. one thread and a
large value of n, and b. two threads and the same value of n. Explain what happens? Then Do the
same measurements for the omp_trap2.c code.
1. How does its performance compare with the performance of the omp_trap1.c ?
2. Explain your answers.
3. provide with your answers the code and detailed screenshots showing how did you compile
and run the code and times for each run:
/* File: omp_trap1.c * Purpose: Estimate definite integral (or area under curve) using trapezoidal
rule. * Input: a, b, n * Output: estimate of integral from a to b of f(x) * using n trapezoids. * *
Compile: gcc -g -Wall -fopenmp -o omp_trap1 omp_trap1.c * Usage: ./omp_trap1 * * Notes: *
1. The function f(x) is hardwired. * 2. In this version, each thread explicitly computes the
integral over its assigned subinterval, a critical directive is used for the global sum. * 3. This
version assumes that n is evenly divisible by the number of threads #include #include #include
#include void Usage(char* prog_name); double f(double x); /* Function we're integrating */
double Local_trap ( double a , double b , i n t n ) ; int main(int argc, char* argv[]) { double
global_result = 0.0; /* Store result in global_result */ double a, b; /* Left and right endpoints */
int n; /* Total number of trapezoids */ int thread_count; if (argc != 2) Usage(argv[0]);
thread_count = strtol(argv[1], NULL, 10); printf("Enter a, b, and n\n"); scanf("%lf %lf %d", &a,
&b, &n); if (n % thread_count != 0) Usage(argv[0]); double global_result = 0.0; # pragma omp p
a r a l l e l num_threads ( t h r e a d _ c o u n t ) { # pragma omp c r i t i c a l global_result +=
Local_trap ( double a , double b , i n t n ) ; } printf("With n = %d trapezoids, our estimate\n", n);
printf("of the integral from %f to %f = %.14e\n", a, b, global_result); return 0; } /* main */ /*----
---------------------------------------------------------------- * Function: Usage * Purpose: Print
command line for function and terminate * In arg: prog_name */ void Usage(char* prog_name) {
fprintf(stderr, "usage: %s \n", prog_name); fprintf(stderr, " number of trapezoids must be evenly
divisible by\n"); fprintf(stderr, " number of threads\n"); exit(0); } /* Usage */ /*---------------------
--------------------------------------------- * Function: f * Purpose: Compute value of function to be
integrated * Input arg: x * Return val: f(x) */ double f(double x) { double return_val; return_val
= x*x; return return_val; } /* f */ /*------------------------------------------.
internship ppt on smartinternz platform as salesforce developer
please help me with this and explain in details also in the first qu.pdf
1. please help me with this and explain in details also in the first question modify them both please
trap1 and trap2:
Q1 Consider the below omp_trap1.c implantation, modify the code so that: i. The time used by
the parallel block is timed using the OpenMP function omp_get_wtime(). The syntax is double
omp_get_wtime ( void ) It returns the number of seconds that have elapsed since some time in
the past. ii. Now on a system with at least two cores, time the program with a. one thread and a
large value of n, and b. two threads and the same value of n. Explain what happens? Then Do the
same measurements for the omp_trap2.c code.
1. How does its performance compare with the performance of the omp_trap1.c ?
2. Explain your answers.
3. provide with your answers the code and detailed screenshots showing how did you compile
and run the code and times for each run:
/* File: omp_trap1.c * Purpose: Estimate definite integral (or area under curve) using trapezoidal
rule. * Input: a, b, n * Output: estimate of integral from a to b of f(x) * using n trapezoids. * *
Compile: gcc -g -Wall -fopenmp -o omp_trap1 omp_trap1.c * Usage: ./omp_trap1 * * Notes: *
1. The function f(x) is hardwired. * 2. In this version, each thread explicitly computes the
integral over its assigned subinterval, a critical directive is used for the global sum. * 3. This
version assumes that n is evenly divisible by the number of threads #include #include #include
#include void Usage(char* prog_name); double f(double x); /* Function we're integrating */
double Local_trap ( double a , double b , i n t n ) ; int main(int argc, char* argv[]) { double
global_result = 0.0; /* Store result in global_result */ double a, b; /* Left and right endpoints */
int n; /* Total number of trapezoids */ int thread_count; if (argc != 2) Usage(argv[0]);
thread_count = strtol(argv[1], NULL, 10); printf("Enter a, b, and nn"); scanf("%lf %lf %d", &a,
&b, &n); if (n % thread_count != 0) Usage(argv[0]); double global_result = 0.0; # pragma omp p
a r a l l e l num_threads ( t h r e a d _ c o u n t ) { # pragma omp c r i t i c a l global_result +=
Local_trap ( double a , double b , i n t n ) ; } printf("With n = %d trapezoids, our estimaten", n);
printf("of the integral from %f to %f = %.14en", a, b, global_result); return 0; } /* main */ /*----
---------------------------------------------------------------- * Function: Usage * Purpose: Print
command line for function and terminate * In arg: prog_name */ void Usage(char* prog_name) {
fprintf(stderr, "usage: %s n", prog_name); fprintf(stderr, " number of trapezoids must be evenly
divisible byn"); fprintf(stderr, " number of threadsn"); exit(0); } /* Usage */ /*---------------------
--------------------------------------------- * Function: f * Purpose: Compute value of function to be
integrated * Input arg: x * Return val: f(x) */ double f(double x) { double return_val; return_val
= x*x; return return_val; } /* f */ /*------------------------------------------------------------------ *
Function: Trap * Purpose: Use trapezoidal rule to estimate definite integral * Input args: * a: left
2. endpoint * b: right endpoint * n: number of trapezoids * Output arg: * integral: estimate of
integral from a to b of f(x) */ double Local_trap(double a, double b, int n) { double h, x,
my_result; double local_a, local_b; int i, local_n; int my_rank = omp_get_thread_num(); int
thread_count = omp_get_num_threads(); h = (b-a)/n; local_n = n/thread_count; local_a = a +
my_rank*local_n*h; local_b = local_a + local_n*h; my_result = (f(local_a) + f(local_b))/2.0; for
(i = 1; i <= local_n-1; i++) { x = local_a + i*h; my_result += f(x); } my_result = my_result*h;
return my_result; } /* Trap */
ANOTHER ONE :
/* File: omp_trap_2.c * Purpose: Estimate definite integral (or area under curve) using
trapezoidal * rule. This version uses a hand-coded reduction after the function * call. * * Input: a,
b, n * Output: estimate of integral from a to b of f(x) * using n trapezoids. * * Compile: gcc -g -
Wall -fopenmp -o omp_trap2a omp_trap2a.c -lm * Usage: ./omp_trap2a * * Notes: * 1. The
function f(x) is hardwired. * 2. This version assumes that n is evenly divisible by the * number of
threads * IPP: Section 5.4 (p. 222) */ #include #include #include #include void Usage(char*
prog_name); double f(double x); /* Function we're integrating */ double Local_trap(double a,
double b, int n); int main(int argc, char* argv[]) { double global_result; /* Store result in
global_result */ double a, b; /* Left and right endpoints */ int n; /* Total number of trapezoids */
int thread_count; if (argc != 2) Usage(argv[0]); thread_count = strtol(argv[1], NULL, 10);
printf("Enter a, b, and nn"); scanf("%lf %lf %d", &a, &b, &n); if (n % thread_count != 0)
Usage(argv[0]); global_result = 0.0; # pragma omp parallel num_threads(thread_count) { double
my_result = 0.0; my_result += Local_trap(a, b, n); # pragma omp critical global_result +=
my_result; } printf("With n = %d trapezoids, our estimaten", n); printf("of the integral from %f
to %f = %.14en", a, b, global_result); return 0; } /* main */ /*------------------------------------------
-------------------------- * Function: Usage * Purpose: Print command line for function and
terminate * In arg: prog_name */ void Usage(char* prog_name) { fprintf(stderr, "usage: %s n",
prog_name); fprintf(stderr, " number of trapezoids must be evenly divisible byn"); fprintf(stderr,
" number of threadsn"); exit(0); } /* Usage */ /*----------------------------------------------------------
-------- * Function: f * Purpose: Compute value of function to be integrated * Input arg: x *
Return val: f(x) */ double f(double x) { double return_val; return_val = x*x; return return_val; }
/* f */ /*------------------------------------------------------------------ * Function: Local_trap *
Purpose: Use trapezoidal rule to estimate part of a definite * integral * Input args: * a: left
endpoint * b: right endpoint * n: number of trapezoids * Return val: estimate of integral from
local_a to local_b * * Note: return value should be added in to an OpenMP * reduction variable
to get estimate of entire * integral */ double Local_trap(double a, double b, int n) { double h, x,
my_result; double local_a, local_b; int i, local_n; int my_rank = omp_get_thread_num(); int
3. thread_count = omp_get_num_threads(); h = (b-a)/n; local_n = n/thread_count; local_a = a +
my_rank*local_n*h; local_b = local_a + local_n*h; my_result = (f(local_a) + f(local_b))/2.0; for
(i = 1; i <= local_n-1; i++) { x = local_a + i*h; my_result += f(x); } my_result = my_result*h;
return my_result; } /* Trap */
Question2) Consider the loop: a [ 0 ] = 0; for ( i = 1; i < n ; i ++) a [ i] = a [ i1] + i ; Theres
clearly a loop-carried dependence, as the value of a[i] cant be computed without the value of
a[i1].
1. Can you see a way to eliminate this dependency and parallelize the loop?
2. Explain your answer and write the code after removing the dependency.
Question3) Modify the trapezoidal rule program that uses a parallel for directive (omp_trap3.c)
so that the parallel for is modified by a schedule(runtime) clause. Run the program with various
assignments to the environment variable OMP_SCHEDULE and determine which iterations are
assigned to which thread. This can be done by allocating an array iterations of n ints and in the
Trap function assigning omp_get_thread_num() to iterations[i] in the ith iteration of the for loop.
1. What is the default assignment of iterations on your system?
2. How are guided schedules determined?
3. Provide code and detailed screenshots showing how did you compile and run your code:
/* File: omp_trap3.c * Purpose: Estimate definite integral (or area under curve) using the *
trapezoidal rule. This version uses a parallel for directive * * Input: a, b, n * Output: estimate of
integral from a to b of f(x) * using n trapezoids. * * Compile: gcc -g -Wall -fopenmp -o
omp_trap3 omp_trap3.c * Usage: ./omp_trap3 * * Notes: * 1. The function f(x) is hardwired. * 2.
In this version, it's not necessary for n to be * evenly divisible by thread_count. * * IPP: Section
5.5 (pp. 224 and ff.) */ #include #include #include #include void Usage(char* prog_name);
double f(double x); /* Function we're integrating */ double Trap(double a, double b, int n, int
thread_count); int main(int argc, char* argv[]) { double global_result = 0.0; /* Store result in
global_result */ double a, b; /* Left and right endpoints */ int n; /* Total number of trapezoids */
int thread_count; if (argc != 2) Usage(argv[0]); thread_count = strtol(argv[1], NULL, 10);
printf("Enter a, b, and nn"); scanf("%lf %lf %d", &a, &b, &n); global_result = Trap(a, b, n,
thread_count); printf("With n = %d trapezoids, our estimaten", n); printf("of the integral from
%f to %f = %.14en", a, b, global_result); return 0; } /* main */ /*-------------------------------------
------------------------------- * Function: Usage * Purpose: Print command line for function and
terminate * In arg: prog_name */ void Usage(char* prog_name) { fprintf(stderr, "usage: %s n",
prog_name); exit(0); } /* Usage */ /*------------------------------------------------------------------ *
Function: f * Purpose: Compute value of function to be integrated * Input arg: x * Return val:
4. f(x) */ double f(double x) { double return_val; return_val = x*x; return return_val; } /* f */ /*----
-------------------------------------------------------------- * Function: Trap * Purpose: Use trapezoidal
rule to estimate definite integral * Input args: * a: left endpoint * b: right endpoint * n: number of
trapezoids * Return val: * approx: estimate of integral from a to b of f(x) */ double Trap(double
a, double b, int n, int thread_count) { double h, approx; int i; h = (b-a)/n; approx = (f(a) +
f(b))/2.0; # pragma omp parallel for num_threads(thread_count) reduction(+: approx) for (i = 1;
i <= n-1; i++) approx += f(a + i*h); approx = h*approx; return approx; } /* Trap */
Question4) The following code can be used as the basis of the implementation of merge sort: /
Sort elements of list from list [ lo ] to list [ hi ] / void Mergesort ( i n t list [ ] , i n t lo , i n t hi) {
i f ( lo < hi) { i n t mid = (lo + hi ) / 2 ; Mergesort ( list , lo , mid ) ; Mergesort ( list , mid +1 , hi )
; Merge ( list , lo , mid , hi ) ; } } / Mergesort / The Mergesort function can be called from the
following main function: i n t main ( i n t argc , char argv [ ] ) i n t list , n ; Get_args ( argc , argv
, &n ) ; list = malloc ( n s i z e o f ( i n t ) ) ; Get_list ( list , n ) ; Mergesort ( list , 0, n1); Print_list
( list , n ) ; free ( list ) ; return 0 ; } / main /
1. Use the task directive (and any other OpenMP directives) to implement a parallel merge sort
program and use appropriate synchronization mechanism.
2. Run your code with 2 , then 5, then 10 threads and check it always gives the correct results.
3. Then explain how your code works and what kind of synchronization you use and why?
Provide the code and detailed screenshots showing how did you compile and run your code.