xdy 0.9.0

Complex RPG dice expression evaluator with histogram support.
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
//! # Parallel implementation of the histogram builder
//!
//! This module contains the parallel implementation of the histogram builder,
//! which is always available. The parallel implementation is only available if
//! the `parallel-histogram` feature is enabled.

use std::{
	cell::RefCell,
	collections::{HashMap, VecDeque},
	num::NonZero,
	sync::{
		Once,
		atomic::{AtomicU64, Ordering}
	},
	thread::available_parallelism
};

use rayon::{
	ThreadPoolBuilder, broadcast,
	iter::{
		ParallelIterator,
		plumbing::{Folder, Reducer, UnindexedConsumer, UnindexedProducer}
	},
	join, scope
};

use super::{CanBuildHistogram, CanIterate, EvaluationState, Histogram};
use crate::{EvaluationError, Evaluator, Function};

////////////////////////////////////////////////////////////////////////////////
//                                Histograms.                                 //
////////////////////////////////////////////////////////////////////////////////

impl Histogram
{
	/// Merges another histogram into this one.
	///
	/// # Parameters
	/// - `other`: The other histogram to merge into this one.
	fn merge(&mut self, other: Self)
	{
		for (key, value) in other.into_iter()
		{
			*self.entry(key).or_insert(0) += value;
		}
	}
}

////////////////////////////////////////////////////////////////////////////////
//                            Histogram building.                             //
////////////////////////////////////////////////////////////////////////////////

/// Builds a histogram of the outcomes of a dice expression, using parallel
/// computation. Each thread involved in the computation will build a partial
/// histogram, using thread local storage to eliminate contention. When the
/// parallel computation ends, the partial histograms are merged into a final
/// histogram.
#[derive(Debug, Clone)]
pub struct HistogramBuilder
{
	/// The function to evaluate.
	function: Function,

	/// The environment in which to evaluate the function, as a map from
	/// external variable indices to values. Missing bindings default to zero.
	environment: HashMap<usize, i32>,

	/// The generation number of the histogram, which is used to identify the
	/// histogram being built by a particular iterator.
	generation: u64
}

/// The thread pool initializer for the histogram builder.
static THREAD_POOL_INITIALIZER: Once = Once::new();

impl HistogramBuilder
{
	/// Tell the global thread pool to use all available parallelism. If the
	/// global thread pool has already been initialized, this function does
	/// nothing. Calling this function is optional. Using [HistogramBuilder] to
	/// create an iterator will automatically call this function.
	pub fn use_available_parallelism()
	{
		// We don't care whether the initialization succeeds or fails, but we
		// want to ensure that we default to using all available parallelism if
		// the user hasn't already initialized the `rayon` global thread pool.
		THREAD_POOL_INITIALIZER.call_once(|| {
			let _ = ThreadPoolBuilder::new()
				.num_threads(available_parallelism().map_or(1, NonZero::get))
				.build_global();
		});
	}

	/// Tell the global thread pool to use the specified number of threads. If
	/// the global thread pool has already been initialized, this function does
	/// nothing. Calling this function is optional.
	///
	/// # Parameters
	/// - `num_threads`: The number of threads to use in the global thread pool.
	pub fn set_parallelism(&self, num_threads: usize)
	{
		// We don't care whether the initialization succeeds or fails, but we
		// want to ensure that we default to using all available parallelism if
		// the user hasn't already initialized the `rayon` global thread pool.
		THREAD_POOL_INITIALIZER.call_once(|| {
			let _ = ThreadPoolBuilder::new()
				.num_threads(num_threads)
				.build_global();
		});
	}
}

/// Increment the histogram for the given outcome.
///
/// # Parameters
/// - `generation`: The generation number of the histogram.
/// - `outcome`: The outcome to increment.
fn increment(generation: u64, outcome: i32)
{
	HISTOGRAM.with_borrow_mut(|histogram| {
		*histogram
			.entry(generation)
			.or_default()
			.entry(outcome)
			.or_insert(0) += 1;
	});
}

