From: miha-q <> Date: Mon, 26 Feb 2024 18:56:09 +0000 (-0500) Subject: Mon Feb 26 01:56:09 PM EST 2024 X-Git-Url: http://www.foleosoft.com/?a=commitdiff_plain;h=dc96ac50720be564bd6688082ee1a0eb4170a502;p=QAnsel.git Mon Feb 26 01:56:09 PM EST 2024 --- diff --git a/src/QAnsel.c b/src/QAnsel.c index 624e72e..b4f3a9d 100644 --- a/src/QAnsel.c +++ b/src/QAnsel.c @@ -10,6 +10,17 @@ uint8_t HIDDEN_VARIABLE = 0; FILE* RANDOM_FILE; +#define GPU_ENABLED +uint8_t USE_GPU = 0; +#ifdef GPU_ENABLED +#define CL_USE_DEPRECATED_OPENCL_1_2_APIS +#define CL_TARGET_OPENCL_VERSION 120 +#include +cl_platform_id GPU_platform_id; +cl_device_id GPU_device_id; +cl_context GPU_context; +#endif + typedef struct { char n[128]; @@ -1339,8 +1350,49 @@ void process(int argc, char** argv) free(lineIDs); } +#ifdef GPU_ENABLED +uint8_t GPU_init() +{ + cl_uint count; + cl_int err; + + err = clGetPlatformIDs(1, &GPU_platform_id, &count); + if (err != CL_SUCCESS || count == 0) + { + if (err == 0) + fprintf(stderr, "GPU disabled: No supported platforms found.\n"); + else + fprintf(stderr, "GPU disabled: clGetPlatformIDs() failed.\n"); + return 0; + } + + err = clGetDeviceIDs(GPU_platform_id, CL_DEVICE_TYPE_GPU, 1, &GPU_device_id, &count); + if (err != CL_SUCCESS || count == 0) + { + if (count == 0) + fprintf(stderr, "GPU disabled: No supported GPUs found.\n"); + else + fprintf(stderr, "GPU disabled: clGetDeviceIDs() failed.\n"); + return 0; + } + + GPU_context = clCreateContext(NULL, 1, &GPU_device_id, NULL, NULL, &err); + if (err != CL_SUCCESS) + { + fprintf(stderr, "GPU disabled: clCreateContext() failed.\n"); + return 0; + } + + return 1; +} +#endif + void main(int argc, char** argv) { + #ifdef GPU_ENABLED + USE_GPU = GPU_init(); + #endif + RANDOM_FILE = fopen("/dev/TrueRNG0", "r"); if (!RANDOM_FILE) RANDOM_FILE = fopen("/dev/random", "r"); process(argc, argv); diff --git a/src/g_mmul.cl b/src/g_mmul.cl new file mode 100644 index 0000000..f8283ea --- /dev/null +++ b/src/g_mmul.cl @@ -0,0 +1,11 @@ +__kernel g_mmul(__global double* A, __global double* B, __global double* C, const unsigned int N, const unsigned int W) +{ + int row = get_global_id(0); + int col = get_global_id(1); + double sum = 0; + for (int i = 0; i < N; i++) + { + sum += A[row * W + i] * B[i * W + col]; + } + C[row * W + col] = sum; +} \ No newline at end of file