Пример #1
0
/**
 * @internal
 *
 * @brief Tests buffer wrapping and unwrapping.
 * */
static void wrap_unwrap_test() {

    /* Test variables. */
    CCLContext * ctx = NULL;
    CCLBuffer * b = NULL;
    CCLBuffer * b_aux = NULL;
    cl_mem buffer = NULL;
    CCLErr * err = NULL;
    size_t buf_size = sizeof(cl_uint) * CCL_TEST_BUFFER_SIZE;
    cl_int status;

    /* Get the test context with the pre-defined device. */
    ctx = ccl_test_context_new(&err);
    g_assert_no_error(err);

    /* Create a buffer using OpenCL functions directly. */
    buffer = clCreateBuffer(
        ccl_context_unwrap(ctx), CL_MEM_READ_ONLY, buf_size, NULL, &status);
    g_assert_cmpint(status, ==, CL_SUCCESS);

    /* Wrap buffer. */
    b = ccl_buffer_new_wrap(buffer);

    /* If we now unwrap the wrapper, we must get the originally created
     * buffer. */
    g_assert(buffer == ccl_buffer_unwrap(b));

    /* If we again wrap the original buffer... */
    b_aux = ccl_buffer_new_wrap(buffer);

    /* ...we must get the same wrapper... */
    g_assert(b == b_aux);

    /* ... and the buffer wrapper ref count must be 2. */
    g_assert_cmpuint(2, ==, ccl_wrapper_ref_count((CCLWrapper *) b));

    /* Unref buffer, twice. */
    ccl_buffer_unref(b);

    /* Check that buffer ref count is 1. */
    g_assert_cmpuint(1, ==, ccl_wrapper_ref_count((CCLWrapper *) b));

    /* Destroy stuff. */
    ccl_buffer_destroy(b);
    ccl_context_destroy(ctx);

    /* Confirm that memory allocated by wrappers has been properly
     * freed. */
    g_assert(ccl_wrapper_memcheck());
}
Пример #2
0
/**
 * Tests creation, getting info from and destruction of
 * kernel wrapper objects.
 * */
