/*
 * Load program source based on filename.
 * 
 * filename - the file to load the source from
 * context - TODO - why does clCreateProgramWIthSource need this?
 *
 * return - the cl_program that was created
 */
cl_program RiverModelCL::loadProgram(string filename)
{
    cl_int error;
    std::ifstream kernelFile(filename.c_str(), std::ios::in);
    if(!kernelFile.is_open())
    {
        cerr << "Failed to open kernel file for reading!" << endl;
        clReleaseContext(context);
        return NULL;
    }

    std::ostringstream oss;
    oss << kernelFile.rdbuf();

    std::string srcStr = oss.str();
    // Load source to cl_program
    cout << "Loading program source" << endl;
    cl_program program = clCreateProgramWithSource(context, 1, (const char**)&srcStr, NULL, &error);
    checkErr(error, "Error loading program source!", true);
    if(error != CL_SUCCESS)
    {
        return NULL;
    }
    else
    {
        return program;
    }
}
Example #2
0
std::string GPU::getKernelSrc(const std::string& kernelPath) {
    std::ifstream kernelFile(kernelPath);
    if(!kernelFile.is_open()) {
        throw std::runtime_error("Failed to open the kernel file.");
    }  
    kernelFile.seekg(0, std::ios::end);
    size_t fileSize = kernelFile.tellg();
    std::string kernelSrc(fileSize, ' ');
    kernelFile.seekg(0);
    kernelFile.read(&kernelSrc[0], fileSize); 
    return kernelSrc;
}
cl_program QHoneycombWidget::CreateProgram(cl_device_id device, const char* fileName)
{
  cl_int errNum;
  cl_program program;

  std::string CLFilePath = GetPath(__FILE__) + "GPGPUHoneycomb.cl";
  std::ifstream kernelFile(CLFilePath, std::ios::in);
  if (!kernelFile.is_open())
  {
    std::cerr << "Failed to open file for reading: " << fileName << std::endl;
    return NULL;
  }

  std::ostringstream oss;
  oss << kernelFile.rdbuf();

  std::string srcStdStr = oss.str();
  const char *srcStr = srcStdStr.c_str();
  program = clCreateProgramWithSource(clcontext, 1, (const char**)&srcStr, NULL, NULL);
  if (program == NULL)
  {
    std::cerr << "Failed to create CL program from source." << std::endl;
    return NULL;
  }

  std::string ArgParams;
  ArgParams += "-D m_NbCell=300 ";
  ArgParams += "-D m_Rayon=20 ";
  ArgParams += "-D m_SquarredRadius=(20.*20.) ";
  ArgParams += "-D m_NbCoul=25 ";
  ArgParams += "-D m_InvSquarredRadius=(1./(m_SquarredRadius)) ";

  errNum = clBuildProgram(program, 0, NULL, ArgParams.c_str(), NULL, NULL);
  if (errNum != CL_SUCCESS)
  {
    // Determine the reason for the error
    char buildLog[16384];
    clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG,
      sizeof(buildLog), buildLog, NULL);

    std::cerr << "Error in kernel: " << std::endl;
    std::cerr << buildLog;
    clReleaseProgram(program);
    return NULL;
  }

  return program;
}
Example #4
0
int main(int argc, char ** argv) {
    cl_device_type deviceType = CL_DEVICE_TYPE_ACCELERATOR;
    std::string kernelName("");
    std::string kernelFile("");

    parse_command_line_args(argc, argv, &deviceType, &kernelName, &kernelFile);

    /******************OPEN CL STUFF************************/
    DigitRecKernel digitreckernel (kernelFile.c_str(), kernelName.c_str(), deviceType);
    fflush(stdout);
    digitreckernel.load_to_memory();
    fflush(stdout);
    digitreckernel.run_kernel();
    fflush(stdout);
    digitreckernel.check_results();
    digitreckernel.finish_and_clean_up();
    /******************END OF OPEN CL STUFF*****************/
    return 0;
}
///
//  Create an OpenCL program from the kernel source file
//
cl_program CreateProgram(cl_context context, cl_device_id device, const char* fileName)
{
    cl_int errNum;
    cl_program program;

    std::ifstream kernelFile(fileName, std::ios::in);
    if (!kernelFile.is_open())
    {
        std::cerr << "Failed to open file for reading: " << fileName << std::endl;
        return NULL;
    }

    std::ostringstream oss;
    oss << kernelFile.rdbuf();

    std::string srcStdStr = oss.str();
    const char *srcStr = srcStdStr.c_str();
    program = clCreateProgramWithSource(context, 1,
                                        (const char**)&srcStr,
                                        NULL, NULL);
    if (program == NULL)
    {
        std::cerr << "Failed to create CL program from source." << std::endl;
        return NULL;
    }

    errNum = clBuildProgram(program, 0, NULL, NULL, NULL, NULL);
    if (errNum != CL_SUCCESS)
    {
        // Determine the reason for the error
        char buildLog[16384];
        clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG,
                              sizeof(buildLog), buildLog, NULL);

        std::cerr << "Error in kernel: " << std::endl;
        std::cerr << buildLog;
        clReleaseProgram(program);
        return NULL;
    }

    return program;
}
Example #6
0
cl_program loadAndBuildProgram( cl_context gpuContext, const char *fileName )
{
    pthread_mutex_lock(&mutex);

    cl_int errNum;
    cl_program program;

    // Load the OpenCL source code from the .cl file
    std::ifstream kernelFile(fileName, std::ios::in);
    if (!kernelFile.is_open())
    {
        std::cerr << "Failed to open file for reading: " << fileName << std::endl;
        return NULL;
    }

    std::ostringstream oss;
    oss << kernelFile.rdbuf();

    std::string srcStdStr = oss.str();
    const char *source = srcStdStr.c_str();

    //checkError(source != NULL, true);

    // Create the program for all GPUs in the context
    program = clCreateProgramWithSource(gpuContext, 1, (const char **)&source, NULL, &errNum);
    //checkError(errNum, CL_SUCCESS);
    // build the program for all devices on the context
    errNum = clBuildProgram(program, 0, NULL, NULL, NULL, NULL);
//    if (errNum != CL_SUCCESS)
//    {
//        char cBuildLog[10240];
//        clGetProgramBuildInfo(program, getFirstDev(gpuContext), CL_PROGRAM_BUILD_LOG,
//                              sizeof(cBuildLog), cBuildLog, NULL );
//
//        cerr << cBuildLog << endl;
//        checkError(errNum, CL_SUCCESS);
//    }

    pthread_mutex_unlock(&mutex);
    return program;
}
Example #7
0
//	Read the kernels that this plan uses from file, and store into the plan
clfftStatus FFTAction::writeKernel( const clfftPlanHandle plHandle, const clfftGenerators gen, const FFTKernelSignatureHeader* data, const cl_context& context, const cl_device_id &device )
{
    FFTRepo& fftRepo	= FFTRepo::getInstance( );

    std::string kernelPath = getKernelName(gen, plHandle, true);

    //	Logic to write string contents out to file
    tofstreamRAII< std::ofstream, std::string > kernelFile( kernelPath.c_str( ) );
    if( !kernelFile.get( ) )
    {
        std::cerr << "Failed to open kernel file for writing: " << kernelPath.c_str( ) << std::endl;
        return CLFFT_FILE_CREATE_FAILURE;
    }

    std::string kernel;
    OPENCL_V( fftRepo.getProgramCode( gen, data, kernel, device, context ), _T( "fftRepo.getProgramCode failed." ) );

    kernelFile.get( ) << kernel << std::endl;

    return	CLFFT_SUCCESS;
}
Chi2Kernel::Chi2Kernel(std::string fileName) {
    std::fstream kernelFile(fileName.c_str(), std::ios_base::in);

    kernelFile >> this->n >> this->step;
}
void Chi2Kernel::save(std::string fileName) const {
    std::fstream kernelFile(fileName.c_str(), std::ios_base::out);

    kernelFile << this->n << std::endl << this->step << std::endl;
}
Example #10
0
int main(int argc, char **argv)
{
    //-----------------------
    // Input pointers
    
    float           *h_Volume;
      
    void*           allMemoryPointers[500];
    int             numberOfMemoryPointers = 0;

	nifti_image*	allNiftiImages[500];
	int				numberOfNiftiImages = 0;           
    
    int             OPENCL_PLATFORM = 0;
    int             OPENCL_DEVICE = 0;

    // Size parameters

    int             DATA_H, DATA_W, DATA_D;
	float			VOXEL_SIZE_X, VOXEL_SIZE_Y, VOXEL_SIZE_Z;
        
    //---------------------    
    
    /* Input arguments */
    FILE *fp = NULL; 
    
    // No inputs, so print help text
    if (argc == 1)
    {   
		printf("\nThe function renders a volume using direct volume rendering.\n\n");     
        printf("Usage:\n\n");
        printf("RenderVolume volume.nii [options]\n\n");
        printf("Options:\n\n");
        printf(" -platform                  The OpenCL platform to use (default 0) \n");
        printf(" -device                    The OpenCL device to use for the specificed platform (default 0) \n");
        printf(" -verbose                   Print extra stuff (default false) \n");
        printf(" -debug                     Get additional debug information saved as nifti files (default no). Warning: This will use a lot of extra memory! \n");
        printf("\n\n");
        
        return EXIT_SUCCESS;
    }
    // Try to open files
    else if (argc > 1)
    {        
        fp = fopen(argv[1],"r");
        if (fp == NULL)
        {
            printf("Could not open file %s !\n",argv[1]);
            return EXIT_FAILURE;
        }
        fclose(fp);        
    }
    
    // Loop over additional inputs
    int i = 2;
    while (i < argc)
    {
        char *input = argv[i];
        char *p;
        if (strcmp(input,"-platform") == 0)
        {
			if ( (i+1) >= argc  )
			{
			    printf("Unable to read value after -platform !\n");
                return EXIT_FAILURE;
			}

            OPENCL_PLATFORM = (int)strtol(argv[i+1], &p, 10);
			
			if (!isspace(*p) && *p != 0)
		    {
		        printf("OpenCL platform must be an integer! You provided %s \n",argv[i+1]);
				return EXIT_FAILURE;
		    }
            else if (OPENCL_PLATFORM < 0)
            {
                printf("OpenCL platform must be >= 0!\n");
                return EXIT_FAILURE;
            }
            i += 2;
        }
        else if (strcmp(input,"-device") == 0)
        {
			if ( (i+1) >= argc  )
			{
			    printf("Unable to read value after -device !\n");
                return EXIT_FAILURE;
			}

            OPENCL_DEVICE = (int)strtol(argv[i+1], &p, 10);

			if (!isspace(*p) && *p != 0)
		    {
		        printf("OpenCL device must be an integer! You provided %s \n",argv[i+1]);
				return EXIT_FAILURE;
		    }
            else if (OPENCL_DEVICE < 0)
            {
                printf("OpenCL device must be >= 0!\n");
                return EXIT_FAILURE;
            }
            i += 2;
        }
        else
        {
            printf("Unrecognized option! %s \n",argv[i]);
            return EXIT_FAILURE;
        }                
    }
    	 
    // Read first volume 
	// -----------------------------------

    nifti_image *inputVolume = nifti_image_read(argv[1],1);
    
    if (inputVolume == NULL)
    {
        printf("Could not open volume to render!\n");
        return EXIT_FAILURE;
    }
	allNiftiImages[numberOfNiftiImages] = inputVolume;
	numberOfNiftiImages++;
  	
	// -----------------------------------

    // Get data dimensions from input data
    DATA_W = inputVolume->nx;
    DATA_H = inputVolume->ny;
    DATA_D = inputVolume->nz;    
    
    // Get voxel sizes from input data
    VOXEL_SIZE_X = inputVolume->dx;
    VOXEL_SIZE_Y = inputVolume->dy;
    VOXEL_SIZE_Z = inputVolume->dz;
                             
    int VOLUME_SIZE = DATA_W * DATA_H * DATA_D * sizeof(float);
    
    // Print some info
    printf("Authored by K.A. Eklund \n");
    printf("Volume size: %i x %i x %i \n",  DATA_W, DATA_H, DATA_D);
    printf("Volume voxel size: %f x %f x %f mm \n", VOXEL_SIZE_X, VOXEL_SIZE_Y, VOXEL_SIZE_Z);    
        
    // ------------------------------------------------
    
    // Allocate memory on the host 
	AllocateMemory(h_Volume, VOLUME_SIZE, allMemoryPointers, numberOfMemoryPointers, allNiftiImages, numberOfNiftiImages, "INPUT_VOLUME");
        
    // Convert data to floats
    if ( inputVolume->datatype == DT_SIGNED_SHORT )
    {
        short int *p = (short int*)inputVolume->data;
    
        for (int i = 0; i < DATA_W * DATA_H * DATA_D; i++)
        {
            h_Volume[i] = (float)p[i];
        }
    }
    else if ( inputVolume->datatype == DT_UINT8 )
    {
        unsigned char *p = (unsigned char*)inputVolume->data;
    
        for (int i = 0; i < DATA_W * DATA_H * DATA_D; i++)
        {
            h_Volume[i] = (float)p[i];
        }
    }
    else if ( inputVolume->datatype == DT_FLOAT )
    {
        float *p = (float*)inputVolume->data;
    
        for (int i = 0; i < DATA_W * DATA_H * DATA_D; i++)
        {
            h_Volume[i] = p[i];
        }
    }
    else
    {
        printf("Unknown data type in input volume, aborting!\n");
        FreeAllMemory(allMemoryPointers,numberOfMemoryPointers);
		FreeAllNiftiImages(allNiftiImages,numberOfNiftiImages);
        return EXIT_FAILURE;
    }
       
    //------------------------
    
    // First initialize OpenGL context, so we can properly setup the OpenGL / OpenCL interop.
    InitGL(&argc, argv); 

    // Create OpenCL context, get device info, select device, select options for image/texture and CL-GL interop
    //createCLContext(argc, (const char**)argv);
	createCLContext(OPENCL_PLATFORM, OPENCL_DEVICE);

	cl_int error;
	

    // create a command-queue
    cqCommandQueue = clCreateCommandQueue(cxGPUContext, cdDevices[uiDeviceUsed], 0, &ciErrNum);
    //oclCheckErrorEX(ciErrNum, CL_SUCCESS, pCleanup);

	//clGetDeviceInfo(cdDevices[uiDeviceUsed], CL_DEVICE_IMAGE_SUPPORT, sizeof(g_bImageSupport), &g_bImageSupport, NULL);
	g_bImageSupport = true;

	// Read the kernel code from file
	std::fstream kernelFile("volumeRender.cl",std::ios::in);

	std::ostringstream oss;
	oss << kernelFile.rdbuf();
	std::string src = oss.str();
	const char *srcstr = src.c_str();


	// Create program and build the code for the selected device
	cpProgram = clCreateProgramWithSource(cxGPUContext, 1, (const char**)&srcstr, NULL, &error);
	printf("Create program with source error is %i \n",error);
    
    // build the program
    std::string buildOpts = "-cl-fast-relaxed-math";
    buildOpts += g_bImageSupport ? " -DIMAGE_SUPPORT" : "";
    ciErrNum = clBuildProgram(cpProgram, 0, NULL, buildOpts.c_str(), NULL, NULL);
	printf("Build program error is %i \n",error);
    if (ciErrNum != CL_SUCCESS)
    {
		printf("Building failed!\n");
        // write out standard error, Build Log and PTX, then cleanup and return error
        //shrlogEx(LOGBOTH | ERRORMSG, ciErrNum, STDERROR);
        //oclLogBuildInfo(cpProgram, oclGetFirstDev(cxGPUContext));
        //oclLogPtx(cpProgram, oclGetFirstDev(cxGPUContext), "oclVolumeRender.ptx");
        Cleanup(EXIT_FAILURE); 
    }

    // create the kernel
    ckKernel = clCreateKernel(cpProgram, "d_render", &error);
	printf("Create kernel error is %i \n",error);
    //oclCheckErrorEX(ciErrNum, CL_SUCCESS, pCleanup);

    // Init OpenCL
    initCLVolume(h_Volume, DATA_W, DATA_H, DATA_D);

    // init timer 1 for fps measurement 
    //shrDeltaT(1);  
    
    // Create buffers and textures, 
    // and then start main GLUT rendering loop for processing and rendering, 
	// or otherwise run No-GL Q/A test sequence
    initPixelBuffer();
    glutMainLoop();

    // Normally unused return path
    Cleanup(EXIT_SUCCESS);

    // Free all memory
    FreeAllMemory(allMemoryPointers,numberOfMemoryPointers);
    FreeAllNiftiImages(allNiftiImages,numberOfNiftiImages);
       
    return EXIT_SUCCESS;
}
Example #11
0
void opencl_init(void) {

	// get the platform

	cl_uint num_platforms;
	clError = clGetPlatformIDs(0, NULL, &num_platforms);
	checkErr(clError, "clGetPlatformIDs( 0, NULL, &num_platforms );");

	if (num_platforms <= 0) {
		std::cout << "No platform..." << std::endl;
		exit(1);
	}

	cl_platform_id* platforms = new cl_platform_id[num_platforms];
	clError = clGetPlatformIDs(num_platforms, platforms, NULL);
	checkErr(clError, "clGetPlatformIDs( num_platforms, &platforms, NULL );");
	if (num_platforms > 1) {
		char platformName[256];
		clError = clGetPlatformInfo(platforms[0], CL_PLATFORM_VENDOR,
				sizeof(platformName), platformName, NULL);
		std::cerr << "Multiple platforms found defaulting to: " << platformName
				<< std::endl;
	}
	platform_id = platforms[0];
	if (getenv("OPENCL_PLATEFORM"))
		platform_id = platforms[1];
	delete platforms;

	// Connect to a compute device
	//
	cl_uint device_count = 0;
	clError = clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_ALL, 0, NULL,
			&device_count);
	checkErr(clError, "Failed to create a device group");
	cl_device_id* deviceIds = (cl_device_id*) malloc(
			sizeof(cl_device_id) * device_count);
	clError = clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_ALL, device_count,
			deviceIds, NULL);
	if (device_count > 1) {
		char device_name[256];
		int compute_units;
		clError = clGetDeviceInfo(deviceIds[0], CL_DEVICE_NAME,
				sizeof(device_name), device_name, NULL);
		checkErr(clError, "clGetDeviceInfo failed");
		clError = clGetDeviceInfo(deviceIds[0], CL_DEVICE_MAX_COMPUTE_UNITS,
				sizeof(cl_uint), &compute_units, NULL);
		checkErr(clError, "clGetDeviceInfo failed");
		std::cerr << "Multiple devices found defaulting to: " << device_name;
		std::cerr << " with " << compute_units << " compute units" << std::endl;
	}
	device_id = deviceIds[0];
	delete deviceIds;
	// Create a compute context 
	//
	context = clCreateContext(0, 1, &device_id, NULL, NULL, &clError);
	checkErr(clError, "Failed to create a compute context!");

	// Create a command commands
	//
	commandQueue = clCreateCommandQueue(context, device_id, 0, &clError);
	checkErr(clError, "Failed to create a command commands!");

	// READ KERNEL FILENAME
	std::string filename = "NOTDEFINED.cl";
	char const* tmp_name = getenv("OPENCL_KERNEL");
	if (tmp_name) {
		filename = std::string(tmp_name);
	} else {
		filename = std::string(__FILE__);
		filename = filename.substr(0, filename.length() - 17);
		filename += "/kernels.cl";

	}

	// READ OPENCL_PARAMETERS
	std::string compile_parameters = "";
	char const* tmp_params = getenv("OPENCL_PARAMETERS");
	if (tmp_params) {
		compile_parameters = std::string(tmp_params);
	}

	std::ifstream kernelFile(filename.c_str(), std::ios::in);

	if (!kernelFile.is_open()) {
		std::cout << "Unable to open " << filename << ". " << __FILE__ << ":"
				<< __LINE__ << "Please set OPENCL_KERNEL" << std::endl;
		exit(1);
	}

	/*
	 * Read the kernel file into an output stream.
	 * Convert this into a char array for passing to OpenCL.
	 */
	std::ostringstream outputStringStream;
	outputStringStream << kernelFile.rdbuf();
	std::string srcStdStr = outputStringStream.str();
	const char* charSource = srcStdStr.c_str();

	kernelFile.close();
	// Create the compute program from the source buffer
	//
	program = clCreateProgramWithSource(context, 1, (const char **) &charSource,
			NULL, &clError);
	if (!program) {
		printf("Error: Failed to create compute program!\n");
		exit(1);
	}

	// Build the program executable
	//
	clError = clBuildProgram(program, 0, NULL, compile_parameters.c_str(), NULL,
			NULL);

	/* Get the size of the build log. */
	size_t logSize = 0;
	clGetProgramBuildInfo(program, device_id, CL_PROGRAM_BUILD_LOG, 0, NULL,
			&logSize);

	if (clError != CL_SUCCESS) {
		if (logSize > 1) {
			char* log = new char[logSize];
			clGetProgramBuildInfo(program, device_id, CL_PROGRAM_BUILD_LOG,
					logSize, log, NULL);

			std::string stringChars(log, logSize);
			std::cerr << "Build log:\n " << stringChars << std::endl;

			delete[] log;
		}
		printf("Error: Failed to build program executable!\n");
		exit(1);
	}

	return;

}
Example #12
0
void initOpenCL(void) {
    
    /* get the platform(s) */
    cl_uint num_platforms;
    clError = clGetPlatformIDs( 0, NULL, &num_platforms );

    checkErr (clError, "clGetPlatformIDs( 0, NULL, &num_platforms );");
    if (num_platforms <= 0) {
        std::cerr << "No platform..." << std::endl;
        exit(1);
    }
    
    cl_platform_id* platforms = new cl_platform_id[num_platforms];
    clError = clGetPlatformIDs(num_platforms, platforms, NULL);
    checkErr(clError, "clGetPlatformIDs( num_platforms, &platforms, NULL );");
    platform_id = platforms[0];
    delete platforms;

    /* Connect to a compute device */
    err = clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_ALL, 1, &device, NULL);
    checkErr(err,"Failed to create a device group");

    /* Create a compute context */
    context = clCreateContext(NULL, 1, &device, NULL, NULL, &err);
    checkErr(err,"Failed to create a compute context!");

    /* Create a command queue */
    commandQueue = clCreateCommandQueue(context, device, 0, &err);
    checkErr(err,"Failed to create a command commands!");

    /* Open Kernel file */
    std::string filename = "/home/ictp17/bagus/kernels.cl";
    std::ifstream kernelFile(filename.c_str(), std::ios::in);
    
    if (not kernelFile.is_open()) {
        std::cout << "Unable to open " << filename << ". " << std::endl;
        exit(1);
    }
    
    /*
     * Read the kernel file into an output stream.
     * Convert this into a char array for passing to OpenCL.
     */
    std::ostringstream outputStringStream;
    outputStringStream << kernelFile.rdbuf();
    std::string srcStdStr = outputStringStream.str();
    const char* charSource = srcStdStr.c_str();
    
    kernelFile.close();
    /* Create the compute program from the source buffer */
    program = clCreateProgramWithSource(context, 1, 
               (const char **) &charSource, NULL, &err);
    if (not program) {
        std::cerr << "Error: Failed to create compute program!" << std::endl;
        exit(1);
    }
    
    /* Build the program executable */
    const char* flags = "";


    err = clBuildProgram(program, 0, NULL, flags, NULL, NULL);
    if (err != CL_SUCCESS) {
        size_t len;
        char buffer[1024];
    
        std::cerr << "Error: Failed to build program executable!" 
                  << std::endl;
        clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, 
                sizeof(buffer), buffer, &len);
        std::cerr << buffer << std::endl;
        exit(1);
    }

    /* Create Kernels objects for all the kernels in the OpenCL program */
    cl_uint num_kernels;
    kernel = clCreateKernel(program, "vector_sum", &err);
    if (err != CL_SUCCESS) {
        size_t len;
        char buffer[1024];
    
        std::cout << "Error: Failed to create kernels in program!" 
                  << std::endl;
        clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, 
                sizeof(buffer), buffer, &len);
        std::cout << buffer << std::endl;
        exit(1);
    }
}