DrTimothyAldenDavis / GraphBLAS

SuiteSparse:GraphBLAS: graph algorithms in the language of linear algebra. For production: (default) STABLE branch. Code development: ask me for the right branch before submitting a PR. video intro: https://youtu.be/Tj5y6d7FegI .
http://faculty.cse.tamu.edu/davis/GraphBLAS.html
Other
359 stars 63 forks source link

Pass a print function for UDTs #143

Open rayegun opened 2 years ago

rayegun commented 2 years ago

It'd be very nice to replace (1,1) [user-defined value] with the actual value using a user provided print function of some sort. I'm not sure what signature the function pointer would have, but I'm sure it shouldn't be difficult.

DrTimothyAldenDavis commented 2 years ago

This would be nice to have too. I see two different approaches for this. One method would be to add a function pointer to the user-defined GrB_Type object. This might be tricky since it would affect all uses of that GrBType. Another would be to pass in a function pointer to GxB_fprint, for = Monoid, Scalar, Vector, and Matrix, which would be a more local revision. It wouldn't change the GrB_Type itself. It could even be used to print built-in types.

Maybe I could just add this as 4 new functions, like GxB_Matrix_fprintf with signature:

GrB_Info GxB_Matrix_fprintf ( GrB_Matrix A, const char *name, GxB_Print_Level pr, FILE *f, GxB_format_function *f) ;

or something, which adds the function pointer f that would define how to print a scalar.

See Source/GB_code_check.c and grep for FUTURE. I already have tagged this as a future extension that would be nice to have.

DrTimothyAldenDavis commented 2 years ago

The GxB_format_function should probably have the signature:

typedef void (*GxB_format_function) (char *, const void *)

where the const void is the scalar to print, and the char would be an array of some large fixed size which is then filled with the string that should be printed. I would want to use the char instead of FILE * because I already have a way to define a printf function for SuiteSparse:GraphBLAS to use.

rayegun commented 2 years ago

What size do you estimate for the char array?

DrTimothyAldenDavis commented 2 years ago

I could just create some large static-size array, say 2048 or something big, with a #define that gives the size.

DrTimothyAldenDavis commented 9 months ago

A better approach would be to have a user-provided function that can do one of two things: (a) return the size of the string required to print a specific user-defined value, and (b) fill a string (provided by GraphBLAS) with the printed value itself. Then I could use a dynamically allocated array, with no limits on the size of the string.