static void create_info_destroy_test() {

	/* Test variables. */
	CCLContext* ctx = NULL;
	cl_context context = NULL;
	CCLProgram* prg = NULL;
	cl_program program = NULL;
	CCLKernel* krnl = NULL;
	cl_kernel kernel = NULL;
	CCLDevice* d = NULL;
	CCLQueue* cq = NULL;
	size_t gws;
	size_t lws;
	cl_uint host_buf[CCL_TEST_KERNEL_BUF_SIZE];
	cl_uint host_buf_aux[CCL_TEST_KERNEL_BUF_SIZE];
	CCLBuffer* buf;
	GError* err = NULL;
	CCLEvent* evt = NULL;
	CCLEventWaitList ewl = NULL;
	const char* krnl_name;
	void* args[] = { NULL, NULL };
	cl_bool release_krnl;
	cl_int ocl_status;

	/* Create a context with devices from first available platform. */
	ctx = ccl_test_context_new(&err);
	g_assert_no_error(err);

	/* Create a new program from source and build it. */
	prg = ccl_program_new_from_source(
		ctx, CCL_TEST_KERNEL_CONTENT, &err);
	g_assert_no_error(err);

	ccl_program_build(prg, NULL, &err);
	g_assert_no_error(err);

	/* Create a command queue. */
	cq = ccl_queue_new(ctx, d, CL_QUEUE_PROFILING_ENABLE, &err);
	g_assert_no_error(err);

	/* Test three ways to create a kernel wrapper. */
	for (cl_uint i = 0; i < 3; ++i) {

		/* Create kernel wrapper. */
		switch (i) {
			case 0:
				/* Instantiate kernel directly. */
				krnl = ccl_kernel_new(prg, CCL_TEST_KERNEL_NAME, &err);
				g_assert_no_error(err);
				release_krnl = CL_TRUE;
				break;
			case 1:
				/* Using the program utility function. No need to free
				 * kernel in this case, because it will be freed when
				 * program is destroyed. */
				krnl = ccl_program_get_kernel(
					prg, CCL_TEST_KERNEL_NAME, &err);
				g_assert_no_error(err);
				release_krnl = CL_FALSE;
				break;
			case 2:
				/* Using the "wrap" constructor. */
				kernel = clCreateKernel(ccl_program_unwrap(prg),
					CCL_TEST_KERNEL_NAME, &ocl_status);
				g_assert_cmpint(ocl_status, ==, CL_SUCCESS);
				krnl = ccl_kernel_new_wrap(kernel);
				g_assert_cmphex(GPOINTER_TO_UINT(kernel), ==,
					GPOINTER_TO_UINT(ccl_kernel_unwrap(krnl)));
				release_krnl = CL_TRUE;
				break;
		}

		/* Get some kernel info, compare it with expected info. */

		/* Get kernel function name from kernel info, compare it with the
		 * expected value. */
		krnl_name = ccl_kernel_get_info_array(
			krnl, CL_KERNEL_FUNCTION_NAME, char*, &err);
		g_assert_no_error(err);
		g_assert_cmpstr(krnl_name, ==, CCL_TEST_KERNEL_NAME);

		/* Check if the kernel context is the same as the initial context
		 * and the program context. */
		context = ccl_kernel_get_info_scalar(
			krnl, CL_KERNEL_CONTEXT, cl_context, &err);
		g_assert_no_error(err);
		g_assert(context == ccl_context_unwrap(ctx));

		program = ccl_kernel_get_info_scalar(
			krnl, CL_KERNEL_PROGRAM, cl_program, &err);
		g_assert_no_error(err);
		g_assert(program == ccl_program_unwrap(prg));

#ifndef OPENCL_STUB

		cl_uint ocl_ver;

		/* Get OpenCL version of kernel's underlying platform. */
		ocl_ver = ccl_kernel_get_opencl_version(krnl, &err);
		g_assert_no_error(err);

		(void)ocl_ver;

#ifdef CL_VERSION_1_1

		size_t kwgz;
		size_t* kcwgs;
		CCLDevice* dev = NULL;

		/* If platform supports kernel work group queries, get kernel
		 * work group information and compare it with expected info. */
		if (ocl_ver >= 110) {

			dev = ccl_context_get_device(ctx, 0, &err);
			g_assert_no_error(err);

			kwgz = ccl_kernel_get_workgroup_info_scalar(
				krnl, dev, CL_KERNEL_WORK_GROUP_SIZE, size_t, &err);
			g_assert_no_error(err);
			(void)kwgz;

			kcwgs = ccl_kernel_get_workgroup_info_array(krnl, dev,
				CL_KERNEL_COMPILE_WORK_GROUP_SIZE, size_t*, &err);
			g_assert_no_error(err);
			(void)kcwgs;

		}

#endif /* ifdef CL_VERSION_1_1 */

#ifdef CL_VERSION_1_2

		cl_kernel_arg_address_qualifier kaaq;
		char* kernel_arg_type_name;
		char* kernel_arg_name;

		/* If platform supports kernel argument queries, get kernel argument
		 * information and compare it with expected info. */
		if (ocl_ver >= 120) {

			kaaq = ccl_kernel_get_arg_info_scalar(krnl, 0,
					CL_KERNEL_ARG_ADDRESS_QUALIFIER,
					cl_kernel_arg_address_qualifier, &err);
			g_assert((err == NULL) || (err->code == CCL_ERROR_INFO_UNAVAILABLE_OCL));
			if (err == NULL) {
				g_assert_cmphex(kaaq, ==, CL_KERNEL_ARG_ADDRESS_GLOBAL);
			} else {
Пример #3
0
/**
 * Tests creation, getting info from and destruction of
 * program wrapper objects.
 * */
static void create_info_destroy_test() {

	/* Test variables. */
	CCLContext* ctx = NULL;
	CCLProgram* prg = NULL;
	CCLProgram* prg2 = NULL;
	CCLKernel* krnl = NULL;
	CCLWrapperInfo* info = NULL;
	CCLDevice* d = NULL;
	CCLQueue* cq = NULL;
	size_t gws;
	size_t lws;
	cl_uint a_h[CCL_TEST_PROGRAM_BUF_SIZE];
	cl_uint b_h[CCL_TEST_PROGRAM_BUF_SIZE];
	cl_uint c_h[CCL_TEST_PROGRAM_BUF_SIZE];
	cl_uint d_h ;
	CCLBuffer* a_w;
	CCLBuffer* b_w;
	CCLBuffer* c_w;
	CCLEvent* evt_w1;
	CCLEvent* evt_w2;
	CCLEvent* evt_kr;
	CCLEvent* evt_r1;
	CCLEventWaitList ewl = NULL;
	GError* err = NULL;
	gchar* tmp_dir_name;
	gchar* tmp_file_prefix;

	/* Get a temp. dir. */
	tmp_dir_name = g_dir_make_tmp("test_program_XXXXXX", &err);
	g_assert_no_error(err);

	/* Get a temp file prefix. */
	tmp_file_prefix = g_strdup_printf("%s%c%s",
		tmp_dir_name, G_DIR_SEPARATOR, CCL_TEST_PROGRAM_SUM_FILENAME);

	/* Create a temporary kernel file. */
	g_file_set_contents(
		tmp_file_prefix, CCL_TEST_PROGRAM_SUM_CONTENT, -1, &err);
	g_assert_no_error(err);

	/* Create a context with devices from first available platform. */
	ctx = ccl_test_context_new(&err);
	g_assert_no_error(err);

	/* Create a new program from kernel file. */
	prg = ccl_program_new_from_source_file(
		ctx, tmp_file_prefix, &err);
	g_assert_no_error(err);

	ccl_program_destroy(prg);

	const char* file_pref = (const char*) tmp_file_prefix;
	prg = ccl_program_new_from_source_files(ctx, 1, &file_pref, &err);
	g_assert_no_error(err);

	g_free(tmp_file_prefix);

	/* Get some program info, compare it with expected info. */
	info = ccl_program_get_info(prg, CL_PROGRAM_CONTEXT, &err);
	g_assert_no_error(err);
	g_assert(*((cl_context*) info->value) == ccl_context_unwrap(ctx));

	/* Get number of devices from program info, check that this is the
	 * same value as the number of devices in context. */
	info = ccl_program_get_info(prg, CL_PROGRAM_NUM_DEVICES, &err);
	g_assert_no_error(err);
	g_assert_cmpuint(*((cl_uint*) info->value),
		==, ccl_context_get_num_devices(ctx, &err));
	g_assert_no_error(err);

	/* Get program source from program info, check that it is the
	 * same as the passed source. */
	info = ccl_program_get_info(prg, CL_PROGRAM_SOURCE, &err);
	g_assert_no_error(err);
	g_assert_cmpstr((char*) info->value,
		==, CCL_TEST_PROGRAM_SUM_CONTENT);

	/* Get first device in context (and in program). */
	d = ccl_context_get_device(ctx, 0, &err);
	g_assert_no_error(err);

	/* Check that no build was performed yet. */
	info = ccl_program_get_build_info(
		prg, d, CL_PROGRAM_BUILD_STATUS, &err);
	g_assert_no_error(err);
	g_assert_cmpint(*((cl_build_status*) info->value), ==, CL_BUILD_NONE);

	/* **** BUILD PROGRAM **** */
	ccl_program_build(prg, NULL, &err);
	g_assert_no_error(err);

	/* Get some program build info, compare it with expected values. */
	info = ccl_program_get_build_info(
		prg, d, CL_PROGRAM_BUILD_STATUS, &err);
	g_assert_no_error(err);
	g_assert((*((cl_build_status*) info->value) == CL_BUILD_SUCCESS)
		|| (*((cl_build_status*) info->value) == CL_BUILD_IN_PROGRESS));

	/* Get the build log, check that no error occurs. */
	info = ccl_program_get_build_info(prg, d, CL_PROGRAM_BUILD_LOG, &err);
	g_assert_no_error(err);

	/* Get kernel wrapper object. */
	krnl = ccl_program_get_kernel(
		prg, CCL_TEST_PROGRAM_SUM, &err);
	g_assert_no_error(err);

	/* Get some kernel info, compare it with expected info. */

	/* Get kernel function name from kernel info, compare it with the
	 * expected value. */
	info = ccl_kernel_get_info(krnl, CL_KERNEL_FUNCTION_NAME, &err);
	g_assert_no_error(err);
	g_assert_cmpstr(
		(gchar*) info->value, ==, CCL_TEST_PROGRAM_SUM);

	/* Check if the kernel context is the same as the initial context
	 * and the program context. */
	info = ccl_kernel_get_info(krnl, CL_KERNEL_CONTEXT, &err);
	g_assert_no_error(err);
	g_assert(*((cl_context*) info->value) == ccl_context_unwrap(ctx));

	info = ccl_kernel_get_info(krnl, CL_KERNEL_PROGRAM, &err);
	g_assert_no_error(err);
	g_assert(*((cl_program*) info->value) == ccl_program_unwrap(prg));

#ifndef OPENCL_STUB
#ifdef CL_VERSION_1_2

	cl_kernel_arg_address_qualifier kaaq;
	char* kernel_arg_type_name;
	char* kernel_arg_name;
	cl_uint ocl_ver;

	/* Get OpenCL version of program's underlying platform. */
	ocl_ver = ccl_program_get_opencl_version(prg, &err);
	g_assert_no_error(err);

	/* If platform supports kernel argument queries, get kernel argument
	 * information and compare it with expected info. */
	if (ocl_ver >= 120) {

		/* First kernel argument. */
		kaaq = ccl_kernel_get_arg_info_scalar(krnl, 0,
				CL_KERNEL_ARG_ADDRESS_QUALIFIER,
				cl_kernel_arg_address_qualifier, &err);
		g_assert((err == NULL) || (err->code == CCL_ERROR_INFO_UNAVAILABLE_OCL));
		if (err == NULL) {
			g_assert_cmphex(kaaq, ==, CL_KERNEL_ARG_ADDRESS_GLOBAL);
		} else {
Пример #4
0
/**
 * Tests creation (using "simple" constructor), getting info from and
 * destruction of sampler wrapper objects.
 * */
static void create_info_destroy_test() {

	/* Test variables. */
	CCLContext* ctx = NULL;
	CCLSampler* s = NULL;
	cl_sampler sampler = NULL;
	GError* err = NULL;
	cl_int ocl_status;
	const cl_sampler_properties sampler_properties[] = {
		CL_SAMPLER_NORMALIZED_COORDS, CL_TRUE,
		CL_SAMPLER_ADDRESSING_MODE, CL_ADDRESS_NONE,
		CL_SAMPLER_FILTER_MODE, CL_FILTER_NEAREST,
		0};

	/* Get the test context with the pre-defined device. */
	ctx = ccl_test_context_new(&err);
	g_assert_no_error(err);

	/* Test three ways to create a sampler. */
	for (cl_uint i = 0; i < 3; ++i) {

		/* Create sampler wrapper. */
		switch (i) {
			case 0:
				/* Create sampler using "simple" constructor. */
				s = ccl_sampler_new(ctx, CL_TRUE, CL_ADDRESS_NONE,
					CL_FILTER_NEAREST, &err);
				g_assert_no_error(err);
				break;
			case 1:
				/* Using the "full" constructor. */
				s = ccl_sampler_new_full(ctx, sampler_properties, &err);
				g_assert_no_error(err);
				break;
			case 2:
				/* Using the "wrap" constructor. */
				CCL_BEGIN_IGNORE_DEPRECATIONS
				sampler = clCreateSampler(ccl_context_unwrap(ctx),
					CL_TRUE, CL_ADDRESS_NONE, CL_FILTER_NEAREST,
					&ocl_status);
				g_assert_cmpint(ocl_status, ==, CL_SUCCESS);
				CCL_END_IGNORE_DEPRECATIONS
				s = ccl_sampler_new_wrap(sampler);
				g_assert_cmphex(GPOINTER_TO_UINT(sampler), ==,
					GPOINTER_TO_UINT(ccl_sampler_unwrap(s)));
				break;
		}

		/* Get some info and check if the return value is as expected. */
		cl_addressing_mode am;
		am = ccl_sampler_get_info_scalar(
			s, CL_SAMPLER_ADDRESSING_MODE, cl_addressing_mode, &err);
		g_assert_no_error(err);
		g_assert_cmpuint(am, ==, CL_ADDRESS_NONE);

		cl_filter_mode fm;
		fm = ccl_sampler_get_info_scalar(
			s, CL_SAMPLER_FILTER_MODE, cl_filter_mode, &err);
		g_assert_no_error(err);
		g_assert_cmpuint(fm, ==, CL_FILTER_NEAREST);

		cl_bool nc;
		nc = ccl_sampler_get_info_scalar(
			s, CL_SAMPLER_NORMALIZED_COORDS, cl_bool, &err);
		g_assert_no_error(err);
		g_assert_cmpuint(nc, ==, CL_TRUE);

		cl_context context;
		context = ccl_sampler_get_info_scalar(
			s, CL_SAMPLER_CONTEXT, cl_context, &err);
		g_assert_no_error(err);
		g_assert_cmphex(GPOINTER_TO_UINT(context), ==,
			GPOINTER_TO_UINT(ccl_context_unwrap(ctx)));

		/* Destroy sampler. */
		ccl_sampler_destroy(s);
	}

	/* Destroy context. */
	ccl_context_destroy(ctx);

	/* Confirm that memory allocated by wrappers has been properly
	 * freed. */
	g_assert(ccl_wrapper_memcheck());

}
Пример #5
0
/**
 * Create a new sampler wrapper object using a list of properties.
 *
 * If a supported property is not specified, a default value is used.
 * Some valid properties are `CL_SAMPLER_NORMALIZED_COORDS` (default
 * value is `CL_TRUE`), `CL_SAMPLER_ADDRESSING_MODE` (default value is
 * `CL_ADDRESS_CLAMP`) and `CL_SAMPLER_FILTER_MODE` (default value is
 * `CL_FILTER_NEAREST`).
 *
 * This function mimicks the style of the OpenCL 2.0 sampler
 * constructor, clCreateSamplerWithProperties(), but can be used with
 * any version of OpenCL. Thus, The underlying OpenCL sampler object is
 * created using:
 *
 * * clCreateSampler() - for platforms with OpenCL version <= 1.2
 * * clCreateSamplerWithProperties() - for platforms with OpenCL version
 * >= 2.0.
 *
 * @public @memberof ccl_sampler
 *
 * @param[in] ctx A context wrapper object.
 * @param[in] sampler_properties A list of sampler property names and
 * their corresponding values. Each sampler property name is immediately
 * followed by the corresponding desired value. The list is terminated
 * with 0. If a supported property is not specified, its default value
 * will be used. If `NULL`, default values for supported sampler
 * properties will be used.
 * @param[out] err Return location for a ::CCLErr object, or `NULL` if error
 * reporting is to be ignored.
 * @return A new sampler wrapper object or `NULL` if an error occurs.
 * */
CCL_EXPORT
CCLSampler* ccl_sampler_new_full(CCLContext* ctx,
	const cl_sampler_properties *sampler_properties, CCLErr** err) {

	/* Make sure err is NULL or it is not set. */
	g_return_val_if_fail((err) == NULL || *(err) == NULL, NULL);
	/* Make sure ctx is not NULL. */
	g_return_val_if_fail(ctx != NULL, NULL);

	/* New sampler wrapper object to create. */
	CCLSampler* smplr = NULL;
	/* OpenCL sampler object to create and wrap. */
	cl_sampler sampler;
	/* OpenCL function status. */
	cl_int ocl_status;

#ifdef CL_VERSION_2_0

	/* OpenCL platform version. */
	double ocl_ver;
	/* Internal error handling object. */
	CCLErr* err_internal = NULL;

	/* Get context platform version. */
	ocl_ver = ccl_context_get_opencl_version(ctx, &err_internal);
	ccl_if_err_propagate_goto(err, err_internal, error_handler);

	/* Create the OpenCL sampler object. */
	if (ocl_ver >= 200) {
		/* Platform is OpenCL >= 2.0, use "new" API. */
		sampler = clCreateSamplerWithProperties(
			ccl_context_unwrap(ctx), sampler_properties, &ocl_status);
	} else {
		/* Platform is OpenCL <= 1.2, use "old" API. */
		struct ccl_sampler_basic_properties sbp =
			ccl_sampler_get_basic_properties(sampler_properties);
		CCL_BEGIN_IGNORE_DEPRECATIONS
		sampler = clCreateSampler(ccl_context_unwrap(ctx),
			sbp.normalized_coords, sbp.addressing_mode, sbp.filter_mode,
			&ocl_status);
		CCL_END_IGNORE_DEPRECATIONS
	}

#else

	/* Create OpenCL sampler object. */
	struct ccl_sampler_basic_properties sbp =
		ccl_sampler_get_basic_properties(sampler_properties);
	sampler = clCreateSampler(ccl_context_unwrap(ctx),
		sbp.normalized_coords, sbp.addressing_mode, sbp.filter_mode,
		&ocl_status);

#endif

	/* Check for errors. */
	ccl_if_err_create_goto(*err, CCL_OCL_ERROR,
		CL_SUCCESS != ocl_status, ocl_status, error_handler,
		"%s: unable to create sampler (OpenCL error %d: %s).",
		CCL_STRD, ocl_status, ccl_err(ocl_status));

	/* Create sampler wrapper. */
	smplr = ccl_sampler_new_wrap(sampler);

	/* If we got here, everything is OK. */
	g_assert(err == NULL || *err == NULL);
	goto finish;

error_handler:

	/* If we got here there was an error, verify that it is so. */
	g_assert(err == NULL || *err != NULL);

finish:

	/* Return sampler wrapper. */
	return smplr;


}