1
0
mirror of https://github.com/hashcat/hashcat.git synced 2025-02-02 02:41:35 +00:00

Initialize CUDA devices and some first attribute queries

This commit is contained in:
Jens Steube 2019-04-28 14:45:50 +02:00
parent 222be0b0dc
commit a415422123
3 changed files with 1058 additions and 833 deletions

View File

@ -39,6 +39,12 @@ int hc_nvrtcGetProgramLog (hashcat_ctx_t *hashcat_ctx, nvrtcProgram prog,
int hc_nvrtcGetPTXSize (hashcat_ctx_t *hashcat_ctx, nvrtcProgram prog, size_t *ptxSizeRet);
int hc_nvrtcGetPTX (hashcat_ctx_t *hashcat_ctx, nvrtcProgram prog, char *ptx);
int hc_cuInit (hashcat_ctx_t *hashcat_ctx, unsigned int Flags);
int hc_cuDeviceGetAttribute (hashcat_ctx_t *hashcat_ctx, int *pi, CUdevice_attribute attrib, CUdevice dev);
int hc_cuDeviceGetCount (hashcat_ctx_t *hashcat_ctx, int *count);
int hc_cuDeviceGet (hashcat_ctx_t *hashcat_ctx, CUdevice *device, int ordinal);
int hc_cuDeviceGetName (hashcat_ctx_t *hashcat_ctx, char *name, int len, CUdevice dev);
int hc_clBuildProgram (hashcat_ctx_t *hashcat_ctx, cl_program program, cl_uint num_devices, const cl_device_id *device_list, const char *options, void (CL_CALLBACK *pfn_notify) (cl_program program, void *user_data), void *user_data);
int hc_clCreateBuffer (hashcat_ctx_t *hashcat_ctx, cl_context context, cl_mem_flags flags, size_t size, void *host_ptr, cl_mem *mem);
int hc_clCreateCommandQueue (hashcat_ctx_t *hashcat_ctx, cl_context context, cl_device_id device, cl_command_queue_properties properties, cl_command_queue *command_queue);

View File

@ -1340,6 +1340,18 @@ typedef struct backend_ctx
void *cuda;
void *nvrtc;
int *backend_device_from_cuda; // from cuda device index to backend device index
int *backend_device_to_cuda; // from backend device index to cuda device index
int *backend_device_from_opencl; // from opencl device index to backend device index
int *backend_device_to_opencl; // from backend device index to opencl device index
int backend_devices_cnt;
int backend_devices_active;
int cuda_devices_cnt;
int cuda_devices_active;
int opencl_devices_cnt;
int opencl_devices_active;
cl_uint platforms_cnt;
cl_platform_id *platforms;
char **platforms_vendor;

View File

@ -851,6 +851,33 @@ void cuda_close (hashcat_ctx_t *hashcat_ctx)
}
}
int hc_cuInit (hashcat_ctx_t *hashcat_ctx, unsigned int Flags)
{
backend_ctx_t *backend_ctx = hashcat_ctx->backend_ctx;
CUDA_PTR *cuda = backend_ctx->cuda;
const CUresult CU_err = cuda->cuInit (Flags);
if (CU_err != CUDA_SUCCESS)
{
const char *pStr = NULL;
if (cuda->cuGetErrorString (CU_err, &pStr) == CUDA_SUCCESS)
{
event_log_error (hashcat_ctx, "cuInit(): %s", pStr);
}
else
{
event_log_error (hashcat_ctx, "cuInit(): %d", CU_err);
}
return -1;
}
return 0;
}
int hc_cuDeviceGetAttribute (hashcat_ctx_t *hashcat_ctx, int *pi, CUdevice_attribute attrib, CUdevice dev)
{
backend_ctx_t *backend_ctx = hashcat_ctx->backend_ctx;
@ -878,6 +905,88 @@ int hc_cuDeviceGetAttribute (hashcat_ctx_t *hashcat_ctx, int *pi, CUdevice_attri
return 0;
}
int hc_cuDeviceGetCount (hashcat_ctx_t *hashcat_ctx, int *count)
{
backend_ctx_t *backend_ctx = hashcat_ctx->backend_ctx;
CUDA_PTR *cuda = backend_ctx->cuda;
const CUresult CU_err = cuda->cuDeviceGetCount (count);
if (CU_err != CUDA_SUCCESS)
{
const char *pStr = NULL;
if (cuda->cuGetErrorString (CU_err, &pStr) == CUDA_SUCCESS)
{
event_log_error (hashcat_ctx, "cuDeviceGetCount(): %s", pStr);
}
else
{
event_log_error (hashcat_ctx, "cuDeviceGetCount(): %d", CU_err);
}
return -1;
}
return 0;
}
int hc_cuDeviceGet (hashcat_ctx_t *hashcat_ctx, CUdevice* device, int ordinal)
{
backend_ctx_t *backend_ctx = hashcat_ctx->backend_ctx;
CUDA_PTR *cuda = backend_ctx->cuda;
const CUresult CU_err = cuda->cuDeviceGet (device, ordinal);
if (CU_err != CUDA_SUCCESS)
{
const char *pStr = NULL;
if (cuda->cuGetErrorString (CU_err, &pStr) == CUDA_SUCCESS)
{
event_log_error (hashcat_ctx, "cuDeviceGet(): %s", pStr);
}
else
{
event_log_error (hashcat_ctx, "cuDeviceGet(): %d", CU_err);
}
return -1;
}
return 0;
}
int hc_cuDeviceGetName (hashcat_ctx_t *hashcat_ctx, char *name, int len, CUdevice dev)
{
backend_ctx_t *backend_ctx = hashcat_ctx->backend_ctx;
CUDA_PTR *cuda = backend_ctx->cuda;
const CUresult CU_err = cuda->cuDeviceGetName (name, len, dev);
if (CU_err != CUDA_SUCCESS)
{
const char *pStr = NULL;
if (cuda->cuGetErrorString (CU_err, &pStr) == CUDA_SUCCESS)
{
event_log_error (hashcat_ctx, "cuDeviceGetName(): %s", pStr);
}
else
{
event_log_error (hashcat_ctx, "cuDeviceGetName(): %d", CU_err);
}
return -1;
}
return 0;
}
// OpenCL
int ocl_init (hashcat_ctx_t *hashcat_ctx)
@ -3286,7 +3395,7 @@ int backend_ctx_init (hashcat_ctx_t *hashcat_ctx)
backend_ctx->devices_param = devices_param;
/**
* Load and map CUDA library calls
* Load and map CUDA library calls, then init CUDA
*/
CUDA_PTR *cuda = (CUDA_PTR *) hcmalloc (sizeof (CUDA_PTR));
@ -3300,6 +3409,13 @@ int backend_ctx_init (hashcat_ctx_t *hashcat_ctx)
cuda_close (hashcat_ctx);
}
const int rc_cuInit = hc_cuInit (hashcat_ctx, 0);
if (rc_cuInit == -1)
{
cuda_close (hashcat_ctx);
}
/**
* Load and map NVRTC library calls
*/
@ -3393,6 +3509,29 @@ int backend_ctx_init (hashcat_ctx_t *hashcat_ctx)
backend_ctx->device_types_filter = device_types_filter;
/**
* Backend structures
*/
#define FREE_BACKEND_CTX_ON_ERROR \
{ \
hcfree (backend_device_from_cuda); \
hcfree (backend_device_to_cuda); \
hcfree (backend_device_from_opencl); \
hcfree (backend_device_to_opencl); \
hcfree (platforms_vendor); \
hcfree (platforms_name); \
hcfree (platforms_version); \
hcfree (platforms_skipped); \
hcfree (platforms); \
hcfree (platform_devices); \
}
int *backend_device_from_cuda = (int *) hccalloc (DEVICES_MAX, sizeof (int));
int *backend_device_to_cuda = (int *) hccalloc (DEVICES_MAX, sizeof (int));
int *backend_device_from_opencl = (int *) hccalloc (DEVICES_MAX, sizeof (int));
int *backend_device_to_opencl = (int *) hccalloc (DEVICES_MAX, sizeof (int));
/**
* OpenCL platforms: detect
*/
@ -3408,19 +3547,9 @@ int backend_ctx_init (hashcat_ctx_t *hashcat_ctx)
int CL_rc = hc_clGetPlatformIDs (hashcat_ctx, CL_PLATFORMS_MAX, platforms, &platforms_cnt);
#define FREE_OPENCL_CTX_ON_ERROR \
{ \
hcfree (platforms_vendor); \
hcfree (platforms_name); \
hcfree (platforms_version); \
hcfree (platforms_skipped); \
hcfree (platforms); \
hcfree (platform_devices); \
}
if (CL_rc == -1)
{
FREE_OPENCL_CTX_ON_ERROR;
FREE_BACKEND_CTX_ON_ERROR;
return -1;
}
@ -3456,7 +3585,7 @@ int backend_ctx_init (hashcat_ctx_t *hashcat_ctx)
event_log_warning (hashcat_ctx, " \"CUDA Toolkit\" (10.1 or later)");
event_log_warning (hashcat_ctx, NULL);
FREE_OPENCL_CTX_ON_ERROR;
FREE_BACKEND_CTX_ON_ERROR;
return -1;
}
@ -3470,7 +3599,7 @@ int backend_ctx_init (hashcat_ctx_t *hashcat_ctx)
event_log_error (hashcat_ctx, "An invalid platform was specified using the --opencl-platforms parameter.");
event_log_error (hashcat_ctx, "The specified platform was higher than the number of available platforms (%u).", platforms_cnt);
FREE_OPENCL_CTX_ON_ERROR;
FREE_BACKEND_CTX_ON_ERROR;
return -1;
}
@ -3505,7 +3634,7 @@ int backend_ctx_init (hashcat_ctx_t *hashcat_ctx)
if (CL_rc == -1)
{
FREE_OPENCL_CTX_ON_ERROR;
FREE_BACKEND_CTX_ON_ERROR;
return -1;
}
@ -3537,6 +3666,11 @@ int backend_ctx_init (hashcat_ctx_t *hashcat_ctx)
backend_ctx->enabled = true;
backend_ctx->backend_device_from_cuda = backend_device_from_cuda;
backend_ctx->backend_device_to_cuda = backend_device_to_cuda;
backend_ctx->backend_device_from_opencl = backend_device_from_opencl;
backend_ctx->backend_device_to_opencl = backend_device_to_opencl;
backend_ctx->platforms_vendor = platforms_vendor;
backend_ctx->platforms_name = platforms_name;
backend_ctx->platforms_version = platforms_version;
@ -3546,6 +3680,8 @@ int backend_ctx_init (hashcat_ctx_t *hashcat_ctx)
backend_ctx->platform_devices_cnt = platform_devices_cnt;
backend_ctx->platform_devices = platform_devices;
#undef FREE_BACKEND_CTX_ON_ERROR
return 0;
}
@ -3555,11 +3691,17 @@ void backend_ctx_destroy (hashcat_ctx_t *hashcat_ctx)
if (backend_ctx->enabled == false) return;
nvrtc_close (hashcat_ctx);
cuda_close (hashcat_ctx);
ocl_close (hashcat_ctx);
hcfree (backend_ctx->devices_param);
hcfree (backend_ctx->backend_device_from_cuda);
hcfree (backend_ctx->backend_device_to_cuda);
hcfree (backend_ctx->backend_device_from_opencl);
hcfree (backend_ctx->backend_device_to_opencl);
hcfree (backend_ctx->platforms);
hcfree (backend_ctx->platform_devices);
hcfree (backend_ctx->platforms_vendor);
@ -3577,6 +3719,94 @@ int backend_ctx_devices_init (hashcat_ctx_t *hashcat_ctx, const int comptime)
if (backend_ctx->enabled == false) return 0;
bool need_adl = false;
bool need_nvml = false;
bool need_nvapi = false;
bool need_sysfs = false;
int backend_devices_idx = 0;
int backend_devices_cnt = 0;
int backend_devices_active = 0;
if (backend_ctx->cuda)
{
int cuda_devices_cnt = 0;
const int rc_cuDeviceGetCount = hc_cuDeviceGetCount (hashcat_ctx, &cuda_devices_cnt);
if (rc_cuDeviceGetCount == -1)
{
cuda_close (hashcat_ctx);
}
backend_ctx->cuda_devices_cnt = cuda_devices_cnt;
backend_devices_cnt += cuda_devices_cnt;
hc_device_param_t *devices_param = backend_ctx->devices_param;
for (int cuda_devices_idx = 0; cuda_devices_idx < cuda_devices_cnt; cuda_devices_idx++, backend_devices_idx++)
{
hc_device_param_t *device_param = &devices_param[backend_devices_idx];
backend_ctx->backend_device_from_cuda[cuda_devices_idx] = backend_devices_idx;
backend_ctx->backend_device_to_cuda[backend_devices_idx] = cuda_devices_idx;
CUdevice device_cuda;
int CU_rc;
CU_rc = hc_cuDeviceGet (hashcat_ctx, &device_cuda, cuda_devices_idx);
if (CU_rc == -1) return -1;
device_param->device_cuda = device_cuda;
// device_name
char *device_name = (char *) hcmalloc (HCBUFSIZ_TINY);
CU_rc = hc_cuDeviceGetName (hashcat_ctx, device_name, HCBUFSIZ_TINY, device_cuda);
if (CU_rc == -1) return -1;
device_param->device_name = device_name;
hc_string_trim_leading (device_name);
hc_string_trim_trailing (device_name);
// sm_minor, sm_major
int sm_major = 0;
int sm_minor = 0;
CU_rc = hc_cuDeviceGetAttribute (hashcat_ctx, &sm_major, CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MAJOR, device_cuda);
if (CU_rc == -1) return -1;
CU_rc = hc_cuDeviceGetAttribute (hashcat_ctx, &sm_minor, CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MINOR, device_cuda);
if (CU_rc == -1) return -1;
device_param->sm_major = sm_major;
device_param->sm_minor = sm_minor;
printf ("%s %d %d\n", device_name, sm_major, sm_minor);
}
}
backend_ctx->backend_devices_cnt = backend_devices_cnt;
backend_ctx->backend_devices_active = backend_devices_active;
u32 devices_cnt = 0;
u32 devices_active = 0;
if (backend_ctx->ocl)
{
/**
* OpenCL devices: simply push all devices from all platforms into the same device array
*/
@ -3586,15 +3816,6 @@ int backend_ctx_devices_init (hashcat_ctx_t *hashcat_ctx, const int comptime)
cl_uint platform_devices_cnt = backend_ctx->platform_devices_cnt;
cl_device_id *platform_devices = backend_ctx->platform_devices;
bool need_adl = false;
bool need_nvml = false;
bool need_nvapi = false;
bool need_sysfs = false;
u32 devices_cnt = 0;
u32 devices_active = 0;
for (u32 platform_id = 0; platform_id < platforms_cnt; platform_id++)
{
size_t param_value_size = 0;
@ -3604,7 +3825,6 @@ int backend_ctx_devices_init (hashcat_ctx_t *hashcat_ctx, const int comptime)
// platform vendor
int CL_rc;
int CU_rc;
CL_rc = hc_clGetPlatformInfo (hashcat_ctx, platform, CL_PLATFORM_VENDOR, 0, NULL, &param_value_size);
@ -4187,19 +4407,6 @@ int backend_ctx_devices_init (hashcat_ctx_t *hashcat_ctx, const int comptime)
int sm_minor = 0;
int sm_major = 0;
//if (backend_ctx->cuda)
if (0)
{
CU_rc = hc_cuDeviceGetAttribute (hashcat_ctx, &sm_minor, CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MINOR, device_param->device_cuda);
if (CU_rc == -1) return -1;
CU_rc = hc_cuDeviceGetAttribute (hashcat_ctx, &sm_major, CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MINOR, device_param->device_cuda);
if (CU_rc == -1) return -1;
}
else
{
CL_rc = hc_clGetDeviceInfo (hashcat_ctx, device_param->device, CL_DEVICE_COMPUTE_CAPABILITY_MINOR_NV, sizeof (sm_minor), &sm_minor, NULL);
if (CL_rc == -1) return -1;
@ -4207,7 +4414,6 @@ int backend_ctx_devices_init (hashcat_ctx_t *hashcat_ctx, const int comptime)
CL_rc = hc_clGetDeviceInfo (hashcat_ctx, device_param->device, CL_DEVICE_COMPUTE_CAPABILITY_MAJOR_NV, sizeof (sm_major), &sm_major, NULL);
if (CL_rc == -1) return -1;
}
device_param->sm_minor = sm_minor;
device_param->sm_major = sm_major;
@ -4560,6 +4766,7 @@ int backend_ctx_devices_init (hashcat_ctx_t *hashcat_ctx, const int comptime)
devices_cnt++;
}
}
}
if (devices_active == 0)
{