MovGP0 | Über mich | Hilfen | Artikel | Weblinks | Literatur | Zitate | Notizen | Programmierung | MSCert | Physik |
Parallel LoopsBearbeiten
MethodsBearbeiten
Loop StateBearbeiten
Loop Cancellation TokenBearbeiten
int n = ...;
var token = cancellationTokenSource.Token;
var options = new ParallelOptions {
CancellationToken = token
};
try
{
Parallel.For(0, n, options, i => {
if(token.IsCancellationRequested)
{
return;
}
// do some work
});
}
catch(OperationCancelledException)
{
// ...
}
Exception HandlingBearbeiten
PartitionerBearbeiten
int n = ...;
var result = double[n];
Parallel.ForEach(Partitioner.Create(0, n), range =>
{
foreach(var index in range)
{
// ...
}
});
Degree of ParallelismBearbeiten
var n = ...;
var options = new ParallelOptions() {
MaxDegreeOfParallelism = 2
};
Parallel.For(0, n, options, i => {
// ...
});
enumerable.AsParallel().WithDegreeOfParallelism(n)
Task-Local StateBearbeiten
int numberOfSteps = ...;
var result = new double[numberOfSteps];
var partitioner = Partitioner.Create(0, numberOfSteps);
var options = new ParallelOptions { /* ... */ };
Parallel.ForEach(partitioner, options, () => new Random(MakeRandomSeed()), (range, loopState, localRandom) =>
{
foreach(var index in range)
{
result[i] = localRandom.NextDouble();
return localRandom;
}
}, localRandom => { /* finally */ });
Note:
Task SchedulerBearbeiten
int n = ...;
var taskScheduler = new TaskScheduler(/* ... */);
var options = new ParallelOptions
{
TaskScheduler = taskScheduler
};
Parallel.For(0, j, options, i => { /* ... */});
AntipatternsBearbeitenStep size other than oneBearbeiten
Hidden loop body dependenciesBearbeiten
Small loop bodies with few iterationsBearbeiten
Processor oversubscription and undersubscriptionBearbeiten
Parallel.ForEach on PLINQ QueryBearbeiten
Duplicates in Input EnumerationBearbeiten
Design NotesBearbeiten
|