Skip to content

Commit

Permalink
adding pi
Browse files Browse the repository at this point in the history
  • Loading branch information
weiguangcui committed Apr 6, 2024
1 parent 3f8e87a commit b0fd722
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 4 deletions.
3 changes: 2 additions & 1 deletion CodingTutorialC++/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
8 changes: 5 additions & 3 deletions CodingTutorialC++/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 $@

Expand Down
12 changes: 12 additions & 0 deletions CodingTutorialC++/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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:
Expand Down
62 changes: 62 additions & 0 deletions CodingTutorialC++/pi.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//
// Created by weiguang on 6/04/24.
//
#include <cmath>
#include <cstddef>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <cstdint>
#include <omp.h>
#include <ctime>
#include <iomanip>

#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<INFTY; k++) {
pi += std::pow(-1.0/3.0, (double)k) / (2.*(double)k+1.);
}
tend = time(NULL);

pi *= std::sqrt(12);
std::cerr << "pi = " << std::fixed << std::setprecision(12) << pi << " (time=" << std::difftime(tend,tstart) << " [sec])\n";

return 0;
}

0 comments on commit b0fd722

Please sign in to comment.