Skip to content

Commit

Permalink
replace LAGr_Reset with LG_get/set
Browse files Browse the repository at this point in the history
  • Loading branch information
DrTimothyAldenDavis committed Dec 6, 2023
1 parent e0ca734 commit d5c547a
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 49 deletions.
22 changes: 15 additions & 7 deletions cmake_modules/FindGraphBLAS.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,10 @@ message ( STATUS "Looking for SuiteSparse GraphBLAS" )

find_package ( GraphBLAS ${GraphBLAS_FIND_VERSION} CONFIG
PATHS ${CMAKE_BINARY_DIR} ${PROJECT_SOURCE_DIR}/../GraphBLAS/build NO_DEFAULT_PATH )
set ( _lagraph_gb_common_tree ON )
if ( NOT TARGET SuiteSparse::GraphBLAS )
find_package ( GraphBLAS ${GraphBLAS_FIND_VERSION} CONFIG )
set ( _lagraph_gb_common_tree OFF )
endif ( )

if ( GraphBLAS_FOUND )
Expand All @@ -83,11 +85,14 @@ if ( GraphBLAS_FOUND )
else ( )
add_library ( GraphBLAS::GraphBLAS ALIAS ${_graphblas_aliased} )
endif ( )
if ( GRAPHBLAS_VERSION LESS "8.3.0" )
# workaround for SuiteSparse:GraphBLAS 8.2.x (did not have "/Include")
if ( GRAPHBLAS_VERSION LESS "8.3.0" AND _lagraph_gb_common_tree )
# workaround for incorrect INTERFACE_INCLUDE_DIRECTORIES of
# SuiteSparse:GraphBLAS 8.2.x before installation
# (did not have "/Include")
get_property ( _inc TARGET GraphBLAS::GraphBLAS PROPERTY
INTERFACE_INCLUDE_DIRECTORIES )
include_directories ( ${_inc}/Include )
target_include_directories ( GraphBLAS::GraphBLAS INTERFACE
${_inc}/Include )
message ( STATUS "additional include: ${_inc}/Include" )
endif ( )
endif ( )
Expand All @@ -101,12 +106,15 @@ if ( GraphBLAS_FOUND )
else ( )
add_library ( GraphBLAS::GraphBLAS_static ALIAS ${_graphblas_aliased} )
endif ( )
if ( GRAPHBLAS_VERSION LESS "8.3.0" )
# workaround for SuiteSparse:GraphBLAS 8.2.x (did not have "/Include")
if ( GRAPHBLAS_VERSION LESS "8.3.0" AND _lagraph_gb_common_tree )
# workaround for incorrect INTERFACE_INCLUDE_DIRECTORIES of
# SuiteSparse:GraphBLAS 8.2.x before installation
# (did not have "/Include")
get_property ( _inc TARGET GraphBLAS::GraphBLAS_static PROPERTY
INTERFACE_INCLUDE_DIRECTORIES )
include_directories ( ${_inc}/Include )
message ( STATUS "additional include: ${_inc}/Include" )
target_include_directories ( GraphBLAS::GraphBLAS_static INTERFACE
${_inc}/Include )
message ( STATUS "additional include: ${_inc}/Include" )
endif ( )
endif ( )
return ( )
Expand Down
2 changes: 0 additions & 2 deletions include/LAGraphX.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ extern "C"
// 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
24 changes: 16 additions & 8 deletions src/test/test_Xinit.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// LAGraph/src/test/test_Xinit.c: test LAGr_Init and LAGraph_Global
//------------------------------------------------------------------------------

// LAGraph, (c) 2019-2022 by The LAGraph Contributors, All Rights Reserved.
// LAGraph, (c) 2019-2023 by The LAGraph Contributors, All Rights Reserved.
// SPDX-License-Identifier: BSD-2-Clause
//
// For additional details (including references to third party source code and
Expand All @@ -17,6 +17,7 @@

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

//------------------------------------------------------------------------------
// global variables
Expand Down Expand Up @@ -47,6 +48,7 @@ void test_Xinit (void)

OK (LAGr_Init (GrB_NONBLOCKING, malloc, calloc, realloc, free, msg)) ;
printf ("msg: [%s]\n", msg) ;
TEST_CHECK (LG_get_LAGr_Init_has_been_called ( ) == true) ;

// LAGr_Init cannot be called twice
int status = LAGr_Init (GrB_NONBLOCKING,
Expand All @@ -56,14 +58,17 @@ void test_Xinit (void)

OK (LAGraph_Finalize (msg)) ;

// the flag is still set after LAGraph_Finalize has been called,
// per LAGraph policy
TEST_CHECK (LG_get_LAGr_Init_has_been_called ( ) == true) ;

// reset and try again
OK (LAGr_Reset (msg)) ;
LG_set_LAGr_Init_has_been_called (false) ;
TEST_CHECK (LG_get_LAGr_Init_has_been_called ( ) == false) ;
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_CHECK (LG_get_LAGr_Init_has_been_called ( ) == true) ;
OK (LAGraph_Finalize (msg)) ;
TEST_CHECK (LG_get_LAGr_Init_has_been_called ( ) == true) ;
}

