Code Pyre

All Code Dies and Burns in Time

Fork me on GitHub

Parallel Functionals

| Comments

I have used the loop tiling code with scheduling algorithms to parallelize Dustin Campbell’s code from his post A Higher Calling. The parallelized functionals can be found below. It uses the scheduling algorithm code from the last post. The parallel reduction is actually based on Joe Duffy’s serial reduction.

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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;

namespace Functionals
{
    public static class Parallel
    {
        private static readonly int ProcessorCount = Environment.ProcessorCount;
 
        [ThreadStatic]
        private static int partitions = ProcessorCount;

        public static int Partitions
        {
            get { return partitions; }
            set { partitions = value; }
        }
 
        [ThreadStatic]
        private static SchedulingAlgorithms algorithm = SchedulingAlgorithms.Factoring;

        public static SchedulingAlgorithms Algorithm
        {
            get { return algorithm; }
            set { algorithm = value; }
        }
 
        [ThreadStatic]
        private static double factor = 0.5;

        public static double Factor
        {
            get { return factor; }
            set { factor = value; }
        }
 
        [ThreadStatic]
        private static int factoringThreshold = 1;

        public static int FactoringThreshold
        {
            get { return factoringThreshold; }
            set { factoringThreshold = value; }
        }

        /// <summary>
        /// Filters the specified source as defined in <see cref="Functionals.Serial"/> except
        /// that the operation is done in parallel with the specified number of simultaneous operations.
        /// </summary>
        /// <typeparam name="TSource">The type of the source.</typeparam>
        /// <param name="source">The source.</param>
        /// <param name="predicate">The predicate.</param>
        /// <returns></returns>
        public static IList<TSource> Filter<TSource>( IList<TSource> source, Predicate<TSource> predicate )
        {
            Debug.Assert( partitions >= 1 );

            List<TSource> result = new List<TSource>();
            if ( source != null )
            {
                For( source, delegate( TSource item )
                            {
                                if ( predicate( item ) )
                                {
                                    result.Add( item );
                                }
                            } );
            }

            return result;
        }

        /// <summary>
        /// Map transforms each item with a conversion function as defined in
        /// <see cref="Functionals.Serial.Map"/> except
        /// that the operation is done in parallel with the specified number of simultaneous operations.
        /// </summary>
        public static IList<TResult> Map<TSource, TResult>( IList<TSource> source,
                                                            Converter<TSource, TResult> converter )
        {
            Debug.Assert( partitions >= 1 );

            List<TResult> result = new List<TResult>();

            if ( source != null )
            {
                For( source, delegate( TSource item ) { result.Add( converter( item ) ); } );
            }

            return result;
        }

        /// <summary>
        /// Reduces the specified source using the given accumlator as defined in
        /// <see cref="Functionals.Serial.Reduce"/> except
        /// that the operation is done in parallel with the specified number of simultaneous operations.
        /// </summary>
        /// <typeparam name="TSource">The type of the source.</typeparam>
        /// <param name="source">The source.</param>
        /// <param name="startValue">The start value.</param>
        /// <param name="accumulator">The accumulator.</param>
        /// <returns></returns>
        public static TSource Reduce<TSource>( IList<TSource> source, TSource startValue, Accumulator<TSource, TSource> accumulator )
        {
            Debug.Assert( partitions >= 1 );
            List<int> gp = GetPartitionList( source.Count );
            partitions = gp.Count;

            TSource[] partialReductionResults = new TSource[partitions];
            int partitionSize = ( source.Count + partitions - 1 ) / partitions;
            For( 0, partitions, delegate( int index )
                                {
                                    partialReductionResults[index] = default( TSource );
                                    int end = Math.Min( source.Count, partitionSize * ( index + 1 ) );
                                    for (int j = partitionSize * index; j < end; j++)
                                    {
                                        partialReductionResults[index] = accumulator( partialReductionResults[index], source[j] );
                                    }
                                } );

            // Do the final reduction on the master thread.
            return Serial.Reduce( partialReductionResults, startValue, accumulator );
        }

