Skip to content

Instantly share code, notes, and snippets.

@omar-3
Created March 29, 2020 03:17
Show Gist options
  • Save omar-3/539132ed3f87f3bd271fedeaf5bf309a to your computer and use it in GitHub Desktop.
Save omar-3/539132ed3f87f3bd271fedeaf5bf309a to your computer and use it in GitHub Desktop.
Implementation of python's dropwhile() method in chapel
use RangeChunk;
use Time;
iter dropwhile(array, function) {
var iterable = array;
var barrier = false;
for i in iterable {
if function(i){
barrier = true;
}
if barrier {
yield i;
}
}
}
// I can't make follower iterators speak with each other
// the leader would get the index of the "barrier" element
// and delegate the yielding task to the followers
iter dropwhile(param tag: iterKind, array, function)
where tag == iterKind.leader {
var firstIndex = 1; // first index
while !function(array[firstIndex]) { // post-fix addition would have been nice here?
firstIndex = firstIndex + 1;
}
var numTasks = here.maxTaskPar;
var Range = firstIndex..array.domain.high;
coforall tid in 0..#numTasks {
var nowRange = chunk(Range, numTasks, tid);
yield (nowRange, );
}
}
iter dropwhile(param tag: iterKind, array, function, followThis)
where tag == iterKind.follower && followThis.size == 1 {
var nowRange = followThis(1);
for i in nowRange {
yield array[i];
}
}
var used = [1,2,3,4,5,4,3,2,3,4,6,7,9,10];
proc threshold(x: int) : bool {
return x == 5;
}
var a = getCurrentTime();
for i in dropwhile(used, threshold) {
writeln(i);
}
writeln("this is serial time");
writeln(getCurrentTime() - a);
var b = getCurrentTime();
forall i in dropwhile(used, threshold) {
writeln(i);
}
writeln("this is the parallel time");
writeln(getCurrentTime() - b);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment