Skip to content

Commit

Permalink
get/set: GrB_Type_get_SIZE returns size_t for sizeof(type)
Browse files Browse the repository at this point in the history
  • Loading branch information
DrTimothyAldenDavis committed Nov 1, 2023
1 parent 0cda566 commit 8a1b6ae
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 55 deletions.
24 changes: 5 additions & 19 deletions Doc/GrB_get_set.tex
Original file line number Diff line number Diff line change
Expand Up @@ -621,9 +621,7 @@ \subsection{{\sf GrB\_Type} Options}
\verb'GrB_Field' & R/W & C type & description \\
\hline
\verb'GrB_ELTYPE_CODE' & R & \verb'int32_t'& type code (see \verb'GrB_Type_Code') \\
\verb'GrB_SIZE' & R & \verb'int32_t'& \verb'sizeof' the type \\
\verb'GrB_SIZE' & R & \verb'uint64_t'& \verb'sizeof' the type
(as a \verb'GrB_Scalar') \\
\verb'GrB_SIZE' & R & \verb'size_t' & \verb'sizeof' the type \\
\hline
\verb'GrB_NAME' & R/W1 & \verb'char *' & % GrB_ALREADY_SET (type)
name of the type. For built-in types, this returns the GraphBLAS
Expand Down Expand Up @@ -654,29 +652,17 @@ \subsection{{\sf GrB\_Type} Options}
but it cannot use the JIT, even if the type is given a name later on after
the operator is created.

The size of the type can be returned as an \verb'int32_t' C scalar, or as a
\verb'GrB_Scalar', normally of type \verb'GrB_INT32' (according to the v2.1 C
API). Typecasting of the value of the size is done as needed, if the scalar
has a different type. A \verb'GrB_Scalar' of type \verb'GrB_UINT64' is perhaps
safest (as shown in the example below), since it allows arbitrary type sizes to
be handled. In practice, SuiteSparse:GraphBLAS would currently not work with
types larger than \verb'INT32_MAX', so either method will work. All three of
the examples below return the size of \verb'GrB_FP32' as \verb'sizeof (float)':
The size of the type can be returned as a \verb'size_t' C scalar, or as a
\verb'GrB_Scalar', normally of type \verb'GrB_UINT64', with the examples below.

{\footnotesize
\begin{verbatim}
int32_t size ;
size_t size ;
GrB_get (GrB_FP32, &size, GrB_SIZE) ;
assert (size == sizeof (float)) ;
GrB_Scalar t ;
GrB_Scalar_new (&t, GrB_INT32) ;
GrB_get (GrB_FP32, t, GrB_SIZE) ;
GrB_Scalar_extractElement (&size, t) ;
assert (size == sizeof (float)) ;
uint64_t size2 ;
GrB_Scalar s ;
uint64_t size2 ;
GrB_Scalar_new (&s, GrB_UINT64) ;
GrB_get (GrB_FP32, s, GrB_SIZE) ;
GrB_Scalar_extractElement (&size2, s) ;
Expand Down
Binary file modified Doc/GraphBLAS_UserGuide.pdf
Binary file not shown.
14 changes: 6 additions & 8 deletions Source/GrB_Type_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,6 @@ GrB_Info GrB_Type_get_INT32
(*value) = (int32_t) GB_type_code_get (type->code) ;
break ;

case GrB_SIZE :

// int32_t is fine for small types but for GrB_Scalar,
// UINT64 is used instead (see above)
(*value) = (int32_t) type->size ;
break ;

default :
return (GrB_INVALID_VALUE) ;
}
Expand Down Expand Up @@ -191,11 +184,16 @@ GrB_Info GrB_Type_get_SIZE
// get the field
//--------------------------------------------------------------------------

const char *s ;
const char *s = NULL ;

