Esempio n. 1
0
/**
 * Tests creation, getting info from and destruction of
 * buffer wrapper objects.
 * */
static void buffer_create_info_destroy_test() {

	/* Test variables. */
	CCLContext* ctx = NULL;
	CCLBuffer* b = NULL;
	GError* err = NULL;
	size_t buf_size = sizeof(cl_uint) * CCL_TEST_BUFFER_SIZE;

	/* Get a context with any device. */
	ctx = ccl_context_new_any(&err);
	g_assert_no_error(err);

	/* Create regular buffer. */
	b = ccl_buffer_new(ctx, CL_MEM_READ_WRITE, buf_size, NULL, &err);
	g_assert_no_error(err);

	/* Get some info and check if the return value is as expected. */
	cl_mem_object_type mot;
	mot = ccl_memobj_get_info_scalar(
		b, CL_MEM_TYPE, cl_mem_object_type, &err);
	g_assert_no_error(err);
	g_assert_cmpuint(mot, ==, CL_MEM_OBJECT_BUFFER);

	cl_mem_flags flags;
	flags = ccl_memobj_get_info_scalar(
		b, CL_MEM_FLAGS, cl_mem_flags, &err);
	g_assert_no_error(err);
	g_assert_cmpuint(flags, ==, CL_MEM_READ_WRITE);

	size_t mem_size;
	mem_size = ccl_memobj_get_info_scalar(b, CL_MEM_SIZE, size_t, &err);
	g_assert_no_error(err);
	g_assert_cmpuint(mem_size, ==, buf_size);

	void* host_ptr = NULL;
	host_ptr = ccl_memobj_get_info_scalar(
		b, CL_MEM_HOST_PTR, void*, &err);
	g_assert((err == NULL) || (err->code == CCL_ERROR_INFO_UNAVAILABLE_OCL));
	g_assert_cmphex((gulong) host_ptr, ==, (gulong) NULL);
	g_clear_error(&err);

	cl_context context;
	context = ccl_memobj_get_info_scalar(
		b, CL_MEM_CONTEXT, cl_context, &err);
	g_assert_no_error(err);
	g_assert_cmphex((gulong) context, ==, (gulong) ccl_context_unwrap(ctx));

	/* 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());

}
Esempio n. 2
0
/**
 * Tests map/unmap operations in buffer objects.
 * */
static void buffer_map_unmap() {

	/* Test variables. */
	CCLContext* ctx = NULL;
	CCLDevice* d = NULL;
	CCLBuffer* b = NULL;
	CCLQueue* q;
	cl_uint h_in[CCL_TEST_BUFFER_SIZE];
	cl_uint* h_out;
	size_t buf_size = sizeof(cl_uint) * CCL_TEST_BUFFER_SIZE;
	GError* err = NULL;

	/* Create a host array, put some stuff in it. */
	for (guint i = 0; i < CCL_TEST_BUFFER_SIZE; ++i)
		h_in[i] = g_test_rand_int();

	/* Get a context with any device. */
	ctx = ccl_context_new_any(&err);
	g_assert_no_error(err);

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

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

	/* Create regular buffer and write data from the host buffer. */
	b = ccl_buffer_new(ctx, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR,
		buf_size, h_in, &err);
	g_assert_no_error(err);

	/* Map buffer onto host memory. */
	h_out = ccl_buffer_enqueue_map(b, q, CL_TRUE, CL_MAP_READ, 0,
		buf_size, NULL, NULL, &err);
	g_assert_no_error(err);

	/* Check data is OK. */
	for (guint i = 0; i < CCL_TEST_BUFFER_SIZE; ++i)
		g_assert_cmpuint(h_in[i], ==, h_out[i]);

	/* Unmap buffer. */
	ccl_memobj_enqueue_unmap((CCLMemObj*) b, q, h_out, NULL, &err);
	g_assert_no_error(err);

	/* Free stuff. */
	ccl_buffer_destroy(b);
	ccl_queue_destroy(q);
	ccl_context_destroy(ctx);

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

}
Esempio n. 3
0
/**
 * Tests buffer wrapper class reference counting.
 * */
static void buffer_ref_unref_test() {

	/* Test variables. */
	CCLContext* ctx = NULL;
	CCLBuffer* b = NULL;
	GError* err = NULL;
	size_t buf_size = sizeof(cl_uint) * CCL_TEST_BUFFER_SIZE;

	/* Get a context with any device. */
	ctx = ccl_context_new_any(&err);
	g_assert_no_error(err);

	/* Create regular buffer. */
	b = ccl_buffer_new(ctx, CL_MEM_READ_WRITE, buf_size, NULL, &err);
	g_assert_no_error(err);

	/* Increase buffer reference count. */
	ccl_memobj_ref(b);

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

	/* Unref buffer. */
	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_unref(b);
	ccl_context_destroy(ctx);

	/* Confirm that memory allocated by wrappers has been properly
	 * freed. */
	g_assert(ccl_wrapper_memcheck());
}
Esempio n. 4
0
/**
 * Test RNG with GID-based device generated seeds.
 * */
static void seed_dev_gid_test() {

	/* Test variables. */
	CCLContext* ctx = NULL;
	CCLDevice* dev = NULL;
	CCLQueue* cq = NULL;
	CCLProgram* prg = NULL;
	CCLKernel* krnl = NULL;
	CCLBuffer* seeds_dev = NULL;
	CCLBuffer* output_dev = NULL;
	GError* err = NULL;
	CloRng* rng = NULL;
	size_t lws = 0;
	size_t ws = CLO_RNG_TEST_NUM_SEEDS;
	gchar* src;

	/* Get context and device. */
	ctx = ccl_context_new_any(&err);
	g_assert_no_error(err);

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

	/* Create command queue. */
	cq = ccl_queue_new(ctx, dev, 0, &err);
	g_assert_no_error(err);

	/* Test all RNGs. */
	for (cl_uint i = 0; clo_rng_infos[i].name != NULL; ++i) {

		/* Create RNG object. */
		rng = clo_rng_new(clo_rng_infos[i].name, CLO_RNG_SEED_DEV_GID,
			NULL, CLO_RNG_TEST_NUM_SEEDS, CLO_RNG_TEST_INIT_SEED,
			CLO_RNG_TEST_HASH, ctx, cq, &err);
		g_assert_no_error(err);

		/* Get RNG seeds device buffer. */
		seeds_dev = clo_rng_get_device_seeds(rng);

		/* Get RNG kernels source. */
		src = g_strconcat(
			clo_rng_get_source(rng), CLO_RNG_TEST_SRC, NULL);

		/* Create and build program. */
		prg = ccl_program_new_from_source(ctx, src, &err);
		g_assert_no_error(err);

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

		/* Create output buffer. */
		output_dev = ccl_buffer_new(ctx, CL_MEM_WRITE_ONLY,
			CLO_RNG_TEST_NUM_SEEDS * sizeof(cl_ulong), NULL, &err);
		g_assert_no_error(err);

		/* Get kernel from program. */
		krnl = ccl_program_get_kernel(prg, CLO_RNG_TEST_KERNEL, &err);
		g_assert_no_error(err);

		/* Get a "nice" local worksize. */
		ccl_kernel_suggest_worksizes(
			krnl, dev, 1, &ws, NULL, &lws, &err);
		g_assert_no_error(err);

		/* Execute kernel. */
		ccl_kernel_set_args_and_enqueue_ndrange(
			krnl, cq, 1, NULL, &ws, &lws, NULL, &err,
			seeds_dev, output_dev, NULL);
		g_assert_no_error(err);

		/* Release this iteration stuff. */
		g_free(src);
		ccl_buffer_destroy(output_dev);
		ccl_program_destroy(prg);
		clo_rng_destroy(rng);

	}

	/* Destroy queue and context. */
	ccl_queue_destroy(cq);
	ccl_context_destroy(ctx);

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

}
Esempio n. 5
0
/**
 * Test RNG with client generated seeds in device.
 * */
static void seed_ext_dev_test() {

	/* Test variables. */
	CCLContext* ctx = NULL;
	CCLDevice* dev = NULL;
	CCLQueue* cq = NULL;
	CCLProgram* prg = NULL;
	CCLKernel* krnl = NULL;
	CCLBuffer* seeds_dev = NULL;
	CCLBuffer* output_dev = NULL;
	GError* err = NULL;
	CloRng* rng = NULL;
	size_t lws = 0;
	size_t ws = CLO_RNG_TEST_NUM_SEEDS;
	gchar* src;
	cl_uchar* host_seeds;

	/* Get context and device. */
	ctx = ccl_context_new_any(&err);
	g_assert_no_error(err);

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

	/* Create command queue. */
	cq = ccl_queue_new(ctx, dev, 0, &err);
	g_assert_no_error(err);

	/* Test all RNGs. */
	for (cl_uint i = 0; clo_rng_infos[i].name != NULL; ++i) {

		/* Host seeds must account for the seed size of current RNG. */
		size_t seed_size =
			clo_rng_infos[i].seed_size * CLO_RNG_TEST_NUM_SEEDS;
		host_seeds = g_slice_alloc(seed_size);

		/* Initialize host seeds with any value. */
		for (cl_uint i = 0; i < seed_size; ++i)
			host_seeds[i] = (cl_uchar) (((i + 1) * 3) & 0xFF);

		/* Allocate memory for device seeds and copy host seeds. */
		seeds_dev = ccl_buffer_new(
			ctx, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, seed_size,
			host_seeds, &err);
		g_assert_no_error(err);

		/* Create RNG object. */
		rng = clo_rng_new(clo_rng_infos[i].name, CLO_RNG_SEED_EXT_DEV,
			seeds_dev, CLO_RNG_TEST_NUM_SEEDS, CLO_RNG_TEST_INIT_SEED,
			NULL, ctx, cq, &err);
		g_assert_no_error(err);

		/* Get RNG kernels source. */
		src = g_strconcat(
			clo_rng_get_source(rng), CLO_RNG_TEST_SRC, NULL);

		/* Create and build program. */
		prg = ccl_program_new_from_source(ctx, src, &err);
		g_assert_no_error(err);

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

		/* Create output buffer. */
		output_dev = ccl_buffer_new(ctx, CL_MEM_WRITE_ONLY,
			CLO_RNG_TEST_NUM_SEEDS * sizeof(cl_ulong), NULL, &err);
		g_assert_no_error(err);

		/* Get kernel from program. */
		krnl = ccl_program_get_kernel(prg, CLO_RNG_TEST_KERNEL, &err);
		g_assert_no_error(err);

		/* Get a "nice" local worksize. */
		ccl_kernel_suggest_worksizes(
			krnl, dev, 1, &ws, NULL, &lws, &err);
		g_assert_no_error(err);

		/* Execute kernel. */
		ccl_kernel_set_args_and_enqueue_ndrange(
			krnl, cq, 1, NULL, &ws, &lws, NULL, &err,
			seeds_dev, output_dev, NULL);
		g_assert_no_error(err);

		/* Release this iteration stuff. */
		g_slice_free1(seed_size, host_seeds);
		g_free(src);
		ccl_buffer_destroy(seeds_dev);
		ccl_buffer_destroy(output_dev);
		ccl_program_destroy(prg);
		clo_rng_destroy(rng);

	}

	/* Destroy queue and context. */
	ccl_queue_destroy(cq);
	ccl_context_destroy(ctx);

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

}
Esempio n. 6
0
/**
 * Tests copy operations from one buffer to another.
 * */
static void buffer_copy() {

	/* Test variables. */
	CCLContext* ctx = NULL;
	CCLDevice* d = NULL;
	CCLBuffer* b1 = NULL;
	CCLBuffer* b2 = NULL;
	CCLQueue* q;
	cl_long h1[CCL_TEST_BUFFER_SIZE];
	cl_long h2[CCL_TEST_BUFFER_SIZE];
	size_t buf_size = sizeof(cl_long) * CCL_TEST_BUFFER_SIZE;
	GError* err = NULL;

	/* Create a host array, put some stuff in it. */
	for (guint i = 0; i < CCL_TEST_BUFFER_SIZE; ++i)
		h1[i] = g_test_rand_int();

	/* Get a context with any device. */
	ctx = ccl_context_new_any(&err);
	g_assert_no_error(err);

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

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

	/* Create regular buffer and write data from the host buffer. */
	b1 = ccl_buffer_new(ctx, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR,
		buf_size, h1, &err);
	g_assert_no_error(err);

	/* Create another buffer, double the size. */
	b2 = ccl_buffer_new(ctx, CL_MEM_READ_WRITE, 2 * buf_size, NULL, &err);
	g_assert_no_error(err);

	/* Copy data from first buffer to second buffer, using an offset on
	 * the second buffer. */
	ccl_buffer_enqueue_copy(
		b1, b2, q, 0, buf_size / 2, buf_size, NULL, &err);
	g_assert_no_error(err);

	/* Read data back to host from the second buffer. */
	ccl_buffer_enqueue_read(b2, q, CL_TRUE, buf_size / 2, buf_size, h2,
		NULL, &err);
	g_assert_no_error(err);

	/* Check data is OK. */
	for (guint i = 0; i < CCL_TEST_BUFFER_SIZE; ++i)
		g_assert_cmpuint(h1[i], ==, h2[i]);


	/* Free stuff. */
	ccl_buffer_destroy(b1);
	ccl_buffer_destroy(b2);
	ccl_queue_destroy(q);
	ccl_context_destroy(ctx);

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

}