Example #1
0
gboolean
gegl_cl_init(GError **gError)
{
    cl_int status;

    if (!cl_status.is_opencl_available)
    {
#ifdef G_OS_WIN32
        HINSTANCE module;
        module = LoadLibrary("OpenCL.dll");
#else
        GModule *module;
        module = g_module_open("libOpenCL.so", G_MODULE_BIND_LAZY);
#endif
        if (!module)
        {
            g_warning ("Unable to load OpenCL library");
            return FALSE;
        }

        CL_LOAD_FUNCTION(clGetPlatformIDs                )
        CL_LOAD_FUNCTION(clGetPlatformInfo               )
        CL_LOAD_FUNCTION(clGetDeviceIDs                  )
        CL_LOAD_FUNCTION(clGetDeviceInfo                 )
        CL_LOAD_FUNCTION(clCreateContext                 )
        CL_LOAD_FUNCTION(clCreateContextFromType         )
        CL_LOAD_FUNCTION(clRetainContext                 )
        CL_LOAD_FUNCTION(clReleaseContext                )
        CL_LOAD_FUNCTION(clGetContextInfo                )
        CL_LOAD_FUNCTION(clCreateCommandQueue            )
        CL_LOAD_FUNCTION(clRetainCommandQueue            )
        CL_LOAD_FUNCTION(clReleaseCommandQueue           )
        CL_LOAD_FUNCTION(clGetCommandQueueInfo           )
        CL_LOAD_FUNCTION(clSetCommandQueueProperty       )
        CL_LOAD_FUNCTION(clCreateBuffer                  )
        CL_LOAD_FUNCTION(clCreateSubBuffer               )
        CL_LOAD_FUNCTION(clCreateImage2D                 )
        CL_LOAD_FUNCTION(clCreateImage3D                 )
        CL_LOAD_FUNCTION(clRetainMemObject               )
        CL_LOAD_FUNCTION(clReleaseMemObject              )
        CL_LOAD_FUNCTION(clGetSupportedImageFormats      )
        CL_LOAD_FUNCTION(clGetMemObjectInfo              )
        CL_LOAD_FUNCTION(clGetImageInfo                  )
        CL_LOAD_FUNCTION(clSetMemObjectDestructorCallback)
        CL_LOAD_FUNCTION(clCreateSampler                 )
        CL_LOAD_FUNCTION(clRetainSampler                 )
        CL_LOAD_FUNCTION(clReleaseSampler                )
        CL_LOAD_FUNCTION(clGetSamplerInfo                )
        CL_LOAD_FUNCTION(clCreateProgramWithSource       )
        CL_LOAD_FUNCTION(clCreateProgramWithBinary       )
        CL_LOAD_FUNCTION(clRetainProgram                 )
        CL_LOAD_FUNCTION(clReleaseProgram                )
        CL_LOAD_FUNCTION(clBuildProgram                  )
        CL_LOAD_FUNCTION(clUnloadCompiler                )
        CL_LOAD_FUNCTION(clGetProgramInfo                )
        CL_LOAD_FUNCTION(clGetProgramBuildInfo           )
        CL_LOAD_FUNCTION(clCreateKernel                  )
        CL_LOAD_FUNCTION(clCreateKernelsInProgram        )
        CL_LOAD_FUNCTION(clRetainKernel                  )
        CL_LOAD_FUNCTION(clReleaseKernel                 )
        CL_LOAD_FUNCTION(clSetKernelArg                  )
        CL_LOAD_FUNCTION(clGetKernelInfo                 )
        CL_LOAD_FUNCTION(clGetKernelWorkGroupInfo        )
        CL_LOAD_FUNCTION(clWaitForEvents                 )
        CL_LOAD_FUNCTION(clGetEventInfo                  )
        CL_LOAD_FUNCTION(clCreateUserEvent               )
        CL_LOAD_FUNCTION(clRetainEvent                   )
        CL_LOAD_FUNCTION(clReleaseEvent                  )
        CL_LOAD_FUNCTION(clSetUserEventStatus            )
        CL_LOAD_FUNCTION(clSetEventCallback              )
        CL_LOAD_FUNCTION(clGetEventProfilingInfo         )
        CL_LOAD_FUNCTION(clFlush                         )
        CL_LOAD_FUNCTION(clFinish                        )
        CL_LOAD_FUNCTION(clEnqueueReadBuffer             )
        CL_LOAD_FUNCTION(clEnqueueReadBufferRect         )
        CL_LOAD_FUNCTION(clEnqueueWriteBuffer            )
        CL_LOAD_FUNCTION(clEnqueueWriteBufferRect        )
        CL_LOAD_FUNCTION(clEnqueueCopyBuffer             )
        CL_LOAD_FUNCTION(clEnqueueCopyBufferRect         )
        CL_LOAD_FUNCTION(clEnqueueReadImage              )
        CL_LOAD_FUNCTION(clEnqueueWriteImage             )
        CL_LOAD_FUNCTION(clEnqueueCopyImage              )
        CL_LOAD_FUNCTION(clEnqueueCopyImageToBuffer      )
        CL_LOAD_FUNCTION(clEnqueueCopyBufferToImage      )
        CL_LOAD_FUNCTION(clEnqueueMapBuffer              )
        CL_LOAD_FUNCTION(clEnqueueMapImage               )
        CL_LOAD_FUNCTION(clEnqueueUnmapMemObject         )
        CL_LOAD_FUNCTION(clEnqueueNDRangeKernel          )
        CL_LOAD_FUNCTION(clEnqueueTask                   )
        CL_LOAD_FUNCTION(clEnqueueNativeKernel           )
        CL_LOAD_FUNCTION(clEnqueueMarker                 )
        CL_LOAD_FUNCTION(clEnqueueWaitForEvents          )
        CL_LOAD_FUNCTION(clEnqueueBarrier                )
        CL_LOAD_FUNCTION(clGetExtensionFunctionAddress   )

        /* Initialize OpenCL - Access to available platforms */
        cl_uint num_of_platforms;
        status = gegl_clGetPlatformIDs(0, NULL, &num_of_platforms);
        if (CL_SUCCESS == status && num_of_platforms >0)
        {
            cl_platform_id *platforms = (cl_platform_id*)
                malloc(num_of_platforms * sizeof(cl_platform_id));
            status = gegl_clGetPlatformIDs(num_of_platforms, platforms, NULL);
            if (CL_SUCCESS != status)
            {
                printf("[OpenCL]Error: Calling clGetPlatformsIDs\n");
                return FALSE;
            }
            unsigned int i;
            for (i = 0;i < num_of_platforms;++i)
            {
                status = gegl_clGetPlatformInfo(
                    platforms[i],
                    CL_PLATFORM_VENDOR,
                    sizeof(cl_status.platform_vendor),
                    cl_status.platform_vendor,
                    NULL);
                if (CL_SUCCESS != status)
                {
                    printf("[OpenCL]Error: Calling clGetPlatformInfo\n");
                    return FALSE;
                }
                cl_status.platform_id = platforms[i];
                if (!strcmp(cl_status.platform_vendor,
                    "Advanced Micro Devices, Inc."))
                {
                    break;
                }
            }
            status = gegl_clGetPlatformInfo(
                platforms[i],
                CL_PLATFORM_PROFILE,
                sizeof(cl_status.platform_profile),
                cl_status.platform_profile,
                NULL);
            if (CL_SUCCESS != status)
            {
                printf("[OpenCL]Error: Calling clGetPlatformInfo\n");
                return FALSE;
            }
            status = gegl_clGetPlatformInfo(
                platforms[i],
                CL_PLATFORM_VERSION,
                sizeof(cl_status.platform_version),
                cl_status.platform_version,
                NULL);
            if (CL_SUCCESS != status)
            {
                printf("[OpenCL]Error: Calling clGetPlatformInfo\n");
                return FALSE;
            }
            status = gegl_clGetPlatformInfo(
                platforms[i],
                CL_PLATFORM_NAME,
                sizeof(cl_status.platform_name),
                cl_status.platform_name,
                NULL);
            if (CL_SUCCESS != status)
            {
                printf("[OpenCL]Error: Calling clGetPlatformInfo\n");
                return FALSE;
            }
            status = gegl_clGetPlatformInfo(
                platforms[i],
                CL_PLATFORM_EXTENSIONS,
                sizeof(cl_status.platform_extensions),
                cl_status.platform_extensions,
                NULL);
            if (CL_SUCCESS != status)
            {
                printf("[OpenCL]Error: Calling clGetPlatformInfo\n");
                return FALSE;
            }
            free(platforms);
        }
        else
        {
            printf("[OpenCL]Error: Calling clGetPlatformsIDs\n");
            return FALSE;
        }

        /* Initialize OpenCL - Access to available device */
        cl_int num_of_devices;
        status = gegl_clGetDeviceIDs(
            cl_status.platform_id, CL_DEVICE_TYPE_GPU,
            0, NULL, &num_of_devices);
        if (CL_SUCCESS == status && num_of_devices > 0)
        {
            cl_device_id *devices = (cl_device_id*)
                malloc(num_of_devices * sizeof(cl_device_id));
            status = gegl_clGetDeviceIDs(
                cl_status.platform_id, CL_DEVICE_TYPE_GPU,
                num_of_devices, devices, NULL);
            if (CL_SUCCESS != status)
            {
                printf("[OpenCL]Error: Calling clGetDeviceIDs\n");
                return FALSE;
            }
            unsigned int i;
            for (i = 0;i < num_of_devices; ++i)
            {
                cl_status.device_id = devices[i];
            }
            free(devices);
        }
        else
        {
            printf("[OpenCL]Error: Calling clGetDeviceIDs\n");
            return FALSE;
        }

        /* Initialize OpenCL - Access to available context */
        cl_status.context = gegl_clCreateContext(0, 1,
            &cl_status.device_id, NULL, NULL, &status);
        if (CL_SUCCESS != status)
        {
            printf("[OpenCL]Error: Calling clCreateContext\n");
            return FALSE;
        }

        /* Initialize OpenCL - Access to available command queue */
        cl_status.command_queue = gegl_clCreateCommandQueue(
            cl_status.context, cl_status.device_id, 0, &status);
        if (CL_SUCCESS != status)
        {
            printf("[OpenCL]Error: Calling clCreateCommandQueue\n");
            return FALSE;
        }
    }

    cl_status.is_opencl_available = TRUE;

    if (cl_status.is_opencl_available)
        gegl_cl_color_compile_kernels();

    g_printf("[OpenCL] OK\n");


    return TRUE;
}
Example #2
0
static gboolean
gegl_cl_init_load_functions (GError **error)
{
#ifdef G_OS_WIN32
  HINSTANCE module = LoadLibrary ("OpenCL.dll");
#else
  GModule *module = g_module_open (CL_LIBRARY_NAME, G_MODULE_BIND_LAZY);
#endif

  if (!module)
    {
      GEGL_NOTE (GEGL_DEBUG_OPENCL, "Unable to load OpenCL library");
      g_set_error (error, GEGL_OPENCL_ERROR, 0, "Unable to load OpenCL library");
      return FALSE;
    }

  CL_LOAD_FUNCTION (clGetPlatformIDs)
  CL_LOAD_FUNCTION (clGetPlatformInfo)
  CL_LOAD_FUNCTION (clGetDeviceIDs)
  CL_LOAD_FUNCTION (clGetDeviceInfo)

  CL_LOAD_FUNCTION (clCreateContext)
  CL_LOAD_FUNCTION (clCreateContextFromType)
  CL_LOAD_FUNCTION (clCreateCommandQueue)
  CL_LOAD_FUNCTION (clCreateProgramWithSource)
  CL_LOAD_FUNCTION (clBuildProgram)
  CL_LOAD_FUNCTION (clGetProgramBuildInfo)

  CL_LOAD_FUNCTION (clCreateKernel)
  CL_LOAD_FUNCTION (clSetKernelArg)
  CL_LOAD_FUNCTION (clGetKernelWorkGroupInfo)
  CL_LOAD_FUNCTION (clCreateBuffer)
  CL_LOAD_FUNCTION (clEnqueueWriteBuffer)
  CL_LOAD_FUNCTION (clEnqueueReadBuffer)
  CL_LOAD_FUNCTION (clEnqueueCopyBuffer)
  CL_LOAD_FUNCTION (clEnqueueReadBufferRect)
  CL_LOAD_FUNCTION (clEnqueueWriteBufferRect)
  CL_LOAD_FUNCTION (clEnqueueCopyBufferRect)
  CL_LOAD_FUNCTION (clCreateImage2D)
  CL_LOAD_FUNCTION (clCreateImage3D)
  CL_LOAD_FUNCTION (clEnqueueReadImage)
  CL_LOAD_FUNCTION (clEnqueueWriteImage)
  CL_LOAD_FUNCTION (clEnqueueCopyImage)
  CL_LOAD_FUNCTION (clEnqueueCopyImageToBuffer)
  CL_LOAD_FUNCTION (clEnqueueCopyBufferToImage)

  CL_LOAD_FUNCTION (clEnqueueMapBuffer)
  CL_LOAD_FUNCTION (clEnqueueMapImage)
  CL_LOAD_FUNCTION (clEnqueueUnmapMemObject)

  CL_LOAD_FUNCTION (clEnqueueNDRangeKernel)
  CL_LOAD_FUNCTION (clEnqueueBarrier)
  CL_LOAD_FUNCTION (clFinish)

  CL_LOAD_FUNCTION (clGetEventProfilingInfo)

  CL_LOAD_FUNCTION (clReleaseKernel)
  CL_LOAD_FUNCTION (clReleaseProgram)
  CL_LOAD_FUNCTION (clReleaseCommandQueue)
  CL_LOAD_FUNCTION (clReleaseContext)
  CL_LOAD_FUNCTION (clReleaseMemObject)

  CL_LOAD_FUNCTION (clGetExtensionFunctionAddress);

  return TRUE;
}