ℹ️ Your data is safe here... unless you make the SNIP gods angry. And let's just say they have a really bad sense of humor.

From Trent, 9 Months ago, written in C++.
This paste will slip away in 1 Month.
Embed
  1. #include <CL/cl.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5.  
  6. // OpenCL kernel to check if a number is prime
  7. const char* kernelSource =
  8. "__kernel void is_prime(__global const unsigned long* num, __global int* result) { \n"
  9. "    unsigned long n = *num; \n"
  10. "    int is_prime = 1; \n"
  11. "    for (unsigned long i = 2; i <= sqrt((float)n); i++) { \n"
  12. "        if (n % i == 0) { \n"
  13. "            is_prime = 0; \n"
  14. "            break; \n"
  15. "        } \n"
  16. "    } \n"
  17. "    *result = is_prime; \n"
  18. "} \n";
  19.  
  20. int main() {
  21.     // Number to test (example: 31, which is 2^5 - 1)
  22.     unsigned long p = 5;
  23.     unsigned long M_p = (1UL << p) - 1;
  24.  
  25.     // Initialize OpenCL
  26.     cl_platform_id platform_id = NULL;
  27.     cl_device_id device_id = NULL;
  28.     cl_context context = NULL;
  29.     cl_command_queue command_queue = NULL;
  30.     cl_mem num_mem = NULL;
  31.     cl_mem result_mem = NULL;
  32.     cl_program program = NULL;
  33.     cl_kernel kernel = NULL;
  34.     cl_int ret;
  35.  
  36.     // Get platform and device information
  37.     ret = clGetPlatformIDs(1, &platform_id, NULL);
  38.     ret = clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_DEFAULT, 1, &device_id, NULL);
  39.  
  40.     // Create an OpenCL context
  41.     context = clCreateContext(NULL, 1, &device_id, NULL, NULL, &ret);
  42.  
  43.     // Create a command queue
  44.     command_queue = clCreateCommandQueue(context, device_id, 0, &ret);
  45.  
  46.     // Create memory buffers on the device for the number and result
  47.     num_mem = clCreateBuffer(context, CL_MEM_READ_ONLY, sizeof(unsigned long), NULL, &ret);
  48.     result_mem = clCreateBuffer(context, CL_MEM_WRITE_ONLY, sizeof(int), NULL, &ret);
  49.  
  50.     // Copy the number to the memory buffer
  51.     ret = clEnqueueWriteBuffer(command_queue, num_mem, CL_TRUE, 0, sizeof(unsigned long), &M_p, 0, NULL, NULL);
  52.  
  53.     // Create a program from the kernel source
  54.     program = clCreateProgramWithSource(context, 1, (const char**)&kernelSource, NULL, &ret);
  55.  
  56.     // Build the program
  57.     ret = clBuildProgram(program, 1, &device_id, NULL, NULL, NULL);
  58.  
  59.     // Create the OpenCL kernel
  60.     kernel = clCreateKernel(program, "is_prime", &ret);
  61.  
  62.     // Set the arguments of the kernel
  63.     ret = clSetKernelArg(kernel, 0, sizeof(cl_mem), (void*)&num_mem);
  64.     ret = clSetKernelArg(kernel, 1, sizeof(cl_mem), (void*)&result_mem);
  65.  
  66.     // Execute the OpenCL kernel
  67.     size_t global_item_size = 1;
  68.     size_t local_item_size = 1;
  69.     ret = clEnqueueNDRangeKernel(command_queue, kernel, 1, NULL, &global_item_size, &local_item_size, 0, NULL, NULL);
  70.  
  71.     // Read the result from the device
  72.     int result;
  73.     ret = clEnqueueReadBuffer(command_queue, result_mem, CL_TRUE, 0, sizeof(int), &result, 0, NULL, NULL);
  74.  
  75.     // Display the result
  76.     if (result) {
  77.         printf("%lu is a Mersenne prime.\n", M_p);
  78.     } else {
  79.         printf("%lu is not a Mersenne prime.\n", M_p);
  80.     }
  81.  
  82.     // Clean up
  83.     ret = clFlush(command_queue);
  84.     ret = clFinish(command_queue);
  85.     ret = clReleaseKernel(kernel);
  86.     ret = clReleaseProgram(program);
  87.     ret = clReleaseMemObject(num_mem);
  88.     ret = clReleaseMemObject(result_mem);
  89.     ret = clReleaseCommandQueue(command_queue);
  90.     ret = clReleaseContext(context);
  91.  
  92.     return 0;
  93. }