Last active
May 31, 2016 06:54
-
-
Save VlasovRoman/4c78a123acd44b3da8b96b43637df080 to your computer and use it in GitHub Desktop.
This file contains 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
module pmap; | |
import std.algorithm; | |
import std.parallelism; | |
import std.range; | |
template pmap(fun...) { | |
auto pmap(Range) (Range inputRange) { | |
alias RangeElementType = ElementType!Range; | |
auto chunkSize = taskPool.defaultWorkUnitSize(inputRange.length); | |
auto inputChunks = inputRange.chunks(chunkSize); | |
alias Result = typeof(MapResult!(fun, Range).front); | |
auto size = inputRange.length; | |
Result[] outputRange = new Result[size]; | |
auto outputChunks = outputRange.chunks(chunkSize); | |
foreach(ioChunk; zip(inputChunks, outputChunks).parallel) { | |
auto inputChunk = ioChunk[0]; | |
auto outputChunk = ioChunk[1]; | |
int i = 0; | |
foreach(atom; inputChunk) { | |
outputChunk[i++] = fun[0](atom); | |
} | |
} | |
return MapResult!(a => a, Result[])(outputRange); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment