Ejemplo n.º 1
0
bool
CLD3D11DeviceContext::Initialize(ID3D11DeviceContext *d3dDeviceContext) {

#if defined(OPENSUBDIV_HAS_DX11SDK) && \
    (defined(OPENSUBDIV_HAS_CL_D3D11_H) || defined(OPENSUBDIV_HAS_CL_D3D11_EXT_H))

    _d3dDeviceContext = d3dDeviceContext;

    cl_int ciErrNum;
    cl_platform_id cpPlatform = findPlatform();

    ID3D11Device *device;
    d3dDeviceContext->GetDevice(&device);

#if defined(OPENSUBDIV_HAS_CL_D3D11_H)
    cl_context_properties props[] = {
        CL_CONTEXT_D3D11_DEVICE_KHR, (cl_context_properties)device,
        CL_CONTEXT_PLATFORM, (cl_context_properties)cpPlatform,
        0
    };
#elif defined(OPENSUBDIV_HAS_CL_D3D11_EXT_H)
    cl_context_properties props[] = {
        CL_CONTEXT_D3D11_DEVICE_NV, (cl_context_properties)device,
        CL_CONTEXT_PLATFORM, (cl_context_properties)cpPlatform,
        0
    };
#endif

    // get the number of GPU devices available to the platform
    cl_uint numDevices = 0;
    clGetDeviceIDs(cpPlatform, CL_DEVICE_TYPE_GPU, 0, NULL, &numDevices);
    if (numDevices == 0) {
        error("No CL GPU device found.\n");
        return false;
    }

    // create the device list
    cl_device_id *clDevices = new cl_device_id[numDevices];
    clGetDeviceIDs(cpPlatform, CL_DEVICE_TYPE_GPU, numDevices, clDevices, NULL);

    // we're cheating a little bit.
    // try to find both cl_khr_d3d11_sharing and cl_nv_d3d11_sharing.
    const char *extension = "_d3d11_sharing";
    int clDeviceUsed = findExtensionSupportedDevice(clDevices, numDevices,
                                                    extension);

    if (clDeviceUsed < 0) {
        error("No device found that supports CL/D3D11 context sharing\n");
        delete[] clDevices;
        return false;
    }

    _clContext = clCreateContext(props, 1, &clDevices[clDeviceUsed],
                                 NULL, NULL, &ciErrNum);
    if (ciErrNum != CL_SUCCESS) {
        error("Error %d in clCreateContext\n", ciErrNum);
        delete[] clDevices;
        return false;
    }

    _clCommandQueue = clCreateCommandQueue(_clContext, clDevices[clDeviceUsed],
                                    0, &ciErrNum);
    delete[] clDevices;
    if (ciErrNum != CL_SUCCESS) {
        error("Error %d in clCreateCommandQueue\n", ciErrNum);
        return false;
    }
    return true;
#else
    (void)d3dDeviceContext;  // unused
    return false;
#endif
}
Ejemplo n.º 2
0
	// Initializes the OpenCL objects.
	bool init_opencl(const int maxInputSize, const int maxOutputSize, const int maxWeightSize, const int maxBiasSize) {
		cl_int status;

		if (!setCwdToExeDir()) {
			return false;
		}

		// Get the OpenCL platform.
		platform = findPlatform("Altera");
		if (platform == NULL) {
			printf("ERROR: Unable to find Altera OpenCL platform.\n");
			return false;
		}

		printf("Platform: %s\n", getPlatformName(platform).c_str());

		// Query the available OpenCL devices.
		scoped_array<cl_device_id> devices;
		cl_uint num_devices;

		devices.reset(getDevices(platform, CL_DEVICE_TYPE_ALL, &num_devices));

		// We'll just use the first device.
		device = devices[0];
		printf("Device: %s\n", getDeviceName(device).c_str());

		// Create the context.
		context = clCreateContext(NULL, 1, &device, &oclContextCallback, NULL, &status);
		checkError(status, "Failed to create context");

		// Create the command queue.
		queue = clCreateCommandQueue(context, device, CL_QUEUE_PROFILING_ENABLE, &status);
		checkError(status, "Failed to create command queue");

		// Create the program.
		std::string binary_file = getBoardBinaryFile("waifu2x", device);
		printf("Using AOCX: %s\n", binary_file.c_str());
		program = createProgramFromBinary(context, binary_file.c_str(), &device, 1);

		// Build the program that was just created.
		status = clBuildProgram(program, 0, NULL, "", NULL, NULL);
		checkError(status, "Failed to build program");

		// Create the kernel - name passed in here must match kernel name in the
		// original CL file, that was compiled into an AOCX file using the AOC tool
		const char *kernel_name = "waifu2x";  // Kernel name, as defined in the CL file
		kernel = clCreateKernel(program, kernel_name, &status);
		checkError(status, "Failed to create kernel");

		input_buf = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_BANK_1_ALTERA,
			maxInputSize * sizeof(float), NULL, &status);
		checkError(status, "Failed to create buffer for input");

		weight_buf = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_BANK_2_ALTERA,
			maxWeightSize * sizeof(float), NULL, &status);
		checkError(status, "Failed to create buffer for weight");

		bias_buf = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_BANK_2_ALTERA,
			maxBiasSize * sizeof(double), NULL, &status);
		checkError(status, "Failed to create buffer for bias");

		output_buf = clCreateBuffer(context, CL_MEM_WRITE_ONLY | CL_MEM_BANK_1_ALTERA,
			maxOutputSize * sizeof(float), NULL, &status);
		checkError(status, "Failed to create buffer for output");

		return true;
	}
