-
-
Notifications
You must be signed in to change notification settings - Fork 702
/
parallelism.d
4858 lines (4128 loc) · 144 KB
/
parallelism.d
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
`std.parallelism` implements high-level primitives for SMP parallelism.
These include parallel foreach, parallel reduce, parallel eager map, pipelining
and future/promise parallelism. `std.parallelism` is recommended when the
same operation is to be executed in parallel on different data, or when a
function is to be executed in a background thread and its result returned to a
well-defined main thread. For communication between arbitrary threads, see
`std.concurrency`.
`std.parallelism` is based on the concept of a `Task`. A `Task` is an
object that represents the fundamental unit of work in this library and may be
executed in parallel with any other `Task`. Using `Task`
directly allows programming with a future/promise paradigm. All other
supported parallelism paradigms (parallel foreach, map, reduce, pipelining)
represent an additional level of abstraction over `Task`. They
automatically create one or more `Task` objects, or closely related types
that are conceptually identical but not part of the public API.
After creation, a `Task` may be executed in a new thread, or submitted
to a `TaskPool` for execution. A `TaskPool` encapsulates a task queue
and its worker threads. Its purpose is to efficiently map a large
number of `Task`s onto a smaller number of threads. A task queue is a
FIFO queue of `Task` objects that have been submitted to the
`TaskPool` and are awaiting execution. A worker thread is a thread that
is associated with exactly one task queue. It executes the `Task` at the
front of its queue when the queue has work available, or sleeps when
no work is available. Each task queue is associated with zero or
more worker threads. If the result of a `Task` is needed before execution
by a worker thread has begun, the `Task` can be removed from the task queue
and executed immediately in the thread where the result is needed.
Warning: Unless marked as `@trusted` or `@safe`, artifacts in
this module allow implicit data sharing between threads and cannot
guarantee that client code is free from low level data races.
Source: $(PHOBOSSRC std/parallelism.d)
Author: David Simcha
Copyright: Copyright (c) 2009-2011, David Simcha.
License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0)
*/
module std.parallelism;
version (OSX)
version = Darwin;
else version (iOS)
version = Darwin;
else version (TVOS)
version = Darwin;
else version (WatchOS)
version = Darwin;
///
@system unittest
{
import std.algorithm.iteration : map;
import std.math : isClose;
import std.parallelism : taskPool;
import std.range : iota;
// Parallel reduce can be combined with
// std.algorithm.iteration.map to interesting effect.
// The following example (thanks to Russel Winder)
// calculates pi by quadrature using
// std.algorithm.map and TaskPool.reduce.
// getTerm is evaluated in parallel as needed by
// TaskPool.reduce.
//
// Timings on an Intel i5-3450 quad core machine
// for n = 1_000_000_000:
//
// TaskPool.reduce: 1.067 s
// std.algorithm.reduce: 4.011 s
enum n = 1_000_000;
enum delta = 1.0 / n;
alias getTerm = (int i)
{
immutable x = ( i - 0.5 ) * delta;
return delta / ( 1.0 + x * x ) ;
};
immutable pi = 4.0 * taskPool.reduce!"a + b"(n.iota.map!getTerm);
assert(pi.isClose(3.14159, 1e-5));
}
import core.atomic;
import core.memory;
import core.sync.condition;
import core.thread;
import std.functional;
import std.meta;
import std.range.primitives;
import std.traits;
/*
(For now public undocumented with reserved name.)
A lazily initialized global constant. The underlying value is a shared global
statically initialized to `outOfBandValue` which must not be a legit value of
the constant. Upon the first call the situation is detected and the global is
initialized by calling `initializer`. The initializer is assumed to be pure
(even if not marked as such), i.e. return the same value upon repeated calls.
For that reason, no special precautions are taken so `initializer` may be called
more than one time leading to benign races on the cached value.
In the quiescent state the cost of the function is an atomic load from a global.
Params:
T = The type of the pseudo-constant (may be qualified)
outOfBandValue = A value that cannot be valid, it is used for initialization
initializer = The function performing initialization; must be `nothrow`
Returns:
The lazily initialized value
*/
@property pure
T __lazilyInitializedConstant(T, alias outOfBandValue, alias initializer)()
if (is(Unqual!T : T)
&& is(typeof(initializer()) : T)
&& is(typeof(outOfBandValue) : T))
{
static T impl() nothrow
{
// Thread-local cache
static Unqual!T tls = outOfBandValue;
auto local = tls;
// Shortest path, no atomic operations
if (local != outOfBandValue) return local;
// Process-level cache
static shared Unqual!T result = outOfBandValue;
// Initialize both process-level cache and tls
local = atomicLoad(result);
if (local == outOfBandValue)
{
local = initializer();
atomicStore(result, local);
}
tls = local;
return local;
}
import std.traits : SetFunctionAttributes;
alias Fun = SetFunctionAttributes!(typeof(&impl), "D",
functionAttributes!(typeof(&impl)) | FunctionAttribute.pure_);
auto purified = (() @trusted => cast(Fun) &impl)();
return purified();
}
// Returns the size of a cache line.
alias cacheLineSize =
__lazilyInitializedConstant!(immutable(size_t), size_t.max, cacheLineSizeImpl);
private size_t cacheLineSizeImpl() @nogc nothrow @trusted
{
size_t result = 0;
import core.cpuid : datacache;
foreach (ref const cachelevel; datacache)
{
if (cachelevel.lineSize > result && cachelevel.lineSize < uint.max)
{
result = cachelevel.lineSize;
}
}
return result;
}
@nogc @safe nothrow unittest
{
assert(cacheLineSize == cacheLineSizeImpl);
}
/* Atomics code. These forward to core.atomic, but are written like this
for two reasons:
1. They used to actually contain ASM code and I don' want to have to change
to directly calling core.atomic in a zillion different places.
2. core.atomic has some misc. issues that make my use cases difficult
without wrapping it. If I didn't wrap it, casts would be required
basically everywhere.
*/
private void atomicSetUbyte(T)(ref T stuff, T newVal)
if (__traits(isIntegral, T) && is(T : ubyte))
{
//core.atomic.cas(cast(shared) &stuff, stuff, newVal);
atomicStore(*(cast(shared) &stuff), newVal);
}
private ubyte atomicReadUbyte(T)(ref T val)
if (__traits(isIntegral, T) && is(T : ubyte))
{
return atomicLoad(*(cast(shared) &val));
}
// This gets rid of the need for a lot of annoying casts in other parts of the
// code, when enums are involved.
private bool atomicCasUbyte(T)(ref T stuff, T testVal, T newVal)
if (__traits(isIntegral, T) && is(T : ubyte))
{
return core.atomic.cas(cast(shared) &stuff, testVal, newVal);
}
/*--------------------- Generic helper functions, etc.------------------------*/
private template MapType(R, functions...)
{
static assert(functions.length);
ElementType!R e = void;
alias MapType =
typeof(adjoin!(staticMap!(unaryFun, functions))(e));
}
private template ReduceType(alias fun, R, E)
{
alias ReduceType = typeof(binaryFun!fun(E.init, ElementType!R.init));
}
private template noUnsharedAliasing(T)
{
enum bool noUnsharedAliasing = !hasUnsharedAliasing!T;
}
// This template tests whether a function may be executed in parallel from
// @safe code via Task.executeInNewThread(). There is an additional
// requirement for executing it via a TaskPool. (See isSafeReturn).
private template isSafeTask(F)
{
enum bool isSafeTask =
(functionAttributes!F & (FunctionAttribute.safe | FunctionAttribute.trusted)) != 0 &&
(functionAttributes!F & FunctionAttribute.ref_) == 0 &&
(isFunctionPointer!F || !hasUnsharedAliasing!F) &&
allSatisfy!(noUnsharedAliasing, Parameters!F);
}
@safe unittest
{
alias F1 = void function() @safe;
alias F2 = void function();
alias F3 = void function(uint, string) @trusted;
alias F4 = void function(uint, char[]);
static assert( isSafeTask!F1);
static assert(!isSafeTask!F2);
static assert( isSafeTask!F3);
static assert(!isSafeTask!F4);
alias F5 = uint[] function(uint, string) pure @trusted;
static assert( isSafeTask!F5);
}
// This function decides whether Tasks that meet all of the other requirements
// for being executed from @safe code can be executed on a TaskPool.
// When executing via TaskPool, it's theoretically possible
// to return a value that is also pointed to by a worker thread's thread local
// storage. When executing from executeInNewThread(), the thread that executed
// the Task is terminated by the time the return value is visible in the calling
// thread, so this is a non-issue. It's also a non-issue for pure functions
// since they can't read global state.
private template isSafeReturn(T)
{
static if (!hasUnsharedAliasing!(T.ReturnType))
{
enum isSafeReturn = true;
}
else static if (T.isPure)
{
enum isSafeReturn = true;
}
else
{
enum isSafeReturn = false;
}
}
private template randAssignable(R)
{
enum randAssignable = isRandomAccessRange!R && hasAssignableElements!R;
}
private enum TaskStatus : ubyte
{
notStarted,
inProgress,
done
}
private template AliasReturn(alias fun, T...)
{
alias AliasReturn = typeof({ T args; return fun(args); });
}
// Should be private, but std.algorithm.reduce is used in the zero-thread case
// and won't work w/ private.
template reduceAdjoin(functions...)
{
static if (functions.length == 1)
{
alias reduceAdjoin = binaryFun!(functions[0]);
}
else
{
T reduceAdjoin(T, U)(T lhs, U rhs)
{
alias funs = staticMap!(binaryFun, functions);
foreach (i, Unused; typeof(lhs.expand))
{
lhs.expand[i] = funs[i](lhs.expand[i], rhs);
}
return lhs;
}
}
}
private template reduceFinish(functions...)
{
static if (functions.length == 1)
{
alias reduceFinish = binaryFun!(functions[0]);
}
else
{
T reduceFinish(T)(T lhs, T rhs)
{
alias funs = staticMap!(binaryFun, functions);
foreach (i, Unused; typeof(lhs.expand))
{
lhs.expand[i] = funs[i](lhs.expand[i], rhs.expand[i]);
}
return lhs;
}
}
}
private template isRoundRobin(R : RoundRobinBuffer!(C1, C2), C1, C2)
{
enum isRoundRobin = true;
}
private template isRoundRobin(T)
{
enum isRoundRobin = false;
}
@safe unittest
{
static assert( isRoundRobin!(RoundRobinBuffer!(void delegate(char[]), bool delegate())));
static assert(!isRoundRobin!(uint));
}
// This is the base "class" for all of the other tasks. Using C-style
// polymorphism to allow more direct control over memory allocation, etc.
private struct AbstractTask
{
AbstractTask* prev;
AbstractTask* next;
// Pointer to a function that executes this task.
void function(void*) runTask;
Throwable exception;
ubyte taskStatus = TaskStatus.notStarted;
bool done() @property
{
if (atomicReadUbyte(taskStatus) == TaskStatus.done)
{
if (exception)
{
throw exception;
}
return true;
}
return false;
}
void job()
{
runTask(&this);
}
}
/**
`Task` represents the fundamental unit of work. A `Task` may be
executed in parallel with any other `Task`. Using this struct directly
allows future/promise parallelism. In this paradigm, a function (or delegate
or other callable) is executed in a thread other than the one it was called
from. The calling thread does not block while the function is being executed.
A call to `workForce`, `yieldForce`, or `spinForce` is used to
ensure that the `Task` has finished executing and to obtain the return
value, if any. These functions and `done` also act as full memory barriers,
meaning that any memory writes made in the thread that executed the `Task`
are guaranteed to be visible in the calling thread after one of these functions
returns.
The $(REF task, std,parallelism) and $(REF scopedTask, std,parallelism) functions can
be used to create an instance of this struct. See `task` for usage examples.
Function results are returned from `yieldForce`, `spinForce` and
`workForce` by ref. If `fun` returns by ref, the reference will point
to the returned reference of `fun`. Otherwise it will point to a
field in this struct.
Copying of this struct is disabled, since it would provide no useful semantics.
If you want to pass this struct around, you should do so by reference or
pointer.
Bugs: Changes to `ref` and `out` arguments are not propagated to the
call site, only to `args` in this struct.
*/
struct Task(alias fun, Args...)
{
AbstractTask base = {runTask : &impl};
alias base this;
private @property AbstractTask* basePtr()
{
return &base;
}
private static void impl(void* myTask)
{
import std.algorithm.internal : addressOf;
Task* myCastedTask = cast(typeof(this)*) myTask;
static if (is(ReturnType == void))
{
fun(myCastedTask._args);
}
else static if (is(typeof(&(fun(myCastedTask._args)))))
{
myCastedTask.returnVal = addressOf(fun(myCastedTask._args));
}
else
{
myCastedTask.returnVal = fun(myCastedTask._args);
}
}
private TaskPool pool;
private bool isScoped; // True if created with scopedTask.
Args _args;
/**
The arguments the function was called with. Changes to `out` and
`ref` arguments will be visible here.
*/
static if (__traits(isSame, fun, run))
{
alias args = _args[1..$];
}
else
{
alias args = _args;
}
// The purpose of this code is to decide whether functions whose
// return values have unshared aliasing can be executed via
// TaskPool from @safe code. See isSafeReturn.
static if (__traits(isSame, fun, run))
{
static if (isFunctionPointer!(_args[0]))
{
private enum bool isPure =
(functionAttributes!(Args[0]) & FunctionAttribute.pure_) != 0;
}
else
{
// BUG: Should check this for delegates too, but std.traits
// apparently doesn't allow this. isPure is irrelevant
// for delegates, at least for now since shared delegates
// don't work.
private enum bool isPure = false;
}
}
else
{
// We already know that we can't execute aliases in @safe code, so
// just put a dummy value here.
private enum bool isPure = false;
}
/**
The return type of the function called by this `Task`. This can be
`void`.
*/
alias ReturnType = typeof(fun(_args));
static if (!is(ReturnType == void))
{
static if (is(typeof(&fun(_args))))
{
// Ref return.
ReturnType* returnVal;
ref ReturnType fixRef(ReturnType* val)
{
return *val;
}
}
else
{
ReturnType returnVal;
ref ReturnType fixRef(ref ReturnType val)
{
return val;
}
}
}
private void enforcePool()
{
import std.exception : enforce;
enforce(this.pool !is null, "Job not submitted yet.");
}
static if (Args.length > 0)
{
private this(Args args)
{
_args = args;
}
}
// Work around DMD bug https://issues.dlang.org/show_bug.cgi?id=6588,
// allow immutable elements.
static if (allSatisfy!(isAssignable, Args))
{
typeof(this) opAssign(typeof(this) rhs)
{
foreach (i, Type; typeof(this.tupleof))
{
this.tupleof[i] = rhs.tupleof[i];
}
return this;
}
}
else
{
@disable typeof(this) opAssign(typeof(this) rhs);
}
/**
If the `Task` isn't started yet, execute it in the current thread.
If it's done, return its return value, if any. If it's in progress,
busy spin until it's done, then return the return value. If it threw
an exception, rethrow that exception.
This function should be used when you expect the result of the
`Task` to be available on a timescale shorter than that of an OS
context switch.
*/
@property ref ReturnType spinForce() @trusted
{
enforcePool();
this.pool.tryDeleteExecute(basePtr);
while (atomicReadUbyte(this.taskStatus) != TaskStatus.done) {}
if (exception)
{
throw exception;
}
static if (!is(ReturnType == void))
{
return fixRef(this.returnVal);
}
}
/**
If the `Task` isn't started yet, execute it in the current thread.
If it's done, return its return value, if any. If it's in progress,
wait on a condition variable. If it threw an exception, rethrow that
exception.
This function should be used for expensive functions, as waiting on a
condition variable introduces latency, but avoids wasted CPU cycles.
*/
@property ref ReturnType yieldForce() @trusted
{
enforcePool();
this.pool.tryDeleteExecute(basePtr);
if (done)
{
static if (is(ReturnType == void))
{
return;
}
else
{
return fixRef(this.returnVal);
}
}
pool.waiterLock();
scope(exit) pool.waiterUnlock();
while (atomicReadUbyte(this.taskStatus) != TaskStatus.done)
{
pool.waitUntilCompletion();
}
if (exception)
{
throw exception; // nocoverage
}
static if (!is(ReturnType == void))
{
return fixRef(this.returnVal);
}
}
/**
If this `Task` was not started yet, execute it in the current
thread. If it is finished, return its result. If it is in progress,
execute any other `Task` from the `TaskPool` instance that
this `Task` was submitted to until this one
is finished. If it threw an exception, rethrow that exception.
If no other tasks are available or this `Task` was executed using
`executeInNewThread`, wait on a condition variable.
*/
@property ref ReturnType workForce() @trusted
{
enforcePool();
this.pool.tryDeleteExecute(basePtr);
while (true)
{
if (done) // done() implicitly checks for exceptions.
{
static if (is(ReturnType == void))
{
return;
}
else
{
return fixRef(this.returnVal);
}
}
AbstractTask* job;
{
// Locking explicitly and calling popNoSync() because
// pop() waits on a condition variable if there are no Tasks
// in the queue.
pool.queueLock();
scope(exit) pool.queueUnlock();
job = pool.popNoSync();
}
if (job !is null)
{
version (verboseUnittest)
{
stderr.writeln("Doing workForce work.");
}
pool.doJob(job);
if (done)
{
static if (is(ReturnType == void))
{
return;
}
else
{
return fixRef(this.returnVal);
}
}
}
else
{
version (verboseUnittest)
{
stderr.writeln("Yield from workForce.");
}
return yieldForce;
}
}
}
/**
Returns `true` if the `Task` is finished executing.
Throws: Rethrows any exception thrown during the execution of the
`Task`.
*/
@property bool done() @trusted
{
// Explicitly forwarded for documentation purposes.
return base.done;
}
/**
Create a new thread for executing this `Task`, execute it in the
newly created thread, then terminate the thread. This can be used for
future/promise parallelism. An explicit priority may be given
to the `Task`. If one is provided, its value is forwarded to
`core.thread.Thread.priority`. See $(REF task, std,parallelism) for
usage example.
*/
void executeInNewThread() @trusted
{
pool = new TaskPool(basePtr);
}
/// Ditto
void executeInNewThread(int priority) @trusted
{
pool = new TaskPool(basePtr, priority);
}
@safe ~this()
{
if (isScoped && pool !is null && taskStatus != TaskStatus.done)
{
yieldForce;
}
}
// When this is uncommented, it somehow gets called on returning from
// scopedTask even though the struct shouldn't be getting copied.
//@disable this(this) {}
}
// Calls `fpOrDelegate` with `args`. This is an
// adapter that makes `Task` work with delegates, function pointers and
// functors instead of just aliases.
ReturnType!F run(F, Args...)(F fpOrDelegate, ref Args args)
{
return fpOrDelegate(args);
}
/**
Creates a `Task` on the GC heap that calls an alias. This may be executed
via `Task.executeInNewThread` or by submitting to a
$(REF TaskPool, std,parallelism). A globally accessible instance of
`TaskPool` is provided by $(REF taskPool, std,parallelism).
Returns: A pointer to the `Task`.
Example:
---
// Read two files into memory at the same time.
import std.file;
void main()
{
// Create and execute a Task for reading
// foo.txt.
auto file1Task = task!read("foo.txt");
file1Task.executeInNewThread();
// Read bar.txt in parallel.
auto file2Data = read("bar.txt");
// Get the results of reading foo.txt.
auto file1Data = file1Task.yieldForce;
}
---
---
// Sorts an array using a parallel quick sort algorithm.
// The first partition is done serially. Both recursion
// branches are then executed in parallel.
//
// Timings for sorting an array of 1,000,000 doubles on
// an Athlon 64 X2 dual core machine:
//
// This implementation: 176 milliseconds.
// Equivalent serial implementation: 280 milliseconds
void parallelSort(T)(T[] data)
{
// Sort small subarrays serially.
if (data.length < 100)
{
std.algorithm.sort(data);
return;
}
// Partition the array.
swap(data[$ / 2], data[$ - 1]);
auto pivot = data[$ - 1];
bool lessThanPivot(T elem) { return elem < pivot; }
auto greaterEqual = partition!lessThanPivot(data[0..$ - 1]);
swap(data[$ - greaterEqual.length - 1], data[$ - 1]);
auto less = data[0..$ - greaterEqual.length - 1];
greaterEqual = data[$ - greaterEqual.length..$];
// Execute both recursion branches in parallel.
auto recurseTask = task!parallelSort(greaterEqual);
taskPool.put(recurseTask);
parallelSort(less);
recurseTask.yieldForce;
}
---
*/
auto task(alias fun, Args...)(Args args)
{
return new Task!(fun, Args)(args);
}
/**
Creates a `Task` on the GC heap that calls a function pointer, delegate, or
class/struct with overloaded opCall.
Example:
---
// Read two files in at the same time again,
// but this time use a function pointer instead
// of an alias to represent std.file.read.
import std.file;
void main()
{
// Create and execute a Task for reading
// foo.txt.
auto file1Task = task(&read!string, "foo.txt", size_t.max);
file1Task.executeInNewThread();
// Read bar.txt in parallel.
auto file2Data = read("bar.txt");
// Get the results of reading foo.txt.
auto file1Data = file1Task.yieldForce;
}
---
Notes: This function takes a non-scope delegate, meaning it can be
used with closures. If you can't allocate a closure due to objects
on the stack that have scoped destruction, see `scopedTask`, which
takes a scope delegate.
*/
auto task(F, Args...)(F delegateOrFp, Args args)
if (is(typeof(delegateOrFp(args))) && !isSafeTask!F)
{
return new Task!(run, F, Args)(delegateOrFp, args);
}
/**
Version of `task` usable from `@safe` code. Usage mechanics are
identical to the non-@safe case, but safety introduces some restrictions:
1. `fun` must be @safe or @trusted.
2. `F` must not have any unshared aliasing as defined by
$(REF hasUnsharedAliasing, std,traits). This means it
may not be an unshared delegate or a non-shared class or struct
with overloaded `opCall`. This also precludes accepting template
alias parameters.
3. `Args` must not have unshared aliasing.
4. `fun` must not return by reference.
5. The return type must not have unshared aliasing unless `fun` is
`pure` or the `Task` is executed via `executeInNewThread` instead
of using a `TaskPool`.
*/
@trusted auto task(F, Args...)(F fun, Args args)
if (is(typeof(fun(args))) && isSafeTask!F)
{
return new Task!(run, F, Args)(fun, args);
}
/**
These functions allow the creation of `Task` objects on the stack rather
than the GC heap. The lifetime of a `Task` created by `scopedTask`
cannot exceed the lifetime of the scope it was created in.
`scopedTask` might be preferred over `task`:
1. When a `Task` that calls a delegate is being created and a closure
cannot be allocated due to objects on the stack that have scoped
destruction. The delegate overload of `scopedTask` takes a `scope`
delegate.
2. As a micro-optimization, to avoid the heap allocation associated with
`task` or with the creation of a closure.
Usage is otherwise identical to `task`.
Notes: `Task` objects created using `scopedTask` will automatically
call `Task.yieldForce` in their destructor if necessary to ensure
the `Task` is complete before the stack frame they reside on is destroyed.
*/
auto scopedTask(alias fun, Args...)(Args args)
{
auto ret = Task!(fun, Args)(args);
ret.isScoped = true;
return ret;
}
/// Ditto
auto scopedTask(F, Args...)(scope F delegateOrFp, Args args)
if (is(typeof(delegateOrFp(args))) && !isSafeTask!F)
{
auto ret = Task!(run, F, Args)(delegateOrFp, args);
ret.isScoped = true;
return ret;
}
/// Ditto
@trusted auto scopedTask(F, Args...)(F fun, Args args)
if (is(typeof(fun(args))) && isSafeTask!F)
{
auto ret = Task!(run, F, Args)(fun, args);
ret.isScoped = true;
return ret;
}
/**
The total number of CPU cores available on the current machine, as reported by
the operating system.
*/
alias totalCPUs =
__lazilyInitializedConstant!(immutable(uint), uint.max, totalCPUsImpl);
uint totalCPUsImpl() @nogc nothrow @trusted
{
version (Windows)
{
// BUGS: Only works on Windows 2000 and above.
import core.sys.windows.winbase : SYSTEM_INFO, GetSystemInfo;
import std.algorithm.comparison : max;
SYSTEM_INFO si;
GetSystemInfo(&si);
return max(1, cast(uint) si.dwNumberOfProcessors);
}
else version (linux)
{
import core.sys.linux.sched : CPU_COUNT, cpu_set_t, sched_getaffinity;
import core.sys.posix.unistd : _SC_NPROCESSORS_ONLN, sysconf;
cpu_set_t set = void;
if (sched_getaffinity(0, cpu_set_t.sizeof, &set) == 0)
{
int count = CPU_COUNT(&set);
if (count > 0)
return cast(uint) count;
}
return cast(uint) sysconf(_SC_NPROCESSORS_ONLN);
}
else version (Darwin)
{
import core.sys.darwin.sys.sysctl : sysctlbyname;
uint result;
size_t len = result.sizeof;
sysctlbyname("hw.physicalcpu", &result, &len, null, 0);
return result;
}
else version (DragonFlyBSD)
{
import core.sys.dragonflybsd.sys.sysctl : sysctlbyname;
uint result;
size_t len = result.sizeof;
sysctlbyname("hw.ncpu", &result, &len, null, 0);
return result;
}
else version (FreeBSD)
{
import core.sys.freebsd.sys.sysctl : sysctlbyname;
uint result;
size_t len = result.sizeof;
sysctlbyname("hw.ncpu", &result, &len, null, 0);
return result;
}
else version (NetBSD)
{
import core.sys.netbsd.sys.sysctl : sysctlbyname;
uint result;
size_t len = result.sizeof;
sysctlbyname("hw.ncpu", &result, &len, null, 0);
return result;