News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

OpenCL GPGPU Demo Application

Started by aw27, June 25, 2019, 05:37:43 AM

Previous topic - Next topic

LiaoMi

Quote from: AW on June 27, 2019, 10:23:27 PM
So, a lot of people had OpenCL available for the CPUs (much more than I was thinking, but now I joined the gang too  :biggrin:).

I modified again the mandelbrot C/C++ sample provided by LiaoMi, this time to make it work 1st priority for OpenCL CPU, if available, if not available it will work for the GPU.

BTW, the modifications are all in a single function, so I will leave it here:


cl_context create_context(cl_uint* num_devices) {
cl_platform_id * platforms;
cl_uint num_platforms;
cl_int err;
cl_device_id *devices;
cl_uint num_cpus=0;
cl_context context;
cl_uint n_devices = 0;
int i;

*num_devices = 0;

if (clGetPlatformIDs(0, NULL, &num_platforms) != CL_SUCCESS)
{
perror("No platforms");
exit(1);
}
platforms = malloc(sizeof(cl_platform_id)*num_platforms);

err = clGetPlatformIDs(num_platforms, platforms, NULL);
if (err < 0) {
perror("Couldn't identify a platform");
exit(1);
}
// Look for a CL_DEVICE_TYPE_CPU
for (i = 0; i < num_platforms; i++)
{
if (clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_CPU, 0, NULL, &num_cpus) == CL_SUCCESS)
{
*num_devices = num_cpus;
devices = malloc(num_cpus * sizeof(cl_device_id));
clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_CPU, num_cpus, devices, NULL);
                        break;
}

}

if (num_cpus == 0)
{

for (i = 0; i < num_platforms; i++)
{
if (clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_GPU, 0, NULL, &n_devices) == CL_SUCCESS)
{
*num_devices = n_devices;
devices = malloc(n_devices * sizeof(cl_device_id));
clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_GPU, n_devices, devices, NULL);
break;
}

}

}

context = clCreateContext(0, *num_devices, devices, NULL, NULL, &err);
check_succeeded((char*)"Creating context", err);

return context;
}


I attach also the built .exe

This is what I got:

Device 0: Intel(R) Corporation Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz


Conclusion: it works and produces the expected mandelbrot.

@daydreamer
Well, from what I read CUDA, may be 10 to 25% faster in most cases. But I have not done any tests on that (yet).

Hi AW,

my results, does this example work for everyone?
QuoteNew folder>OpenCLMandelBrot
Device 0: Intel(R) Corporation Intel(R) Core(TM) i7-4810MQ CPU @ 2.80GHz
Loading kernel: Invalid value
Building program: Invalid program
Setting kernel arg: Invalid kernel
Running kernel: Invalid kernel

aw27

I forgot to include the kernel file in ZIP file, sorry.  :sad:
It is in attachment, please place it in the same folder as the .exe.