/// Build the final histogram by merging the partial histograms from all
/// threads involved in the computation.
///
/// # Parameters
/// - `generation`: The generation number of the histogram.
///
/// # Returns
/// The final histogram.
fn finish(generation: u64) -> Histogram
{
	// Merge the partial histogram into the global histogram. We need to
	// broadcast a merge request to all threads within the current
	// `rayon` scope to ensure that the global histogram is updated
	// with the partial histograms from all threads.
	let partials = broadcast(|_| {
		HISTOGRAM
			.with_borrow_mut(|histogram| histogram.remove(&generation))
			.unwrap_or_default()
	});
	partials
		.into_iter()
		.reduce(|mut a, b| {
			a.merge(b);
			a
		})
		.unwrap()
}

impl<'inst> super::HistogramBuilder<'inst, EvaluationStateIterator<'inst>>
	for HistogramBuilder
{
	fn new(evaluator: Evaluator) -> Self
	{
		HistogramBuilder {
			function: evaluator.function,
			environment: evaluator.environment,
			generation: GENERATION.fetch_add(1, Ordering::Relaxed)
		}
	}

	fn build_while(
		&self,
		args: impl IntoIterator<Item = i32> + Send,
		condition: impl Fn(&i32) -> bool + Send + Sync
	) -> Result<Histogram, EvaluationError<'_>>
	{
		// Establish a scope for the parallel computation, to ensure that the
		// thread local storage is available to all threads.
		let generation = self.generation;
		scope(|_| {
			CanBuildHistogram::iter(self, args)?
				.flat_map(|state| state.result)
				.take_any_while(condition)
				.for_each(|outcome| increment(generation, outcome));
			Ok(finish(generation))
		})
	}

	#[inline]
	fn iter(
		&'inst self,
		args: impl IntoIterator<Item = i32>
	) -> Result<EvaluationStateIterator<'inst>, EvaluationError<'inst>>
	{
		CanBuildHistogram::iter(self, args)
	}
}

impl<'inst> CanBuildHistogram<'inst, EvaluationStateIterator<'inst>>
	for HistogramBuilder
{
	#[inline]
	fn function(&self) -> &Function { &self.function }

	#[inline]
	fn environment(&self) -> &HashMap<usize, i32> { &self.environment }

	fn create_iterator(
		&'inst self,
		initial_state: EvaluationState<'inst>
	) -> EvaluationStateIterator<'inst>
	{
		HistogramBuilder::use_available_parallelism();
		EvaluationStateIterator {
			generation: self.generation,
			states: [initial_state].into(),
			completed: VecDeque::new()
		}
	}
}

////////////////////////////////////////////////////////////////////////////////
//                                 Iteration.                                 //
////////////////////////////////////////////////////////////////////////////////

/// The generation number of the histogram. The generation number is incremented
/// each time a new histogram is started, and is used to identify the histogram
/// being built by a particular iterator.
static GENERATION: AtomicU64 = AtomicU64::new(0);

thread_local! {
	/// The histograms being built by this thread, keyed by generation number.
	/// Each generation number corresponds to a different histogram being built
	/// concurrently. Partial histograms are stored in a thread-local variable
	/// to avoid the need for synchronization, then merged into the global
	/// histogram when the relevant iterator is dropped.
	pub static HISTOGRAM: RefCell<HashMap<u64, Histogram>> =
		RefCell::new(Default::default());
}

/// An open-ended iterator of [states](EvaluationState), representing the
/// continuations of a histogram builder's execution. Each state represents a
/// point in the builder's execution where it may be suspended and later
/// resumed. Each state corresponds to the internal state of a range or roll
/// instruction, as these are the only instructions that may be suspended. Each
/// state represents having rolled a particular sequence of ranges or dice, and
/// the full iterator represents the entire space of possible outcomes of a
/// dice expression. The consumer may choose to terminate the iteration early,
/// in which case the builder will contain the partial histogram computed up to
/// the point of termination.
#[derive(Debug, Clone)]
pub struct EvaluationStateIterator<'inst>
{
	/// The generation number of the histogram, which uniquely identifies the
	/// histogram being built by this iterator.
	generation: u64,

	/// The remaining states to evaluate.
	states: VecDeque<EvaluationState<'inst>>,

	/// The completed states that have been evaluated and are ready for
	/// consumption.
	completed: VecDeque<EvaluationState<'inst>>
}

