SlideShare a Scribd company logo
1 of 4
Download to read offline
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
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
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:
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.

More Related Content

Similar to please help me with this and explain in details also in the first qu.pdf

C aptitude questions
C aptitude questionsC aptitude questions
C aptitude questionsSrikanth
 
C - aptitude3
C - aptitude3C - aptitude3
C - aptitude3Srikanth
 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial javaTpoint s
 
Functions And Header Files In C++ | Bjarne stroustrup
Functions And Header Files In C++ | Bjarne stroustrupFunctions And Header Files In C++ | Bjarne stroustrup
Functions And Header Files In C++ | Bjarne stroustrupSyedHaroonShah4
 
Best C++ Programming Homework Help
Best C++ Programming Homework HelpBest C++ Programming Homework Help
Best C++ Programming Homework HelpC++ Homework Help
 
Router Queue Simulation in C++ in MMNN and MM1 conditions
Router Queue Simulation in C++ in MMNN and MM1 conditionsRouter Queue Simulation in C++ in MMNN and MM1 conditions
Router Queue Simulation in C++ in MMNN and MM1 conditionsMorteza Mahdilar
 
golang_getting_started.pptx
golang_getting_started.pptxgolang_getting_started.pptx
golang_getting_started.pptxGuy Komari
 
C++ Programming Homework Help
C++ Programming Homework HelpC++ Programming Homework Help
C++ Programming Homework HelpC++ Homework Help
 
httplinux.die.netman3execfork() creates a new process by.docx
httplinux.die.netman3execfork() creates a new process by.docxhttplinux.die.netman3execfork() creates a new process by.docx
httplinux.die.netman3execfork() creates a new process by.docxadampcarr67227
 
Please do Part A, Ill be really gratefulThe main.c is the skeleto.pdf
Please do Part A, Ill be really gratefulThe main.c is the skeleto.pdfPlease do Part A, Ill be really gratefulThe main.c is the skeleto.pdf
Please do Part A, Ill be really gratefulThe main.c is the skeleto.pdfaioils
 
Lab Assignment 4 CSE330 Spring 2014 Skeleton Code for ex.docx
 Lab Assignment 4 CSE330 Spring 2014  Skeleton Code for ex.docx Lab Assignment 4 CSE330 Spring 2014  Skeleton Code for ex.docx
Lab Assignment 4 CSE330 Spring 2014 Skeleton Code for ex.docxMARRY7
 
Merge Sort implementation in C++ The implementation for Mergesort gi.pdf
Merge Sort implementation in C++ The implementation for Mergesort gi.pdfMerge Sort implementation in C++ The implementation for Mergesort gi.pdf
Merge Sort implementation in C++ The implementation for Mergesort gi.pdfmdameer02
 
write the To Dos to get the exact outputNOte A valid Fraction .pdf
write the To Dos to get the exact outputNOte A valid Fraction .pdfwrite the To Dos to get the exact outputNOte A valid Fraction .pdf
write the To Dos to get the exact outputNOte A valid Fraction .pdfjyothimuppasani1
 
function in in thi pdf you will learn what is fu...
function in  in thi pdf you will learn   what                           is fu...function in  in thi pdf you will learn   what                           is fu...
function in in thi pdf you will learn what is fu...kushwahashivam413
 
2.1 ### uVision Project, (C) Keil Software .docx
2.1   ### uVision Project, (C) Keil Software    .docx2.1   ### uVision Project, (C) Keil Software    .docx
2.1 ### uVision Project, (C) Keil Software .docxtarifarmarie
 

Similar to please help me with this and explain in details also in the first qu.pdf (20)

C Programming Homework Help
C Programming Homework HelpC Programming Homework Help
C Programming Homework Help
 
ch08.ppt
ch08.pptch08.ppt
ch08.ppt
 
C aptitude questions
C aptitude questionsC aptitude questions
C aptitude questions
 
C - aptitude3
C - aptitude3C - aptitude3
C - aptitude3
 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial
 
Functions And Header Files In C++ | Bjarne stroustrup
Functions And Header Files In C++ | Bjarne stroustrupFunctions And Header Files In C++ | Bjarne stroustrup
Functions And Header Files In C++ | Bjarne stroustrup
 
Best C++ Programming Homework Help
Best C++ Programming Homework HelpBest C++ Programming Homework Help
Best C++ Programming Homework Help
 
Router Queue Simulation in C++ in MMNN and MM1 conditions
Router Queue Simulation in C++ in MMNN and MM1 conditionsRouter Queue Simulation in C++ in MMNN and MM1 conditions
Router Queue Simulation in C++ in MMNN and MM1 conditions
 
golang_getting_started.pptx
golang_getting_started.pptxgolang_getting_started.pptx
golang_getting_started.pptx
 
