Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Parallelize the FOR loop that iterates over the frames in the fractal code using a cyclic assignment of iterations to threads. Follow these steps when

Parallelize the FOR loop that iterates over the frames in the fractal code using a cyclic assignment of iterations to threads. Follow these steps when writing the pthread code:

1. Include the pthread header file.

2. Add a new parameter at the end of the command line to specify the total number of threads and update the program usage message accordingly.

3. Make sure the number of threads is at least one.

4. Have the master thread print the number of requested threads.

5. Make all global variables and functions (other than main) static.

6. Turn the fractal function into a function that newly created threads can execute.

7. Create threads - 1 worker threads and have the master thread also compute part of the fractal, i.e., make the master thread call the same function that the new threads call (like it is done in the provided collatz code).

8. Parallelize the FOR loop that iterates over the frames using a cyclic assignment of iterations to threads.

9. Start the timer just before creating the worker threads and stop the timer just after joining the worker threads.

10. Only pass the rank to the function that runs in parallel and return NULL from it. All other information is passed to and from this function via global variables.

11. Convert the void pointer argument to an integer and do not use the argument otherwise.

12. Make sure your code runs correctly for different numbers of threads, i.e., it produces the same results as the serial code.

13. Free all dynamically allocated memory before normal program termination.

14. Do not allocate or free any dynamic memory in the timed code section.

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

//Given Code

#include #include #include #include "cs43805351.h"

static const double Delta = 0.004; static const double xMid = 0.2389; static const double yMid = 0.55267;

static void fractal(const int width, const int frames, unsigned char* pic) { // compute frames for (int frame = 0; frame < frames; frame++) { const double delta = Delta * pow(0.98, frame); const double xMin = xMid - delta; const double yMin = yMid - delta; const double dw = 2.0 * delta / width; for (int row = 0; row < width; row++) { const double cy = yMin + row * dw; for (int col = 0; col < width; col++) { const double cx = xMin + col * dw; double x = cx; double y = cy; int depth = 256; double x2, y2; do { x2 = x * x; y2 = y * y; y = 2 * x * y + cy; x = x2 - y2 + cx; depth--; } while ((depth > 0) && ((x2 + y2) < 5.0)); pic[frame * width * width + row * width + col] = (unsigned char)depth; } } } }

int main(int argc, char *argv[]) { printf("Fractal v1.7 ");

// check command line if (argc != 3) {fprintf(stderr, "usage: %s frame_width num_frames ", argv[0]); exit(-1);} const int width = atoi(argv[1]); if (width < 10) {fprintf(stderr, "error: frame_width must be at least 10 "); exit(-1);} const int frames = atoi(argv[2]); if (frames < 1) {fprintf(stderr, "error: num_frames must be at least 1 "); exit(-1);} printf("computing %d frames of %d by %d fractal ", frames, width, width);

// allocate picture array unsigned char* pic = new unsigned char[frames * width * width];

// start time timeval start, end; gettimeofday(&start, NULL);

fractal(width, frames, pic);

// end time gettimeofday(&end, NULL); const double runtime = end.tv_sec - start.tv_sec + (end.tv_usec - start.tv_usec) / 1000000.0; printf("compute time: %.3f s ", runtime);

// verify result by writing frames to BMP files if ((width <= 256) && (frames <= 100)) { for (int frame = 0; frame < frames; frame++) { char name[32]; sprintf(name, "fractal%d.bmp", frame + 1000); writeBMP(width, width, &pic[frame * width * width], name); } }

delete [] pic; return 0; }

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

Database Systems Design Implementation And Management

Authors: Peter Robb,Carlos Coronel

5th Edition

061906269X, 9780619062699

More Books

Students also viewed these Databases questions

Question

10. Are you a. a leader? b. a follower? _______

Answered: 1 week ago

Question

What did they do? What did they say?

Answered: 1 week ago