88
99namespace diffeq ::examples::parallelism {
1010
11+ /* *
12+ * @brief Quick Start Example - Simplified Parallel Interface
13+ *
14+ * This shows the easiest way to add parallelism to your diffeq computations.
15+ * No complex configuration needed!
16+ */
17+ void quick_start_example () {
18+ std::cout << " \n === Quick Start: Simplified Parallel Interface ===\n " ;
19+
20+ // Example: Parallel ODE integration for multiple initial conditions
21+ std::vector<std::vector<double >> initial_conditions;
22+ for (int i = 0 ; i < 100 ; ++i) {
23+ initial_conditions.push_back ({static_cast <double >(i), 0.0 });
24+ }
25+
26+ // Simple exponential decay: dy/dt = -0.1 * y
27+ auto system = [](double t, const std::vector<double >& y, std::vector<double >& dydt) {
28+ dydt[0 ] = -0.1 * y[0 ];
29+ dydt[1 ] = -0.2 * y[1 ];
30+ };
31+
32+ std::cout << " Integrating " << initial_conditions.size () << " initial conditions in parallel...\n " ;
33+
34+ // THIS IS ALL YOU NEED FOR PARALLEL EXECUTION!
35+ diffeq::execution::parallel_for_each (initial_conditions, [&](std::vector<double >& state) {
36+ diffeq::RK4Integrator<std::vector<double >> integrator (system);
37+ integrator.step (state, 0.01 ); // Single integration step
38+ });
39+
40+ std::cout << " ✓ Parallel integration completed!\n " ;
41+ std::cout << " Result for initial condition 10: [" << initial_conditions[10 ][0 ]
42+ << " , " << initial_conditions[10 ][1 ] << " ]\n " ;
43+
44+ // Want to use GPU if available? Just one line:
45+ diffeq::execution::enable_gpu_acceleration ();
46+
47+ // Want more workers? Just one line:
48+ diffeq::execution::set_parallel_workers (8 );
49+
50+ std::cout << " Current worker count: " << diffeq::execution::parallel ().worker_count () << " \n " ;
51+ }
52+
1153/* *
1254 * @brief Robotics Control Systems Example
1355 *
@@ -42,7 +84,38 @@ struct RobotArmSystem {
4284void demonstrate_realtime_control () {
4385 std::cout << " \n === Robotics Control System with Real-time Parallelism ===\n " ;
4486
45- // Configure for real-time robotics control
87+ // SIMPLE APPROACH: Use the simplified parallel interface for basic needs
88+ std::cout << " \n --- Simple Parallel Approach ---\n " ;
89+
90+ // Setup multiple control systems (e.g., different robot joints)
91+ std::vector<std::vector<double >> joint_states;
92+ for (int i = 0 ; i < 6 ; ++i) { // 6-DOF robot arm
93+ joint_states.push_back ({0.1 * i, 0.0 }); // [angle, angular_velocity]
94+ }
95+
96+ // Create simple parallel executor
97+ auto parallel = diffeq::execution::Parallel (4 ); // 4 worker threads
98+
99+ auto simple_start_time = std::chrono::high_resolution_clock::now ();
100+
101+ // Parallel control loop - very simple!
102+ parallel.for_each (joint_states, [](std::vector<double >& state) {
103+ RobotArmSystem system;
104+ diffeq::RK4Integrator<std::vector<double >> integrator (system);
105+ integrator.step (state, 0.001 ); // 1ms control timestep
106+ });
107+
108+ auto simple_end_time = std::chrono::high_resolution_clock::now ();
109+ auto simple_duration = std::chrono::duration_cast<std::chrono::microseconds>(simple_end_time - simple_start_time);
110+
111+ std::cout << " Simple parallel control completed in " << simple_duration.count () << " μs\n " ;
112+ std::cout << " Average per joint: " << simple_duration.count () / joint_states.size () << " μs\n " ;
113+
114+ // ADVANCED APPROACH: Use full facade for complex real-time requirements
115+ std::cout << " \n --- Advanced Facade Approach (for complex scenarios) ---\n " ;
116+
117+ // For applications requiring precise real-time control, load balancing,
118+ // hardware-specific optimizations, etc., use the full facade:
46119 auto parallel_config = diffeq::execution::presets::robotics_control ()
47120 .realtime_priority ()
48121 .workers (4 ) // Dedicated cores for control
@@ -52,7 +125,7 @@ void demonstrate_realtime_control() {
52125
53126 // Create integrator for robot dynamics
54127 auto robot_system = RobotArmSystem{};
55- auto integrator = diffeq::ode::factory::make_rk4_integrator <std::vector<double >, double >(robot_system);
128+ auto integrator = diffeq::RK4Integrator <std::vector<double >>(robot_system);
56129
57130 // Initial state: [angle=0.1 rad, angular_velocity=0]
58131 std::vector<double > state = {0.1 , 0.0 };
@@ -62,7 +135,7 @@ void demonstrate_realtime_control() {
62135 std::cout << " Running real-time robot control simulation...\n " ;
63136 std::cout << " Target frequency: 1kHz (1ms timestep)\n " ;
64137
65- auto start_time = std::chrono::high_resolution_clock::now ();
138+ auto advanced_start_time = std::chrono::high_resolution_clock::now ();
66139
67140 // Simulate real-time control loop
68141 for (double t = 0.0 ; t < simulation_time; t += dt) {
@@ -89,11 +162,11 @@ void demonstrate_realtime_control() {
89162 }
90163 }
91164
92- auto end_time = std::chrono::high_resolution_clock::now ();
93- auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time );
165+ auto advanced_end_time = std::chrono::high_resolution_clock::now ();
166+ auto advanced_duration = std::chrono::duration_cast<std::chrono::milliseconds>(advanced_end_time - advanced_start_time );
94167
95- std::cout << " Simulation completed in " << duration .count () << " ms\n " ;
96- std::cout << " Average loop time: " << duration .count () / (simulation_time / dt) << " ms\n " ;
168+ std::cout << " Simulation completed in " << advanced_duration .count () << " ms\n " ;
169+ std::cout << " Average loop time: " << advanced_duration .count () / (simulation_time / dt) << " ms\n " ;
97170 std::cout << " Final robot state: angle=" << state[0 ] << " rad, velocity=" << state[1 ] << " rad/s\n " ;
98171}
99172
@@ -156,7 +229,7 @@ void demonstrate_monte_carlo_simulation() {
156229 for (size_t i = 0 ; i < num_simulations; ++i) {
157230 simulation_futures.push_back (parallel_config->async ([=]() {
158231 // Create integrator for this simulation
159- auto integrator = diffeq::ode::factory::make_rk4_integrator <std::vector<double >, double >(gbm_system);
232+ auto integrator = diffeq::RK4Integrator <std::vector<double >>(gbm_system);
160233
161234 // Random number generator for this thread
162235 std::mt19937 rng (std::random_device{}() + i);
@@ -286,7 +359,7 @@ void benchmark_hardware_targets() {
286359
287360 // Parallel integration using the unified interface
288361 parallel_facade->parallel_for_each (states.begin (), states.end (), [&](auto & state) {
289- auto integrator = diffeq::ode::factory::make_rk4_integrator <std::vector<double >, double >(system);
362+ auto integrator = diffeq::RK4Integrator <std::vector<double >>(system);
290363
291364 double t = 0.0 ;
292365 while (t < end_time) {
@@ -324,9 +397,12 @@ void benchmark_hardware_targets() {
324397 */
325398void demonstrate_all_parallelism_features () {
326399 std::cout << " === Enhanced Parallelism Capabilities Demo ===\n " ;
327- std::cout << " Demonstrating modern C++ parallelism with unified hardware interface\n " ;
400+ std::cout << " Demonstrating both simple and advanced parallelism interfaces\n " ;
401+
402+ // Start with the simple interface for new users
403+ quick_start_example ();
328404
329- // Run robotics control example
405+ // Run robotics control example (shows both simple and advanced)
330406 robotics_control::demonstrate_realtime_control ();
331407
332408 // Run stochastic research example
0 commit comments