Stream Compaction #124
-
How would you recommend performing stream compaction with this library? Using indexes? |
Beta Was this translation helpful? Give feedback.
Replies: 18 comments
-
I believe stream compaction is equivalent to the following 3 steps in that order.
Given below is the C++ equivalent of above steps. array a = randu(10); // input data
array b = a > 0.4; // boolean map of relevant values
array c = scan(b); // scan the boolean map
array i = c(where(b)); // gather scanned map values where the map wasn't zero originally
array dense = a(i); // index original input array with gathered map values The rust version would follow the exact same operations with syntactical difference and |
Beta Was this translation helpful? Give feedback.
-
The rust version does not have an operator equivalent to |
Beta Was this translation helpful? Give feedback.
-
@lobachevzky It is called |
Beta Was this translation helpful? Give feedback.
-
@lobachevzky I am sorry about not mentioning that |
Beta Was this translation helpful? Give feedback.
-
This is what I'm using currently: fn filter(bools: &af::Array, array: &af::Array) -> af::Array {
let mut idxr = af::Indexer::new();
idxr.set_index(&af::locate(bools), 0, None);
af::index_gen(array, idxr)
} By the way, what does the |
Beta Was this translation helpful? Give feedback.
-
@lobachevzky Ley us consider the function add as an example. Say, the input Arrays are of mismatched dimensions i.e. one Array is two dimensional and the other is three dimensional. In this case, if the |
Beta Was this translation helpful? Give feedback.
-
Got it. Thanks you! |
Beta Was this translation helpful? Give feedback.
-
One note on this approach: Array::new(&[] as &[i32], Dim4::new(&[0, 0, 0, 0])) throws:
|
Beta Was this translation helpful? Give feedback.
-
@lobachevzky Try using the latest version of arrayfire libs. I think you are using v3.3 version of the libs. |
Beta Was this translation helpful? Give feedback.
-
I am using v3.3.2 for arrayfire:
and v3.4.2 for the rust library:
(this is from my Cargo.toml) |
Beta Was this translation helpful? Give feedback.
-
@lobachevzky can you upgrade to v3.4.2 for the arrayfire libs and try again ? |
Beta Was this translation helpful? Give feedback.
-
Sorry for the delayed response. Getting the binary downloaded from the website to work took some doing. I am running v3.4.2 and I still get the exact same error message for empty arrays. |
Beta Was this translation helpful? Give feedback.
-
I know that in Rust we can do
Is the approach @lobachevzky mentions achieving the same with different methods? I've tried his filter method on an array with dims [1,3,1,1] and elements [2,5,6]. It outputs an array with dims [3,1,1,1] and elements [6,2,6]. The I've verified with |
Beta Was this translation helpful? Give feedback.
-
extern crate arrayfire as af;
use af::*;
fn main() {
let dims = af::Dim4::new(&[1, 3, 1, 1]);
let bools = af::Array::new(&[1, 0, 1], dims);
let values = af::Array::new(&[2, 5, 6], dims);
print( &bools );
print( &filter_edited(&values, &bools) );
print( &af::locate(&bools) );
print( &filter_edited(&values, &af::locate(&bools)) );
print( &filter_original(&values, &bools) );
}
fn filter_edited(array: &af::Array, bools: &af::Array) -> af::Array {
let mut idxr = af::Indexer::new();
idxr.set_index(bools, 0, None);
af::index_gen(array, idxr)
}
fn filter_original(array: &af::Array, bools: &af::Array) -> af::Array {
let mut idxr = af::Indexer::new();
idxr.set_index(&af::locate(bools), 0, None);
af::index_gen(array, idxr)
} Output:
Change Dims to [3, 1, 1, 1] however:
The original method by @lobachevzky fails on me. I've been using [1, 3, 1, 1] because of errors that dims of [3, 1, 1, 1] were giving me earlier. While @lobachevzky method doesn't crash it does produce unexpected results. Filtering results to a smaller array to send back to the host is something I'd been spending quite a bit of time trying to figure out myself, this would make a good example to add :) |
Beta Was this translation helpful? Give feedback.
-
It seems to work correctly if I change
To this:
I've been told the reason for this is:
I guess the Rust wrapper for ArrayFire has plenty of cases like this where the compiler could have picked up on the errors I've been running into like this and other methods. I'll raise this as a separate issue. |
Beta Was this translation helpful? Give feedback.
-
@polarathene I haven't been able to reproduce the error you got in the comment that says However, I do see the difference in output from changing bools from [3 1 1 1] dims to [1 3 1 1] dims. I am looking into it. |
Beta Was this translation helpful? Give feedback.
-
@lobachevzky W.r.t to your comment, you can create empty arrays ([0, 0, 0, 0] dims) using Array::new_empty(). This is not yet available in latest crate release but available on current HEAD of devel branch. Will try to do a fix release if 3.5.0 doesn't happen soon enough. |
Beta Was this translation helpful? Give feedback.
-
@polarathene Moving the discussion related to |
Beta Was this translation helpful? Give feedback.
I believe stream compaction is equivalent to the following 3 steps in that order.
Given below is the C++ equivalent of above steps.
The rust version would follow…