Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor buffer ReadWrite and Copy tests #2259

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 38 additions & 23 deletions test_conformance/basic/test_arrayreadwrite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,44 @@
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <vector>


#include "procs.h"


int
test_arrayreadwrite(cl_device_id device, cl_context context, cl_command_queue queue, int num_elements)
static int test_arrayreadwrite_impl(cl_device_id device, cl_context context,
cl_command_queue queue, int num_elements,
cl_mem_flags flags)
{
cl_uint *inptr, *outptr;
cl_mem streams[1];
clMemWrapper buffer;
int num_tries = 400;
num_elements = 1024 * 1024 * 4;
int i, j, err;
MTdata d;
MTdataHolder d(gRandomSeed);

inptr = (cl_uint*)malloc(num_elements*sizeof(cl_uint));
outptr = (cl_uint*)malloc(num_elements*sizeof(cl_uint));
std::vector<cl_uint> reference_ptr(num_elements);
std::vector<cl_uint> inptr(num_elements);
std::vector<cl_uint> outptr(num_elements);

// randomize data
d = init_genrand( gRandomSeed );
for (i=0; i<num_elements; i++)
for (int i = 0; i < num_elements; i++)
{
inptr[i] = (cl_uint)(genrand_int32(d) & 0x7FFFFFFF);
reference_ptr[i] = (cl_uint)(genrand_int32(d) & 0x7FFFFFFF);
}

void* host_ptr = nullptr;
if ((flags & CL_MEM_USE_HOST_PTR) || (flags & CL_MEM_COPY_HOST_PTR))
{
host_ptr = inptr.data();
}

streams[0] = clCreateBuffer(context, CL_MEM_READ_WRITE,
sizeof(cl_uint) * num_elements, NULL, &err);
cl_int err = CL_SUCCESS;
buffer = clCreateBuffer(context, flags, sizeof(cl_uint) * num_elements,
host_ptr, &err);
test_error(err, "clCreateBuffer failed");

for (i=0; i<num_tries; i++)
for (int i = 0; i < num_tries; i++)
{
int offset;
int cb;
Expand All @@ -61,15 +71,20 @@ test_arrayreadwrite(cl_device_id device, cl_context context, cl_command_queue qu
if (cb > (num_elements - offset))
cb = num_elements - offset;

err = clEnqueueWriteBuffer(queue, streams[0], CL_TRUE, offset*sizeof(cl_uint), sizeof(cl_uint)*cb,&inptr[offset], 0, NULL, NULL);
err = clEnqueueWriteBuffer(
queue, buffer, CL_TRUE, offset * sizeof(cl_uint),
sizeof(cl_uint) * cb, &reference_ptr[offset], 0, nullptr, nullptr);
test_error(err, "clEnqueueWriteBuffer failed");

err = clEnqueueReadBuffer( queue, streams[0], CL_TRUE, offset*sizeof(cl_uint), cb*sizeof(cl_uint), &outptr[offset], 0, NULL, NULL );
err = clEnqueueReadBuffer(
queue, buffer, CL_TRUE, offset * sizeof(cl_uint),
cb * sizeof(cl_uint), &outptr[offset], 0, nullptr, nullptr);
test_error(err, "clEnqueueReadBuffer failed");

for (j=offset; j<offset+cb; j++)
const cl_uint* expected_buffer_values = reference_ptr.data();
for (int j = offset; j < offset + cb; j++)
{
if (inptr[j] != outptr[j])
if (expected_buffer_values[j] != outptr[j])
{
log_error("ARRAY read, write test failed\n");
err = -1;
Expand All @@ -81,16 +96,16 @@ test_arrayreadwrite(cl_device_id device, cl_context context, cl_command_queue qu
break;
}

free_mtdata(d);
clReleaseMemObject(streams[0]);
free(inptr);
free(outptr);

if (!err)
log_info("ARRAY read, write test passed\n");

return err;
}



int test_arrayreadwrite(cl_device_id device, cl_context context,
cl_command_queue queue, int num_elements)
{
return test_arrayreadwrite_impl(device, context, queue, num_elements,
CL_MEM_READ_WRITE);
}
42 changes: 34 additions & 8 deletions test_conformance/basic/test_bufferreadwriterect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,9 +344,22 @@ void CL_CALLBACK mem_obj_destructor_callback( cl_mem, void *data )
free( data );
}

using test_fn = int (*)(size_t, size_t[3], size_t[3], size_t, size_t[3],
size_t[3]);
struct TestFunctions
{
test_fn copy;
test_fn read;
test_fn write;
};

// This is the main test function for the conformance test.
int
test_bufferreadwriterect(cl_device_id device, cl_context context, cl_command_queue queue_, int num_elements)
static int test_bufferreadwriterect_impl(cl_device_id device,
cl_context context,
cl_command_queue queue_,
int num_elements,
cl_map_flags buffer_flags,
const TestFunctions& test_functions)
{
queue = queue_;
cl_int err;
Expand Down Expand Up @@ -436,7 +449,8 @@ test_bufferreadwriterect(cl_device_id device, cl_context context, cl_command_que
memcpy(backing[i], verify[i], size_bytes);

// Create the CL buffer.
buffer[i] = clCreateBuffer (context, CL_MEM_USE_HOST_PTR | CL_MEM_READ_WRITE, size_bytes, backing[i], &err);
buffer[i] =
clCreateBuffer(context, buffer_flags, size_bytes, backing[i], &err);
CL_EXIT_ERROR(err,"clCreateBuffer failed for buffer %u", i);

// Make sure buffer is cleaned up appropriately if we encounter an error in the rest of the calls.
Expand Down Expand Up @@ -501,7 +515,8 @@ test_bufferreadwriterect(cl_device_id device, cl_context context, cl_command_que
doffset[0], doffset[1], doffset[2], sregion[0],
sregion[1], sregion[2],
sregion[0] * sregion[1] * sregion[2]);
if ((err = copy_region(src, soffset, sregion, dst, doffset, dregion)))
if ((err = test_functions.copy(src, soffset, sregion, dst,
doffset, dregion)))
return err;
break;
case 1:
Expand All @@ -511,7 +526,8 @@ test_bufferreadwriterect(cl_device_id device, cl_context context, cl_command_que
doffset[0], doffset[1], doffset[2], sregion[0],
sregion[1], sregion[2],
sregion[0] * sregion[1] * sregion[2]);
if ((err = read_verify_region(src, soffset, sregion, dst, doffset, dregion)))
if ((err = test_functions.read(src, soffset, sregion, dst,
doffset, dregion)))
return err;
break;
case 2:
Expand All @@ -521,7 +537,8 @@ test_bufferreadwriterect(cl_device_id device, cl_context context, cl_command_que
doffset[0], doffset[1], doffset[2], sregion[0],
sregion[1], sregion[2],
sregion[0] * sregion[1] * sregion[2]);
if ((err = write_region(src, soffset, sregion, dst, doffset, dregion)))
if ((err = test_functions.write(src, soffset, sregion, dst,
doffset, dregion)))
return err;
break;
}
Expand Down Expand Up @@ -565,5 +582,14 @@ test_bufferreadwriterect(cl_device_id device, cl_context context, cl_command_que
return err;
}



int test_bufferreadwriterect(cl_device_id device, cl_context context,
cl_command_queue queue_, int num_elements)
{
TestFunctions test_functions;
test_functions.copy = copy_region;
test_functions.read = read_verify_region;
test_functions.write = write_region;
return test_bufferreadwriterect_impl(
device, context, queue_, num_elements,
CL_MEM_USE_HOST_PTR | CL_MEM_READ_WRITE, test_functions);
}
Loading
Loading