예제 #1
0
파일: kmeans.cpp 프로젝트: markusd/gpgpu
void initCL()
{
	ocl::createContextEx(CL_DEVICE_TYPE_ALL, clPlatform, clDevices, clContext, clQueues);
	cl_int clError = CL_SUCCESS;

	std::ifstream t("kmeans.cl");
	std::string code((std::istreambuf_iterator<char>(t)), std::istreambuf_iterator<char>());

	std::string header = "#define DIM ";
	header +=  util::toString(DIM);
	header += "\n";
	header += "#define K ";
	header += util::toString(K);
	header += "\n";
	header += "#define N ";
	header += util::toString(N);
	header += "\n";
	header += "#define AM_LWS ";
	header += util::toString(AM_LWS);
	header += "\n";
	header += "#define RP_LWS ";
	header += util::toString(RP_LWS);
	header += "\n\n\n";

	code = header + code;

	try {
		cl::Program::Sources source(1, std::make_pair(code.c_str(), code.size()));
		clProgram = cl::Program(clContext, source);
		clProgram.build(clDevices, "-cl-fast-relaxed-math -cl-unsafe-math-optimizations -cl-mad-enable");

		std::string info("");
		for (std::vector<cl::Device>::iterator itr = clDevices.begin(); itr != clDevices.end(); ++itr) {
			clProgram.getBuildInfo(*itr, CL_PROGRAM_BUILD_LOG, &info);
			if (info.size() > 0)
				std::cout << "Build log: " << info << std::endl;
		}

		for (int i = 0; i < clDevices.size(); ++i) {
			clClusterAssignment.push_back(cl::Kernel(clProgram, "cluster_assignment", &clError));
			clClusterReposition.push_back(cl::Kernel(clProgram, "cluster_reposition", &clError));
			clClusterReposition_k.push_back(cl::Kernel(clProgram, "cluster_reposition_k", &clError));
			clClusterReposition_k_c.push_back(cl::Kernel(clProgram, "c_cluster_reposition", &clError));
			clComputeCost.push_back(cl::Kernel(clProgram, "compute_cost", &clError));
		}

	} catch (const cl::Error& err) {
		std::cout << "OpenCL Error 4: " << err.what() << " (" << err.err() << ")" << std::endl;
		std::string info("");
		for (std::vector<cl::Device>::iterator itr = clDevices.begin(); itr != clDevices.end(); ++itr) {
			clProgram.getBuildInfo(*itr, CL_PROGRAM_BUILD_LOG, &info);
			if (info.size() > 0)
				std::cout << "Build log: " << info << std::endl;
		}
		std::cin.get();
	}
}
예제 #2
0
파일: host.cpp 프로젝트: jmriviere/pbrt-v2
string Host::buildLog(cl::Program prog) {
    string options, blog, status;
    ostringstream info;

    prog.getBuildInfo(_devices[d_index], CL_PROGRAM_BUILD_OPTIONS, &options);
    prog.getBuildInfo(_devices[d_index], CL_PROGRAM_BUILD_LOG, &blog);
    prog.getBuildInfo(_devices[d_index], CL_PROGRAM_BUILD_STATUS, &status);

    info << "Building kernels" << endl
         << "Build Options: " << options << endl
         << "Build Status: " << status << endl
         << endl << "Build Log: " << blog << endl;

    return info.str();
}
예제 #3
0
파일: clgl.cpp 프로젝트: Gardarik/CLGL
void InitCL()
{
	//cl_int err = CL_SUCCESS;
	try
	{
		//Identify platforms
		cl::Platform::get(&clPlatformList);
		//Select first platform with any GPU devices
		for(unsigned int i=0; i<clPlatformList.size(); i++)
		{
			clPlatformList[i].getDevices(CL_DEVICE_TYPE_GPU, &clDeviceList);
			if(!clDeviceList.empty())	break;
		}

		//Set Context Properties: Get associated cl_platform_id using getInfo() on the first GPU
		//Thus conveniently avoiding previous C++ bindings issues :)
		cl_context_properties clProps[] = 
		{
			CL_GL_CONTEXT_KHR,		(cl_context_properties)wglGetCurrentContext(),
			CL_WGL_HDC_KHR,			(cl_context_properties)wglGetCurrentDC(),
			CL_CONTEXT_PLATFORM,	(cl_context_properties)clDeviceList[0].getInfo<CL_DEVICE_PLATFORM>(),
			0
		};
		//Create interop context from GPU devices
		clContext = cl::Context(CL_DEVICE_TYPE_GPU, clProps);

		//Generate program with source and build
		std::string progFile = ReadKernels(KERNEL_FILE);
		cl::Program::Sources clSource(1, std::make_pair(progFile.c_str(), progFile.size()));
		clProgram = cl::Program(clContext, clSource);
		clProgram.build(clDeviceList);
		//Initialize kernels
		for(int i=0; i<NUM_KERNELS; i++)
		{
			clKernels[i] = cl::Kernel(clProgram, kernelName[i]);
		}
		//Create Command Queue with profiling enabled
		clQueue = cl::CommandQueue(clContext, clDeviceList[0], CL_QUEUE_PROFILING_ENABLE);
	}
	catch(cl::Error e)
	{
		cout << "OpenCL initialization failure: " << e.what() << endl
			<< "Error code: " << e.err() << endl;
		if(e.err() == -11)
		{
			std::string clProgLog;
			clProgram.getBuildInfo(clDeviceList[0], CL_PROGRAM_BUILD_LOG, &clProgLog);
			cout << clProgLog;
			system("pause");
			exit(EXIT_FAILURE);
		}
		throw;
	}
}