Skip to content

Commit 77f6099

Browse files
committed
Using unsafe pointers to views to try to reduce some vector copy and allocation time. Doesn't seem to have made much difference.
1 parent ea88974 commit 77f6099

File tree

1 file changed

+48
-20
lines changed

1 file changed

+48
-20
lines changed

src/test/bench/graph500-bfs.rs

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ fn pbfs(graph: graph, key: node_id) -> bfs_result {
273273
// Do the BFS.
274274
log(info, #fmt("PBFS iteration %?", i));
275275
i += 1u;
276+
let old_len = colors.len();
276277
colors = par::mapi(colors) {|i, c, copy colors|
277278
let c : color = c;
278279
alt c {
@@ -296,7 +297,8 @@ fn pbfs(graph: graph, key: node_id) -> bfs_result {
296297
gray(parent) { black(parent) }
297298
black(parent) { black(parent) }
298299
}
299-
}
300+
};
301+
assert(colors.len() == old_len);
300302
}
301303

302304
// Convert the results.
@@ -403,10 +405,10 @@ fn validate(edges: [(node_id, node_id)],
403405
}
404406

405407
fn main() {
406-
let scale = 14u;
407-
let num_keys = 16u;
408+
let scale = 18u;
409+
let num_keys = 64u;
408410
let do_validate = false;
409-
let do_sequential = true;
411+
let do_sequential = false;
410412

411413
let start = time::precise_time_s();
412414
let edges = make_edges(scale, 16u);
@@ -522,7 +524,9 @@ const min_granularity : uint = 1024u;
522524
523525
This is used to build most of the other parallel vector functions,
524526
like map or alli."]
525-
fn map_slices<A: send, B: send>(xs: [A], f: fn~(uint, [A]) -> B) -> [B] {
527+
fn map_slices<A: send, B: send>(xs: [A], f: fn~(uint, [const A]/&) -> B)
528+
-> [B] {
529+
526530
let len = xs.len();
527531
if len < min_granularity {
528532
log(info, "small slice");
@@ -538,21 +542,37 @@ fn map_slices<A: send, B: send>(xs: [A], f: fn~(uint, [A]) -> B) -> [B] {
538542
let mut base = 0u;
539543
log(info, "spawning tasks");
540544
while base < len {
541-
let slice = vec::slice(xs, base,
542-
uint::min(len, base + items_per_task));
543-
let f = ptr::addr_of(f);
544-
futures += [future::spawn() {|copy base|
545-
unsafe {
546-
(*f)(base, slice)
547-
}
548-
}];
545+
let end = uint::min(len, base + items_per_task);
546+
// FIXME: why is the ::<A, ()> annotation required here?
547+
vec::unpack_slice::<A, ()>(xs) {|p, _len|
548+
let f = ptr::addr_of(f);
549+
futures += [future::spawn() {|copy base|
550+
unsafe {
551+
let len = end - base;
552+
let slice = (ptr::offset(p, base),
553+
len * sys::size_of::<A>());
554+
log(info, #fmt("pre-slice: %?", (base, slice)));
555+
let slice : [const A]/& =
556+
unsafe::reinterpret_cast(slice);
557+
log(info, #fmt("slice: %?",
558+
(base, vec::len(slice), end - base)));
559+
assert(vec::len(slice) == end - base);
560+
(*f)(base, slice)
561+
}
562+
}];
563+
};
549564
base += items_per_task;
550565
}
551566
log(info, "tasks spawned");
552567

553-
futures.map() {|ys|
568+
log(info, #fmt("num_tasks: %?", (num_tasks, futures.len())));
569+
assert(num_tasks == futures.len());
570+
571+
let r = futures.map() {|ys|
554572
ys.get()
555-
}
573+
};
574+
assert(r.len() == futures.len());
575+
r
556576
}
557577
}
558578

@@ -565,17 +585,25 @@ fn map<A: send, B: send>(xs: [A], f: fn~(A) -> B) -> [B] {
565585

566586
#[doc="A parallel version of mapi."]
567587
fn mapi<A: send, B: send>(xs: [A], f: fn~(uint, A) -> B) -> [B] {
568-
vec::concat(map_slices(xs) {|base, slice|
569-
slice.mapi() {|i, x|
588+
let slices = map_slices(xs) {|base, slice|
589+
vec::mapi(slice) {|i, x|
570590
f(i + base, x)
571591
}
572-
})
592+
};
593+
log(info, slices.len());
594+
for vec::eachi(slices) {|i, inner|
595+
log(info, #fmt("slice %?: %?", i, inner.len()));
596+
}
597+
let r = vec::concat(slices);
598+
log(info, (r.len(), xs.len()));
599+
assert(r.len() == xs.len());
600+
r
573601
}
574602

575603
#[doc="Returns true if the function holds for all elements in the vector."]
576604
fn alli<A: send>(xs: [A], f: fn~(uint, A) -> bool) -> bool {
577605
vec::all(map_slices(xs) {|base, slice|
578-
slice.alli() {|i, x|
606+
vec::alli(slice) {|i, x|
579607
f(i + base, x)
580608
}
581609
}) {|x| x }
@@ -584,7 +612,7 @@ fn alli<A: send>(xs: [A], f: fn~(uint, A) -> bool) -> bool {
584612
#[doc="Returns true if the function holds for any elements in the vector."]
585613
fn any<A: send>(xs: [A], f: fn~(A) -> bool) -> bool {
586614
vec::any(map_slices(xs) {|_base, slice|
587-
slice.any(f)
615+
vec::any(slice, f)
588616
}) {|x| x }
589617
}
590618

0 commit comments

Comments
 (0)