]> foleosoft.com Git - QAnsel.git/commitdiff
Mon Feb 26 01:56:09 PM EST 2024
authormiha-q <>
Mon, 26 Feb 2024 18:56:09 +0000 (13:56 -0500)
committermiha-q <>
Mon, 26 Feb 2024 18:56:09 +0000 (13:56 -0500)
src/QAnsel.c
src/g_mmul.cl [new file with mode: 0644]

index 624e72e0348ee71ba7e643a0168beae07b9dc4ad..b4f3a9d3c4f05318443d435fc2c3894c3b70f23d 100644 (file)
 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/cl.h>
+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 (file)
index 0000000..f8283ea
--- /dev/null
@@ -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