Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

Opencl platform and device selection #67

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
23 changes: 19 additions & 4 deletions src/opencl-ecc-ed25519/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,14 @@ int main(int argc, const char* argv[]) {
}
}

if ((argc - arg) != 6) {
printf("usage: %s [-v] <num_signatures> <num_elems> <num_sigs_per_packet> <num_threads> <num_iterations> <use_non_default_stream>\n", argv[0]);
if ((argc - arg) < 6 || (argc - arg) > 8) {
printf("usage: %s [-v] <num_signatures> <num_elems> "
"<num_sigs_per_packet> <num_threads> <num_iterations> "
"<use_non_default_stream> <cl_platform_id> <cl_device_id>\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) {
Expand Down Expand Up @@ -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<verify_cpu_ctx_t> vctx = std::vector<verify_cpu_ctx_t>(num_threads);
Expand Down Expand Up @@ -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);
}

Expand Down
3 changes: 3 additions & 0 deletions src/opencl-platform/cl_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ extern bool g_verbose;
#include <CL/cl.h>
#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);
Expand Down
23 changes: 16 additions & 7 deletions src/opencl-platform/cl_init_platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -288,20 +294,23 @@ 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;
cout << endl;
}
}

DIE(dev_selected == false, "no platform or device selected");

// clean
delete[] platform_list;
delete[] device_list;
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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);
Expand Down