Ejemplo n.º 3
0
bool
CLDeviceContext::Initialize() {

#ifdef OPENSUBDIV_HAS_CLEW
    if (!clGetPlatformIDs) {
        error("Error clGetPlatformIDs function not bound.\n");
        return false;
    }
#endif

    cl_int ciErrNum;
    cl_platform_id cpPlatform = findPlatform();

#if defined(_WIN32)
    cl_context_properties props[] = {
        CL_GL_CONTEXT_KHR, (cl_context_properties)wglGetCurrentContext(),
        CL_WGL_HDC_KHR, (cl_context_properties)wglGetCurrentDC(),
        CL_CONTEXT_PLATFORM, (cl_context_properties)cpPlatform,
        0
    };
#elif defined(__APPLE__)
    CGLContextObj kCGLContext = CGLGetCurrentContext();
    CGLShareGroupObj kCGLShareGroup = CGLGetShareGroup(kCGLContext);
    cl_context_properties props[] = {
        CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE, (cl_context_properties)kCGLShareGroup,
        0
    };
#else
    cl_context_properties props[] = {
        CL_GL_CONTEXT_KHR, (cl_context_properties)glXGetCurrentContext(),
        CL_GLX_DISPLAY_KHR, (cl_context_properties)glXGetCurrentDisplay(),
        CL_CONTEXT_PLATFORM, (cl_context_properties)cpPlatform,
        0
    };
#endif

#if defined(__APPLE__)
    _clContext = clCreateContext(props, 0, NULL, clLogMessagesToStdoutAPPLE,
                                 NULL, &ciErrNum);
    if (ciErrNum != CL_SUCCESS) {
        error("Error %d in clCreateContext\n", ciErrNum);
        return false;
    }

    size_t devicesSize = 0;
    clGetGLContextInfoAPPLE(_clContext, kCGLContext,
                            CL_CGL_DEVICES_FOR_SUPPORTED_VIRTUAL_SCREENS_APPLE,
                            0, NULL, &devicesSize);
    int numDevices = int(devicesSize / sizeof(cl_device_id));
    if (numDevices == 0) {
        error("No sharable devices.\n");
        return false;
    }
    cl_device_id *clDevices = new cl_device_id[numDevices];
    clGetGLContextInfoAPPLE(_clContext, kCGLContext,
                            CL_CGL_DEVICES_FOR_SUPPORTED_VIRTUAL_SCREENS_APPLE,
                            numDevices * sizeof(cl_device_id), clDevices, NULL);
    int clDeviceUsed = 0;

#else   // not __APPLE__

    // get the number of GPU devices available to the platform
    cl_uint numDevices = 0;
    clGetDeviceIDs(cpPlatform, CL_DEVICE_TYPE_GPU, 0, NULL, &numDevices);
    if (numDevices == 0) {
        error("No CL GPU device found.\n");
        return false;
    }

    // create the device list
    cl_device_id *clDevices = new cl_device_id[numDevices];
    clGetDeviceIDs(cpPlatform, CL_DEVICE_TYPE_GPU, numDevices, clDevices, NULL);

    const char *extension = "cl_khr_gl_sharing";
    int clDeviceUsed = findExtensionSupportedDevice(clDevices, numDevices,
                                                    extension);

    if (clDeviceUsed < 0) {
        error("No device found that supports CL/GL context sharing\n");
        delete[] clDevices;
        return false;
    }

    _clContext = clCreateContext(props, 1, &clDevices[clDeviceUsed],
                                 NULL, NULL, &ciErrNum);

#endif   // not __APPLE__

    if (ciErrNum != CL_SUCCESS) {
        error("Error %d in clCreateContext\n", ciErrNum);
        delete[] clDevices;
        return false;
    }

    _clCommandQueue = clCreateCommandQueue(_clContext, clDevices[clDeviceUsed],
                                    0, &ciErrNum);
    delete[] clDevices;
    if (ciErrNum != CL_SUCCESS) {
        error("Error %d in clCreateCommandQueue\n", ciErrNum);
        return false;
    }
    return true;
}