impl<'inst> CanIterate<'inst> for EvaluationStateIterator<'inst>
{
	/// Answer the next state to evaluate.
	///
	/// # Returns
	/// The next state to evaluate, or `None` if there are no more states to
	/// evaluate.
	fn next_state(&mut self) -> Option<EvaluationState<'inst>>
	{
		self.states.pop_front()
	}

	/// Inject the successors of the given state back into the iterator.
	///
	/// # Parameters
	/// - `state`: The state whose successors should be injected.
	fn inject_successors(&mut self, state: &mut EvaluationState<'inst>)
	{
		if let Some(ref mut successors) = state.successors
		{
			self.states.extend(successors.drain(..));
		}
	}
}

impl<'inst> ParallelIterator for EvaluationStateIterator<'inst>
{
	type Item = EvaluationState<'inst>;

	fn drive_unindexed<C>(mut self, consumer: C) -> C::Result
	where
		C: UnindexedConsumer<Self::Item>
	{
		// We absolutely cannot use any of `rayon`'s bridging methods here, as
		// they are incapable of dealing with dynamic workloads. Because we are
		// exhaustively exploring a state space, we cannot reliably predict the
		// number of states that will be generated in the general case (where
		// some range and roll instructions are dynamically controlled by other
		// range and roll instructions). So we have to oscillate between
		// evaluating states to generate successors and outcomes, splitting
		// successors into new iterators and waiting to reduce their results,
		// and consuming leaf states with outcomes until the consumer is
		// satisfied or the state space is exhausted.
		loop
		{
			if consumer.full()
			{
				// The consumer has found whatever it was looking for, and is no
				// longer interested in consuming more states. We can stop here.
				return consumer.into_folder().complete()
			}
			else
			{
				// Evaluate states until enough successors are available to make
				// splitting worthwhile.
				while self.states.len() <= EvaluationState::MAX_SUCCESSORS
				{
					match self.next_state()
					{
						Some(mut state) =>
						{
							// Evaluate the state, injecting its successors back
							// into the iterator.
							let outcome = state.evaluate();
							self.inject_successors(&mut state);
							if outcome.is_some()
							{
								// The state has an outcome, and therefore it
								// does not have any successors, so
								// place it into the completed queue for
								// the consumer to consume.
								self.completed.push_back(state);
							}
						},
						None =>
						{
							// There are no more states to evaluate, so we can
							// consume the completed states and return control
							// to the caller.
							return UnindexedProducer::fold_with(
								self,
								consumer.into_folder()
							)
							.complete()
						}
					}
				}
				// Split the iterator.
				match self.split()
				{
					(left_producer, Some(right_producer)) =>
					{
						// Split the consumer. Fork jobs for the left and right
						// producer-consumer pairs, then reduce the results.
						let (reducer, left_consumer, right_consumer) = (
							consumer.to_reducer(),
							consumer.split_off_left(),
							consumer
						);
						let (left_result, right_result) = join(
							|| left_producer.drive_unindexed(left_consumer),
							|| right_producer.drive_unindexed(right_consumer)
						);
						return reducer.reduce(left_result, right_result)
					},
					(producer, None) =>
					{
						// There weren't enough states to be worth splitting, so
						// loop back around and continue evaluating states.
						self = producer;
					}
				}
			}
		}
	}
}

impl<'inst> UnindexedProducer for EvaluationStateIterator<'inst>
{
	type Item = EvaluationState<'inst>;

	fn split(mut self) -> (Self, Option<Self>)
	{
		match self.states.len()
		{
			0..=EvaluationState::MAX_SUCCESSORS =>
			{
				// There are not enough states to be worth splitting.
				(self, None)
			},
			n =>
			{
				// Split the states into two halves.
				let right = self.states.split_off(n / 2);
				let right = EvaluationStateIterator {
					generation: self.generation,
					states: right,
					completed: VecDeque::new()
				};
				(self, Some(right))
			}
		}
	}

	fn fold_with<F>(mut self, mut folder: F) -> F
	where
		F: Folder<Self::Item>
	{
		// Only completed states are consumed by the folder, so the folder can
		// never generate new states.
		let completed = self.completed.drain(..).collect::<Vec<_>>();
		for state in completed
		{
			folder = folder.consume(state);
			if folder.full()
			{
				return folder
			}
		}
		folder
	}
}