Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Repeat problem #1 above using 4 P-threads for the computations(This is the question) Problem #1: Write a C program that uses 4 P-threads to speed

Repeat problem #1 above using 4 P-threads for the computations(This is the question)

Problem #1: Write a C program that uses 4 P-threads to speed up an algorithm of your choice, such as finding the Shubert function minimum. In your report, explain the algorithm you chose and whether you are using public sample code. Each of n threads should perform 1/nth of the work. Note that you may wish to pass an argument to a thread function, and the textbooks sample code for P-threads demonstrates this. Print the local result for each thread, the overall (global) result, and the run time of the parent/server. Configure the settings such that the overall run time is at least 1 second. You may divide the work between the parent thread and 1 child thread or between 2 child threads. Include the output in your report. Below is an example of output for finding the Shubert minimum: Total time was 10.65 seconds. Local min = -62.12412 Local min = -186.73064 Global min = -186.73064

shubert.c

# include # include # include # include

// Prototype for computation double shubert(double x1, double x2);

int main () { double x1, x2, y, min = 0; struct timeval start_time, stop_time, elapsed_time; // timers gettimeofday(&start_time,NULL); // Unix timer // Loops for (x1 = -2; x1 <= 2; x1 += 0.5) { // x1 parameter of Shubert function for (x2 = -2; x2 <= 2; x2 += 0.5) { // x2 parameter of Shubert function y = shubert(x1, x2); // Evaluate Shubert function defined below printf("%.2f ", y); // Print value for demonstration purposes if (y < min) // Check for global minimum min = y; } printf(" "); // Print on next row } gettimeofday(&stop_time,NULL); timersub(&stop_time, &start_time, &elapsed_time); // Unix time subtract routine printf("Total time was %0.2f seconds. ", elapsed_time.tv_sec+elapsed_time.tv_usec/1000000.0); printf("minimum = %0.2f ", min); }

// Compute Shubert value for x1,x2 combination double shubert(double x1, double x2) { double sum1 = 0; double sum2 = 0; int i; for (i = 1; i <= 5; i++) { sum1 += i * cos((i + 1) * x1 + i); sum2 += i * cos((i + 1) * x2 + i); } return sum1 * sum2; }

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

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

How autonomous should the target be left after the merger deal?

Answered: 1 week ago