Ejemplo n.º 1
0
JNIEXPORT jint JNICALL Java_org_lwjgl_opencl_CL12_nclCompileProgram(JNIEnv *env, jclass clazz, jlong program, jint num_devices, jlong device_list, jlong options, jint num_input_headers, jlong input_header, jlong header_include_name, jlong pfn_notify, jlong user_data, jlong function_pointer) {
	const cl_device_id *device_list_address = (const cl_device_id *)(intptr_t)device_list;
	const cl_char *options_address = (const cl_char *)(intptr_t)options;
	const cl_program *input_header_address = (const cl_program *)(intptr_t)input_header;
	const cl_char *header_include_name_address = (const cl_char *)(intptr_t)header_include_name;
	clCompileProgramPROC clCompileProgram = (clCompileProgramPROC)((intptr_t)function_pointer);
	cl_int __result = clCompileProgram((cl_program)(intptr_t)program, num_devices, device_list_address, options_address, num_input_headers, input_header_address, (const cl_char **)&header_include_name_address, (cl_program_callback)(intptr_t)pfn_notify, (void *)(intptr_t)user_data);
	return __result;
}
Ejemplo n.º 2
0
    /// Compiles the program with \p options.
    ///
    /// \opencl_version_warning{1,2}
    ///
    /// \see_opencl_ref{clCompileProgram}
    void compile(const std::string &options = std::string(),
                 const std::vector<std::pair<std::string, program> > &headers =
                    std::vector<std::pair<std::string, program> >())
    {
        const char *options_string = 0;

        if(!options.empty()){
            options_string = options.c_str();
        }

        cl_int ret;
        if (headers.empty())
        {
            ret = clCompileProgram(
                m_program, 0, 0, options_string, 0, 0, 0, 0, 0
            );
        }
        else
        {
            std::vector<const char*> header_names(headers.size());
            std::vector<cl_program> header_programs(headers.size());
            for (size_t i = 0; i < headers.size(); ++i)
            {
                header_names[i] = headers[i].first.c_str();
                header_programs[i] = headers[i].second.m_program;
            }

            ret = clCompileProgram(
                m_program,
                0,
                0,
                options_string,
                static_cast<cl_uint>(headers.size()),
                header_programs.data(),
                header_names.data(),
                0,
                0
            );
        }

        if(ret != CL_SUCCESS){
            BOOST_THROW_EXCEPTION(opencl_error(ret));
        }
    }
Ejemplo n.º 3
0
    /// Compiles the program with \p options.
    ///
    /// \opencl_version_warning{1,2}
    ///
    /// \see_opencl_ref{clCompileProgram}
    void compile(const std::string &options = std::string())
    {
        const char *options_string = 0;

        if(!options.empty()){
            options_string = options.c_str();
        }

        cl_int ret = clCompileProgram(
            m_program, 0, 0, options_string, 0, 0, 0, 0, 0
        );

        if(ret != CL_SUCCESS){
            BOOST_THROW_EXCEPTION(opencl_error(ret));
        }
    }
