Skip to content

Commit

Permalink
Add functions to clear error messages (#1190)
Browse files Browse the repository at this point in the history
* Add `hypre_error_handler_clear_messages`
* Add missing fcloses. This was caught with valgrind
* Check all kinds of leaks with valgrind
* Add `HYPRE_ClearErrorMessages`
  • Loading branch information
victorapm authored Dec 5, 2024
1 parent aa1f66d commit c49433c
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 13 deletions.
4 changes: 4 additions & 0 deletions src/test/ij.c
Original file line number Diff line number Diff line change
Expand Up @@ -9398,6 +9398,10 @@ main( hypre_int argc,
}
}

/* Free the memory buffer allocated for storing error messages when using mode 1 for printing errors
Note: This call is redundant since the cleanup is already handled in HYPRE_Finalize. */
HYPRE_ClearErrorMessages();

/* Finalize Hypre */
HYPRE_Finalize();

Expand Down
2 changes: 2 additions & 0 deletions src/test/maxwell_unscaled.c
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,8 @@ ReadData( char *filename,
/* read the next input line */
sdata_line = fgets((sdata + sdata_size), maxline, file);
}

fclose(file);
}

/* broadcast the data size */
Expand Down
2 changes: 1 addition & 1 deletion src/test/runtest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ do
;;
-valgrind)
shift
Valgrind="valgrind -q --suppressions=`pwd`/runtest.valgrind --leak-check=yes --track-origins=yes"
Valgrind="valgrind -q --suppressions=`pwd`/runtest.valgrind --track-origins=yes --leak-check=full --show-leak-kinds=all"
;;
-cudamemcheck)
shift
Expand Down
4 changes: 4 additions & 0 deletions src/test/sstruct.c
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,9 @@ ReadData( char *filename,
/* read the next input line */
sdata_line = fgets((sdata + sdata_size), maxline, file);
}

/* Close file handle */
fclose(file);
}
/* broadcast the data size */
hypre_MPI_Bcast(&sdata_size, 1, HYPRE_MPI_INT, 0, hypre_MPI_COMM_WORLD);
Expand Down Expand Up @@ -3969,6 +3972,7 @@ main( hypre_int argc,
}
}
}
fclose(file);

/* re-initializes x to 0 */
hypre_SStructAxpy(-1.0, b, x);
Expand Down
3 changes: 3 additions & 0 deletions src/utilities/HYPRE_utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,9 @@ HYPRE_Int HYPRE_GetErrorMessages(char **buffer, HYPRE_Int *bufsz);
/* Print the error messages and clear them in hypre */
HYPRE_Int HYPRE_PrintErrorMessages(MPI_Comm comm);

/* Clear the error messages in hypre and free any related memory allocated */
HYPRE_Int HYPRE_ClearErrorMessages(void);

/* Print GPU information */
HYPRE_Int HYPRE_PrintDeviceInfo(void);

Expand Down
1 change: 1 addition & 0 deletions src/utilities/_hypre_utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,7 @@ extern hypre_Error hypre__global_error;
*--------------------------------------------------------------------------*/

void hypre_error_handler(const char *filename, HYPRE_Int line, HYPRE_Int ierr, const char *msg);
void hypre_error_handler_clear_messages(void);
void hypre_error_code_save(void);
void hypre_error_code_restore(void);

Expand Down
46 changes: 34 additions & 12 deletions src/utilities/error.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,22 @@ hypre_error_handler(const char *filename, HYPRE_Int line, HYPRE_Int ierr, const
hypre__global_error = err;
}

/*--------------------------------------------------------------------------
* hypre_error_handler_clear_messages
*--------------------------------------------------------------------------*/

void
hypre_error_handler_clear_messages(void)
{
hypre_Error err = hypre__global_error;

hypre_TFree(err.memory, HYPRE_MEMORY_HOST);
err.mem_sz = 0;
err.msg_sz = 0;

hypre__global_error = err;
}

/*--------------------------------------------------------------------------
*--------------------------------------------------------------------------*/

Expand Down Expand Up @@ -252,11 +268,9 @@ HYPRE_GetErrorMessages(char **buffer, HYPRE_Int *bufsz)
*buffer = hypre_CTAlloc(char, *bufsz, HYPRE_MEMORY_HOST);
hypre_TMemcpy(*buffer, err.memory, char, *bufsz, HYPRE_MEMORY_HOST, HYPRE_MEMORY_HOST);

hypre_TFree(err.memory, HYPRE_MEMORY_HOST);
err.mem_sz = 0;
err.msg_sz = 0;
/* Clear error messages */
hypre_error_handler_clear_messages();

hypre__global_error = err;
return hypre_error_flag;
}

Expand All @@ -267,21 +281,29 @@ HYPRE_GetErrorMessages(char **buffer, HYPRE_Int *bufsz)
HYPRE_Int
HYPRE_PrintErrorMessages(MPI_Comm comm)
{
hypre_Error err = hypre__global_error;

HYPRE_Int myid;
char *msg;
hypre_Error err = hypre__global_error;
HYPRE_Int myid;
char *msg;

hypre_MPI_Comm_rank(comm, &myid);
for (msg = err.memory; msg < (err.memory + err.msg_sz); msg += strlen(msg) + 1)
{
hypre_fprintf(stderr, "%d: %s", myid, msg);
}

hypre_TFree(err.memory, HYPRE_MEMORY_HOST);
err.mem_sz = 0;
err.msg_sz = 0;
/* Clear error messages */
hypre_error_handler_clear_messages();

return hypre_error_flag;
}

/*--------------------------------------------------------------------------
*--------------------------------------------------------------------------*/

HYPRE_Int
HYPRE_ClearErrorMessages(void)
{
hypre_error_handler_clear_messages();

hypre__global_error = err;
return hypre_error_flag;
}
1 change: 1 addition & 0 deletions src/utilities/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ extern hypre_Error hypre__global_error;
*--------------------------------------------------------------------------*/

void hypre_error_handler(const char *filename, HYPRE_Int line, HYPRE_Int ierr, const char *msg);
void hypre_error_handler_clear_messages(void);
void hypre_error_code_save(void);
void hypre_error_code_restore(void);

Expand Down
3 changes: 3 additions & 0 deletions src/utilities/general.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ hypre_HandleDestroy(hypre_Handle *hypre_handle_)
hypre_HandleDeviceData(hypre_handle_) = NULL;
#endif

/* Deallocate error messages in error handler */
hypre_error_handler_clear_messages();

/* Note: Directly using free since this variable was allocated with calloc */
free((void*) hypre_handle_);

Expand Down

0 comments on commit c49433c

Please sign in to comment.