Skip to content

Commit e43ca43

Browse files
author
Jason Ramapuram
committed
working example for accessing rows and columns
1 parent 0f3e532 commit e43ca43

File tree

2 files changed

+30
-64
lines changed

2 files changed

+30
-64
lines changed

examples/helloworld.rs

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

1010
let num_rows: u64 = 5;
11-
let num_columns: u64 = 3;
11+
let num_cols: u64 = 3;
1212

13-
let dims = Dim4::new(&[num_rows, num_columns, 1, 1]);
13+
let dims = Dim4::new(&[num_rows, num_cols, 1, 1]);
1414

1515
println!("Create a 5-by-3 matrix of random floats on the GPU");
1616
let a = match randu(dims, Aftype::F32) {
@@ -46,11 +46,10 @@ fn main() {
4646
println!("Fourier transform the result");
4747
fft(&b, 1.0, 0).map(|x| print(&x));
4848

49-
println!("Grab last row");
50-
let c = row(&a, 4).unwrap();
51-
print(&c);
52-
//array c = C.row(end);
53-
// af_print(c);
49+
println!("Grab last row & col of the random matrix");
50+
print(&a);
51+
print(&row(&a, num_rows - 1).unwrap());
52+
print(&col(&a, num_cols - 1).unwrap());
5453

5554
println!("Create 2-by-3 matrix from host data");
5655
let d_dims = Dim4::new(&[2, 3, 1, 1]);

src/index.rs

Lines changed: 24 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,19 @@ use defines::Aftype;
88
use self::libc::{c_int, c_uint, c_longlong};
99

1010
type MutAfArray = *mut self::libc::c_longlong;
11-
type MutAfSeq = *mut Seq;
1211
type MutMutAfIndex = *mut *mut self::libc::c_longlong;
1312
type MutAfIndex = *mut self::libc::c_longlong;
14-
type MutDouble = *mut self::libc::c_double;
15-
type MutUint = *mut self::libc::c_uint;
1613
type AfArray = self::libc::c_longlong;
1714
type AfIndex = self::libc::c_longlong;
1815
type DimT = self::libc::c_longlong;
19-
type SeqT = self::libc::c_longlong;
2016
type IndexT = self::libc::c_longlong;
2117

2218
#[allow(dead_code)]
2319
extern {
2420
fn af_create_seq_index(result: MutMutAfIndex, input: *const Seq, is_batch: c_int) -> c_int;
2521
fn af_create_array_index(result: MutMutAfIndex, input: AfArray) -> c_int;
2622
fn af_release_index(indexer: MutAfIndex) -> c_int;
27-
fn af_index(out: MutAfArray, input: AfArray, ndims: c_uint, index: *const IndexT) -> c_int;
23+
fn af_index(out: MutAfArray, input: AfArray, ndims: c_uint, index: *const Seq) -> c_int;
2824
fn af_lookup(out: MutAfArray, arr: AfArray, indices: AfArray, dim: c_uint) -> c_int;
2925
fn af_assign_seq(out: MutAfArray, lhs: AfArray, ndims: c_uint, indices: *const IndexT, rhs: AfArray) -> c_int;
3026
fn af_index_gen(out: MutAfArray, input: AfArray, ndims: DimT, indices: *const IndexT) -> c_int;
@@ -63,7 +59,7 @@ impl Index {
6359
}
6460
}
6561

66-
pub fn get(&self) -> i64 {
62+
pub fn get(&self) -> i64{
6763
self.handle
6864
}
6965

@@ -91,9 +87,11 @@ impl Drop for Index {
9187
pub fn index(input: &Array, seqs: &[Seq]) -> Result<Array, AfError> {
9288
unsafe {
9389
let mut temp: i64 = 0;
90+
println!("size is: {}", seqs.len() as u32);
9491
let err_val = af_index(&mut temp as MutAfArray
95-
, input.get() as AfArray, seqs.len() as u32
96-
, seqs.as_ptr() as *const SeqT);
92+
, input.get() as AfArray
93+
, seqs.len() as u32
94+
, seqs.clone().as_ptr() as *const Seq);
9795
match err_val {
9896
0 => Ok(Array::from(temp)),
9997
_ => Err(AfError::from(err_val)),
@@ -103,61 +101,26 @@ pub fn index(input: &Array, seqs: &[Seq]) -> Result<Array, AfError> {
103101

104102
pub fn row(input: &Array, row_num: u64) -> Result<Array, AfError> {
105103
let dims_err = input.dims();
106-
let mut dims = [0, 0, 0, 0];
104+
let mut dims = Dim4::default();
107105
match dims_err {
108-
Ok(dim) => dims = dim.get().clone(),
106+
Ok(dim) => dims = dim.clone(),
109107
Err(e) => panic!("Error unwrapping dims in row(): {}", e),
110108
}
111-
112-
let array_end = dims[0] as f64 * dims[1] as f64 * dims[2] as f64 * dims[3] as f64;
113-
let row_begin: f64 = dims[1] as f64 * row_num as f64;
114-
assert!(row_begin <= array_end - dims[1] as f64);
115-
116-
let row_end: f64 = row_begin + dims[1] as f64 - 1.0;
117-
assert!(row_end <= array_end);
118-
119-
let index = Index::new(None, Some(Seq::new(row_begin, row_end, 1.0)), false);
120-
println!("origin size {} x {} x {} x {}", dims[0], dims[1], dims[2], dims[3]);
121-
println!("passed index gen from {} to {}", row_begin, row_end);
122-
123-
match index {
124-
Ok(i) => { println!("index raw handle: {}", i.get()); index_gen(input, Dim4::new(&[1, 1, 1, 1]), &[i])},
125-
Err(e) => Err(AfError::from(e)),
126-
}
109+
110+
index(input, &[Seq::new(row_num as f64, row_num as f64, 1.0)
111+
,Seq::new(0.0, dims[1] as f64 - 1.0, 1.0)])
127112
}
128113

129114
pub fn col(input: &Array, col_num: u64) -> Result<Array, AfError> {
130115
let dims_err = input.dims();
131-
let mut dims = [0, 0, 0, 0];
116+
let mut dims = Dim4::default();
132117
match dims_err {
133-
Ok(dim) => dims = dim.get().clone(),
134-
Err(e) => panic!("Error unwrapping dims in col(): {}", e),
135-
}
136-
137-
let input_type_err = input.get_type();
138-
let mut input_type = Aftype::F32;
139-
match input_type_err {
140-
Ok(t) => input_type = t,
141-
Err(e) => panic!("Error unwrapping type in col(): {}", e),
142-
}
143-
144-
let mut indices = vec![col_num];
145-
for i in 0..dims[0]-1 {
146-
let last_element = indices[indices.len() - 1];
147-
indices.push(last_element + dims[0]);
148-
}
149-
150-
let index_array = Array::new(Dim4::new(&dims), &indices, input_type);
151-
match index_array {
152-
Ok(a) => {
153-
let index = Index::new(Some(a), None, false);
154-
match index {
155-
Ok(i) => index_gen(input, Dim4::new(&[1, 1, 1, 1]), &[i]),
156-
Err(e) => Err(AfError::from(e)),
157-
}
158-
},
159-
Err(e) => Err(AfError::from(e)),
118+
Ok(dim) => dims = dim.clone(),
119+
Err(e) => panic!("Error unwrapping dims in row(): {}", e),
160120
}
121+
122+
index(input, &[Seq::new(0.0, dims[0] as f64 - 1.0, 1.0)
123+
,Seq::new(col_num as f64, col_num as f64, 1.0)])
161124
}
162125

163126
pub fn lookup(input: &Array, indices: &Array, seq_dim: i32) -> Result<Array, AfError> {
@@ -174,7 +137,6 @@ pub fn lookup(input: &Array, indices: &Array, seq_dim: i32) -> Result<Array, AfE
174137
}
175138
}
176139

177-
178140
pub fn assign_seq(lhs: &Array, ndims: usize, seqs: &[Seq], rhs: &Array) -> Result<Array, AfError> {
179141
unsafe{
180142
let mut temp: i64 = 0;
@@ -189,13 +151,18 @@ pub fn assign_seq(lhs: &Array, ndims: usize, seqs: &[Seq], rhs: &Array) -> Resul
189151
}
190152
}
191153

192-
pub fn index_gen(input: &Array, ndims: Dim4, indices: &[Index]) -> Result<Array, AfError> {
154+
pub fn index_gen(input: &Array, ndims: Dim4, indices: &mut [Index]) -> Result<Array, AfError> {
193155
unsafe{
194156
let mut temp: i64 = 0;
157+
let mut index_ptrs = Vec::new();
158+
for index_struct in &mut indices[..] {
159+
index_ptrs.push(index_struct.get());
160+
}
161+
195162
let err_val = af_index_gen(&mut temp as MutAfArray
196163
, input.get() as AfArray
197164
, ndims.get().as_ptr() as DimT
198-
, indices.as_ptr() as *const IndexT);
165+
, index_ptrs.as_ptr() as *const IndexT);
199166
match err_val {
200167
0 => Ok(Array::from(temp)),
201168
_ => Err(AfError::from(err_val)),

0 commit comments

Comments
 (0)