Ejemplo n.º 4
0
JNIEXPORT jint JNICALL Java_org_lwjgl_opencl_CL12_nclCompileProgram3(JNIEnv *env, jclass clazz, jlong program, jint num_devices, jlong device_list, jlong options, jint num_input_headers, jlong input_headers, jobjectArray header_include_names, jlong pfn_notify, jlong user_data, jlong function_pointer) {
	const cl_device_id *device_list_address = (const cl_device_id *)(intptr_t)device_list;
	const cl_char *options_address = (const cl_char *)(intptr_t)options;
	const cl_program *input_headers_address = (const cl_program *)(intptr_t)input_headers;
	unsigned int _ptr_i;
	jobject _ptr_object;
	cl_char **header_include_names_ptr = (cl_char **) malloc(num_input_headers * sizeof(cl_char *));
	clCompileProgramPROC clCompileProgram = (clCompileProgramPROC)((intptr_t)function_pointer);
	cl_int __result;
	_ptr_i = 0;
	while ( _ptr_i < num_input_headers ) {
		_ptr_object = (*env)->GetObjectArrayElement(env, header_include_names, _ptr_i);
		header_include_names_ptr[_ptr_i++] = (cl_char *)(intptr_t)getPointerWrapperAddress(env, _ptr_object);
	}
	__result = clCompileProgram((cl_program)(intptr_t)program, num_devices, device_list_address, options_address, num_input_headers, input_headers_address, (const cl_char **)header_include_names_ptr, (cl_program_callback)(intptr_t)pfn_notify, (void *)(intptr_t)user_data);
	free(header_include_names_ptr);
	return __result;
}
Ejemplo n.º 5
0
JNIEXPORT jint JNICALL Java_org_lwjgl_opencl_CL12_nclCompileProgramMulti(JNIEnv *env, jclass clazz, jlong program, jint num_devices, jlong device_list, jlong options, jint num_input_headers, jlong input_headers, jlong header_include_names, jlong pfn_notify, jlong user_data, jlong function_pointer) {
	const cl_device_id *device_list_address = (const cl_device_id *)(intptr_t)device_list;
	const cl_char *options_address = (const cl_char *)(intptr_t)options;
	const cl_program *input_headers_address = (const cl_program *)(intptr_t)input_headers;
	const cl_char *header_include_names_address = (const cl_char *)(intptr_t)header_include_names;
	unsigned int _str_i;
	cl_char *_str_address;
	cl_char **header_include_names_str = (cl_char **) malloc(num_input_headers * sizeof(cl_char *));
	clCompileProgramPROC clCompileProgram = (clCompileProgramPROC)((intptr_t)function_pointer);
	cl_int __result;
	_str_i = 0;
	_str_address = (cl_char *)header_include_names_address;
	while ( _str_i < num_input_headers ) {
		header_include_names_str[_str_i++] = _str_address;
		_str_address += strlen(_str_address) + 1;
	}
	__result = clCompileProgram((cl_program)(intptr_t)program, num_devices, device_list_address, options_address, num_input_headers, input_headers_address, (const cl_char **)&header_include_names_str, (cl_program_callback)(intptr_t)pfn_notify, (void *)(intptr_t)user_data);
	free(header_include_names_str);
	return __result;
}
Ejemplo n.º 6
0
static cl_program
compile_program(cl_context context,
                cl_uint num_devices, const cl_device_id *device_list,
                cl_uint count, const char **strings,
                const char* err_str) {
	cl_int errNo;
	cl_program program;

	/* Create program with source */
	program = clCreateProgramWithSource(context,
	                                    count,
	                                    strings,
	                                    NULL,
	                                    &errNo);
	if(!piglit_cl_check_error(errNo, CL_SUCCESS)) {
		fprintf(stderr,
		        "Failed (error code: %s): Create program with source (for the %s).\n",
		        piglit_cl_get_error_name(errNo), err_str);
		return NULL;
	}

	/* Compile program */
	errNo = clCompileProgram(program,
	                       num_devices, device_list,
	                       " ",
	                       0, NULL, NULL,
	                       NULL, NULL);

	if(!piglit_cl_check_error(errNo, CL_SUCCESS)) {
		fprintf(stderr, "Failed (error code: %s): Compile program (for the %s).\n",
		        piglit_cl_get_error_name(errNo), err_str);
		clReleaseProgram(program);
		return NULL;
	}

	return program;
}
Ejemplo n.º 7
0
void run_vec_fma(size_t num_elems, size_t buf_size, cl_int* data) {
  cl_int err;

  // Query platforms and devices
  cl_platform_id platform;
  err = clGetPlatformIDs(1, &platform, NULL);

  cl_device_id device;
  err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_ALL, 1, &device, NULL);

  const cl_context_properties prop[] = {
    CL_CONTEXT_PLATFORM, (cl_context_properties)platform,
    0
  };

  // Create context
  cl_context ctx = clCreateContext(prop, 1, &device, NULL, NULL, &err);

  // Create program
  unsigned char* program_file = NULL;
  size_t program_size = 0;

  cl_program prog[2];

  // Comple source - elem.cl
  read_file(&program_file, &program_size, "elem.cl");
  prog[0] = clCreateProgramWithSource(ctx, 1, (const char **)&program_file,
                                      &program_size, &err);
  err = clCompileProgram(prog[0], 1, &device, NULL, 0, NULL, NULL, NULL, NULL);
  free(program_file);

  // Compile source - fma.cl
  read_file(&program_file, &program_size, "fma.cl");
  prog[1] = clCreateProgramWithSource(ctx, 1, (const char **)&program_file,
                                      &program_size, &err);
  err = clCompileProgram(prog[1], 1, &device, NULL, 0, NULL, NULL, NULL, NULL);
  free(program_file);

  // Link programs
  cl_program program =
      clLinkProgram(ctx, 1, &device, NULL, 2, prog, NULL, NULL, &err);

  // Allocate memory buffers (on the device)
  cl_mem a = clCreateBuffer(ctx, CL_MEM_READ_ONLY, buf_size, NULL, &err);
  cl_mem b = clCreateBuffer(ctx, CL_MEM_READ_ONLY, buf_size, NULL, &err);
  cl_mem c = clCreateBuffer(ctx, CL_MEM_READ_ONLY, buf_size, NULL, &err);
  cl_mem d = clCreateBuffer(ctx, CL_MEM_WRITE_ONLY, buf_size, NULL, &err);

  // Create command queue
  cl_command_queue queue = clCreateCommandQueue(ctx, device, 0, NULL);

  // Enqueue the write buffer commands
  cl_event wb_events[2];

  err = clEnqueueWriteBuffer(queue, a, CL_FALSE, 0, buf_size, data, 0,
                             NULL, &wb_events[0]);
  err = clEnqueueWriteBuffer(queue, b, CL_FALSE, 0, buf_size, data, 0,
                             NULL, &wb_events[1]);
  err = clEnqueueWriteBuffer(queue, c, CL_FALSE, 0, buf_size, data, 0,
                             NULL, &wb_events[1]);

  // Enqueue the kernel execution command
  cl_kernel kernel = clCreateKernel(program, "vec_fma", &err);
  err = clSetKernelArg(kernel, 0, sizeof(cl_mem), &d);
  err = clSetKernelArg(kernel, 1, sizeof(cl_mem), &a);
  err = clSetKernelArg(kernel, 2, sizeof(cl_mem), &b);
  err = clSetKernelArg(kernel, 3, sizeof(cl_mem), &c);

  const size_t global_offset = 0;
  cl_event kernel_event;
  err = clEnqueueNDRangeKernel(queue, kernel, 1, &global_offset,
                               &num_elems, NULL, 2, wb_events,
                               &kernel_event);

  // Enqueue the read buffer command
  err = clEnqueueReadBuffer(queue, d, CL_TRUE, 0, buf_size, data, 1,
                            &kernel_event, NULL);

  // Wait until every commands are finished
  err = clFinish(queue);

  // Release the resources
  clReleaseMemObject(a);
  clReleaseMemObject(b);
  clReleaseMemObject(c);
  clReleaseMemObject(d);
  clReleaseKernel(kernel);
  clReleaseProgram(program);
  clReleaseCommandQueue(queue);
  clReleaseContext(ctx);
  clReleaseDevice(device);
}
Ejemplo n.º 8
0
CLWProgram CLWProgram::CreateFromSource(char const* sourcecode,
                                        size_t sourcesize,
                                        char const** headers,
                                        char const** headernames,
                                        size_t* headersizes,
                                        int numheaders,
                                        char const* buildopts,
                                        CLWContext context)
{
    cl_int status = CL_SUCCESS;
    
    std::vector<cl_device_id> deviceIds(context.GetDeviceCount());
    for(unsigned int i = 0; i < context.GetDeviceCount(); ++i)
    {
        deviceIds[i] = context.GetDevice(i);
    }

    std::vector<cl_program> headerPrograms(numheaders);
    for (int i=0; i<numheaders; ++i)
    {
        size_t sourceSize = headersizes[i];
        char const* tempPtr = headers[i];
        headerPrograms[i] = clCreateProgramWithSource(context, 1, (const char**)&tempPtr, &sourceSize, &status);
        ThrowIf(status != CL_SUCCESS, status, "clCreateProgramWithSource failed");
    }
    
    cl_program program = clCreateProgramWithSource(context, 1, (const char**)&sourcecode, &sourcesize, &status);
    
    ThrowIf(status != CL_SUCCESS, status, "clCreateProgramWithSource failed");
    
    status = clCompileProgram(program, context.GetDeviceCount(), &deviceIds[0], buildopts, numheaders, &headerPrograms[0], headernames, nullptr, nullptr);
    
    if(status != CL_SUCCESS)
    {
        std::vector<char> buildLog;
        size_t logSize;
        clGetProgramBuildInfo(program, deviceIds[0], CL_PROGRAM_BUILD_LOG, 0, nullptr, &logSize);
        
        buildLog.resize(logSize);
        clGetProgramBuildInfo(program, deviceIds[0], CL_PROGRAM_BUILD_LOG, logSize, &buildLog[0], nullptr);
        
#ifdef _DEBUG
        std::cout << &buildLog[0] << "\n";
#endif
        
        throw CLWException(status, std::string(&buildLog[0]));
    }
    
    status = clBuildProgram(program, context.GetDeviceCount(), &deviceIds[0], buildopts, nullptr, nullptr);
    
    if(status != CL_SUCCESS)
    {
        std::vector<char> buildLog;
        size_t logSize;
        clGetProgramBuildInfo(program, deviceIds[0], CL_PROGRAM_BUILD_LOG, 0, nullptr, &logSize);
        
        buildLog.resize(logSize);
        clGetProgramBuildInfo(program, deviceIds[0], CL_PROGRAM_BUILD_LOG, logSize, &buildLog[0], nullptr);
        
#ifdef _DEBUG
        std::cout << &buildLog[0] << "\n";
#endif
        
        throw CLWException(status, std::string(&buildLog[0]));
    }
    
    CLWProgram prg(program);
    
    clReleaseProgram(program);
    
    return prg;
}