From 1eabaf5f3338e687e1a4a6b314d9ed7de7f3ec5e Mon Sep 17 00:00:00 2001 From: Wayne Mitchell Date: Tue, 21 Mar 2023 14:31:52 -0700 Subject: [PATCH] Report error on hypre calls outside of HYPRE_Init() - HYPRE_Finalize() (#837) When hypre handle is not initialized and a call to hypre_handle() is made, report an error and call HYPRE_Init() rather than just calling hypre_HandleCreate() with no error reporting. --- src/test/ij.c | 50 ++++++++++++++++++++--------------------- src/utilities/general.c | 9 ++++++-- 2 files changed, 32 insertions(+), 27 deletions(-) diff --git a/src/test/ij.c b/src/test/ij.c index 1476637fd..b3c3b456f 100644 --- a/src/test/ij.c +++ b/src/test/ij.c @@ -475,6 +475,31 @@ main( hypre_int argc, char mem_tracker_name[HYPRE_MAX_FILE_NAME_LEN] = {0}; #endif + /* Initialize MPI */ + hypre_MPI_Init(&argc, &argv); + + hypre_MPI_Comm_size(hypre_MPI_COMM_WORLD, &num_procs ); + hypre_MPI_Comm_rank(hypre_MPI_COMM_WORLD, &myid ); + + /*----------------------------------------------------------------- + * GPU Device binding + * Must be done before HYPRE_Init() and should not be changed after + *-----------------------------------------------------------------*/ + hypre_bind_device(myid, num_procs, hypre_MPI_COMM_WORLD); + + time_index = hypre_InitializeTiming("Hypre init"); + hypre_BeginTiming(time_index); + + /*----------------------------------------------------------- + * Initialize : must be the first HYPRE function to call + *-----------------------------------------------------------*/ + HYPRE_Init(); + + hypre_EndTiming(time_index); + hypre_PrintTiming("Hypre init times", hypre_MPI_COMM_WORLD); + hypre_FinalizeTiming(time_index); + hypre_ClearTiming(); + /* default execution policy and memory space */ #if defined(HYPRE_TEST_USING_HOST) HYPRE_MemoryLocation memory_location = HYPRE_MEMORY_HOST; @@ -529,12 +554,6 @@ main( hypre_int argc, size_t mempool_max_cached_bytes = 2000LL * 1024 * 1024; #endif - /* Initialize MPI */ - hypre_MPI_Init(&argc, &argv); - - hypre_MPI_Comm_size(hypre_MPI_COMM_WORLD, &num_procs ); - hypre_MPI_Comm_rank(hypre_MPI_COMM_WORLD, &myid ); - /*----------------------------------------------------------- * Set defaults *-----------------------------------------------------------*/ @@ -2429,25 +2448,6 @@ main( hypre_int argc, hypre_printf(" solver ID = %d\n\n", solver_id); } - /*----------------------------------------------------------------- - * GPU Device binding - * Must be done before HYPRE_Init() and should not be changed after - *-----------------------------------------------------------------*/ - hypre_bind_device(myid, num_procs, hypre_MPI_COMM_WORLD); - - time_index = hypre_InitializeTiming("Hypre init"); - hypre_BeginTiming(time_index); - - /*----------------------------------------------------------- - * Initialize : must be the first HYPRE function to call - *-----------------------------------------------------------*/ - HYPRE_Init(); - - hypre_EndTiming(time_index); - hypre_PrintTiming("Hypre init times", hypre_MPI_COMM_WORLD); - hypre_FinalizeTiming(time_index); - hypre_ClearTiming(); - #ifdef HYPRE_USING_DEVICE_POOL /* To be effective, hypre_SetCubMemPoolSize must immediately follow HYPRE_Init */ HYPRE_SetGPUMemoryPoolSize( mempool_bin_growth, mempool_min_bin, diff --git a/src/utilities/general.c b/src/utilities/general.c index d52271ace..7f096dd87 100644 --- a/src/utilities/general.c +++ b/src/utilities/general.c @@ -35,7 +35,8 @@ hypre_handle(void) { if (!_hypre_handle) { - _hypre_handle = hypre_HandleCreate(); + hypre_error_w_msg(HYPRE_ERROR_GENERIC, "ERROR - _hypre_handle is not initialized. Calling HYPRE_Init(). All HYPRE_* or hypre_* function calls should occur between HYPRE_Init() and HYPRE_Finalize().\n"); + HYPRE_Init(); } return _hypre_handle; @@ -354,12 +355,16 @@ HYPRE_Finalize(void) hypre_UmpireFinalize(_hypre_handle); #endif +#if defined(HYPRE_USING_SYCL) + /* With sycl, cannot call hypre_GetDeviceLastError() after destroying the handle, so do it here */ + hypre_GetDeviceLastError(); +#endif + hypre_HandleDestroy(_hypre_handle); _hypre_handle = NULL; #if !defined(HYPRE_USING_SYCL) - /* With sycl, cannot call hypre_GetDeviceLastError() after destroying the handle */ hypre_GetDeviceLastError(); #endif