From 9b33e61013f4d25e8f41ba0e6c0ea7c82c4b1eed Mon Sep 17 00:00:00 2001 From: Grigore Lupescu Date: Mon, 31 Aug 2020 13:39:08 +0300 Subject: [PATCH] Add device select and device compile options --- src/Makefile | 9 +++++++-- src/opencl-ecc-ed25519/main.cpp | 23 +++++++++++++++++++---- src/opencl-platform/cl_common.h | 3 +++ src/opencl-platform/cl_init_platform.cpp | 23 ++++++++++++++++------- 4 files changed, 45 insertions(+), 13 deletions(-) diff --git a/src/Makefile b/src/Makefile index 055f5fe..a2c8c65 100644 --- a/src/Makefile +++ b/src/Makefile @@ -20,7 +20,7 @@ CL_POH_VERIFY_MANY_TEST_BIN=cl_poh_verify_many CL_ECC_TEST_BIN=cl_ed25519_verify CL_LIB=cl-crypt -CL_HEADER_DIR:=opencl-platform +CL_PLATFORM_DIR:=opencl-platform CUDA_HEADER_DIR:=cuda-headers CUDA_SHA256_DIR:=cuda-sha256 @@ -34,9 +34,14 @@ CXX ?= g++ CFLAGS_COMMON:=-DENDIAN_NEUTRAL -DLTC_NO_ASM CFLAGS+=$(CFLAGS_COMMON) -I$(CUDA_HEADER_DIR) -I$(CUDA_SHA256_DIR) +# some instances require optimisations disabled (e.g. AMD on Linux) +# remove "-cl-opt-disable" for default Optimisations to take place +CL_DEVICE_CFLAGS=-cl-opt-disable -DENDIAN_NEUTRAL -DLTC_NO_ASM + #use -DUSE_RDTSC for Windows compilation CL_CFLAGS_common:=-fPIC -std=c++11 $(CFLAGS_COMMON) -DOPENCL_VARIANT \ - -I$(CL_HEADER_DIR) -Icommon/ \ + -DCL_DEVICE_CFLAGS="\"$(CL_DEVICE_CFLAGS)\"" \ + -I$(CL_PLATFORM_DIR) -Icommon/ \ -I$(CUDA_DIR)/targets/x86_64-linux/include $(HOST_CFLAGS) CL_CFLAGS_release:=$(CL_CFLAGS_common) -O3 CL_CFLAGS_debug:=$(CL_CFLAGS_common) -O0 -g diff --git a/src/opencl-ecc-ed25519/main.cpp b/src/opencl-ecc-ed25519/main.cpp index 948b1f3..64c1eee 100644 --- a/src/opencl-ecc-ed25519/main.cpp +++ b/src/opencl-ecc-ed25519/main.cpp @@ -102,14 +102,14 @@ int main(int argc, const char* argv[]) { } } - if ((argc - arg) != 6) { - printf("usage: %s [-v] \n", argv[0]); + if ((argc - arg) < 6 || (argc - arg) > 8) { + printf("usage: %s [-v] " + " " + " \n", argv[0]); return 1; } ed25519_set_verbose(verbose); - - DIE(cl_check_init(CL_DEVICE_TYPE_GPU) == false, "OpenCL could not be init"); int num_signatures_per_elem = strtol(argv[arg++], NULL, 10); if (num_signatures_per_elem <= 0) { @@ -147,6 +147,18 @@ int main(int argc, const char* argv[]) { return 1; } + if(argc >= 8) { + query_platform_id = stoi(argv[arg++], NULL, 10); + } + + if(argc >= 9) { + query_device_id = stoi(argv[arg++], NULL, 10); + } + + + DIE(cl_check_init() == false, "OpenCL could not be init"); + LOG("OpenCL init has finished\n"); + LOG("streamer size: %zu elems size: %zu\n", sizeof(streamer_Packet), sizeof(gpu_Elems)); std::vector vctx = std::vector(num_threads); @@ -333,14 +345,17 @@ int main(int argc, const char* argv[]) { for (int thread = 0; thread < num_threads; thread++) { LOG("ret:\n"); + int verify_ok = out_size / (int)sizeof(uint8_t); bool verify_failed = false; for (int i = 0; i < out_size / (int)sizeof(uint8_t); i++) { if (vctx[thread].out_h[i] != 1) { verify_failed = true; + verify_ok--; } } LOG("\n"); fflush(stdout); + printf("Verify OK: %d / %d\n", verify_ok, out_size / (int)sizeof(uint8_t)); assert(verify_failed == false); } diff --git a/src/opencl-platform/cl_common.h b/src/opencl-platform/cl_common.h index b266ea2..c8f15d3 100644 --- a/src/opencl-platform/cl_common.h +++ b/src/opencl-platform/cl_common.h @@ -41,6 +41,9 @@ extern bool g_verbose; #include #endif +extern cl_uint query_device_id; +extern cl_uint query_platform_id; + // runs at the start of any OpenCL entry point crypto function bool cl_check_init(cl_uint sel_device_type); bool cl_check_init(void); diff --git a/src/opencl-platform/cl_init_platform.cpp b/src/opencl-platform/cl_init_platform.cpp index b897988..a117c87 100644 --- a/src/opencl-platform/cl_init_platform.cpp +++ b/src/opencl-platform/cl_init_platform.cpp @@ -9,6 +9,10 @@ #include "cl_common.h" cl_uint query_device_type = CL_DEVICE_TYPE_ALL; + +cl_uint query_platform_id = 0; +cl_uint query_device_id = 0; + bool cl_is_init = false; cl_context context; @@ -230,6 +234,8 @@ bool cl_check_init(void) { CL_ERR( clGetPlatformIDs(platform_num, platform_list, NULL)); cout << "Platforms found: " << platform_num << endl; + bool dev_selected = false; + /* list all platforms and VENDOR/VERSION properties */ for (cl_uint platf = 0; platf < platform_num; platf++) { /* get attribute CL_PLATFORM_VENDOR */ @@ -288,13 +294,14 @@ bool cl_check_init(void) { attr_size, attr_data, NULL)); cout << "\tDevice " << dev << " " << attr_data << " "; - /* select device based on cli arguments */ string tmpAttrData = attr_data; - // always select first device of first platform - if((dev == 0) && (platf == 0)) { + /* select device based on cli arguments or defaults (0, 0) */ + if((dev == query_device_id) && + (platf == query_platform_id)) { device = device_list[dev]; cout << "<----- SELECTED"; + dev_selected = true; } delete[] attr_data; @@ -302,6 +309,8 @@ bool cl_check_init(void) { } } + DIE(dev_selected == false, "no platform or device selected"); + // clean delete[] platform_list; delete[] device_list; @@ -321,7 +330,7 @@ bool cl_check_init(void) { *************************************************/ #ifdef KERNELS_SHA256 - cout << "Compiling sha256 kernels" << endl; + cout << "Compiling sha256 kernels, FLAGS: " << CL_DEVICE_CFLAGS << endl; /* retrieve kernel source */ kernel_src = kernels_sha256_src; @@ -334,7 +343,7 @@ bool cl_check_init(void) { CL_ERR( ret ); /* compile the program for the given set of devices */ - ret = clBuildProgram(program, 1, &device, "-DENDIAN_NEUTRAL -DLTC_NO_ASM", NULL, NULL); + ret = clBuildProgram(program, 1, &device, CL_DEVICE_CFLAGS, NULL, NULL); CL_COMPILE_ERR( ret, program, device ); init_sha256_state_kernel = clCreateKernel(program, "init_sha256_state_kernel", &ret); @@ -349,7 +358,7 @@ bool cl_check_init(void) { *************************************************/ #ifdef KERNELS_PRECOMP_DATA - cout << "Compiling verify kernels" << endl; + cout << "Compiling verify kernels, FLAGS: " << CL_DEVICE_CFLAGS << endl; /* retrieve kernel source */ kernel_src = kernels_precomp_data_src; @@ -363,7 +372,7 @@ bool cl_check_init(void) { CL_ERR( ret ); /* compile the program for the given set of devices */ - ret = clBuildProgram(program, 1, &device, "-DENDIAN_NEUTRAL -DLTC_NO_ASM", NULL, NULL); + ret = clBuildProgram(program, 1, &device, CL_DEVICE_CFLAGS, NULL, NULL); CL_COMPILE_ERR( ret, program, device ); ed25519_sign_kernel = clCreateKernel(program, "ed25519_sign_kernel", &ret);