Skip to content

Commit

Permalink
Improve CUDA error handling in luawid.c
Browse files Browse the repository at this point in the history
If CUDA cannot be initialized for some reason, raise a Lua error.
Not raising an error and just returning nil makes error tracing much
more difficult than necessary.
  • Loading branch information
ipatix committed Nov 8, 2024
1 parent a54a56a commit 5030863
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions src/luawid.c
Original file line number Diff line number Diff line change
Expand Up @@ -2688,25 +2688,24 @@ static int lua_likwid_gpustr_to_gpulist_cuda(lua_State *L) {
int ret = 0;
char *gpustr = (char *)luaL_checkstring(L, 1);
if (!cudatopology_isInitialized) {
if (topology_cuda_init() == EXIT_SUCCESS) {
cudatopo = get_cudaTopology();
cudatopology_isInitialized = 1;
} else {
lua_pushnumber(L, 0);
lua_pushnil(L);
return 2;
}
int init = topology_cuda_init();
if (init != EXIT_SUCCESS)
return luaL_error(L, "Unable to init CUDA topology: %s", strerror(-init));

cudatopo = get_cudaTopology();
cudatopology_isInitialized = 1;
}
int *gpulist = (int *)malloc(cudatopo->numDevices * sizeof(int));
if (gpulist == NULL) {
lua_pushstring(L, "Cannot allocate data for the GPU list");
lua_error(L);
}
if (!gpulist)
return luaL_error(L, "Cannot allocate data for the GPU list");

ret = gpustr_to_gpulist_cuda(gpustr, gpulist, cudatopo->numDevices);
if (ret <= 0) {
lua_pushstring(L, "Cannot parse GPU string");
lua_error(L);
if (ret <= 0)
{
free(gpulist);
return luaL_error(L, "Cannot parse GPU string: %d", ret);
}

lua_pushnumber(L, ret);
lua_newtable(L);
for (int i = 0; i < ret; i++) {
Expand Down

0 comments on commit 5030863

Please sign in to comment.