pagp-lab 1.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pagp_lab-1.0.0/LICENSE +21 -0
- pagp_lab-1.0.0/PKG-INFO +51 -0
- pagp_lab-1.0.0/README.md +36 -0
- pagp_lab-1.0.0/pyproject.toml +35 -0
- pagp_lab-1.0.0/setup.cfg +4 -0
- pagp_lab-1.0.0/src/pagp_lab/__init__.py +3 -0
- pagp_lab-1.0.0/src/pagp_lab/cli.py +47 -0
- pagp_lab-1.0.0/src/pagp_lab/core.py +67 -0
- pagp_lab-1.0.0/src/pagp_lab/cpp/pg1.cpp +35 -0
- pagp_lab-1.0.0/src/pagp_lab/cpp/pg1b.cpp +45 -0
- pagp_lab-1.0.0/src/pagp_lab.egg-info/PKG-INFO +51 -0
- pagp_lab-1.0.0/src/pagp_lab.egg-info/SOURCES.txt +14 -0
- pagp_lab-1.0.0/src/pagp_lab.egg-info/dependency_links.txt +1 -0
- pagp_lab-1.0.0/src/pagp_lab.egg-info/entry_points.txt +4 -0
- pagp_lab-1.0.0/src/pagp_lab.egg-info/top_level.txt +1 -0
- pagp_lab-1.0.0/tests/test_core.py +15 -0
pagp_lab-1.0.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Shreya Prasad
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
pagp_lab-1.0.0/PKG-INFO
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pagp-lab
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Cross-platform pip package for the PAGP Monte Carlo Pi programs
|
|
5
|
+
Author: Shreya Prasad
|
|
6
|
+
License: MIT
|
|
7
|
+
Classifier: Programming Language :: Python :: 3
|
|
8
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
9
|
+
Classifier: Operating System :: OS Independent
|
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
+
Requires-Python: >=3.8
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Dynamic: license-file
|
|
15
|
+
|
|
16
|
+
# pagp-lab
|
|
17
|
+
|
|
18
|
+
`pagp-lab` is a pip package containing the two PAGP Monte Carlo Pi programs.
|
|
19
|
+
It provides deterministic cross-platform implementations, so a compiler or MPI
|
|
20
|
+
installation is not required.
|
|
21
|
+
|
|
22
|
+
## Install
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
pip install pagp-lab
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Usage
|
|
29
|
+
|
|
30
|
+
Run the OpenMP-style threaded implementation:
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
pagp-pi-openmp --threads 4
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Run the MPI-style process implementation:
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
pagp-pi-mpi --processes 4
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
The original C++ sources are also included in the installed package.
|
|
43
|
+
|
|
44
|
+
You can alternatively use the combined command:
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
pagp-lab openmp --threads 4
|
|
48
|
+
pagp-lab mpi --processes 4
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Use `--trials` to change the default of 10,000 trials.
|
pagp_lab-1.0.0/README.md
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# pagp-lab
|
|
2
|
+
|
|
3
|
+
`pagp-lab` is a pip package containing the two PAGP Monte Carlo Pi programs.
|
|
4
|
+
It provides deterministic cross-platform implementations, so a compiler or MPI
|
|
5
|
+
installation is not required.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pip install pagp-lab
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
Run the OpenMP-style threaded implementation:
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
pagp-pi-openmp --threads 4
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Run the MPI-style process implementation:
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
pagp-pi-mpi --processes 4
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
The original C++ sources are also included in the installed package.
|
|
28
|
+
|
|
29
|
+
You can alternatively use the combined command:
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
pagp-lab openmp --threads 4
|
|
33
|
+
pagp-lab mpi --processes 4
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Use `--trials` to change the default of 10,000 trials.
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "pagp-lab"
|
|
7
|
+
version = "1.0.0"
|
|
8
|
+
description = "Cross-platform pip package for the PAGP Monte Carlo Pi programs"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.8"
|
|
11
|
+
license = {text = "MIT"}
|
|
12
|
+
authors = [
|
|
13
|
+
{name = "Shreya Prasad"}
|
|
14
|
+
]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Programming Language :: Python :: 3",
|
|
17
|
+
"Programming Language :: Python :: 3.8",
|
|
18
|
+
"Operating System :: OS Independent",
|
|
19
|
+
"License :: OSI Approved :: MIT License",
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
[project.scripts]
|
|
23
|
+
pagp-lab = "pagp_lab.cli:main"
|
|
24
|
+
pagp-pi-openmp = "pagp_lab.cli:openmp_main"
|
|
25
|
+
pagp-pi-mpi = "pagp_lab.cli:mpi_main"
|
|
26
|
+
|
|
27
|
+
[tool.setuptools]
|
|
28
|
+
package-dir = {"" = "src"}
|
|
29
|
+
include-package-data = true
|
|
30
|
+
|
|
31
|
+
[tool.setuptools.packages.find]
|
|
32
|
+
where = ["src"]
|
|
33
|
+
|
|
34
|
+
[tool.setuptools.package-data]
|
|
35
|
+
pagp_lab = ["cpp/*.cpp"]
|
pagp_lab-1.0.0/setup.cfg
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"""Command-line interface for pagp-lab."""
|
|
2
|
+
|
|
3
|
+
import argparse
|
|
4
|
+
|
|
5
|
+
from .core import DEFAULT_TRIALS, run_mpi_style, run_openmp_style
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def print_result(result, worker_label):
|
|
9
|
+
print(f"Time: {result['time']:.6f}")
|
|
10
|
+
print(f"no. of {worker_label}: {result['threads']}")
|
|
11
|
+
print(f"no. of trials: {result['trials']}")
|
|
12
|
+
print(f"est. of pi: {result['pi']:.12f}")
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def openmp_main():
|
|
16
|
+
parser = argparse.ArgumentParser(description="Monte Carlo Pi (OpenMP-style)")
|
|
17
|
+
parser.add_argument("--threads", "-t", type=int, default=1)
|
|
18
|
+
parser.add_argument("--trials", "-n", type=int, default=DEFAULT_TRIALS)
|
|
19
|
+
args = parser.parse_args()
|
|
20
|
+
print_result(run_openmp_style(args.threads, args.trials), "threads")
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def mpi_main():
|
|
24
|
+
parser = argparse.ArgumentParser(description="Monte Carlo Pi (MPI-style)")
|
|
25
|
+
parser.add_argument("--processes", "-p", type=int, default=1)
|
|
26
|
+
parser.add_argument("--trials", "-n", type=int, default=DEFAULT_TRIALS)
|
|
27
|
+
args = parser.parse_args()
|
|
28
|
+
print_result(run_mpi_style(args.processes, args.trials), "processes")
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def main():
|
|
32
|
+
parser = argparse.ArgumentParser(prog="pagp-lab")
|
|
33
|
+
subparsers = parser.add_subparsers(dest="program", required=True)
|
|
34
|
+
|
|
35
|
+
openmp = subparsers.add_parser("openmp", help="Run the OpenMP-style program")
|
|
36
|
+
openmp.add_argument("--threads", "-t", type=int, default=1)
|
|
37
|
+
openmp.add_argument("--trials", "-n", type=int, default=DEFAULT_TRIALS)
|
|
38
|
+
|
|
39
|
+
mpi = subparsers.add_parser("mpi", help="Run the MPI-style program")
|
|
40
|
+
mpi.add_argument("--processes", "-p", type=int, default=1)
|
|
41
|
+
mpi.add_argument("--trials", "-n", type=int, default=DEFAULT_TRIALS)
|
|
42
|
+
|
|
43
|
+
args = parser.parse_args()
|
|
44
|
+
if args.program == "openmp":
|
|
45
|
+
print_result(run_openmp_style(args.threads, args.trials), "threads")
|
|
46
|
+
else:
|
|
47
|
+
print_result(run_mpi_style(args.processes, args.trials), "processes")
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"""Deterministic implementations equivalent to the Monte Carlo programs."""
|
|
2
|
+
|
|
3
|
+
from concurrent.futures import ProcessPoolExecutor
|
|
4
|
+
import random
|
|
5
|
+
import time
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
SEED = 3655942
|
|
9
|
+
DEFAULT_TRIALS = 10_000
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def count_hits(trials: int, seed: int) -> int:
|
|
13
|
+
rng = random.Random(seed)
|
|
14
|
+
count = 0
|
|
15
|
+
for _ in range(trials):
|
|
16
|
+
x = rng.random()
|
|
17
|
+
y = rng.random()
|
|
18
|
+
z = x * x + y * y
|
|
19
|
+
if z <= 1.0:
|
|
20
|
+
count += 1
|
|
21
|
+
return count
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def run_openmp_style(threads: int, trials: int = DEFAULT_TRIALS):
|
|
25
|
+
if threads < 1:
|
|
26
|
+
raise ValueError("threads must be at least 1")
|
|
27
|
+
if trials < 1:
|
|
28
|
+
raise ValueError("trials must be at least 1")
|
|
29
|
+
|
|
30
|
+
sizes = [trials // threads + (1 if index < trials % threads else 0)
|
|
31
|
+
for index in range(threads)]
|
|
32
|
+
start = time.perf_counter()
|
|
33
|
+
with ProcessPoolExecutor(max_workers=threads) as executor:
|
|
34
|
+
counts = executor.map(
|
|
35
|
+
count_hits,
|
|
36
|
+
sizes,
|
|
37
|
+
[SEED + index for index in range(threads)],
|
|
38
|
+
)
|
|
39
|
+
count = sum(counts)
|
|
40
|
+
elapsed = time.perf_counter() - start
|
|
41
|
+
return {
|
|
42
|
+
"time": elapsed,
|
|
43
|
+
"threads": threads,
|
|
44
|
+
"trials": trials,
|
|
45
|
+
"pi": count / trials * 4,
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def run_mpi_style(processes: int, trials: int = DEFAULT_TRIALS):
|
|
50
|
+
if processes < 1:
|
|
51
|
+
raise ValueError("processes must be at least 1")
|
|
52
|
+
if trials < 1:
|
|
53
|
+
raise ValueError("trials must be at least 1")
|
|
54
|
+
|
|
55
|
+
sizes = [trials // processes + (1 if index < trials % processes else 0)
|
|
56
|
+
for index in range(processes)]
|
|
57
|
+
start = time.perf_counter()
|
|
58
|
+
with ProcessPoolExecutor(max_workers=processes) as executor:
|
|
59
|
+
counts = executor.map(count_hits, sizes, [SEED] * processes)
|
|
60
|
+
count = sum(counts)
|
|
61
|
+
elapsed = time.perf_counter() - start
|
|
62
|
+
return {
|
|
63
|
+
"time": elapsed,
|
|
64
|
+
"threads": processes,
|
|
65
|
+
"trials": trials,
|
|
66
|
+
"pi": count / trials * 4,
|
|
67
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
#include <iostream>
|
|
2
|
+
#include <cstdlib>
|
|
3
|
+
#include <omp.h>
|
|
4
|
+
|
|
5
|
+
#define SEED 3655942
|
|
6
|
+
|
|
7
|
+
using namespace std;
|
|
8
|
+
|
|
9
|
+
int main() {
|
|
10
|
+
int th;
|
|
11
|
+
cout << "Enter the number of threads: ";
|
|
12
|
+
cin >> th;
|
|
13
|
+
omp_set_num_threads(th);
|
|
14
|
+
|
|
15
|
+
double t = omp_get_wtime();
|
|
16
|
+
|
|
17
|
+
int n = 10000;
|
|
18
|
+
int count = 0;
|
|
19
|
+
srand(SEED);
|
|
20
|
+
#pragma omp parallel for reduction(+:count)
|
|
21
|
+
for (int i = 0; i < n; i++) {
|
|
22
|
+
double x = (double)rand() / RAND_MAX;
|
|
23
|
+
double y = (double)rand() / RAND_MAX;
|
|
24
|
+
double z = x*x + y*y;
|
|
25
|
+
if (z <= 1.0) count++;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
t = omp_get_wtime() - t;
|
|
29
|
+
cout << "Time: " << t << endl;
|
|
30
|
+
|
|
31
|
+
double pi = (double)count / n * 4;
|
|
32
|
+
cout << "no. of threads: " << th << endl;
|
|
33
|
+
cout << "no. of trials: " << n << endl;
|
|
34
|
+
cout << "est. of pi: " << pi << endl;
|
|
35
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
#include <iostream>
|
|
2
|
+
#include <cstdlib>
|
|
3
|
+
#include <mpi.h>
|
|
4
|
+
|
|
5
|
+
#define SEED 3655942
|
|
6
|
+
|
|
7
|
+
#define ROOT 0
|
|
8
|
+
|
|
9
|
+
using namespace std;
|
|
10
|
+
|
|
11
|
+
int main(int argc, char *argv[]) {
|
|
12
|
+
MPI_Init(&argc, &argv);
|
|
13
|
+
|
|
14
|
+
int rank, size;
|
|
15
|
+
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
|
|
16
|
+
MPI_Comm_size(MPI_COMM_WORLD, &size);
|
|
17
|
+
|
|
18
|
+
double t;
|
|
19
|
+
if (rank == ROOT) {
|
|
20
|
+
t = MPI_Wtime();
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
int n = 10000;
|
|
24
|
+
int count = 0, red_count;
|
|
25
|
+
srand(SEED);
|
|
26
|
+
for (int i = 0; i < n / size; i++) {
|
|
27
|
+
double x = (double)rand() / RAND_MAX;
|
|
28
|
+
double y = (double)rand() / RAND_MAX;
|
|
29
|
+
double z = x*x + y*y;
|
|
30
|
+
if (z <= 1.0) count++;
|
|
31
|
+
}
|
|
32
|
+
MPI_Reduce(&count, &red_count, 1, MPI_INT, MPI_SUM, ROOT, MPI_COMM_WORLD);
|
|
33
|
+
|
|
34
|
+
if (rank == ROOT) {
|
|
35
|
+
t = MPI_Wtime() - t;
|
|
36
|
+
cout << "Time: " << t << endl;
|
|
37
|
+
|
|
38
|
+
double pi = (double)red_count / n * 4;
|
|
39
|
+
cout << "no. of threads: " << size << endl;
|
|
40
|
+
cout << "no. of trials: " << n << endl;
|
|
41
|
+
cout << "est. of pi: " << pi << endl;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
MPI_Finalize();
|
|
45
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pagp-lab
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Cross-platform pip package for the PAGP Monte Carlo Pi programs
|
|
5
|
+
Author: Shreya Prasad
|
|
6
|
+
License: MIT
|
|
7
|
+
Classifier: Programming Language :: Python :: 3
|
|
8
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
9
|
+
Classifier: Operating System :: OS Independent
|
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
+
Requires-Python: >=3.8
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Dynamic: license-file
|
|
15
|
+
|
|
16
|
+
# pagp-lab
|
|
17
|
+
|
|
18
|
+
`pagp-lab` is a pip package containing the two PAGP Monte Carlo Pi programs.
|
|
19
|
+
It provides deterministic cross-platform implementations, so a compiler or MPI
|
|
20
|
+
installation is not required.
|
|
21
|
+
|
|
22
|
+
## Install
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
pip install pagp-lab
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Usage
|
|
29
|
+
|
|
30
|
+
Run the OpenMP-style threaded implementation:
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
pagp-pi-openmp --threads 4
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Run the MPI-style process implementation:
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
pagp-pi-mpi --processes 4
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
The original C++ sources are also included in the installed package.
|
|
43
|
+
|
|
44
|
+
You can alternatively use the combined command:
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
pagp-lab openmp --threads 4
|
|
48
|
+
pagp-lab mpi --processes 4
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Use `--trials` to change the default of 10,000 trials.
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
src/pagp_lab/__init__.py
|
|
5
|
+
src/pagp_lab/cli.py
|
|
6
|
+
src/pagp_lab/core.py
|
|
7
|
+
src/pagp_lab.egg-info/PKG-INFO
|
|
8
|
+
src/pagp_lab.egg-info/SOURCES.txt
|
|
9
|
+
src/pagp_lab.egg-info/dependency_links.txt
|
|
10
|
+
src/pagp_lab.egg-info/entry_points.txt
|
|
11
|
+
src/pagp_lab.egg-info/top_level.txt
|
|
12
|
+
src/pagp_lab/cpp/pg1.cpp
|
|
13
|
+
src/pagp_lab/cpp/pg1b.cpp
|
|
14
|
+
tests/test_core.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
pagp_lab
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
from pagp_lab.core import run_mpi_style, run_openmp_style
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def test_openmp_style_result_shape():
|
|
5
|
+
result = run_openmp_style(2, 100)
|
|
6
|
+
assert result["threads"] == 2
|
|
7
|
+
assert result["trials"] == 100
|
|
8
|
+
assert 2.0 <= result["pi"] <= 4.0
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def test_mpi_style_result_shape():
|
|
12
|
+
result = run_mpi_style(2, 100)
|
|
13
|
+
assert result["threads"] == 2
|
|
14
|
+
assert result["trials"] == 100
|
|
15
|
+
assert 2.0 <= result["pi"] <= 4.0
|