From b0fd7226739440062e40e61f7f03ceed533d53c1 Mon Sep 17 00:00:00 2001 From: weiguang cui Date: Sat, 6 Apr 2024 12:32:31 +0200 Subject: [PATCH] adding pi --- CodingTutorialC++/CMakeLists.txt | 3 +- CodingTutorialC++/Makefile | 8 +++-- CodingTutorialC++/README.md | 12 +++++++ CodingTutorialC++/pi.cpp | 62 ++++++++++++++++++++++++++++++++ 4 files changed, 81 insertions(+), 4 deletions(-) create mode 100644 CodingTutorialC++/pi.cpp diff --git a/CodingTutorialC++/CMakeLists.txt b/CodingTutorialC++/CMakeLists.txt index 2453ff2..e08ec15 100644 --- a/CodingTutorialC++/CMakeLists.txt +++ b/CodingTutorialC++/CMakeLists.txt @@ -5,4 +5,5 @@ set(CMAKE_CXX_STANDARD 11) add_executable(ACO_practice overflow.cpp MainArgs.cpp - openmp-example.cpp) + openmp-example.cpp + pi.cpp) diff --git a/CodingTutorialC++/Makefile b/CodingTutorialC++/Makefile index 1e5b53e..54c1f63 100644 --- a/CodingTutorialC++/Makefile +++ b/CodingTutorialC++/Makefile @@ -8,19 +8,18 @@ CXXFLAGS = -std=c++11 -Wall -fopenmp TARGET = ACO_practice # Source files -SRCS = overflow.cpp MainArgs.cpp openmp-example.cpp +SRCS = overflow.cpp MainArgs.cpp openmp-example.cpp pi.cpp # Object files OBJS = $(SRCS:.cpp=.o) # Executables -EXECS = overflow MainArgs openmp-example +EXECS = overflow MainArgs openmp-example pi OMP_NUM_THREADS=4 export OMP_NUM_THREADS all: $(TARGET) - ./openmp-example overflow: overflow.o $(CXX) $(CXXFLAGS) -o overflow overflow.o @@ -31,6 +30,9 @@ MainArgs: MainArgs.o openmp-example: openmp-example.o $(CXX) $(CXXFLAGS) -o openmp-example openmp-example.o +pi: pi.o + $(CXX) $(CXXFLAGS) -o pi pi.o + .cpp.o: $(CXX) $(CXXFLAGS) -c $< -o $@ diff --git a/CodingTutorialC++/README.md b/CodingTutorialC++/README.md index 2433003..26cf343 100644 --- a/CodingTutorialC++/README.md +++ b/CodingTutorialC++/README.md @@ -16,6 +16,16 @@ This file contains a program that prints the name of the program and all the arg This file contains a program that uses OpenMP to calculate the sum of numbers from 1 to 1e5. It sets the number of threads to the number of processors available, and each thread calculates a portion of the sum. +### [`pi.cpp`](./pi.cpp) + +This file contains a program that calculates the value of pi using the Leibniz formula for π. The program uses OpenMP for parallel computation, allowing it to calculate pi to a high degree of precision in a shorter amount of time. The number of threads used for the computation can be specified as a command-line argument when running the program. +To build and run `pi.cpp`, use the make command followed by the name of the file (without the .cpp extension), and then run the resulting executable with the number of threads as an argument: + +```bash +make pi +./pi 4 This will build `pi.cpp` and run the resulting `pi` executable with 4 threads. +``` + ## Build Files ### [`Makefile`](./Makefile) @@ -38,6 +48,8 @@ To build individual files, use the make command followed by the name of the file ```bash make overflow make MainArgs +make openmp-example +make pi ``` To clean up the built files, use the make clean command: diff --git a/CodingTutorialC++/pi.cpp b/CodingTutorialC++/pi.cpp new file mode 100644 index 0000000..cdbcfe8 --- /dev/null +++ b/CodingTutorialC++/pi.cpp @@ -0,0 +1,62 @@ +// +// Created by weiguang on 6/04/24. +// +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define INFTY ((uint64_t)1<<30) + +int main(int argc, char **argv) +{ + uint64_t k; + double pi; + time_t tstart, tend; + int nthreads_to_be_used; + + if(argc != 2) { + std::cerr << "usage: ./pi NTHREADS\nABORTING\n"; + return 1; + } + + nthreads_to_be_used = std::atoi(argv[1]); + std::cerr << "using " << nthreads_to_be_used << " threads for execution\n"; + omp_set_num_threads(nthreads_to_be_used); + +#pragma omp parallel shared(std::cerr) default(none) + { + // all private by construction + int nthreads, ncpus, ithread, omp_num_threads; + + // who am I? + ithread = omp_get_thread_num(); + + /* only one thread (==0) does this */ + if (ithread == 0) + { + ncpus = omp_get_num_procs(); + nthreads = omp_get_num_threads(); + std::cerr << "ncpus=" << ncpus << " nthreads=" << nthreads << "\n"; + } + } + + pi = 0.0; + + tstart = time(NULL); +#pragma omp parallel for default(none) private(k) reduction(+:pi) schedule(static) + for(k=0; k