developpement:dotnet:csharp:introduction
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédenteDernière révisionLes deux révisions suivantes | ||
developpement:dotnet:csharp:introduction [2023/10/04 17:23] – [Task Parallel Library (TPL)] sgariepy | developpement:dotnet:csharp:introduction [2023/10/05 15:11] – [PLINQ] sgariepy | ||
---|---|---|---|
Ligne 1271: | Ligne 1271: | ||
Le cas en parallèle prend plus de temps dû au overhead. | Le cas en parallèle prend plus de temps dû au overhead. | ||
+ | |||
+ | |||
+ | <code csharp> | ||
+ | // Générer des images: https:// | ||
+ | |||
+ | void Main() | ||
+ | { | ||
+ | var path = Directory.GetCurrentDirectory(); | ||
+ | var files = Directory.GetFiles(path + @" | ||
+ | | ||
+ | var normalAlteredPath = path + @" | ||
+ | var parallelAlteredPath = path + @" | ||
+ | Directory.CreateDirectory(normalAlteredPath); | ||
+ | Directory.CreateDirectory(parallelAlteredPath); | ||
+ | | ||
+ | ParallelExecutionMode(files, | ||
+ | NormalExecutionMode(files, | ||
+ | } | ||
+ | |||
+ | void NormalExecutionMode(string[] files, string alteredPath) | ||
+ | { | ||
+ | Stopwatch stopwatch = Stopwatch.StartNew(); | ||
+ | foreach (var currentFile in files) | ||
+ | { | ||
+ | var file = Path.GetFileName(currentFile); | ||
+ | using (var fileBitmap = new Bitmap(currentFile)) | ||
+ | { | ||
+ | fileBitmap.RotateFlip(RotateFlipType.Rotate270FlipX); | ||
+ | fileBitmap.Save(Path.Combine(alteredPath, | ||
+ | Console.WriteLine(" | ||
+ | } | ||
+ | } | ||
+ | Console.WriteLine(" | ||
+ | stopwatch.Stop(); | ||
+ | } | ||
+ | |||
+ | void ParallelExecutionMode(string[] files, string alteredPath) | ||
+ | { | ||
+ | Stopwatch stopwatch = Stopwatch.StartNew(); | ||
+ | |||
+ | Parallel.ForEach(files, | ||
+ | var file = Path.GetFileName(currentFile); | ||
+ | using (var fileBitmap = new Bitmap(currentFile)) | ||
+ | { | ||
+ | fileBitmap.RotateFlip(RotateFlipType.Rotate270FlipX); | ||
+ | fileBitmap.Save(Path.Combine(alteredPath, | ||
+ | Console.WriteLine(" | ||
+ | } | ||
+ | }); | ||
+ | |||
+ | Console.WriteLine(" | ||
+ | stopwatch.Stop(); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | <code csharp> | ||
+ | void Main() | ||
+ | { | ||
+ | var list = Enumerable.Range(0, | ||
+ | CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); | ||
+ | | ||
+ | ParallelOptions parallelOptions = new ParallelOptions(); | ||
+ | parallelOptions.CancellationToken = cancellationTokenSource.Token; | ||
+ | parallelOptions.MaxDegreeOfParallelism = System.Environment.ProcessorCount; | ||
+ | | ||
+ | Console.WriteLine(" | ||
+ | | ||
+ | Task.Factory.StartNew(() => | ||
+ | { | ||
+ | if (Console.ReadLine() == " | ||
+ | { | ||
+ | cancellationTokenSource.Cancel(); | ||
+ | } | ||
+ | | ||
+ | long total = 0; | ||
+ | | ||
+ | try | ||
+ | { | ||
+ | Parallel.For< | ||
+ | { | ||
+ | Thread.Sleep(200); | ||
+ | parallelOptions.CancellationToken.ThrowIfCancellationRequested(); | ||
+ | subtotal += list[count]; | ||
+ | return subtotal; | ||
+ | }, | ||
+ | (x) => | ||
+ | { | ||
+ | Interlocked.Add(ref total, x); | ||
+ | }); | ||
+ | } | ||
+ | catch (OperationCanceledException ex) | ||
+ | { | ||
+ | Console.WriteLine(" | ||
+ | } | ||
+ | finally | ||
+ | { | ||
+ | cancellationTokenSource.Dispose(); | ||
+ | } | ||
+ | Console.WriteLine(" | ||
+ | }); | ||
+ | | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== Continuation with state ===== | ||
+ | |||
+ | <code csharp> | ||
+ | void Main() | ||
+ | { | ||
+ | Task< | ||
+ | List< | ||
+ | | ||
+ | for (int i = 0; i < 3; i++) | ||
+ | { | ||
+ | task = task.ContinueWith((x, | ||
+ | continuationTasks.Add(task); | ||
+ | } | ||
+ | | ||
+ | task.Wait(); | ||
+ | | ||
+ | foreach (var continuation in continuationTasks) | ||
+ | { | ||
+ | Person person = continuation.AsyncState as Person; | ||
+ | Console.WriteLine(" | ||
+ | } | ||
+ | } | ||
+ | |||
+ | static DateTime DoSomething() | ||
+ | { | ||
+ | return DateTime.Now; | ||
+ | } | ||
+ | |||
+ | internal class Person | ||
+ | { | ||
+ | public int Id { get; set; } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== TaskCompletionSource ===== | ||
+ | |||
+ | <code csharp> | ||
+ | void Main() | ||
+ | { | ||
+ | TaskCompletionSource< | ||
+ | Task< | ||
+ | | ||
+ | Task.Factory.StartNew(() => { | ||
+ | Thread.Sleep(2000); | ||
+ | taskCompletionSource.SetResult(new Product { Id = 1, Name = "Some name" }); | ||
+ | }); | ||
+ | | ||
+ | Task.Factory.StartNew(() => | ||
+ | { | ||
+ | if (Console.ReadLine() == " | ||
+ | { | ||
+ | Product result = lazyTask.Result; | ||
+ | Console.WriteLine(" | ||
+ | } | ||
+ | }); | ||
+ | | ||
+ | Thread.Sleep(5000); | ||
+ | } | ||
+ | |||
+ | class Product | ||
+ | { | ||
+ | public int Id { get; set; } | ||
+ | public string Name { get; set; } | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | ===== PLINQ ===== | ||
+ | |||
+ | Parallel LINQ: | ||
+ | |||
+ | * Automates parallelization | ||
+ | * Considéré déclaratif plutôt qu' | ||
+ | * Opérateurs qui font en sorte que ce n'est pas parallélisé: | ||
+ | * Take, Select, SelectMany, Skip, TakeWhile, SkipWhile, ElementAt | ||
+ | * Anomalies | ||
+ | * Join, GroupBy, GroupJoin, Distinct, Union, Intersect, Except | ||
+ | * Force parallelism: | ||
+ | * .AsParallel().withExecutionMode(ParallelExecution.ForceParallelism) | ||
+ | |||
+ | |||
+ | <code csharp> | ||
+ | void Main() | ||
+ | { | ||
+ | var list = Enumerable.Range(1, | ||
+ | var primeNumbers = list | ||
+ | .AsParallel() | ||
+ | .Where(IsPrime); | ||
+ | Console.WriteLine(" | ||
+ | } | ||
+ | |||
+ | bool IsPrime(int x) | ||
+ | { | ||
+ | if (x == 1) return false; | ||
+ | if (x == 2) return true; | ||
+ | if (x % 2 == 0) return false; | ||
+ | var boundary = (int)Math.Floor(Math.Sqrt(x)); | ||
+ | |||
+ | for (int i = 3; i <= boundary; i += 2) | ||
+ | { | ||
+ | if (x % i == 0) | ||
+ | { | ||
+ | return false; | ||
+ | } | ||
+ | } | ||
+ | return true; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ==== Degree of Parallelism ==== | ||
+ | |||
+ | <code csharp> | ||
+ | void Main() | ||
+ | { | ||
+ | List< | ||
+ | websites.Add(" | ||
+ | websites.Add(" | ||
+ | websites.Add(" | ||
+ | | ||
+ | List< | ||
+ | .AsParallel() | ||
+ | .WithDegreeOfParallelism(websites.Count()) | ||
+ | .Select(PingSites) | ||
+ | .ToList(); | ||
+ | | ||
+ | foreach (var response in responses) | ||
+ | { | ||
+ | Console.WriteLine(response.Address + " " + response.Status + " " + response.RoundtripTime); | ||
+ | } | ||
+ | | ||
+ | Console.ReadLine(); | ||
+ | } | ||
+ | |||
+ | private static PingReply PingSites(string websiteName) | ||
+ | { | ||
+ | Ping ping = new Ping(); | ||
+ | return ping.Send(websiteName); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | |||
====== Thread Marshalling ====== | ====== Thread Marshalling ====== |
developpement/dotnet/csharp/introduction.txt · Dernière modification : 2023/10/06 05:06 de sgariepy