/** * @internal * * @brief Tests the ccl_buffer_new_from_region() function. * */ static void create_from_region_test() { /* Test variables. */ CCLContext * ctx = NULL; CCLDevice * dev = NULL; CCLQueue * cq = NULL; CCLBuffer * buf = NULL; CCLBuffer * subbuf = NULL; CCLEvent * evt = NULL; CCLEventWaitList ewl = NULL; CCLErr * err = NULL; cl_ulong * hbuf; cl_ulong * hsubbuf; cl_uint min_align; size_t siz_buf; size_t siz_subbuf; /* Get the test context with the pre-defined device. */ ctx = ccl_test_context_new(&err); g_assert_no_error(err); /* Get first device in context. */ dev = ccl_context_get_device(ctx, 0, &err); g_assert_no_error(err); /* Get minimum alignment for sub-buffer in bits. */ min_align = ccl_device_get_info_scalar( dev, CL_DEVICE_MEM_BASE_ADDR_ALIGN, cl_uint, &err); g_assert_no_error(err); /* Determine buffer and sub-buffer sizes (divide by 64 because its * the number of bits in cl_ulong). */ siz_subbuf = sizeof(cl_ulong) * min_align / 64; siz_buf = 4 * siz_subbuf; /* Allocate memory for host buffer and host sub-buffer. */ hbuf = g_slice_alloc(siz_buf); hsubbuf = g_slice_alloc(siz_subbuf); /* Initialize initial host buffer. */ for (cl_uint i = 0; i < siz_buf / sizeof(cl_ulong); ++i) hbuf[i] = g_test_rand_int(); /* Create a command queue. */ cq = ccl_queue_new(ctx, dev, 0, &err); g_assert_no_error(err); /* Create a regular buffer, put some data in it. */ buf = ccl_buffer_new( ctx, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, siz_buf, hbuf, &err); g_assert_no_error(err); /* Create sub-buffer from indexes 16 to 31 (16 positions) of * original buffer. */ subbuf = ccl_buffer_new_from_region( buf, 0, siz_subbuf, siz_subbuf, &err); g_assert_no_error(err); /* Get data in sub-buffer to a new host buffer. */ evt = ccl_buffer_enqueue_read( subbuf, cq, CL_FALSE, 0, siz_subbuf, hsubbuf, NULL, &err); g_assert_no_error(err); /* Wait for read to be complete. */ ccl_event_wait(ccl_ewl(&ewl, evt, NULL), &err); g_assert_no_error(err); /* Check that expected values were successfully read. */ for (cl_uint i = 0; i < siz_subbuf / sizeof(cl_ulong); ++i) g_assert_cmpuint( hsubbuf[i], ==, hbuf[i + siz_subbuf / sizeof(cl_ulong)]); /* Destroy stuff. */ ccl_buffer_destroy(buf); ccl_buffer_destroy(subbuf); ccl_queue_destroy(cq); ccl_context_destroy(ctx); g_slice_free1(siz_buf, hbuf); g_slice_free1(siz_subbuf, hsubbuf); /* Confirm that memory allocated by wrappers has been properly * freed. */ g_assert(ccl_wrapper_memcheck()); }
/** * Tests creation, getting info from and destruction of * profiler objects, and their relationship with context, device and * queue wrapper objects. * */ static void create_add_destroy_test() { /* Test variables. */ CCLErr* err = NULL; CCLBuffer* buf1 = NULL; CCLBuffer* buf2 = NULL; CCLProf* prof = NULL; CCLContext* ctx = NULL; CCLDevice* d = NULL; CCLQueue* cq1 = NULL; CCLQueue* cq2 = NULL; CCLEvent* evt = NULL; CCLEventWaitList ewl = NULL; size_t buf_size = 8 * sizeof(cl_short); cl_short hbuf[8] = {1, 2, 3, 4, 5, 6, 7, 8}; cl_ulong duration, eff_duration; double time_elapsed; /* Create a new profile object. */ prof = ccl_prof_new(); /* Get a context and a device. */ ctx = ccl_test_context_new(&err); g_assert_no_error(err); d = ccl_context_get_device(ctx, 0, &err); g_assert_no_error(err); /* Create two command queue wrappers. */ cq1 = ccl_queue_new(ctx, d, CL_QUEUE_PROFILING_ENABLE, &err); g_assert_no_error(err); cq2 = ccl_queue_new(ctx, d, CL_QUEUE_PROFILING_ENABLE, &err); g_assert_no_error(err); /* Create device buffers. */ buf1 = ccl_buffer_new(ctx, CL_MEM_READ_ONLY, buf_size, NULL, &err); g_assert_no_error(err); buf2 = ccl_buffer_new(ctx, CL_MEM_READ_WRITE, buf_size, NULL, &err); g_assert_no_error(err); /* Start profile object timer. */ ccl_prof_start(prof); /* Transfer data to buffer. */ evt = ccl_buffer_enqueue_write( buf1, cq1, CL_FALSE, 0, buf_size, hbuf, NULL, &err); g_assert_no_error(err); /* Transfer data from one buffer to another. */ evt = ccl_buffer_enqueue_copy(buf1, buf2, cq2, 0, 0, buf_size, ccl_ewl(&ewl, evt, NULL), &err); g_assert_no_error(err); /* Wait for copy. */ ccl_event_wait(ccl_ewl(&ewl, evt, NULL), &err); g_assert_no_error(err); /* Stop profile object timer. */ ccl_prof_stop(prof); /* Add both queues to profile object. */ ccl_prof_add_queue(prof, "A Queue", cq1); ccl_prof_add_queue(prof, "Another Queue", cq2); /* Process queues. */ ccl_prof_calc(prof, &err); g_assert_no_error(err); /* Request some profiling information. */ time_elapsed = ccl_prof_time_elapsed(prof); duration = ccl_prof_get_duration(prof); eff_duration = ccl_prof_get_eff_duration(prof); g_debug("Profiling time elapsed: %lf", time_elapsed); g_debug("Profiling duration: %d", (cl_int) duration); g_debug("Profiling eff. duration: %d", (cl_int) eff_duration); /* Destroy buffers. */ ccl_buffer_destroy(buf1); ccl_buffer_destroy(buf2); /* Unref cq1, which should not be destroyed because it is held * by the profile object. */ ccl_queue_destroy(cq1); /* Destroy the profile object, which will also destroy cq1. cq2 * will me merely unrefed and must still be explicitly destroyed. */ ccl_prof_destroy(prof); /* Destroy cq2. */ ccl_queue_destroy(cq2); /* Destroy the context. */ ccl_context_destroy(ctx); /* Confirm that memory allocated by wrappers has been properly * freed. */ g_assert(ccl_wrapper_memcheck()); }