Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to use std::fmt::Display for arrayfire? #229

Answered by 9prady9
nocotan asked this question in Q&A
Discussion options

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?

You must be logged in to vote

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

Comment options

@nocotan I am not sure I understand the question completely. Are you requesting some sort of fmt impl for Array object ?

You must be logged in to vote
0 replies
Comment options

@9prady9 Thanks for the reply.
Yes, my question is about the fmt impl for Array object.

You must be logged in to vote
0 replies
Comment options

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!.

You must be logged in to vote
0 replies
Comment options

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.

You must be logged in to vote
0 replies
Comment options

@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...

You must be logged in to vote
0 replies
Comment options

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

You must be logged in to vote
0 replies
Answer selected by 9prady9
Comment options

@9prady9 I see. Thank you so much!

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #229 on December 09, 2020 05:15.

AltStyle によって変換されたページ (->オリジナル) /