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
Go to the documentation of this file.
1// ========================================
2// main.cpp - Simulation Launcher
3// ========================================
39
40#include "montecarlo.hpp"
41#include "benchmark.hpp"
42#include <chrono>
43#include <iostream>
44#include <random>
45#include <thread>
46#include <unordered_set>
47#include <vector>
48
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}
67
81int main(int argc, char* argv[]) {
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}
Wall-clock + cycle-accurate benchmarking for performance profiling.
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
int main(int argc, char *argv[])
Entry point for running Monte Carlo simulations via CLI.
Definition main.cpp:81
void print_arch_info()
Prints detected platform architecture and SIMD capability.
Definition main.cpp:55
High-performance Monte Carlo π Estimation Engine — SIMD-accelerated, memory-optimized.
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.