- 
  Notifications
 You must be signed in to change notification settings 
- Fork 58
-
I know arrayfire-rust has some useful macro for stdout (e.g., af_print!).
But I want to create some struct which has the member of arrayfire:
pub struct SomeStruct { pub data: af::Array<f32>, } impl std::fmt::Display for SumStruct { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { let message = "???????" // <- I want to know how to prepare the message write!(f, "{}", message) } }
Is there any good way?
Beta Was this translation helpful? Give feedback.
All reactions
Array doesn't have fmt implemented in the library code. Hence, I was suggesting you could do it or you can just using existing methods to print the relevant info.
pub struct SomeStruct { pub data: arrayfire::Array<f32>, pub handle: i32, } impl std::fmt::Display for SomeStruct { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { af_print!("entire array:", self.data); write!(f, "Handle value: {}", self.handle) } } fn main() { let s = SomeStruct{handle: 0, data: arrayfire::constant(0f32, Dim4::new(&[5, 5, 1, 1]))}; println!("{}", s); }
The associated output looks like below
entire array:
[5 5 1 1]
 0.0000 0.0000 0.0000 0....Replies: 7 comments
-
@nocotan I am not sure I understand the question completely. Are you requesting some sort of fmt impl for Array object ?
Beta Was this translation helpful? Give feedback.
All reactions
-
@9prady9 Thanks for the reply.
Yes, my question is about the fmt impl for Array object.
Beta Was this translation helpful? Give feedback.
All reactions
-
Since Array is just wrapper for handle, there isn't much useful to print from it(has the internal handle). fmt::write for Array could print the entire array to it, is that what you are looking for ? This may be useful for smaller arrays, but not for larger arrays. I guess the question is how much or what info of Array should be printed when passed onto println!.
Beta Was this translation helpful? Give feedback.
All reactions
-
To clarify better, what I meant was Array implementing fmt on it's own isn't going to be of much help I think since, the wrapper then would have to call all the info methods of Array Object and print them whether the user wants all of that info or not. For example, if they don't deal with sparse matrices, info about that in the print out is not helpful.
I think you should print the info relevant to your code from Array doing something like the below
impl std::fmt::Display for SumStruct {
 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
 let message = "???????" // <- I want to know how to prepare the message
impl std::fmt::Display for SumStruct {
 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
 let message = "???????" // <- I want to know how to prepare the message
 write!(f, "Array Shape {}", f.data.dims())
 write!(f, "Is it complex data {}", f.data.is_complex())
 }
}
 }
}
Please note that I didn't compile the above code, so kindly check for the right method name.
Hope that helps.
Beta Was this translation helpful? Give feedback.
All reactions
-
@9prady9 Thank you for your kind reply!
In fact, I was wondering how to write the entire arrayfire elements.
pub struct SomeStruct { pub data: af::Array<f32>, } impl std::fmt::Display for SomeStruct { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { write!(f, "entire array {}", self.data) } }
However, I get the following error:
| 25 | write!(f, "entire array {}", self.data) | `arrayfire::array::Array<f32>` cannot be formatted with the default formatter | = help: the trait `std::fmt::Display` is not implemented for `arrayfire::array::Array<f32>`
I'm sure it's due to my lack of Rust skills...
Beta Was this translation helpful? Give feedback.
All reactions
-
Array doesn't have fmt implemented in the library code. Hence, I was suggesting you could do it or you can just using existing methods to print the relevant info.
pub struct SomeStruct { pub data: arrayfire::Array<f32>, pub handle: i32, } impl std::fmt::Display for SomeStruct { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { af_print!("entire array:", self.data); write!(f, "Handle value: {}", self.handle) } } fn main() { let s = SomeStruct{handle: 0, data: arrayfire::constant(0f32, Dim4::new(&[5, 5, 1, 1]))}; println!("{}", s); }
The associated output looks like below
entire array:
[5 5 1 1]
 0.0000 0.0000 0.0000 0.0000 0.0000 
 0.0000 0.0000 0.0000 0.0000 0.0000 
 0.0000 0.0000 0.0000 0.0000 0.0000 
 0.0000 0.0000 0.0000 0.0000 0.0000 
 0.0000 0.0000 0.0000 0.0000 0.0000 
Handle value: 0
If you don't want to use af_print, then you would have to use Array::host() method to fetch the data from GPU memory to a rust vector and then handle it's print out on your own. However, I would advice against that since the data won't be formatted according to it's shape(dimensions) which is taken care of by af_print
Beta Was this translation helpful? Give feedback.
All reactions
-
@9prady9 I see. Thank you so much!
Beta Was this translation helpful? Give feedback.
All reactions
- 
 👍 1