Question
The program shubert.c demonstrates the use of the Shubert function, which is a test case for optimization algorithms (see Appendix). Using two processes and the
The program shubert.c demonstrates the use of the Shubert function, which is a test case for optimization algorithms (see Appendix). Using two processes and the communication methods below, print the minimum values (local and global) of the Shubert function for -2.0 x1,x2 2.0 and the run time of the parent/server. Use a step size for both loops to yield a run time of at least 1 second. Compare your run time to that of the sample program using the same step size. Each process should perform an equal amount of work to find a local minimum in its range. For example, one process can do half of the x1 values (-2.0 to 0.0), and the other process can to the other half. You dont need to modify the other loop. The parent process can determine which of the two local minima is the least and print it as the global minimum. Include the output in your report for each of the three programs and the sample program (shubert.c) using appropriate step size. Below is an example of output for one program:
Total time was 10.65 seconds. Local min = -62.12412 Local min = -186.73064 Global min = -186.73064
Create a single program that uses fork() and shared memory that performs the same function as Schubert.c and gives the same output.
Tip: you can create shared memory with a value of type double and access the value using the dereference operator *. The program shared_double.c is provided as an example.
Schubert.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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started