Skip to content

Commit 6d0d1b5

Browse files
authored
Merge pull request #293 from khaotik/errmsg
better error message for CUDA JIT linking
2 parents fde02d2 + 3c55c60 commit 6d0d1b5

3 files changed

Lines changed: 75 additions & 6 deletions

File tree

src/gpuarray_buffer_cuda.c

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,22 @@ static gpukernel *cuda_newkernel(gpucontext *c, unsigned int count,
984984
CUdevice dev;
985985
unsigned int i;
986986
int major, minor;
987+
strb debug_msg = STRB_STATIC_INIT;
988+
989+
// options for cuModuleLoadDataEx
990+
const size_t cujit_log_size = 4096;
991+
char *cujit_info_log = NULL;
992+
unsigned int num_cujit_opts = 4;
993+
CUjit_option cujit_opts[] = {
994+
CU_JIT_INFO_LOG_BUFFER_SIZE_BYTES,
995+
CU_JIT_INFO_LOG_BUFFER,
996+
CU_JIT_ERROR_LOG_BUFFER_SIZE_BYTES,
997+
CU_JIT_ERROR_LOG_BUFFER
998+
};
999+
void *cujit_opt_vals[] = {
1000+
(void*)(size_t)cujit_log_size, NULL,
1001+
(void*)(size_t)cujit_log_size, NULL,
1002+
};
9871003

9881004
if (count == 0) FAIL(NULL, GA_VALUE_ERROR);
9891005

@@ -1069,10 +1085,9 @@ static gpukernel *cuda_newkernel(gpucontext *c, unsigned int count,
10691085
&log, &log_len, ret);
10701086
if (bin == NULL) {
10711087
if (err_str != NULL) {
1072-
strb debug_msg = STRB_STATIC_INIT;
10731088

10741089
// We're substituting debug_msg for a string with this first line:
1075-
strb_appends(&debug_msg, "CUDA kernel build failure ::\n");
1090+
strb_appends(&debug_msg, "CUDA kernel compile failure ::\n");
10761091

10771092
/* Delete the final NUL */
10781093
sb.l--;
@@ -1089,7 +1104,7 @@ static gpukernel *cuda_newkernel(gpucontext *c, unsigned int count,
10891104
}
10901105
strb_clear(&sb);
10911106
cuda_exit(ctx);
1092-
return NULL;
1107+
FAIL(NULL, GA_IMPL_ERROR);
10931108
}
10941109
}
10951110

@@ -1122,15 +1137,46 @@ static gpukernel *cuda_newkernel(gpucontext *c, unsigned int count,
11221137
FAIL(NULL, GA_MEMORY_ERROR);
11231138
}
11241139

1125-
ctx->err = cuModuleLoadData(&res->m, bin);
1140+
// for both info/err log
1141+
cujit_info_log = (char*)malloc(2*cujit_log_size*sizeof(char));
1142+
if(cujit_info_log == NULL) {
1143+
_cuda_freekernel(res);
1144+
strb_clear(&sb);
1145+
cuda_exit(ctx);
1146+
FAIL(NULL, GA_MEMORY_ERROR);
1147+
}
1148+
cujit_info_log[0] = 0;
1149+
cujit_info_log[cujit_log_size] = 0;
1150+
cujit_opt_vals[1] = (void*)cujit_info_log;
1151+
cujit_opt_vals[3] = (void*)(cujit_info_log+cujit_log_size);
1152+
1153+
ctx->err = cuModuleLoadDataEx(
1154+
&res->m, bin,
1155+
num_cujit_opts, cujit_opts, (void**)cujit_opt_vals);
11261156

11271157
if (ctx->err != CUDA_SUCCESS) {
1158+
if (err_str != NULL) {
1159+
strb_appends(&debug_msg, "CUDA kernel link failure::\n");
1160+
if (cujit_info_log[0]) {
1161+
strb_appends(&debug_msg, "\nLinker msg:\n");
1162+
strb_appends(&debug_msg, cujit_info_log);
1163+
}
1164+
if (cujit_info_log[cujit_log_size]) {
1165+
strb_appends(&debug_msg, "\nLinker error log:\n");
1166+
strb_appends(&debug_msg, cujit_info_log+cujit_log_size);
1167+
}
1168+
strb_append0(&debug_msg);
1169+
*err_str = strb_cstr(&debug_msg);
1170+
}
1171+
free(cujit_info_log);
11281172
_cuda_freekernel(res);
11291173
strb_clear(&sb);
11301174
cuda_exit(ctx);
11311175
FAIL(NULL, GA_IMPL_ERROR);
11321176
}
11331177

1178+
free(cujit_info_log);
1179+
11341180
ctx->err = cuModuleGetFunction(&res->k, res->m, fname);
11351181
if (ctx->err != CUDA_SUCCESS) {
11361182
_cuda_freekernel(res);
@@ -1352,7 +1398,7 @@ static int cuda_property(gpucontext *c, gpudata *buf, gpukernel *k, int prop_id,
13521398
*((char **)res) = s;
13531399
cuda_exit(ctx);
13541400
return GA_NO_ERROR;
1355-
1401+
13561402
case GA_CTX_PROP_PCIBUSID:
13571403
cuda_enter(ctx);
13581404
ctx->err = cuCtxGetDevice(&id);

src/loaders/libcuda.fn

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ DEF_PROC_V2(cuCtxPushCurrent, (CUcontext ctx));
1818
DEF_PROC_V2(cuCtxPopCurrent, (CUcontext *pctx));
1919

2020
DEF_PROC(cuModuleLoadData, (CUmodule *module, const void *image));
21+
DEF_PROC(cuModuleLoadDataEx, (CUmodule *module, const void *image, unsigned int numOptions, CUjit_option *options, void **optionValues));
2122
DEF_PROC(cuModuleUnload, (CUmodule hmod));
2223
DEF_PROC(cuModuleGetFunction, (CUfunction *hfunc, CUmodule hmod, const char *name));
2324

@@ -50,4 +51,4 @@ DEF_PROC_V2(cuStreamDestroy, (CUstream hStream));
5051

5152
DEF_PROC(cuIpcGetMemHandle, (CUipcMemHandle *pHandle, CUdeviceptr dptr));
5253
DEF_PROC(cuIpcOpenMemHandle, (CUdeviceptr *pdptr, CUipcMemHandle handle, unsigned int Flags));
53-
DEF_PROC(cuIpcCloseMemHandle, (CUdeviceptr dptr));
54+
DEF_PROC(cuIpcCloseMemHandle, (CUdeviceptr dptr));

src/loaders/libcuda.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ typedef enum CUfunction_attribute_enum CUfunction_attribute;
2929
typedef enum CUevent_flags_enum CUevent_flags;
3030
typedef enum CUctx_flags_enum CUctx_flags;
3131
typedef enum CUipcMem_flags_enum CUipcMem_flags;
32+
typedef enum CUjit_option_enum CUjit_option;
3233

3334
#define CU_IPC_HANDLE_SIZE 64
3435

@@ -184,4 +185,25 @@ enum CUipcMem_flags_enum {
184185
CU_IPC_MEM_LAZY_ENABLE_PEER_ACCESS = 0x1
185186
};
186187

188+
enum CUjit_option_enum {
189+
CU_JIT_MAX_REGISTERS = 0,
190+
CU_JIT_THREADS_PER_BLOCK,
191+
CU_JIT_WALL_TIME,
192+
CU_JIT_INFO_LOG_BUFFER,
193+
CU_JIT_INFO_LOG_BUFFER_SIZE_BYTES,
194+
CU_JIT_ERROR_LOG_BUFFER,
195+
CU_JIT_ERROR_LOG_BUFFER_SIZE_BYTES,
196+
CU_JIT_OPTIMIZATION_LEVEL,
197+
CU_JIT_TARGET_FROM_CUCONTEXT,
198+
CU_JIT_TARGET,
199+
CU_JIT_FALLBACK_STRATEGY,
200+
CU_JIT_GENERATE_DEBUG_INFO,
201+
CU_JIT_LOG_VERBOSE,
202+
CU_JIT_GENERATE_LINE_INFO,
203+
CU_JIT_CACHE_MODE,
204+
CU_JIT_NEW_SM3X_OPT,
205+
CU_JIT_FAST_COMPILE,
206+
CU_JIT_NUM_OPTIONS
207+
};
208+
187209
#endif

0 commit comments

Comments
 (0)