        /// <summary>
        /// Implements a parallel for loop of the form
        /// <code>
        /// for(int index = from; index &lt; to; index++)
        /// {
        ///    indexBoundClause(index);
        /// }
        /// </code>
        /// which will attempt to run in partitions equal to the number of processors.
        /// </summary>
        /// <param name="from">The start index.</param>
        /// <param name="to">the end index + 1.</param>
        /// <param name="indexBoundClause">The index bound clause to execute.</param>
        public static void For( int from, int to, Action<int> indexBoundClause )
        {
            For<int>( null, from, to, null, indexBoundClause );
        }

        /// <summary>
        /// Implements a parallel for loop of the form
        /// <code>
        /// for(int index = 0; index &lt; data.Count; index++)
        /// {
        ///    dataBoundClause(data[index]);
        /// }
        /// </code>
        /// which will attempt to run in partitions equal to the number of processors.
        /// </summary>
        /// <typeparam name="TInput">The type of the input.</typeparam>
        /// <param name="data">The data.</param>
        /// <param name="dataBoundClause">The data bound clause which is fed pieces of data.</param>
        public static void For<TInput>( IList<TInput> data, Action<TInput> dataBoundClause )
        {
            For( data, 0, data.Count, dataBoundClause, null );
        }

        /// <summary>
        /// Implements a parallel for loop that is either data or index bound. Depending on the
        /// overload that was called we will either be iterating over data or acting on an index.
        /// Either way we determine the bounds of our loop and partition the work using delegates
        /// and the ThreadPool. Once all pooled threads have finished this call will exit.
        /// </summary>
        /// <typeparam name="TInput">The type of the input.</typeparam>
        /// <param name="data">The data to pass into the dataBoundClause.</param>
        /// <param name="from">The starting index.</param>
        /// <param name="to">The ending index + 1.</param>
        /// <param name="dataBoundClause">The data bound clause.</param>
        /// <param name="indexBoundClause">The index bound clause.</param>
        private static void For<TInput>( IList<TInput> data, int from, int to, Action<TInput> dataBoundClause, Action<int> indexBoundClause )
        {
            Debug.Assert( from < to );
            Debug.Assert( ( dataBoundClause != null ) || ( indexBoundClause != null ) );
            Debug.Assert( ( data != null && dataBoundClause != null ) || ( data == null && dataBoundClause == null ) );

            int size = to - from;
            int offset = 0;
            List<int> gp = GetPartitionList( size );

            int parts = gp.Count;
            CountdownLatch latch = new CountdownLatch( parts );
            for (int i = 0; i < parts; i++)
            {
                int start = offset + from;
                int partitionSize = gp[0];
                gp.RemoveAt( 0 );
                int end = Math.Min( to, start + partitionSize );
                offset += partitionSize;

                Debug.WriteLine( string.Format( "From: {0}\tTo: {1}\tTotal: {2}", start, end, end - start ) );

                ThreadPool.QueueUserWorkItem( delegate
                                              {
                                                  for (int j = start; j < end; j++)
                                                  {
                                                      if ( data != null )
                                                      {
                                                          dataBoundClause( data[j] );
                                                      }
                                                      else
                                                      {
                                                          indexBoundClause( j );
                                                      }
                                                  }

                                                  latch.Signal();
                                              } );
            }
            latch.Wait();
        }

        /// <summary>
        /// Gets the partition list.
        /// </summary>
        /// <param name="size">The size.</param>
        /// <returns></returns>
        private static List<int> GetPartitionList( int size )
        {
            ISchedulingAlgorithm schedulingAlgorith = null;

            switch ( Algorithm )
            {
                case SchedulingAlgorithms.FixedChunking:
                    schedulingAlgorith = new FixedChunking( size, Math.Min( size, partitions ) );
                    break;
                case SchedulingAlgorithms.GuidedSelfScheduling:
                    schedulingAlgorith = new GuidedSelfScheduling( size );
                    break;
                case SchedulingAlgorithms.Factoring:
                    schedulingAlgorith = new Factoring( size, factor, factoringThreshold );
                    break;
            }

            Debug.Assert( schedulingAlgorith != null );
            return schedulingAlgorith.GetPartitionSizes();
        }
    }
}

Comments