DegreeOfParallelism.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using System;
  2. namespace ARMeilleure.Translation.PTC
  3. {
  4. class DegreeOfParallelism
  5. {
  6. public double GiBRef { get; } // GiB.
  7. public double WeightRef { get; } // %.
  8. public double IncrementByGiB { get; } // %.
  9. private double _coefficient;
  10. public DegreeOfParallelism(double gibRef, double weightRef, double incrementByGiB)
  11. {
  12. GiBRef = gibRef;
  13. WeightRef = weightRef;
  14. IncrementByGiB = incrementByGiB;
  15. _coefficient = weightRef - (incrementByGiB * gibRef);
  16. }
  17. public int GetDegreeOfParallelism(int min, int max)
  18. {
  19. double degreeOfParallelism = (GetProcessorCount() * GetWeight(GetAvailableMemoryGiB())) / 100d;
  20. return Math.Clamp((int)Math.Round(degreeOfParallelism), min, max);
  21. }
  22. public static double GetProcessorCount()
  23. {
  24. return (double)Environment.ProcessorCount;
  25. }
  26. public double GetWeight(double gib)
  27. {
  28. return (IncrementByGiB * gib) + _coefficient;
  29. }
  30. public static double GetAvailableMemoryGiB()
  31. {
  32. GCMemoryInfo gcMemoryInfo = GC.GetGCMemoryInfo();
  33. return FromBytesToGiB(gcMemoryInfo.TotalAvailableMemoryBytes - gcMemoryInfo.MemoryLoadBytes);
  34. }
  35. private static double FromBytesToGiB(long bytes)
  36. {
  37. return Math.ScaleB((double)bytes, -30);
  38. }
  39. }
  40. }