-
-
Save jnorwood/affd05b69795c20989a3 to your computer and use it in GitHub Desktop.
ndslice parallel median attempt with error message.
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
import std.array : array; | |
import std.algorithm; | |
import std.datetime; | |
import std.conv : to; | |
import std.stdio; | |
import std.experimental.ndslice; | |
shared double[] medians; | |
double[] data; | |
shared double[] dbuf; | |
int numTasks; | |
const int smalld = 1000; | |
const int bigd = 10_000; | |
const int fulld = bigd*smalld; | |
/** | |
Params: | |
r = input range | |
buf = buffer with length no less than the number of elements in `r` | |
Returns: | |
median value over the range `r` | |
*/ | |
T median(Range, T)(Range r, T[] buf) | |
{ | |
import std.algorithm.sorting: sort; | |
size_t n; | |
foreach (e; r) { | |
buf[n++] = e; | |
} | |
buf[0 .. n].sort(); | |
immutable m = n >> 1; | |
return n & 1 ? buf[m] : cast(T)((buf[m - 1] + buf[m]) / 2); | |
} | |
void f3() { | |
import std.parallelism; | |
auto sl = data.sliced(smalld,bigd); | |
auto slb = dbuf.sliced(numTasks,bigd); | |
foreach(i,vec; parallel(sl)){ | |
int task = taskPool.workerIndex; | |
int j = task*bigd; | |
int k = j+bigd; | |
medians[i] = median(vec,slb[task]); | |
//medians[i] = median(vec,dbuf[j .. k]); | |
} | |
} | |
void main() { | |
import std.parallelism; | |
numTasks = taskPool.size+1; | |
data = new double[fulld]; | |
dbuf = new double[bigd*numTasks]; | |
medians = new double[smalld]; | |
for(int i=0;i<fulld;i++){ data[i] = i/(fulld*1.0);} | |
StopWatch sw3; | |
sw3.start(); | |
f3() ; | |
auto r3 = sw3.peek().msecs; | |
writeln("medians parallel:",medians); | |
writeln("parallel time medians msec:",r3); | |
} |
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
{ | |
"name" : "nd10", | |
"description" : "Hello World - A minimal DUB bundle.", | |
"dependencies" : { | |
"dip80-ndslice": "~>0.8.8" | |
} | |
} |
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
Severity Code Description Project File Line Suppression State | |
Error Error: template app.median cannot deduce function from argument types !()(Slice!(1u, double*), Slice!(1u, shared(double)*)), candidates are: D:\ec_mars_ddt\workspace\nd10\src\app.d 47 | |
Error app.median(Range, T)(Range r, T[] buf) D:\ec_mars_ddt\workspace\nd10\src\app.d 23 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment