Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this program, we will be demonstrating the speedup that you can achieve by performing certain functions in parallel. Your program should begin by reading

In this program, we will be demonstrating the speedup that you can achieve by performing certain functions in parallel. Your program should begin by reading in a range from the supplied range.txt file. This file will be formatted to have a lower bounds on the first line and an upper bounds on line 2. The program should include a menu that allows users to choose from two different methods of executing your program, using standard sequential summation and by using parallel summation. Once the user has selected a summation method, your program should begin a timer, using the supplied "timer.cpp" file below and call a function that will return your summation. Begin by finding all prime numbers that occur between the lower and upper bounds. As you loop through the number range, if the number is prime it should be added to an aggregator, otherwise it should be ignored. Once you have reached the upper limit, return the aggregator value to the main program and terminate the timer. Your program should output both the sum you calculated as well as the time necessary to accomplish the job. PRIME NUMBERS A number is considered prime if it is greater than 1 and has no factors between 2 and the square root of the number rounded up, inclusive. To accomplish this, your program should #include the cmath library to access the sqrt() function. FUNCTIONS Your header file (functions.h) should contain all necessary include files for your functions.cpp and parallel_lab.cpp source files. The header file should also contain all the necessary function prototypes. ISPRIME FUNCTION bool isPrime(int); Given an integer n, returns true if n is prime and false if n is not prime. A number is considered prime if it is greater than 1 and has no factors between 2 and the square root of the number rounded up, inclusive. To accomplish this, your program should #include the cmath library to access the sqrt() function. PARALLEL_NUM_PRIMES FUNCTION int parallel_num_primes(int, int); Given integers lower_bound and upper_bound, returns the number of primes in that range inclusive. Print message to user before calculation that tells the user that you are finding the number of primes between the lower & upper bound...and make sure to print out the actual numbers of the lower & upper bound. Calculation will be done in parallel. NOTE: must place the code (in red) below BEFORE the for loop that goes through the upper & lower bounds. #pragma omp parallel for reduction (+:counter) SEQUENTIAL_NUM_PRIMES FUNCTION int sequential_num_primes(int, int); Given integers lower_bound and upper_bound, returns the number of primes in that range inclusive. Print message to user before calculation that tells the user that you are finding the number of primes between the lower & upper bound...and make sure to print out the actual numbers of the lower & upper bound. Calculation is done sequentially. DISPLAYMENUGETCHOICE FUNCTION int displayMenuGetChoice(); Displays a menu and returns the choice of the user. Validates input! MAIN FUNCTION 1. Open input file called range.txt (only continue program if the file can be found). Read in two integers....the first is the lower bound and the second is the upper bound. Display a menu to the user with the following choices, get the users choice & validate the users choice. You will loop this program until the user chooses #3 (to quit). If the user chooses #1 then Start the timer (use Timer.h, Timer.cpp to help with this which is provided for you) Call the sequential_num_primes function and get the number of primes End the timer Get the total time If the user chooses #2 then Start the timer Call the parallel_num_primes function and get the number of primes End the timer Get the total time Display the number of primes & the total time like the sample output below: TIMER You have been provided with the library file Timer.h and the source file Timer.cpp. Use these files in the creation of your program. You will need to #include this library in your parallel_lab.cpp. Timer.h contains functions for capturing the system time as well as for getting the total elapsed time in seconds between two time values. COMPILING WITH OPENMP CODE g++ -fopenmp I ./ functions.cpp parallel_lab.cpp Timer.cpp o parallel_lab SAMPLE OUTPUT The sample output is on the next page. If you make your range in range.txt like mine below (1 to 10000000), then you should have similar run times to my sample output. If you do not, then it is highly possible that your isPrime function is not efficient. range.txt 1 10000000

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

=+1 Is the decision fair to employees?

Answered: 1 week ago