Skip to content

Commit c48e842

Browse files
committed
Fixed indexing functions, udpated arrayfire submodule to latest devel
1 parent 5d731d7 commit c48e842

File tree

4 files changed

+99
-77
lines changed

4 files changed

+99
-77
lines changed

arrayfire

Submodule arrayfire updated 544 files

examples/helloworld.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ fn main() {
99

1010
let num_rows: u64 = 5;
1111
let num_cols: u64 = 3;
12+
let values: &[f32] = &[1.0, 2.0, 3.0];
13+
let indices = Array::new(Dim4::new(&[3, 1, 1, 1]), values, Aftype::F32).unwrap();
1214

1315
let dims = Dim4::new(&[num_rows, num_cols, 1, 1]);
1416

@@ -39,6 +41,24 @@ fn main() {
3941
let test = &a + &b;
4042
println!("a + b"); print(&test);
4143

44+
// Index array using sequences
45+
let seqs = &[Seq::new(1.0, 3.0, 1.0), Seq::default()];
46+
let sub = index(&a, seqs).unwrap();
47+
println!("a(seq(1,3,1), span)"); print(&sub);
48+
49+
//Index array using array and sequence
50+
let seq4gen = Seq::new(0.0, 2.0, 1.0);
51+
52+
let mut idxrs = match Indexer::new() {
53+
Ok(v) => v,
54+
Err(e) => panic!("{}",e),
55+
};
56+
idxrs.set_index(&indices, 0, None);
57+
idxrs.set_index(&seq4gen, 1, Some(false));
58+
59+
let sub2 = index_gen(&a, idxrs).unwrap();
60+
println!("a(indices, seq(0, 2, 1))"); print(&sub2);
61+
4262
// printf("Negate the first three elements of second column\n");
4363
// B(seq(0, 2), 1) = B(seq(0, 2), 1) * -1;
4464
// af_print(B);

src/index.rs

Lines changed: 77 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -2,83 +2,100 @@ extern crate libc;
22

33
use array::Array;
44
use defines::AfError;
5-
use dim4::Dim4;
65
use seq::Seq;
7-
use defines::Aftype;
86
use self::libc::{c_int, c_uint, c_longlong};
97

10-
type MutAfArray = *mut self::libc::c_longlong;
11-
type MutMutAfIndex = *mut *mut self::libc::c_longlong;
128
type MutAfIndex = *mut self::libc::c_longlong;
9+
type MutAfArray = *mut self::libc::c_longlong;
1310
type AfArray = self::libc::c_longlong;
14-
type AfIndex = self::libc::c_longlong;
1511
type DimT = self::libc::c_longlong;
1612
type IndexT = self::libc::c_longlong;
1713

1814
#[allow(dead_code)]
1915
extern {
20-
fn af_create_seq_index(result: MutMutAfIndex, input: *const Seq, is_batch: c_int) -> c_int;
21-
fn af_create_array_index(result: MutMutAfIndex, input: AfArray) -> c_int;
22-
fn af_release_index(indexer: MutAfIndex) -> c_int;
16+
fn af_create_indexers(indexers: MutAfIndex) -> c_int;
17+
fn af_set_array_indexer(indexer: MutAfIndex, idx: AfArray, dim: DimT) -> c_int;
18+
fn af_set_seq_indexer(indexer: MutAfIndex, idx: *const Seq, dim: DimT, is_batch: c_int) -> c_int;
19+
fn af_release_indexers(indexers: MutAfIndex) -> c_int;
20+
2321
fn af_index(out: MutAfArray, input: AfArray, ndims: c_uint, index: *const Seq) -> c_int;
2422
fn af_lookup(out: MutAfArray, arr: AfArray, indices: AfArray, dim: c_uint) -> c_int;
25-
fn af_assign_seq(out: MutAfArray, lhs: AfArray, ndims: c_uint, indices: *const IndexT, rhs: AfArray) -> c_int;
23+
fn af_assign_seq(out: MutAfArray, lhs: AfArray, ndims: c_uint, indices: *const Seq, rhs: AfArray) -> c_int;
2624
fn af_index_gen(out: MutAfArray, input: AfArray, ndims: DimT, indices: *const IndexT) -> c_int;
2725
fn af_assign_gen(out: MutAfArray, lhs: AfArray, ndims: DimT, indices: *const IndexT, rhs: AfArray) -> c_int;
2826
}
2927

30-
pub struct Index {
28+
pub struct Indexer {
3129
handle: i64,
32-
is_batch: bool,
33-
is_seq: bool,
30+
count: u32,
31+
}
32+
33+
pub trait Indexable {
34+
fn set(&self, idxr: &Indexer, dim: u32, is_batch: Option<bool>) -> Result<(), AfError>;
35+
}
36+
37+
impl Indexable for Array {
38+
#[allow(unused_variables)]
39+
fn set(&self, idxr: &Indexer, dim: u32, is_batch: Option<bool>) -> Result<(), AfError> {
40+
unsafe {
41+
let err_val = af_set_array_indexer(idxr.clone().get() as MutAfIndex,
42+
self.get() as AfArray,
43+
dim as DimT);
44+
match err_val {
45+
0 => Ok(()),
46+
_ => Err(AfError::from(err_val)),
47+
}
48+
}
49+
}
50+
}
51+
52+
impl Indexable for Seq {
53+
fn set(&self, idxr: &Indexer, dim: u32, is_batch: Option<bool>) -> Result<(), AfError> {
54+
unsafe {
55+
let err_val = af_set_seq_indexer(idxr.clone().get() as MutAfIndex, self as *const Seq,
56+
dim as DimT, is_batch.unwrap() as c_int);
57+
match err_val {
58+
0 => Ok(()),
59+
_ => Err(AfError::from(err_val)),
60+
}
61+
}
62+
}
3463
}
3564

36-
impl Index {
65+
impl Indexer {
3766
#[allow(unused_mut)]
38-
pub fn new(arr: Option<Array>, seq: Option<Seq>, is_batch: bool) -> Result<Index, AfError> {
67+
pub fn new() -> Result<Indexer, AfError> {
3968
unsafe {
40-
let mut err_val: c_int = 0;
4169
let mut temp: i64 = 0;
42-
let mut is_seq = false;
43-
err_val = match arr {
44-
//c_func(&mut (x as *mut libc::c_void)); --> (&mut x) as *mut _ as *mut *mut libc::c_void.
45-
//&mut temp as MutMutAfIndex, x),
46-
Some(mut x) => { is_seq = false; af_create_array_index((&mut temp) as *mut _ as MutMutAfIndex, x.get() as AfArray) },
47-
None => 0,
48-
};
49-
50-
err_val = match seq {
51-
Some(mut x) => { is_seq = true; af_create_seq_index((&mut temp) as *mut _ as MutMutAfIndex, &mut x, is_batch as c_int) },
52-
None => AfError::ERR_UNKNOWN as c_int,
53-
};
54-
70+
let err_val = af_create_indexers(&mut temp as MutAfIndex);
5571
match err_val {
56-
0 => Ok(Index {handle: temp, is_batch: is_batch, is_seq: is_seq}),
72+
0 => Ok(Indexer{handle: temp, count: 0}),
5773
_ => Err(AfError::from(err_val)),
5874
}
5975
}
6076
}
6177

62-
pub fn get(&self) -> i64{
63-
self.handle
78+
pub fn set_index<T: Indexable>(&mut self, idx: &T, dim: u32, is_batch: Option<bool>) -> Result<(), AfError> {
79+
self.count = self.count + 1;
80+
idx.set(self, dim, is_batch)
6481
}
6582

66-
pub fn is_seq(&self) -> bool{
67-
self.is_seq
83+
pub fn get(&self) -> i64 {
84+
self.handle
6885
}
6986

70-
pub fn is_batch(&self) -> bool{
71-
self.is_batch
87+
pub fn len(&self) -> u32 {
88+
self.count
7289
}
7390
}
7491

75-
impl Drop for Index {
92+
impl Drop for Indexer {
7693
fn drop(&mut self) {
7794
unsafe {
78-
let ret_val = af_release_index(self.handle as MutAfIndex);
95+
let ret_val = af_release_indexers(self.handle as MutAfIndex);
7996
match ret_val {
8097
0 => (),
81-
_ => panic!("Failed to destruct Index: {}", ret_val),
98+
_ => panic!("Failed to release indexers resource: {}", ret_val),
8299
}
83100
}
84101
}
@@ -88,9 +105,8 @@ pub fn index(input: &Array, seqs: &[Seq]) -> Result<Array, AfError> {
88105
unsafe {
89106
let mut temp: i64 = 0;
90107
let err_val = af_index(&mut temp as MutAfArray
91-
, input.get() as AfArray
92-
, seqs.len() as u32
93-
, seqs.clone().as_ptr() as *const Seq);
108+
, input.get() as AfArray, seqs.len() as u32
109+
, seqs.as_ptr() as *const Seq);
94110
match err_val {
95111
0 => Ok(Array::from(temp)),
96112
_ => Err(AfError::from(err_val)),
@@ -100,35 +116,31 @@ pub fn index(input: &Array, seqs: &[Seq]) -> Result<Array, AfError> {
100116

101117
pub fn row(input: &Array, row_num: u64) -> Result<Array, AfError> {
102118
let dims_err = input.dims();
103-
let mut dims = Dim4::default();
104-
match dims_err {
105-
Ok(dim) => dims = dim.clone(),
119+
let dims = match dims_err {
120+
Ok(dim) => dim.clone(),
106121
Err(e) => panic!("Error unwrapping dims in row(): {}", e),
107-
}
108-
122+
};
123+
109124
index(input, &[Seq::new(row_num as f64, row_num as f64, 1.0)
110125
,Seq::new(0.0, dims[1] as f64 - 1.0, 1.0)])
111126
}
112127

113128
pub fn col(input: &Array, col_num: u64) -> Result<Array, AfError> {
114129
let dims_err = input.dims();
115-
let mut dims = Dim4::default();
116-
match dims_err {
117-
Ok(dim) => dims = dim.clone(),
130+
let dims = match dims_err {
131+
Ok(dim) => dim.clone(),
118132
Err(e) => panic!("Error unwrapping dims in row(): {}", e),
119-
}
120-
133+
};
134+
121135
index(input, &[Seq::new(0.0, dims[0] as f64 - 1.0, 1.0)
122136
,Seq::new(col_num as f64, col_num as f64, 1.0)])
123137
}
124138

125139
pub fn lookup(input: &Array, indices: &Array, seq_dim: i32) -> Result<Array, AfError> {
126140
unsafe {
127141
let mut temp: i64 = 0;
128-
let err_val = af_lookup(&mut temp as MutAfArray
129-
, input.get() as AfArray
130-
, indices.get() as AfArray
131-
, seq_dim as c_uint);
142+
let err_val = af_lookup(&mut temp as MutAfArray, input.get() as AfArray,
143+
indices.get() as AfArray, seq_dim as c_uint);
132144
match err_val {
133145
0 => Ok(Array::from(temp)),
134146
_ => Err(AfError::from(err_val)),
@@ -139,44 +151,34 @@ pub fn lookup(input: &Array, indices: &Array, seq_dim: i32) -> Result<Array, AfE
139151
pub fn assign_seq(lhs: &Array, ndims: usize, seqs: &[Seq], rhs: &Array) -> Result<Array, AfError> {
140152
unsafe{
141153
let mut temp: i64 = 0;
142-
let err_val = af_assign_seq(&mut temp as MutAfArray
143-
, lhs.get() as AfArray
144-
, ndims as c_uint, seqs.as_ptr() as *const IndexT
145-
, rhs.get() as AfArray);
154+
let err_val = af_assign_seq(&mut temp as MutAfArray, lhs.get() as AfArray,
155+
ndims as c_uint, seqs.as_ptr() as *const Seq,
156+
rhs.get() as AfArray);
146157
match err_val {
147158
0 => Ok(Array::from(temp)),
148159
_ => Err(AfError::from(err_val)),
149160
}
150161
}
151162
}
152163

153-
pub fn index_gen(input: &Array, ndims: Dim4, indices: &mut [Index]) -> Result<Array, AfError> {
164+
pub fn index_gen(input: &Array, indices: Indexer) -> Result<Array, AfError> {
154165
unsafe{
155166
let mut temp: i64 = 0;
156-
let mut index_ptrs = Vec::new();
157-
for index_struct in &mut indices[..] {
158-
index_ptrs.push(index_struct.get());
159-
}
160-
161-
let err_val = af_index_gen(&mut temp as MutAfArray
162-
, input.get() as AfArray
163-
, ndims.get().as_ptr() as DimT
164-
, index_ptrs.as_ptr() as *const IndexT);
167+
let err_val = af_index_gen(&mut temp as MutAfArray, input.get() as AfArray,
168+
indices.len() as DimT, indices.get() as *const IndexT);
165169
match err_val {
166170
0 => Ok(Array::from(temp)),
167171
_ => Err(AfError::from(err_val)),
168172
}
169173
}
170174
}
171175

172-
pub fn assign_gen(lhs: &Array, ndims: Dim4, indices: &[Index], rhs: &Array) -> Result<Array, AfError> {
176+
pub fn assign_gen(lhs: &Array, indices: &Indexer, rhs: &Array) -> Result<Array, AfError> {
173177
unsafe{
174178
let mut temp: i64 = 0;
175-
let err_val = af_assign_gen(&mut temp as MutAfArray
176-
, lhs.get() as AfArray
177-
, ndims.get().as_ptr() as DimT
178-
, indices.as_ptr() as *const IndexT
179-
, rhs.get() as AfArray);
179+
let err_val = af_assign_gen(&mut temp as MutAfArray, lhs.get() as AfArray,
180+
indices.len() as DimT, indices.get() as *const IndexT,
181+
rhs.get() as AfArray);
180182
match err_val {
181183
0 => Ok(Array::from(temp)),
182184
_ => Err(AfError::from(err_val)),

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ mod defines;
4444
pub use dim4::Dim4;
4545
mod dim4;
4646

47-
pub use index::{Index, index, row, col, lookup, assign_seq, index_gen, assign_gen};
47+
pub use index::{Indexer, index, row, col, lookup, assign_seq, index_gen, assign_gen};
4848
mod index;
4949

5050
pub use seq::Seq;

0 commit comments

Comments
 (0)