aboutsummaryrefslogtreecommitdiff
path: root/ARMeilleure/Translation/PTC/DegreeOfParallelism.cs
blob: e4752c5e3f8c5e99362d101cd79ca709f00149e2 (plain)
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
using System;

namespace ARMeilleure.Translation.PTC
{
    class DegreeOfParallelism
    {
        public double GiBRef { get; } // GiB.
        public double WeightRef { get; } // %.
        public double IncrementByGiB { get; } // %.
        private double _coefficient;

        public DegreeOfParallelism(double gibRef, double weightRef, double incrementByGiB)
        {
            GiBRef = gibRef;
            WeightRef = weightRef;
            IncrementByGiB = incrementByGiB;

            _coefficient = weightRef - (incrementByGiB * gibRef);
        }

        public int GetDegreeOfParallelism(int min, int max)
        {
            double degreeOfParallelism = (GetProcessorCount() * GetWeight(GetAvailableMemoryGiB())) / 100d;

            return Math.Clamp((int)Math.Round(degreeOfParallelism), min, max);
        }

        public static double GetProcessorCount()
        {
            return (double)Environment.ProcessorCount;
        }

        public double GetWeight(double gib)
        {
            return (IncrementByGiB * gib) + _coefficient;
        }

        public static double GetAvailableMemoryGiB()
        {
            GCMemoryInfo gcMemoryInfo = GC.GetGCMemoryInfo();

            return FromBytesToGiB(gcMemoryInfo.TotalAvailableMemoryBytes - gcMemoryInfo.MemoryLoadBytes);
        }

        private static double FromBytesToGiB(long bytes)
        {
            return Math.ScaleB((double)bytes, -30);
        }
    }
}