switch ((int) field)
{

case GrB_SIZE :
(*value) = type->size ;
#pragma omp flush
return (GrB_SUCCESS) ;

case GrB_NAME :
case GrB_ELTYPE_STRING :

Expand Down
60 changes: 32 additions & 28 deletions Test/GB_mex_test26.c
Original file line number Diff line number Diff line change
Expand Up @@ -239,45 +239,48 @@ void mexFunction
OK (GrB_Scalar_extractElement_UINT64_(&u64, s_uint64)) ;
CHECK (u64 == sizeof (double complex)) ;

// type size (using an int32_t)
OK (GrB_Type_get_INT32_(GrB_BOOL, &i, GrB_SIZE)) ;
CHECK (i == sizeof (bool)) ;

OK (GrB_Type_get_INT32_(GrB_INT8, &i, GrB_SIZE)) ;
CHECK (i == sizeof (int8_t)) ;
// type size (using a size_t)
size = 0 ;
OK (GrB_Type_get_SIZE_(GrB_BOOL, &size, GrB_SIZE)) ;
CHECK (size == sizeof (bool)) ;

OK (GrB_Type_get_INT32_(GrB_INT16, &i, GrB_SIZE)) ;
CHECK (i == sizeof (int16_t)) ;
OK (GrB_Type_get_SIZE_(GrB_INT8, &size, GrB_SIZE)) ;
CHECK (size == sizeof (int8_t)) ;

OK (GrB_Type_get_INT32_(GrB_INT32, &i, GrB_SIZE)) ;
CHECK (i == sizeof (int32_t)) ;
OK (GrB_Type_get_SIZE_(GrB_INT16, &size, GrB_SIZE)) ;
CHECK (size == sizeof (int16_t)) ;

OK (GrB_Type_get_INT32_(GrB_INT64, &i, GrB_SIZE)) ;
CHECK (i == sizeof (int64_t)) ;
OK (GrB_Type_get_SIZE_(GrB_INT32, &size, GrB_SIZE)) ;
CHECK (size == sizeof (int32_t)) ;

OK (GrB_Type_get_INT32_(GrB_UINT8, &i, GrB_SIZE)) ;
CHECK (i == sizeof (uint8_t)) ;
OK (GrB_Type_get_SIZE_(GrB_INT64, &size, GrB_SIZE)) ;
CHECK (size == sizeof (int64_t)) ;

OK (GrB_Type_get_INT32_(GrB_UINT16, &i, GrB_SIZE)) ;
CHECK (i == sizeof (uint16_t)) ;
OK (GrB_Type_get_SIZE_(GrB_UINT8, &size, GrB_SIZE)) ;
CHECK (size == sizeof (uint8_t)) ;

OK (GrB_Type_get_INT32_(GrB_UINT32, &i, GrB_SIZE)) ;
CHECK (i == sizeof (uint32_t)) ;
OK (GrB_Type_get_SIZE_(GrB_UINT16, &size, GrB_SIZE)) ;
CHECK (size == sizeof (uint16_t)) ;

OK (GrB_Type_get_INT32_(GrB_UINT64, &i, GrB_SIZE)) ;
CHECK (i == sizeof (uint64_t)) ;
OK (GrB_Type_get_SIZE_(GrB_UINT32, &size, GrB_SIZE)) ;
CHECK (size == sizeof (uint32_t)) ;

OK (GrB_Type_get_INT32_(GrB_FP32, &i, GrB_SIZE)) ;
CHECK (i == sizeof (float)) ;
OK (GrB_Type_get_SIZE_(GrB_UINT64, &size, GrB_SIZE)) ;
CHECK (size == sizeof (uint64_t)) ;

OK (GrB_Type_get_INT32_(GrB_FP64, &i, GrB_SIZE)) ;
CHECK (i == sizeof (double)) ;
OK (GrB_Type_get_SIZE_(GrB_FP32, &size, GrB_SIZE)) ;
CHECK (size == sizeof (float)) ;

OK (GrB_Type_get_INT32_(GxB_FC32, &i, GrB_SIZE)) ;
CHECK (i == sizeof (float complex)) ;
OK (GrB_Type_get_SIZE_(GrB_FP64, &size, GrB_SIZE)) ;
CHECK (size == sizeof (double)) ;

OK (GrB_Type_get_SIZE_(GxB_FC32, &size, GrB_SIZE)) ;
CHECK (size == sizeof (float complex)) ;

OK (GrB_Type_get_SIZE_(GxB_FC64, &size, GrB_SIZE)) ;
CHECK (size == sizeof (double complex)) ;

OK (GrB_Type_get_INT32_(GxB_FC64, &i, GrB_SIZE)) ;
CHECK (i == sizeof (double complex)) ;


// built-in type definition
Expand Down Expand Up @@ -346,7 +349,8 @@ void mexFunction
ERR (GrB_Type_get_Scalar_(type, s_int32, GrB_OUTP)) ;
ERR (GrB_Type_get_String_(type, name, GrB_OUTP)) ;
ERR (GrB_Type_get_SIZE_(type, &size, GrB_OUTP)) ;
ERR (GrB_Type_get_SIZE_(GrB_FP32, &size, GrB_SIZE)) ;

ERR (GrB_Type_get_SIZE_(GrB_FP32, &i, GrB_SIZE)) ;

expected = GrB_INVALID_VALUE ;
ERR (GrB_Type_get_VOID_(type, nothing, 0)) ;
Expand Down

0 comments on commit 8a1b6ae

Please sign in to comment.