예제 #1
0
파일: program.hpp 프로젝트: 2bbb/compute
    /// Links the programs in \p programs with \p options in \p context.
    ///
    /// \opencl_version_warning{1,2}
    ///
    /// \see_opencl_ref{clLinkProgram}
    static program link(const std::vector<program> &programs,
                        const context &context,
                        const std::string &options = std::string())
    {
        const char *options_string = 0;

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

        cl_int ret;
        cl_program program_ = clLinkProgram(
            context.get(),
            0,
            0,
            options_string,
            static_cast<uint_>(programs.size()),
            reinterpret_cast<const cl_program*>(&programs[0]),
            0,
            0,
            &ret
        );

        if(!program_){
            BOOST_THROW_EXCEPTION(opencl_error(ret));
        }

        return program(program_, false);
    }
예제 #2
0
JNIEXPORT jlong JNICALL Java_org_lwjgl_opencl_CL12_nclLinkProgram(JNIEnv *env, jclass clazz, jlong context, jint num_devices, jlong device_list, jlong options, jint num_input_programs, jlong input_programs, jlong pfn_notify, jlong user_data, jlong errcode_ret, 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_programs_address = (const cl_program *)(intptr_t)input_programs;
	cl_int *errcode_ret_address = (cl_int *)(intptr_t)errcode_ret;
	clLinkProgramPROC clLinkProgram = (clLinkProgramPROC)((intptr_t)function_pointer);
	cl_program __result = clLinkProgram((cl_context)(intptr_t)context, num_devices, device_list_address, options_address, num_input_programs, input_programs_address, (cl_program_callback)(intptr_t)pfn_notify, (void *)(intptr_t)user_data, errcode_ret_address);
	return (intptr_t)__result;
}
예제 #3
0
static bool
test(cl_context context,
     cl_uint num_devices, const cl_device_id *device_list,
     const char *options,
     cl_uint num_input_programs, const cl_program *input_programs,
     void (CL_CALLBACK *pfn_notify)(cl_program program, void *user_data),
     void *user_data,
     cl_program *ret_program,
     cl_int expected_error, enum piglit_result* result,
     const char* test_str) {
	cl_program program;
	cl_int errNo;

	program = clLinkProgram(context,
	                        num_devices, device_list,
	                        options,
	                        num_input_programs, input_programs,
	                        pfn_notify, user_data,
	                        &errNo);

	if (ret_program) {
		*ret_program = program;
	} else {
		if (program)
			clReleaseProgram(program);
	}

	if(!piglit_cl_check_error(errNo, expected_error)) {
		fprintf(stderr, "Failed (error code: %s): %s.\n",
		        piglit_cl_get_error_name(errNo), test_str);
		piglit_merge_result(result, PIGLIT_FAIL);
		return false;
	}

	return true;
}
예제 #4
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);
}