C++_notes.pdf
C++_notes.pdfC++_notes.pdf
C++_notes.pdf
 
C++ Programming Homework Help
C++ Programming Homework HelpC++ Programming Homework Help
C++ Programming Homework Help
 
httplinux.die.netman3execfork() creates a new process by.docx
httplinux.die.netman3execfork() creates a new process by.docxhttplinux.die.netman3execfork() creates a new process by.docx
httplinux.die.netman3execfork() creates a new process by.docx
 
Please do Part A, Ill be really gratefulThe main.c is the skeleto.pdf
Please do Part A, Ill be really gratefulThe main.c is the skeleto.pdfPlease do Part A, Ill be really gratefulThe main.c is the skeleto.pdf
Please do Part A, Ill be really gratefulThe main.c is the skeleto.pdf
 
Lab Assignment 4 CSE330 Spring 2014 Skeleton Code for ex.docx
 Lab Assignment 4 CSE330 Spring 2014  Skeleton Code for ex.docx Lab Assignment 4 CSE330 Spring 2014  Skeleton Code for ex.docx
Lab Assignment 4 CSE330 Spring 2014 Skeleton Code for ex.docx
 
Merge Sort implementation in C++ The implementation for Mergesort gi.pdf
Merge Sort implementation in C++ The implementation for Mergesort gi.pdfMerge Sort implementation in C++ The implementation for Mergesort gi.pdf
Merge Sort implementation in C++ The implementation for Mergesort gi.pdf
 
Computer Network Assignment Help
Computer Network Assignment HelpComputer Network Assignment Help
Computer Network Assignment Help
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
write the To Dos to get the exact outputNOte A valid Fraction .pdf
write the To Dos to get the exact outputNOte A valid Fraction .pdfwrite the To Dos to get the exact outputNOte A valid Fraction .pdf
write the To Dos to get the exact outputNOte A valid Fraction .pdf
 
function in in thi pdf you will learn what is fu...
function in  in thi pdf you will learn   what                           is fu...function in  in thi pdf you will learn   what                           is fu...
function in in thi pdf you will learn what is fu...
 
2.1 ### uVision Project, (C) Keil Software .docx
2.1   ### uVision Project, (C) Keil Software    .docx2.1   ### uVision Project, (C) Keil Software    .docx
2.1 ### uVision Project, (C) Keil Software .docx
 

More from newfaransportsfitnes

Please respond to the following in a substantive post (3�4 paragraph.pdf
Please respond to the following in a substantive post (3�4 paragraph.pdfPlease respond to the following in a substantive post (3�4 paragraph.pdf
Please respond to the following in a substantive post (3�4 paragraph.pdfnewfaransportsfitnes
 
Please watch the Executive Training Video on Emotional Intelligence..pdf
Please watch the Executive Training Video on Emotional Intelligence..pdfPlease watch the Executive Training Video on Emotional Intelligence..pdf
Please watch the Executive Training Video on Emotional Intelligence..pdfnewfaransportsfitnes
 
Please provide both parts for this problem and do not copy paste f.pdf
Please provide both parts for this problem and do not copy paste f.pdfPlease provide both parts for this problem and do not copy paste f.pdf
Please provide both parts for this problem and do not copy paste f.pdfnewfaransportsfitnes
 
Please help. Thank you very much Stop the Database, Stop the Liste.pdf
Please help. Thank you very much Stop the Database, Stop the Liste.pdfPlease help. Thank you very much Stop the Database, Stop the Liste.pdf
Please help. Thank you very much Stop the Database, Stop the Liste.pdfnewfaransportsfitnes
 
Please help! thank you ) At May 1,2020 , Sunland Company had beginn.pdf
Please help! thank you ) At May 1,2020 , Sunland Company had beginn.pdfPlease help! thank you ) At May 1,2020 , Sunland Company had beginn.pdf
Please help! thank you ) At May 1,2020 , Sunland Company had beginn.pdfnewfaransportsfitnes
 
Please help me with this�..Due this Saturday (107) Press Esc to exi.pdf
Please help me with this�..Due this Saturday (107) Press Esc to exi.pdfPlease help me with this�..Due this Saturday (107) Press Esc to exi.pdf
Please help me with this�..Due this Saturday (107) Press Esc to exi.pdfnewfaransportsfitnes
 
Please help me answer these 3 multiple choice questions 1. Neil.pdf
Please help me answer these 3 multiple choice questions 1. Neil.pdfPlease help me answer these 3 multiple choice questions 1. Neil.pdf
Please help me answer these 3 multiple choice questions 1. Neil.pdfnewfaransportsfitnes
 
Please help me answer these four multiple choice questionsa) pa.pdf
Please help me answer these four multiple choice questionsa) pa.pdfPlease help me answer these four multiple choice questionsa) pa.pdf
Please help me answer these four multiple choice questionsa) pa.pdfnewfaransportsfitnes
 
