1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
use crate;
use crateScope;
use crateTaskQueue;
use NonZeroUsize;
/// Abstraction for parallel execution environments and thread pool management.
///
/// `ThreadPool` defines how parallel computations are executed on a set of worker threads.
/// Any type implementing this trait can serve as a thread pool for orx-parallel computations.
///
/// # Thread Count Decision
///
/// The actual number of threads used in a computation is determined by combining multiple
/// configuration layers:
///
/// 1. **Pool Layer** (`max_num_threads()`) - The thread pool's maximum capacity
/// 2. **Environment Layer** (`ORX_NUM_THREADS`) - Global limit from environment variable
/// 3. **Computation Layer** (`.num_threads()` on Par) - Per-computation request
/// 4. **Input Size** - Cannot exceed the number of input elements
///
/// The `max_num_threads_for_computation()` method implements this logic by returning the
/// minimum of all these constraints.
///
/// # Example
///
/// ```ignore
/// use orx_parallel::*;
///
/// // Pool setup: 8 threads requested, but env limits to 4
/// // ORX_NUM_THREADS=4 is set
/// let pool = Pool::once(8); // pool.max_num_threads() == 4
///
/// // Computation: request 6 threads on 100-element input
/// let result: Vec<_> = (0..100)
/// .into_par()
/// .map(|x| x * 2)
/// .pool(pool)
/// .num_threads(6)
/// .collect();
/// // Result: min(min(6, 100), 4) = 4 threads used
/// ```
///
/// # Implementations
///
/// - `Pool::basic` - Persistent thread pool (default)
/// - `Pool::once` - Lightweight virtual pool, spawns threads on-demand
/// - `Pool::rayon` - rayon thread pools
///
/// See the [`thread_usage.md`](https://github.com/orxfun/orx-parallel/blob/main/docs/thread_usage.md) documentation for a complete guide.
/// Calculates the actual thread count for a computation considering multiple constraints.
///
/// This method implements the core thread count decision logic by combining:
///
/// 1. **Pool constraint** (`self.max_num_threads()`)
/// - The thread pool's maximum capacity
/// - Already includes environment variable constraints
///
/// 2. **Computation constraint** (`params.num_threads`)
/// - Per-computation request from `.num_threads()` method
/// - Can be `NumThreads::Auto` (use all available)
/// - Or `NumThreads::Max(n)` (hard limit)
///
/// 3. **Input size constraint** (known upper bound from `size_hint.1`)
/// - Cannot spawn more threads than input elements
/// - When input size is unknown (None), this constraint doesn't apply
///
/// # Returns
///
/// The minimum of all constraints, representing the actual thread count to use.
///
/// # Decision Logic
///
/// ```text
/// let available = self.max_num_threads() // Pool limit
///
/// let requested = match (size_hint.1, params.num_threads) {
/// (Some(len), Auto) => min(len, MaxUsize), // Cap by input size
/// (Some(len), Max(n)) => min(len, n), // Cap by input size and request
/// (None, Auto) => MaxUsize, // No constraints
/// (None, Max(n)) => n, // Only respect request
/// };
///
/// return min(requested, available) // Final decision
/// ```
///
/// # Parameters
///
/// - `params` - Contains `.num_threads` setting from `.num_threads()` method
/// - `size_hint` - Tuple of (lower_bound, Option<upper_bound>) for input size
/// - If upper_bound is `None`, input size is unknown
/// - If upper_bound is `Some(n)`, input has at most n elements