/
parallel-options.ts
71 lines (60 loc) · 2.69 KB
/
parallel-options.ts
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
/**
* @module parallel
*/
/** needed, typedoc issue */
import {IThreadPool} from "../thread-pool/thread-pool";
import {IParallelJobScheduler} from "./scheduling/parallel-job-scheduler";
import {FunctionCallSerializer} from "../function/function-call-serializer";
/**
* Options that affect how a parallel operation is executed.
*/
export interface IParallelOptions {
/**
* Serializer used to serialize {@link FunctionCall}s
*/
functionCallSerializer?: FunctionCallSerializer;
/**
* The minimum number of values assigned to a single task before the work is split and assigned another task
*/
minValuesPerTask?: number;
/**
* The maximum number of values assigned to a single task. If the number of values exceed the maxValuesPerTask, an additional
* task is created to process the values (until the values per Task are less then maxValuesPerTask)
*/
maxValuesPerTask?: number;
/**
* The thread pool to use to schedule the tasks
*/
threadPool?: IThreadPool;
/**
* The scheduler that should be used to determine the number of tasks to create.
*/
scheduler?: IParallelJobScheduler;
/**
* Defines the max degree of parallelism to use for a scheduled job. The value defines in how many task the scheduler is allowed
* to split the task at most. A value of 1 creates exactly one task. Undefined does not limit the max degree and leaves it to the
* scheduler to decide.
* @default undefined / unlimited
*/
maxDegreeOfParallelism?: number;
}
/**
* Initialized parallel options
*/
export interface IDefaultInitializedParallelOptions extends IParallelOptions {
functionCallSerializer: FunctionCallSerializer;
threadPool: IThreadPool;
scheduler: IParallelJobScheduler;
}
const greaterThanZeroOptions = ["maxValuesPerTask", "minValuesPerTask", "maxDegreeOfParallelism"];
export function validateOptions(options: IParallelOptions) {
for (const optionName of greaterThanZeroOptions) {
const optionValue = (options as {[name: string]: number | undefined })[optionName];
if (typeof (optionValue) !== "undefined" && (typeof(optionValue) !== "number" || optionValue <= 0 || optionValue % 1 !== 0)) {
throw new Error(`Illegal parallel options: ${optionName} (${optionValue}) has to be an integer greater than zero`);
}
}
if (typeof (options.minValuesPerTask) !== "undefined" && typeof(options.maxValuesPerTask) !== "undefined" && options.minValuesPerTask > options.maxValuesPerTask) {
throw new Error(`Illegal parallel options: minValuesPerTask (${options.minValuesPerTask}) must be equal or less than maxValuesPerTask (${options.maxValuesPerTask}).`);
}
}