From b1259ea317237b5279d4f196f349222dd9503d95 Mon Sep 17 00:00:00 2001 From: magnum Date: Mon, 14 Jan 2013 01:51:17 +0100 Subject: [PATCH] First version of --devices=N[,..] parsing. It fills an array ocl_device_list[MAXGPUS] but currently nothing is doing anything with that array. The code blocks in john.c that do this should ideally move out to cuda_init() and start_opencl_devices() (or some other function). --- src/common-opencl.h | 1 + src/john.c | 98 +++++++++++++++++++++++++++++++++++++++++++++++------ src/options.c | 28 ++++++--------- src/options.h | 7 ++-- 4 files changed, 102 insertions(+), 32 deletions(-) diff --git a/src/common-opencl.h b/src/common-opencl.h index e543fc4..1ab5085 100644 --- a/src/common-opencl.h +++ b/src/common-opencl.h @@ -35,6 +35,7 @@ /* Common OpenCL variables */ int ocl_gpu_id, platform_id, device_id; +int ocl_device_list[MAXGPUS]; typedef struct { cl_platform_id platform; diff --git a/src/john.c b/src/john.c index 831661b..5056a3c 100644 --- a/src/john.c +++ b/src/john.c @@ -6,6 +6,7 @@ */ #include +#include /* for --device parsing that should be moved */ #ifndef _MSC_VER #include #else @@ -69,6 +70,7 @@ #include "common-opencl.h" #endif #ifdef HAVE_CUDA +extern int cuda_gpu_id; extern void cuda_device_list(); #endif #ifdef NO_JOHN_BLD @@ -1177,27 +1179,99 @@ static void john_init(char *name, int argc, char **argv) } #ifdef HAVE_OPENCL + /* This code should move some init() in common-opencl.c */ + + if (options.ocl_platform) { + struct list_entry *current; + + platform_id = atoi(options.ocl_platform); + + /* Legacy syntax --platform + --device */ + if ((current = options.gpu_devices->head)) { + if (current->next) { + fprintf(stderr, "Only one OpenCL device supported with --platform syntax.\n"); + exit(1); + } + if (!isdigit(current->data[0])) { + fprintf(stderr, "Invalid OpenCL device id %s\n", + current->data); + exit(1); + } + ocl_gpu_id = atoi(current->data); + } else + ocl_gpu_id = -1; + } else { + struct list_entry *current; + + /* New syntax, sequential --device */ + if ((current = options.gpu_devices->head)) { + int n = 0; + + do { + if (!isdigit(current->data[0])) { + fprintf(stderr, + "Invalid OpenCL device id \"%s\"\n", + current->data); + exit(1); + } + ocl_device_list[n++] = atoi(current->data); + } while ((current = current->next)); + + ocl_gpu_id = ocl_device_list[0]; // FIXME? + } else + ocl_gpu_id = -1; + } + if (!options.ocl_platform) { - if ((options.ocl_platform = - cfg_get_param(SECTION_OPTIONS, SUBSECTION_OPENCL, "Platform"))) - platform_id = atoi(options.ocl_platform); + char *devcfg; + + if ((devcfg = + cfg_get_param(SECTION_OPTIONS, SUBSECTION_OPENCL, + "Platform"))) + platform_id = atoi(devcfg); else platform_id = -1; } - if (!options.gpu_device) { - if ((options.gpu_device = - cfg_get_param(SECTION_OPTIONS, SUBSECTION_OPENCL, "Device"))) - ocl_gpu_id = atoi(options.gpu_device); + + if (!options.gpu_devices) { + char *devcfg; + + if ((devcfg = cfg_get_param(SECTION_OPTIONS, SUBSECTION_OPENCL, + "Device"))) + ocl_gpu_id = atoi(devcfg); else ocl_gpu_id = -1; } + if (platform_id == -1 || ocl_gpu_id == -1) opencl_find_gpu(&ocl_gpu_id, &platform_id); - //Use the sequential number on ocl_gpu_id. - start_opencl_devices(); - device_id = ocl_gpu_id; - ocl_gpu_id = get_sequential_id(device_id, platform_id); + //Use the sequential number on ocl_gpu_id. + start_opencl_devices(); + device_id = ocl_gpu_id; + ocl_gpu_id = get_sequential_id(device_id, platform_id); +#endif +#ifdef HAVE_CUDA + /* This code should move to cuda_init() and drop the #ifndef OPENCL */ + { + struct list_entry *current; + + if ((current = options.gpu_devices->head)) { +#ifndef HAVE_OPENCL + if (current->next) { + fprintf(stderr, "Only one CUDA device supported.\n"); + exit(1); + } +#endif + if (!isdigit(current->data[0])) { + fprintf(stderr, "Invalid CUDA device id \"%s\"\n", + current->data); + exit(1); + } + cuda_gpu_id = atoi(current->data); + } else + cuda_gpu_id = 0; + } #endif common_init(); @@ -1318,8 +1392,10 @@ static void john_done(void) log_event("Session completed"); fmt_done(database.format); } +#ifdef HAVE_OPENCL //Release OpenCL stuff. clean_opencl_devices(); +#endif log_done(); path_done(); diff --git a/src/options.c b/src/options.c index c874d1f..701eab1 100644 --- a/src/options.c +++ b/src/options.c @@ -42,9 +42,6 @@ #ifdef HAVE_OPENCL #include "common-opencl.h" #endif -#if defined(HAVE_CUDA) -extern int cuda_gpu_id; -#endif struct options_main options; static char *field_sep_char_string; @@ -152,8 +149,10 @@ static struct opt_entry opt_list[] = { OPT_FMT_STR_ALLOC, &options.ocl_platform}, #endif #if defined(HAVE_OPENCL) || defined(HAVE_CUDA) - {"device", FLG_NONE, FLG_NONE, 0, OPT_REQ_PARAM, - OPT_FMT_STR_ALLOC, &options.gpu_device}, + {"devices", FLG_NONE, FLG_NONE, 0, OPT_REQ_PARAM, + OPT_FMT_ADD_LIST_MULTI, &options.gpu_devices}, +#endif +#ifdef HAVE_OPENCL {"request-vectorize", FLG_VECTORIZE, FLG_VECTORIZE, 0, FLG_SCALAR}, {"request-scalar", FLG_SCALAR, FLG_SCALAR, 0, FLG_VECTORIZE}, #endif @@ -212,14 +211,14 @@ static struct opt_entry opt_list[] = { #if defined(HAVE_OPENCL) && defined(HAVE_CUDA) #define JOHN_USAGE_GPU \ "--platform=N set OpenCL platform (list using --list=opencl-devices)\n" \ -"--device=N set OpenCL or CUDA device\n" +"--devices=N[,..] set OpenCL or CUDA device(s)\n" #elif defined(HAVE_OPENCL) #define JOHN_USAGE_GPU \ "--platform=N set OpenCL platform\n" \ -"--device=N set OpenCL device (list using --list=opencl-devices)\n" +"--devices=N[,..] set OpenCL device(s) (list using --list=opencl-devices)\n" #elif defined (HAVE_CUDA) #define JOHN_USAGE_GPU \ -"--device=N set CUDA device\n" +"--device=N set CUDA device (list using --list=cuda-devices)\n" #endif static int qcmpstr(const void *p1, const void *p2) @@ -337,6 +336,9 @@ void opt_init(char *name, int argc, char **argv, int show_usage) #ifdef HAVE_DL list_init(&options.fmt_dlls); #endif +#if defined(HAVE_OPENCL) || defined(HAVE_CUDA) + list_init(&options.gpu_devices); +#endif options.length = -1; @@ -368,16 +370,6 @@ void opt_init(char *name, int argc, char **argv, int show_usage) return; } -#ifdef HAVE_OPENCL - if (options.ocl_platform) - platform_id = atoi(options.ocl_platform); - if (options.gpu_device) - ocl_gpu_id = atoi(options.gpu_device); -#endif -#ifdef HAVE_CUDA - if (options.gpu_device) - cuda_gpu_id = atoi(options.gpu_device); -#endif if (options.flags & FLG_STATUS_CHK) { rec_restore_args(0); options.flags |= FLG_STATUS_SET; diff --git a/src/options.h b/src/options.h index 39355b5..66cd0a3 100644 --- a/src/options.h +++ b/src/options.h @@ -233,9 +233,10 @@ struct options_main { char dynamic_raw_hashes_always_valid; #ifdef HAVE_OPENCL - char *ocl_platform, *gpu_device; -#elif defined(HAVE_CUDA) - char *gpu_device; + char *ocl_platform; +#endif +#if defined(HAVE_OPENCL) || defined(HAVE_CUDA) + struct list_main *gpu_devices; #endif /* -list=WHAT Get a config list (eg. a list of incremental modes available) */ char *listconf; -- 1.8.0.1