//------------------------------------------------------------------------------
Expand Down Expand Up @@ -136,12 +141,15 @@ void test_Xinit_brutal (void)
}
}

TEST_CHECK (LG_get_LAGr_Init_has_been_called ( ) == true) ;

for (int nbrutal = 0 ; nbrutal < 1000 ; nbrutal++)
{
LG_brutal = nbrutal ;
// reset both GraphBLAS and LAGraph
GB_Global_GrB_init_called_set (false) ;
OK (LAGr_Reset (msg)) ;
LG_set_LAGr_Init_has_been_called (false) ;
TEST_CHECK (LG_get_LAGr_Init_has_been_called ( ) == false) ;
// try to initialize GraphBLAS and LAGraph
int result = LAGr_Init (GrB_NONBLOCKING,
LG_brutal_malloc, LG_brutal_calloc,
Expand Down
60 changes: 28 additions & 32 deletions src/utility/LAGr_Init.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// LAGr_Init: start GraphBLAS and LAGraph, and set malloc/etc functions
//------------------------------------------------------------------------------

// LAGraph, (c) 2019-2022 by The LAGraph Contributors, All Rights Reserved.
// LAGraph, (c) 2019-2023 by The LAGraph Contributors, All Rights Reserved.
// SPDX-License-Identifier: BSD-2-Clause
//
// For additional details (including references to third party source code and
Expand All @@ -20,10 +20,33 @@
#include "LG_internal.h"

//------------------------------------------------------------------------------
// LG_init_has_been_called: a static value accessible within this file only
// LG_LAGr_Init_has_been_called: a static value only accessible within this file
//------------------------------------------------------------------------------

static bool LG_init_has_been_called = false ;
// LG_LAGr_Init_has_been_called indicates if LAGr_Init has been called.
// LAGr_Init (or LAGraph_Init) can be called only once, even after
// LAGraph_Finalize has been called. For testing purposes, the flag can be
// cleared by src/test/test_Xinit, to allow LAGr_Init or LAGraph_Init to be
// called again.

static bool LG_LAGr_Init_has_been_called = false ;

// LG_LAGr_Init_has_been_called is only modified or accessed by these two
// routines (even in this file):

#include "LG_init.h"

LAGRAPH_PUBLIC
void LG_set_LAGr_Init_has_been_called (bool setting)
{
LG_LAGr_Init_has_been_called = setting ;
}

LAGRAPH_PUBLIC
bool LG_get_LAGr_Init_has_been_called (void)
{
return (LG_LAGr_Init_has_been_called) ;
}

//------------------------------------------------------------------------------
// LAGraph global objects
Expand Down Expand Up @@ -113,7 +136,7 @@ int LAGr_Init
GrB_Info info ;

// ensure LAGr_Init has not already been called
LG_ASSERT_MSG (!LG_init_has_been_called, GrB_INVALID_VALUE,
LG_ASSERT_MSG (!LG_get_LAGr_Init_has_been_called ( ), GrB_INVALID_VALUE,
"LAGr*_Init can only be called once") ;

//--------------------------------------------------------------------------
Expand Down Expand Up @@ -297,37 +320,10 @@ 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.
LG_set_LAGr_Init_has_been_called (true) ;
return (GrB_SUCCESS) ;
}

32 changes: 32 additions & 0 deletions src/utility/LG_init.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//------------------------------------------------------------------------------
// LG_init.h: include file for use within LAGraph itself
//------------------------------------------------------------------------------

// LAGraph, (c) 2019-2023 by The LAGraph Contributors, All Rights Reserved.
// SPDX-License-Identifier: BSD-2-Clause
//
// For additional details (including references to third party source code and
// other files) see the LICENSE file or contact [email protected]. See
// Contributors.txt for a full list of contributors. Created, in part, with
// funding and support from the U.S. Government (see Acknowledgments.txt file).
// DM22-0790

// Contributed by Timothy A. Davis, Texas A&M University

//------------------------------------------------------------------------------

// These definitions are not meant for the user application that relies on
// LAGraph and/or GraphBLAS. LG_* methods are for internal use in LAGraph.

#ifndef LG_INIT_H
#define LG_INIT_H

//------------------------------------------------------------------------------
// definitions used in LAGr_Init.c and for testing
//------------------------------------------------------------------------------

LAGRAPH_PUBLIC void LG_set_LAGr_Init_has_been_called (bool setting) ;
LAGRAPH_PUBLIC bool LG_get_LAGr_Init_has_been_called (void) ;

#endif

0 comments on commit d5c547a

Please sign in to comment.