cl: Add tests for the behavior of buffers mapped for read

Submitted by Bruno Jimenez on May 29, 2014, 5:39 p.m.

Details

Message ID 1401385165-2398-1-git-send-email-brunojimen@gmail.com
State New
Headers show

Not browsing as part of any series.

Commit Message

Bruno Jimenez May 29, 2014, 5:39 p.m.
According to the OpenCL 1.2 spec, it is allowed to read from a
buffer even if it is mapped for reading.

In fact, while it is mapped for reading you can:
- Create new maps that read from the same map.
- Read from it using commands like 'clEnqueueReadBuffer'
    or 'clEnqueueCopyBuffer'
- Launch kernels that read from it.

This test checks that a buffer mapped for reading can be copied
and that it retains it's contents after a kernel has read from it
---
 tests/cl.py                             |   1 +
 tests/cl/custom/CMakeLists.cl.txt       |   1 +
 tests/cl/custom/behavior_of_read_maps.c | 148 ++++++++++++++++++++++++++++++++
 3 files changed, 150 insertions(+)
 create mode 100644 tests/cl/custom/behavior_of_read_maps.c

Patch hide | download patch | download mbox

diff --git a/tests/cl.py b/tests/cl.py
index e41d115..b3382b6 100644
--- a/tests/cl.py
+++ b/tests/cl.py
@@ -45,6 +45,7 @@  add_plain_test(custom, 'Run simple kernel', ['cl-custom-run-simple-kernel'])
 add_plain_test(custom, 'Flush after enqueue kernel', ['cl-custom-flush-after-enqueue-kernel'])
 add_plain_test(custom, 'r600 create release buffer bug', ['cl-custom-r600-create-release-buffer-bug'])
 add_plain_test(custom, 'r600 mapping bug', ['cl-custom-r600-mapping-bug'])
+add_plain_test(custom, 'behavior of read maps', ['cl-custom-behavior-of-read-maps'])
 add_plain_test(custom, 'Buffer flags', ['cl-custom-buffer-flags'])
 
 # API