Please help asap and in advance thank you very much.Create a bookl.pdf
Please help asap and in advance thank you very much.Create a bookl.pdfPlease help asap and in advance thank you very much.Create a bookl.pdf
Please help asap and in advance thank you very much.Create a bookl.pdfnewfaransportsfitnes
 
Please help me with this. Income Statement f.pdf
Please help me with this.  Income Statement f.pdfPlease help me with this.  Income Statement f.pdf
Please help me with this. Income Statement f.pdfnewfaransportsfitnes
 
Primary Resource �Fiji�s new budget Balancing consolidation and su.pdf
Primary Resource �Fiji�s new budget Balancing consolidation and su.pdfPrimary Resource �Fiji�s new budget Balancing consolidation and su.pdf
Primary Resource �Fiji�s new budget Balancing consolidation and su.pdfnewfaransportsfitnes
 
Price(Dollars per unit)Quantity Demanded(Units)Refer t.pdf
Price(Dollars per unit)Quantity Demanded(Units)Refer t.pdfPrice(Dollars per unit)Quantity Demanded(Units)Refer t.pdf
Price(Dollars per unit)Quantity Demanded(Units)Refer t.pdfnewfaransportsfitnes
 

More from newfaransportsfitnes (12)

Please respond to the following in a substantive post (3�4 paragraph.pdf
Please respond to the following in a substantive post (3�4 paragraph.pdfPlease respond to the following in a substantive post (3�4 paragraph.pdf
Please respond to the following in a substantive post (3�4 paragraph.pdf
 
Please watch the Executive Training Video on Emotional Intelligence..pdf
Please watch the Executive Training Video on Emotional Intelligence..pdfPlease watch the Executive Training Video on Emotional Intelligence..pdf
Please watch the Executive Training Video on Emotional Intelligence..pdf
 
Please provide both parts for this problem and do not copy paste f.pdf
Please provide both parts for this problem and do not copy paste f.pdfPlease provide both parts for this problem and do not copy paste f.pdf
Please provide both parts for this problem and do not copy paste f.pdf
 
Please help. Thank you very much Stop the Database, Stop the Liste.pdf
Please help. Thank you very much Stop the Database, Stop the Liste.pdfPlease help. Thank you very much Stop the Database, Stop the Liste.pdf
Please help. Thank you very much Stop the Database, Stop the Liste.pdf
 
Please help! thank you ) At May 1,2020 , Sunland Company had beginn.pdf
Please help! thank you ) At May 1,2020 , Sunland Company had beginn.pdfPlease help! thank you ) At May 1,2020 , Sunland Company had beginn.pdf
Please help! thank you ) At May 1,2020 , Sunland Company had beginn.pdf
 
Please help me with this�..Due this Saturday (107) Press Esc to exi.pdf
Please help me with this�..Due this Saturday (107) Press Esc to exi.pdfPlease help me with this�..Due this Saturday (107) Press Esc to exi.pdf
Please help me with this�..Due this Saturday (107) Press Esc to exi.pdf
 
Please help me answer these 3 multiple choice questions 1. Neil.pdf
Please help me answer these 3 multiple choice questions 1. Neil.pdfPlease help me answer these 3 multiple choice questions 1. Neil.pdf
Please help me answer these 3 multiple choice questions 1. Neil.pdf
 
Please help me answer these four multiple choice questionsa) pa.pdf
Please help me answer these four multiple choice questionsa) pa.pdfPlease help me answer these four multiple choice questionsa) pa.pdf
Please help me answer these four multiple choice questionsa) pa.pdf
 
Please help asap and in advance thank you very much.Create a bookl.pdf
Please help asap and in advance thank you very much.Create a bookl.pdfPlease help asap and in advance thank you very much.Create a bookl.pdf
Please help asap and in advance thank you very much.Create a bookl.pdf
 
Please help me with this. Income Statement f.pdf
Please help me with this.  Income Statement f.pdfPlease help me with this.  Income Statement f.pdf
Please help me with this. Income Statement f.pdf
 
Primary Resource �Fiji�s new budget Balancing consolidation and su.pdf
Primary Resource �Fiji�s new budget Balancing consolidation and su.pdfPrimary Resource �Fiji�s new budget Balancing consolidation and su.pdf
Primary Resource �Fiji�s new budget Balancing consolidation and su.pdf
 
Price(Dollars per unit)Quantity Demanded(Units)Refer t.pdf
Price(Dollars per unit)Quantity Demanded(Units)Refer t.pdfPrice(Dollars per unit)Quantity Demanded(Units)Refer t.pdf
Price(Dollars per unit)Quantity Demanded(Units)Refer t.pdf
 

Recently uploaded

Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 

Recently uploaded (20)

OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
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.