Skip to content

Commit

Permalink
make LG_init_has_been_called a static value
Browse files Browse the repository at this point in the history
  • Loading branch information
DrTimothyAldenDavis committed Dec 6, 2023
1 parent a92165c commit e0ca734
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 12 deletions.
6 changes: 4 additions & 2 deletions include/LAGraphX.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ extern "C"
//==============================================================================

// Do not rely on these in production. These methods are still under
// development, and is intended only for illustration not benchmarking. Do not
// use for benchmarking, without asking the authors.
// development, and is intended only for illustration or testing, not
// benchmarking. Do not use for benchmarking without asking the authors.

int LAGr_Reset (char *msg) ; // primarily for testing

//------------------------------------------------------------------------------
// LAGraph_Random_*: Random number generator
Expand Down
27 changes: 20 additions & 7 deletions src/test/test_Xinit.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
//------------------------------------------------------------------------------

#include "LAGraph_test.h"
#include "LAGraphX.h"

//------------------------------------------------------------------------------
// global variables
Expand All @@ -30,19 +31,19 @@ char msg [LAGRAPH_MSG_LEN] ;
void test_Xinit (void)
{

printf ("\nTesting LAGr_Init:\n") ;
printf ("\nTesting LAGr_Init: with expected errors\n") ;

TEST_CHECK (LAGr_Init (GrB_NONBLOCKING, NULL, NULL, NULL, NULL, msg)
== GrB_NULL_POINTER) ;
printf ("msg: %s\n", msg) ;
printf ("msg: [%s]\n", msg) ;

TEST_CHECK (LAGr_Init (GrB_NONBLOCKING, malloc, NULL, NULL, NULL, msg)
== GrB_NULL_POINTER) ;
printf ("msg: %s\n", msg) ;
printf ("msg: [%s]\n", msg) ;

TEST_CHECK (LAGr_Init (GrB_NONBLOCKING, NULL, NULL, NULL, free, msg)
== GrB_NULL_POINTER) ;
printf ("msg: %s\n", msg) ;
printf ("msg: [%s]\n", msg) ;

OK (LAGr_Init (GrB_NONBLOCKING, malloc, calloc, realloc, free, msg)) ;
printf ("msg: [%s]\n", msg) ;
Expand All @@ -51,17 +52,25 @@ void test_Xinit (void)
int status = LAGr_Init (GrB_NONBLOCKING,
malloc, calloc, realloc, free, msg) ;
TEST_CHECK (status != GrB_SUCCESS) ;
printf ("msg: %s\n", msg) ;
printf ("msg: [%s]\n", msg) ;

OK (LAGraph_Finalize (msg)) ;

// reset and try again
OK (LAGr_Reset (msg)) ;
OK (LAGr_Init (GrB_NONBLOCKING, malloc, calloc, realloc, free, msg)) ;

// test the failure mode in LAGr_Reset
status = LAGr_Reset (msg) ;
printf ("msg: [%s]\n", msg) ;
TEST_CHECK (status == GrB_INVALID_VALUE) ;
}

//------------------------------------------------------------------------------
// test_Xinit_brutal: test LAGr_Init with brutal memory debug
//------------------------------------------------------------------------------

#if LAGRAPH_SUITESPARSE
bool LG_init_has_been_called ;
void test_Xinit_brutal (void)
{
// no brutal memory failures, but test LG_brutal_malloc/calloc/realloc/free
Expand Down Expand Up @@ -130,19 +139,23 @@ void test_Xinit_brutal (void)
for (int nbrutal = 0 ; nbrutal < 1000 ; nbrutal++)
{
LG_brutal = nbrutal ;
// reset both GraphBLAS and LAGraph
GB_Global_GrB_init_called_set (false) ;
LG_init_has_been_called = false ;
OK (LAGr_Reset (msg)) ;
// try to initialize GraphBLAS and LAGraph
int result = LAGr_Init (GrB_NONBLOCKING,
LG_brutal_malloc, LG_brutal_calloc,
LG_brutal_realloc, LG_brutal_free, msg) ;
if (result == 0)
{
// success
OK (LAGraph_Finalize (msg)) ;
printf ("LAGr_Init: finally: %d %g\n", nbrutal,
(double) LG_nmalloc) ;
TEST_CHECK (LG_nmalloc == 0) ;
break ;
}
// failure: free anything partially allocated
OK (LAGraph_Finalize (msg)) ;
}
}
Expand Down
38 changes: 35 additions & 3 deletions src/utility/LAGr_Init.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@
#include "LG_internal.h"

//------------------------------------------------------------------------------
// LAGraph global objects
// LG_init_has_been_called: a static value accessible within this file only
//------------------------------------------------------------------------------

bool LG_init_has_been_called = false ;
static bool LG_init_has_been_called = false ;

//------------------------------------------------------------------------------
// LAGraph global objects
//------------------------------------------------------------------------------

// LAGraph_plus_first_T: using the GrB_PLUS_MONOID_T monoid and the
// corresponding GrB_FIRST_T multiplicative operator.
Expand Down Expand Up @@ -111,7 +115,6 @@ int LAGr_Init
// ensure LAGr_Init has not already been called
LG_ASSERT_MSG (!LG_init_has_been_called, GrB_INVALID_VALUE,
"LAGr*_Init can only be called once") ;
LG_init_has_been_called = true ;

//--------------------------------------------------------------------------
// start GraphBLAS
Expand Down Expand Up @@ -294,8 +297,37 @@ int LAGr_Init
GrB_MIN_MONOID_UINT64 , GrB_ONEB_UINT64)) ;
GRB_TRY (GrB_Semiring_new (&LAGraph_any_one_fp32,
GrB_MIN_MONOID_FP32 , GrB_ONEB_FP32 )) ;
// LAGraph_any_one_fp64 is the last object created:
GRB_TRY (GrB_Semiring_new (&LAGraph_any_one_fp64,
GrB_MIN_MONOID_FP64 , GrB_ONEB_FP64 )) ;

LG_init_has_been_called = true ;
return (GrB_SUCCESS) ;
}

//------------------------------------------------------------------------------
// LAGr_Reset
//------------------------------------------------------------------------------

// This method is meant primarily for testing. It can be only called after
// LAGraph_Finalize has been called; it returns GrB_INVALID otherwise. The
// method allows the testing framework to reset the internal flag that says
// LAGr_Init has been called, so that LAGr_Init can be called again within the
// test.

int LAGr_Reset (char *msg)
{
LG_CLEAR_MSG ;
// check if the last created object has been freed
if (LAGraph_any_one_fp64 != NULL)
{
LG_ASSERT_MSG (false, GrB_INVALID_VALUE,
"LAGr_Reset can only be called after LAGraph_Finalize"
" or before LAGr_Init") ;
}
// only set it to false if the object has been cleared.
LG_init_has_been_called = false ;
// now LAGr_Init can be called again.
return (GrB_SUCCESS) ;
}

0 comments on commit e0ca734

Please sign in to comment.