diff --git a/tests/cl/custom/CMakeLists.cl.txt b/tests/cl/custom/CMakeLists.cl.txt
index c4ebc00..801f4d6 100644
--- a/tests/cl/custom/CMakeLists.cl.txt
+++ b/tests/cl/custom/CMakeLists.cl.txt
@@ -4,3 +4,4 @@  piglit_cl_add_custom_test (r600-create-release-buffer-bug r600-create-release-bu
 piglit_cl_add_custom_test (buffer-flags buffer-flags.c)
 piglit_cl_add_custom_test (use-sub-buffer-in-kernel use-sub-buffer-in-kernel.c)
 piglit_cl_add_custom_test (r600-mapping-bug r600-mapping-bug.c)
+piglit_cl_add_custom_test (behavior-of-read-maps behavior_of_read_maps.c)
diff --git a/tests/cl/custom/behavior_of_read_maps.c b/tests/cl/custom/behavior_of_read_maps.c
new file mode 100644
index 0000000..6e4ac94
--- /dev/null
+++ b/tests/cl/custom/behavior_of_read_maps.c
@@ -0,0 +1,148 @@ 
+/*
+ * Copyright 2014 Bruno Jiménez
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ * Authors: Bruno Jiménez <brunojimen@gmail.com>
+ *
+ */
+
+#include "piglit-framework-cl-custom.h"
+
+PIGLIT_CL_CUSTOM_TEST_CONFIG_BEGIN
+
+	config.name = "Check for undefined behavior of read maps";
+	config.run_per_device = true;
+
+PIGLIT_CL_CUSTOM_TEST_CONFIG_END
+
+char *source =
+	"kernel void test(global uint *in, global uint *out) { \n"
+	"	int gid = get_global_id(0); \n"
+	"	out[gid] = in[gid]; \n"
+	"}";
+
+enum piglit_result
+piglit_cl_test(const int argc,
+			   const char **argv,
+			   const struct piglit_cl_custom_test_config *config,
+			   const struct piglit_cl_custom_test_env *env)
+{
+	unsigned int i;
+	cl_int err;
+	cl_mem buffer0, buffer1;
+	uint32_t local_data0[16];
+	uint32_t local_data1[16];
+
+	uint32_t *map0;
+
+	piglit_cl_context context = NULL;
+	cl_command_queue queue;
+
+	cl_program program = NULL;
+	cl_kernel kernel = NULL;
+
+	size_t global_size = 16;
+	size_t local_size = 1;
+
+	context = piglit_cl_create_context(env->platform_id, &env->device_id, 1);
+	queue = context->command_queues[0];
+
+	buffer0 = piglit_cl_create_buffer(context, CL_MEM_READ_WRITE,
+				sizeof(local_data0));
+	buffer1 = piglit_cl_create_buffer(context, CL_MEM_READ_WRITE,
+				sizeof(local_data1));
+
+	/* First, we copy some known data to buffer0, which will be the
+	 * buffer mapped for read */
+
+	memset(local_data0, 0x55, sizeof(local_data0));
+	piglit_cl_write_whole_buffer(queue, buffer0, local_data0);
+
+	map0 = clEnqueueMapBuffer(queue, buffer0, CL_TRUE, CL_MAP_READ,
+				0, sizeof(local_data0), 0, NULL, NULL, &err);
+	if (err != CL_SUCCESS)
+		return PIGLIT_FAIL;
+
+	/* OpenCL 1.2 allows a buffer to be read even if it is mapped for read,
+	 * for example by copying the buffer */
+
+	err = clEnqueueCopyBuffer(queue, buffer0, buffer1, 0, 0, sizeof(local_data0),
+			0, NULL, NULL);
+	if (err != CL_SUCCESS)
+		return PIGLIT_FAIL;
+
+	piglit_cl_read_whole_buffer(queue, buffer1, local_data1);
+	for (i = 0; i < 16; i++) {
+		if(local_data1[i] != 0x55555555) {
+			return PIGLIT_FAIL;
+		}
+	}
+
+	/* The kernel we will try will copy data from buffer0 to buffer1,
+	 * so first we make sure that buffer1 has garbage */
+	memset(local_data1, 0x00, sizeof(local_data1));
+	piglit_cl_write_whole_buffer(queue, buffer1, local_data1);
+
+	/* NOTE: we can even leave a buffer mapped for read and launch a kernel
+	 * that reads from it! */
+	program = piglit_cl_build_program_with_source(context, 1, &source, "");
+	kernel = piglit_cl_create_kernel(program, "test");
+
+	piglit_cl_set_kernel_buffer_arg(kernel, 0, &buffer0);
+	piglit_cl_set_kernel_buffer_arg(kernel, 1, &buffer1);
+
+	piglit_cl_execute_ND_range_kernel(queue, kernel, 1,
+			&global_size, &local_size);
+
+	/* Supposedly, the kernel would have copied the data from buffer0
+	 * to buffer1 too. */
+	piglit_cl_read_whole_buffer(queue, buffer1, local_data1);
+	for (i = 0; i < 16; i++) {
+		if(local_data1[i] != 0x55555555) {
+			return PIGLIT_FAIL;
+		}
+	}
+
+	/* Finally, we check the mapping we have done at the very begining.
+	 * Also, we 'remap' the buffer and check for the data */
+	for (i = 0; i < 16; i++) {
+		if(map0[i] != 0x55555555) {
+			return PIGLIT_FAIL;
+		}
+	}
+
+	err = clEnqueueUnmapMemObject(queue, buffer0, map0, 0, NULL, NULL);
+	if (err != CL_SUCCESS)
+		return PIGLIT_FAIL;
+
+	map0 = clEnqueueMapBuffer(queue, buffer0, CL_TRUE, CL_MAP_READ,
+				0, sizeof(local_data0), 0, NULL, NULL, &err);
+	if (err != CL_SUCCESS)
+		return PIGLIT_FAIL;
+
+	for (i = 0; i < 16; i++) {
+		if(map0[i] != 0x55555555) {
+			return PIGLIT_FAIL;
+		}
+	}
+
+	return PIGLIT_PASS;
+}