Monte Carlo Benchmarking Engine
High-performance SIMD Monte Carlo engine (AVX2/NEON) with custom memory allocators and perf logging.
 
Loading...
Searching...
No Matches
main.cpp File Reference

CLI runner for benchmarking Monte Carlo simulation methods. More...

#include "montecarlo.hpp"
#include "benchmark.hpp"
#include <chrono>
#include <iostream>
#include <random>
#include <thread>
#include <unordered_set>
#include <vector>

Go to the source code of this file.

Functions

void print_arch_info ()
 Prints detected platform architecture and SIMD capability.
 
int main (int argc, char *argv[])
 Entry point for running Monte Carlo simulations via CLI.
 

Detailed Description

CLI runner for benchmarking Monte Carlo simulation methods.

Launches π-estimation simulations using various memory and threading strategies. Allows selection of individual method or batch benchmarking across all methods.

Usage

./montecarlo # Run all methods (default trials = 100M)
./montecarlo 5000000 # Run all methods with 5M trials
./montecarlo 1e7 SIMD # Run only SIMD with 10M trials

CLI Arguments

  • argv[1] — Number of simulation trials (optional, default: 100_000_000)
  • argv[2] — Method name: Sequential, Heap, Pool, SIMD, or All (optional, default: All)

Methods

  • Sequential: Single-threaded naive implementation
  • Heap (Threaded): Threaded heap allocation per thread
  • Pool (Threaded): Threaded use of a bump allocator (fast reuse, aligned)
  • SIMD (Threaded): Threaded SIMD-enhanced Monte Carlo with vectorization

Output

Each benchmark logs:

  • Estimated π value
  • Runtime in seconds and nanoseconds
  • Total hits (inside circle) used to compute the estimate

Notes

  • Uses fixed thread count (4) for parallel methods
  • AVX2/NEON support is detected at runtime and reported
  • Each threaded method aggregates results manually

Definition in file main.cpp.

Function Documentation

◆ main()

int main ( int argc,
char * argv[] )

Entry point for running Monte Carlo simulations via CLI.

Parses CLI arguments and dispatches benchmark runs using one of the available simulation methods: Sequential, Heap, Pool, SIMD, or All.

Runs timing and aggregation logic per method and prints π estimates and execution times.

Parameters
argcNumber of CLI arguments
argvArray of CLI argument strings
Returns
0 on success, non-zero on invalid method or failure

Definition at line 81 of file main.cpp.

81 {
83
84 int totalTrials = 100'000'000;
85 std::string method = "All";
86
87 if (argc > 1) totalTrials = std::atoi(argv[1]);
88 if (argc > 2) method = argv[2];
89
90 std::unordered_set<std::string> validMethods = {
91 "Sequential", "Heap", "Pool", "SIMD", "All"
92 };
93 if (!validMethods.count(method)) {
94 std::cerr << "[ERROR] Unknown method: " << method << "\n";
95 std::cerr << "Valid options: Sequential, Heap, Pool, SIMD, All\n";
96 return EXIT_FAILURE;
97 }
98
99 int threadCount = 4;
100
101 if (method == "Sequential" || method == "All") {
102 benchmark("Sequential", totalTrials, [&]() {
103 return monteCarloPI_SEQUENTIAl(totalTrials);
104 });
105 }
106
107 if (method == "Heap" || method == "All") {
108 benchmark("Heap (Threaded)", totalTrials, [&]() {
109 std::vector<std::thread> threads;
110 std::vector<int*> results(threadCount);
111 int perThread = totalTrials / threadCount;
112
113 for (int t = 0; t < threadCount; ++t) {
114 threads.emplace_back([&, t]() {
115 results[t] = monteCarloPI_HEAP(perThread);
116 });
117 }
118
119 for (auto& th : threads) th.join();
120
121 int totalHits = 0;
122 for (auto ptr : results) {
123 totalHits += *ptr;
124 delete ptr;
125 }
126
127 return totalHits;
128 });
129 }
130
131
132 if (method == "Pool" || method == "All") {
133 benchmark("Pool (Threaded)", totalTrials, [&]() {
134 std::vector<std::thread> threads;
135 std::vector<int*> results(threadCount);
136 int perThread = totalTrials / threadCount;
137
138 for (int t = 0; t < threadCount; ++t) {
139 threads.emplace_back([&, t]() {
140 results[t] = monteCarloPI_POOL(perThread);
141 });
142 }
143
144 for (auto& th : threads) th.join();
145
146 int totalHits = 0;
147 for (auto ptr : results) {
148 // NOTE: No delete required — memory allocated from PoolAllocator
149 totalHits += *ptr;
150 }
151
152 return totalHits;
153 });
154 }
155
156 if (method == "SIMD" || method == "All") {
157 benchmark("SIMD (Threaded)", totalTrials, [&]() {
158 std::vector<std::thread> threads;
159 std::vector<int*> results(threadCount);
160 int perThread = totalTrials / threadCount;
161
162 for (int t = 0; t < threadCount; ++t) {
163 threads.emplace_back([&, t]() {
164 results[t] = monteCarloPI_SIMD(perThread);
165 });
166 }
167
168 for (auto& th : threads) th.join();
169
170 int totalHits = 0;
171 for (auto ptr : results) {
172 // NOTE: No delete required — memory allocated from PoolAllocator
173 totalHits += *ptr;
174 }
175
176 return totalHits;
177 });
178 }
179
180 return 0;
181}
void benchmark(const std::string &name, int trials, std::function< int()> func)
Benchmark wrapper that logs wall time and CPU cycles.
Definition benchmark.hpp:44
void print_arch_info()
Prints detected platform architecture and SIMD capability.
Definition main.cpp:55
int * monteCarloPI_SIMD(int numberOfTrials)
Estimates π using SIMD acceleration (AVX2 or NEON) and pool-allocated result storage.
int monteCarloPI_SEQUENTIAl(int numberOfTrials)
Estimates π using sequential dart throwing.
int * monteCarloPI_POOL(int numberOfTrials)
Estimates π using a thread-local memory pool (bump allocator).
int * monteCarloPI_HEAP(int numberOfTrials)
Estimates π using heap-allocated result storage.

◆ print_arch_info()

void print_arch_info ( )

Prints detected platform architecture and SIMD capability.

Detects and logs support for AVX2 (x86) or NEON (ARM) at runtime. Helps validate compatibility for SIMD-accelerated paths.

Definition at line 55 of file main.cpp.

55 {
56 std::cout << "[INFO] Detected platform: ";
57 system("uname -m");
58
59#if defined(__AVX2__)
60 std::cout << "[INFO] SIMD: AVX2 enabled\n";
61#elif defined(__ARM_NEON)
62 std::cout << "[INFO] SIMD: NEON enabled\n";
63#else
64 std::cout << "[WARN] No SIMD support detected\n";
65#endif
66}