Introduction to OpenMP (25 points)

Overview

In this assignment, you will extend your previous Monte Carlo implementation for calculating π (pi.cc) to create a new program, autopiOMP.cc. This new program will automatically calculate π to some number of significant digits. You will measure the computational effort (iterations and time) required to reach a level of precision and output this data to a file.

Objective and Expected Learning Outcomes

The primary objectives of this assignment are:

Automate the Monte Carlo Method:

Extend your code to calculate π to different levels of precision automatically and accelerate with OpenMP.

Performance Measurement:

Measure and record the number of iterations and computation time required to achieve each precision level (you should have at least five levels, should be large enough that you see the benefit of parallel calculation). You can continue to use the chrono library, but you can also use the OpenMP timing infrastructure. With all performance measurements you should run your experiments multiple times to find an average.

// Record the start time using OpenMP's wall-clock timer
    double startTime = omp_get_wtime();

 // Example parallel work: calculate the sum of numbers from 0 to 999,999
    int sum = 0;
    #pragma omp parallel for reduction(+:sum)
    for (int i = 0; i < 1000000; ++i) {
        sum += i;
    }

 // Record the end time
    double endTime = omp_get_wtime();

NOTE: Potential reasons to use OpenMP infrastructure over chrono:

  • Seamless Integration: OpenMP’s timing functions are part of the parallel runtime, making them easy to use within parallel regions without mixing in other libraries.
  • Simplicity and Low Overhead: The API is very straightforward (call omp_get_wtime() before and after your code), and it’s designed to incur minimal overhead, which is especially useful for short, parallel tasks.
  • Consistency Across OpenMP Environments: Since omp_get_wtime() is part of the OpenMP standard, it provides consistent behavior across various platforms and compilers that support OpenMP.

Data Visualization:

Produce a plot from the CSV file illustrating the relationship between significant digits and computational effort, for a varying number of threads (e.g., 1, 2, 4, 8).

Build System Proficiency:

Create a Makefile to compile your C++ code efficiently for the new program that compiles the code into an executable named autopiOMP and includes a make all and make clean target. You will need to add CXXFLAGS=-fopenmp to your Makefile.

Expected Outcomes:

  1. Enhanced C++ Programming Skills:
    • Modify and extend existing code to add automated precision testing, data logging, and acceleration with OpenMP.
  2. Effective Data Collection:
    • Use your technique of choice to measure execution time for the various tasks accurately.
  3. Data Visualization:
    • Produce well-formatted plots that convey the outcomes of your experiments and clearly show the benefits of parallelism.
  4. Makefile Development:
    • Construct a Makefile to manage the build process and clean up build artifacts.

Instructions

Part 1: Extending the C++ Program

  1. Create a file named autopiOMP.cc by copying your previous code that calculated pi for several significant digits of the prior assignment to this repository and accelerate it with OpenMP.
  2. Output from autopiOMP.cc needs to create a CSV file named autopiOMP.csv with the following formatting:
 Precision, Iterations, Thread, Time, Pi
 1, <iterations>, <thread>, <time>, <pi value>
 2, <iterations>, <thread>, <time>, <pi value>
 ...
 8, <iterations>, <thread>, <time>, <pi value>
  1. Include proper error handling and in-code documentation.

Part 2: Makefile

  1. Create a Makefile that:
    • Compiles autopiOMP.cc into an executable named autopiOMP.
    • Includes make all and make clean targets that build’s executable and remove build artifacts and executables, respectively.
  2. Test the Makefile on the designated systems to ensure it works correctly.

Part 3: Experiment Analysis and Visualization

  1. Create a visualization (graph) that clearly illustrates the relationship between the number of OpenMP threads and the computation time required to determine the number of significant digits. The graph should present a single, easy-to-interpret view of this relationship. You may use any technology of your choice to generate the visualization.
  2. Follow best practices for data visualization to ensure clarity and readability.

Submission Guidelines and Evaluation Criteria

  • Add and/or edit the following files in your GitHub repository:
    • autopiOMP.cc - code for assignment
    • Makefile - makefile to build code
    • autopiOMP.csv - data collected as part of the experiment (all data, not just averages)
    • autopiOMP.png - graph of results
    • autopiOMP.txt - reflection text
  • Include in your submission a brief reflection (autopiOMP.txt) that recounts your experience with the assignment. In your reflection, describe how you approached writing the C++ program with OpenMP to estimate π using the Monte Carlo method, your design decisions, and any challenges you encountered along the way—including any difficulties in measuring performance or generating the expected speedup graph. Discuss whether the experimental outcomes aligned with your expectations and, if not, explore what factors might have influenced the differences. Share the lessons you learned from this process and how these insights might shape your future work in parallel programming.
  • When your program is ready for grading, commit and push your local repository to the remote git classroom repository following the Assignment Submission Instructions.
  • Your work will be evaluated on your adherence to instructions, code quality, file naming, completeness, and your reflection on the assignment.

Additional Resources