Skip to content

Commit

Permalink
Add an option to finalize libnomp without finalizing Python interpreter
Browse files Browse the repository at this point in the history
  • Loading branch information
thilinarmtb committed Jan 24, 2024
1 parent 81bd210 commit 98036be
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 18 deletions.
2 changes: 2 additions & 0 deletions include/nomp.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ int nomp_get_err_no(unsigned id);

int nomp_finalize(void);

int nomp_finalize_excluding_interpreter(void);

#ifdef __cplusplus
}
#endif
Expand Down
5 changes: 2 additions & 3 deletions src/loopy.c
Original file line number Diff line number Diff line change
Expand Up @@ -506,10 +506,9 @@ int nomp_py_finalize(int interpreter) {
Py_XDECREF(py_pymbolic_to_symengine_str), py_pymbolic_to_symengine_str = NULL;
Py_XDECREF(py_backend_str), py_backend_str = NULL;

// We only finalize the python interpreter if user explicitly asked for it.
// We only finalize the Python interpreter if user explicitly asked for it.
// This is because some modules like numpy can't be re-initialized in the
// same program.
// See: https://github.com/pybind/pybind11/issues/3112
// same process. See: https://github.com/pybind/pybind11/issues/3112
if (interpreter)
Py_FinalizeEx();

Expand Down
45 changes: 32 additions & 13 deletions src/nomp.c
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ static unsigned mems_max = 0;

/**
* @ingroup nomp_mem_utils
*
* @brief Returns the nomp_mem object corresponding to host pointer \p p.
*
* Returns the nomp_mem object corresponding to host ponter \p p. If no buffer
Expand Down Expand Up @@ -589,6 +590,7 @@ int nomp_jit(int *id, const char *csrc, const char **clauses, int nargs, ...) {

/**
* @ingroup nomp_user_api
*
* @brief Runs the kernel generated by nomp_jit().
*
* @details Runs the kernel with a given kernel id. Kernel id is followed by the
Expand Down Expand Up @@ -675,6 +677,7 @@ int nomp_run(int id, ...) {

/**
* @ingroup nomp_user_api
*
* @brief Synchronize task execution on device.
*
* Implement a host-side barrier till the device finish executing all the
Expand All @@ -684,18 +687,7 @@ int nomp_run(int id, ...) {
*/
int nomp_sync(void) { return nomp.sync(&nomp); }

/**
* @ingroup nomp_user_api
* @brief Finalizes libnomp runtime.
*
* @details Frees allocated runtime resources for libnomp. Returns a non-zero
* value if an error occurs during the finalize process, otherwise returns 0.
* Calling this method before nomp_init() will return an error. Calling this
* method twice will also return an error.
*
* @return int
*/
int nomp_finalize(void) {
static int nomp_finalize_impl(int interpreter) {
if (!initialized)
return NOMP_FINALIZE_FAILURE;

Expand Down Expand Up @@ -729,7 +721,7 @@ int nomp_finalize(void) {
nomp_free(&progs[i]);
}
nomp_free(&progs), progs_n = progs_max = 0;
nomp_check(nomp_py_finalize(0));
nomp_check(nomp_py_finalize(interpreter));

// Free bookkeeping structures for the logger and profiler since these can be
// released irrespective of whether libnomp is initialized or not.
Expand All @@ -741,3 +733,30 @@ int nomp_finalize(void) {

return 0;
}

/**
* @ingroup nomp_user_api
*
* @brief Finalizes libnomp runtime including the Python interpreter.
*
* @details Frees allocated runtime resources for libnomp. Returns a non-zero
* value if an error occurs during the finalize process, otherwise returns 0.
* Calling this method before nomp_init() will return an error. Calling this
* method twice will also return an error.
*
* @return int
*/
int nomp_finalize(void) { return nomp_finalize_impl(1); }

/**
* @ingroup nomp_user_api
*
* @brief Finalizes libnomp runtime without finalizing the Python interpreter.
*
* @details Acts the same as nomp_finalize() except that this method does not
* finalize the Python interpreter. This method is useful when libnomp has to
* be re-initialized multiple times in the same process.
*
* @return int
*/
int nomp_finalize_excluding_interpreter(void) { return nomp_finalize_impl(0); }
2 changes: 1 addition & 1 deletion tests/nomp-api-000.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ static int test_nomp_init_twice(int argc, const char **argv) {
nomp_free(&desc);
nomp_test_assert(eq);

nomp_test_check(nomp_finalize());
nomp_test_check(nomp_finalize_excluding_interpreter());

return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/nomp-api-021.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ static int test_valid_platform_id(int argc, const char **argv) {
set_test_env(platform, "NOMP_PLATFORM", "0");

nomp_test_check(nomp_init(argc, argv));
nomp_test_check(nomp_finalize());
nomp_test_check(nomp_finalize_excluding_interpreter());

reset_env(platform, "NOMP_PLATFORM");

Expand Down

0 comments on commit 98036be

Please sign in to comment.