Created
May 30, 2012 15:54
-
-
Save prabirshrestha/2837187 to your computer and use it in GitHub Desktop.
MapReduce in C# using TPL.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // http://ox.no/files/MapReduce.cs | |
| public static class MapReduce | |
| { | |
| public static Task<TResult> Start<TInput, TPartialResult, TResult>(Func<TInput, TPartialResult> map, Func<TPartialResult[], TResult> reduce, params TInput[] inputs) | |
| { | |
| var mapTasks = CreateMapTasks(map, inputs); | |
| var reduceTask = CreateReduceTask(reduce, mapTasks); | |
| return reduceTask; | |
| } | |
| private static Task<TResult> CreateReduceTask<TPartialResult, TResult>(Func<TPartialResult[], TResult> reduce, Task<TPartialResult>[] mapTasks) | |
| { | |
| return Task.Factory.ContinueWhenAll(mapTasks, tasks => PerformReduce(reduce, tasks)); | |
| } | |
| private static TResult PerformReduce<TPartialResult, TResult>(Func<TPartialResult[], TResult> reduce, Task<TPartialResult>[] tasks) | |
| { | |
| var results = tasks.Select(task => task.Result); | |
| return reduce(results.ToArray()); | |
| } | |
| private static Task<TPartialResult>[] CreateMapTasks<TInput, TPartialResult>(Func<TInput, TPartialResult> map, TInput[] inputs) | |
| { | |
| var tasks = new Task<TPartialResult>[inputs.Length]; | |
| for (int i = 0; i < inputs.Length; ++i) | |
| { | |
| var input = inputs[i]; | |
| tasks[i] = Task.Factory.StartNew(() => map(input)); | |
| } | |
| return tasks; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment