Code Pyre

All Code Dies and Burns in Time

Fork me on GitHub

Scheduling Algorithms

| Comments

Below is the scheduling algorithms code that I referenced in my previous post. I no longer have access to a multi core/processor machine so I cannot benchmark them properly right now.

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
using System;
using System.Collections.Generic;

namespace Functionals
{
    public enum SchedulingAlgorithms : int
    {
        FixedChunking = 0,
        GuidedSelfScheduling,
        Factoring
    }

    public interface ISchedulingAlgorithm
    {
        /// <summary>
        /// Gets the partition sizes.
        /// </summary>
        /// <returns></returns>
        List<int> GetPartitionSizes();
    }

    public abstract class SchedulingAlgorithmBase : ISchedulingAlgorithm
    {
        protected static readonly int ProcessorCount = Environment.ProcessorCount;

        #region ISchedulingAlgorithm Members

        /// <summary>
        /// Gets the partition sizes.
        /// </summary>
        /// <returns></returns>
        public abstract List<int> GetPartitionSizes();

        #endregion
    }

    public sealed class FixedChunking : SchedulingAlgorithmBase
    {
        private readonly int size;
        private readonly int partitions;

        /// <summary>
        /// Initializes a new instance of the <see cref="FixedChunking"/> class.
        /// </summary>
        /// <param name="size">The size.</param>
        /// <param name="partitions">The partitions.</param>
        public FixedChunking( int size, int partitions )
        {
            this.size = size;
            this.partitions = partitions;
        }

        #region ISchedulingAlgorithm Members

        /// <summary>
        /// Gets the partition sizes.
        /// </summary>
        /// <returns></returns>
        public override List<int> GetPartitionSizes()
        {
            int numberOfPartitions = Math.Min( partitions, size );
            List<int> granualPartitionSizes = new List<int>( numberOfPartitions );
            int partitionSize = ( size + numberOfPartitions - 1 ) / numberOfPartitions;
            for (int i = 0; i < numberOfPartitions; i++)
            {
                granualPartitionSizes.Add( partitionSize );
            }

            return granualPartitionSizes;
        }

        #endregion
    }

    public sealed class GuidedSelfScheduling : SchedulingAlgorithmBase
    {
        private readonly int size;

        /// <summary>
        /// Initializes a new instance of the <see cref="GuidedSelfScheduling"/> class.
        /// </summary>
        /// <param name="size">The size.</param>
        public GuidedSelfScheduling( int size )
        {
            this.size = size;
        }

        #region ISchedulingAlgorithm Members

        /// <summary>
        /// Gets the partition sizes.
        /// </summary>
        /// <returns></returns>
        public override List<int> GetPartitionSizes()
        {
            double processors = Math.Max( ProcessorCount, 2 );
            int partitions = 1;
            if ( size > 1 )
            {
                partitions = (int) Math.Ceiling( Math.Log( size, processors ) );
            }
            int remaining = size;
            List<int> granualPartitionSizes = new List<int>( partitions );
            for (int i = 0; i < partitions; i++)
            {
                int partitionSize = (int) Math.Ceiling( ( remaining / processors ) );
                remaining -= partitionSize;
                granualPartitionSizes.Add( partitionSize );
            }

            return granualPartitionSizes;
        }

        #endregion
    }

    public sealed class Factoring : SchedulingAlgorithmBase
    {
        private readonly int threshold = 1;
        private readonly int size;
        private readonly double factor;

        /// <summary>
        /// Initializes a new instance of the <see cref="Factoring"/> class.
        /// </summary>
        /// <param name="size">The size.</param>
        /// <param name="factor">The factor.</param>
        public Factoring( int size, double factor )
        {
            this.size = size;
            this.factor = factor;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Factoring"/> class.
        /// </summary>
        /// <param name="size">The size.</param>
        /// <param name="factor">The factor.</param>
        /// <param name="threshold">The threshold.</param>
        public Factoring( int size, double factor, int threshold )
        {
            this.size = size;
            this.factor = factor;
            this.threshold = threshold;
        }

        #region ISchedulingAlgorithm Members

        /// <summary>
        /// Gets the partition sizes.
        /// </summary>
        /// <returns></returns>
        public override List<int> GetPartitionSizes()
        {
            int processors = Math.Max( ProcessorCount, 2 );
            int remaining = size;
            int granualSize = 1;
            List<int> granualPartitionSizes = new List<int>();
            while ( ( remaining > threshold ) &&
                    ( granualSize > 0 ) )
            {
                granualSize = (int) Math.Ceiling( ( remaining * factor ) / processors );
                remaining = remaining - ( granualSize * processors );
                if ( granualSize > 0 )
                {
                    for (int i = 0; i < processors; i++)
                    {
                        granualPartitionSizes.Add( granualSize );
                    }
                }
            }
            if ( remaining > 0 )
            {
                granualPartitionSizes.Add( remaining );
            }
            return granualPartitionSizes;
        }

        #endregion
    }
}

« Correctness of Parallel Code Parallel Functionals »

Comments