From 2df00c65a10b1db8e672b95de467bd944c1db502 Mon Sep 17 00:00:00 2001 From: Kamil-Goras-Mobica <141216953+kamil-goras-mobica@users.noreply.github.com> Date: Tue, 17 Dec 2024 17:44:27 +0100 Subject: [PATCH] Added unique_ptrs and MTdataHolder to test_atomics.cpp (#2169) Fixes https://github.com/KhronosGroup/OpenCL-CTS/issues/1793 according to description --- test_conformance/atomics/test_atomics.cpp | 133 ++++++++++------------ 1 file changed, 61 insertions(+), 72 deletions(-) diff --git a/test_conformance/atomics/test_atomics.cpp b/test_conformance/atomics/test_atomics.cpp index d74cd033..754ec2aa 100644 --- a/test_conformance/atomics/test_atomics.cpp +++ b/test_conformance/atomics/test_atomics.cpp @@ -17,6 +17,7 @@ #include "harness/typeWrappers.h" #include +#include #define INT_TEST_VALUE 402258822 #define LONG_TEST_VALUE 515154531254381446LL @@ -133,12 +134,12 @@ int test_atomic_function(cl_device_id deviceID, cl_context context, int error; size_t threads[1]; clMemWrapper streams[2]; - void *refValues, *startRefValues; + std::vector refValues; + std::vector startRefValues; size_t threadSize, groupSize; const char *programLines[4]; char pragma[512]; char programHeader[512]; - MTdata d; size_t typeSize = get_explicit_type_size(dataType); @@ -244,23 +245,20 @@ int test_atomic_function(cl_device_id deviceID, cl_context context, log_info("\t(thread count %d, group size %d)\n", (int)threadSize, (int)groupSize); - refValues = (cl_int *)malloc(typeSize * threadSize); + refValues.resize(typeSize * threadSize); if (testFns.GenerateRefsIntFn != NULL) { // We have a ref generator provided - d = init_genrand(gRandomSeed); - startRefValues = malloc(typeSize * threadSize); + MTdataHolder d_holder(gRandomSeed); + startRefValues.resize(typeSize * threadSize); if (typeSize == 4) - testFns.GenerateRefsIntFn(threadSize, (cl_int *)startRefValues, d); + testFns.GenerateRefsIntFn( + threadSize, (cl_int *)startRefValues.data(), d_holder); else - testFns.GenerateRefsLongFn(threadSize, (cl_long *)startRefValues, - d); - free_mtdata(d); - d = NULL; + testFns.GenerateRefsLongFn( + threadSize, (cl_long *)startRefValues.data(), d_holder); } - else - startRefValues = NULL; // If we're given a num_results function, we need to determine how many // result objects we need. If we don't have it, we assume it's just 1 @@ -268,28 +266,26 @@ int test_atomic_function(cl_device_id deviceID, cl_context context, ? testFns.NumResultsFn(threadSize, dataType) : 1; - char *destItems = new char[typeSize * numDestItems]; - if (destItems == NULL) - { - log_error("ERROR: Unable to allocate memory!\n"); - return -1; - } + std::vector destItems(typeSize * numDestItems); + void *startValue = (typeSize == 4) ? (void *)&testFns.mIntStartValue : (void *)&testFns.mLongStartValue; for (size_t i = 0; i < numDestItems; i++) - memcpy(destItems + i * typeSize, startValue, typeSize); + memcpy(destItems.data() + i * typeSize, startValue, typeSize); - streams[0] = clCreateBuffer(context, CL_MEM_COPY_HOST_PTR, - typeSize * numDestItems, destItems, NULL); + streams[0] = + clCreateBuffer(context, CL_MEM_COPY_HOST_PTR, typeSize * numDestItems, + destItems.data(), NULL); if (!streams[0]) { log_error("ERROR: Creating output array failed!\n"); return -1; } - streams[1] = clCreateBuffer( - context, - ((startRefValues != NULL ? CL_MEM_COPY_HOST_PTR : CL_MEM_READ_WRITE)), - typeSize * threadSize, startRefValues, NULL); + streams[1] = + clCreateBuffer(context, + ((startRefValues.data() != NULL ? CL_MEM_COPY_HOST_PTR + : CL_MEM_READ_WRITE)), + typeSize * threadSize, startRefValues.data(), NULL); if (!streams[1]) { log_error("ERROR: Creating reference array failed!\n"); @@ -307,7 +303,7 @@ int test_atomic_function(cl_device_id deviceID, cl_context context, error = clSetKernelArg(kernel, 2, typeSize * numDestItems, NULL); test_error(error, "Unable to set indexed local kernel argument"); - cl_int numDestItemsInt = (cl_int)numDestItems; + cl_int numDestItemsInt = numDestItems; error = clSetKernelArg(kernel, 3, sizeof(cl_int), &numDestItemsInt); test_error(error, "Unable to set indexed kernel argument"); } @@ -320,12 +316,12 @@ int test_atomic_function(cl_device_id deviceID, cl_context context, error = clEnqueueReadBuffer(queue, streams[0], true, 0, typeSize * numDestItems, - destItems, 0, NULL, NULL); + destItems.data(), 0, NULL, NULL); test_error(error, "Unable to read result value!"); error = clEnqueueReadBuffer(queue, streams[1], true, 0, typeSize * threadSize, - refValues, 0, NULL, NULL); + refValues.data(), 0, NULL, NULL); test_error(error, "Unable to read reference values!"); // If we have an expectedFn, then we need to generate a final value to @@ -342,27 +338,29 @@ int test_atomic_function(cl_device_id deviceID, cl_context context, { // Int version intVal = testFns.ExpectedValueIntFn( - threadSize, (cl_int *)startRefValues, i); + threadSize, (cl_int *)startRefValues.data(), i); memcpy(expected, &intVal, sizeof(intVal)); } else { // Long version longVal = testFns.ExpectedValueLongFn( - threadSize, (cl_long *)startRefValues, i); + threadSize, (cl_long *)startRefValues.data(), i); memcpy(expected, &longVal, sizeof(longVal)); } - if (memcmp(expected, destItems + i * typeSize, typeSize) != 0) + if (memcmp(expected, destItems.data() + i * typeSize, typeSize) + != 0) { if (typeSize == 4) { - cl_int *outValue = (cl_int *)(destItems + i * typeSize); + cl_int *outValue = + (cl_int *)(destItems.data() + i * typeSize); log_error("ERROR: Result %zu from kernel does not " "validate! (should be %d, was %d)\n", i, intVal, *outValue); - cl_int *startRefs = (cl_int *)startRefValues; - cl_int *refs = (cl_int *)refValues; + cl_int *startRefs = (cl_int *)startRefValues.data(); + cl_int *refs = (cl_int *)refValues.data(); for (i = 0; i < threadSize; i++) { if (startRefs != NULL) @@ -374,13 +372,14 @@ int test_atomic_function(cl_device_id deviceID, cl_context context, } else { - cl_long *outValue = (cl_long *)(destItems + i * typeSize); + cl_long *outValue = + (cl_long *)(destItems.data() + i * typeSize); log_error("ERROR: Result %zu from kernel does not " "validate! (should be %" PRId64 ", was %" PRId64 ")\n", i, longVal, *outValue); - cl_long *startRefs = (cl_long *)startRefValues; - cl_long *refs = (cl_long *)refValues; + cl_long *startRefs = (cl_long *)startRefValues.data(); + cl_long *refs = (cl_long *)refValues.data(); for (i = 0; i < threadSize; i++) { if (startRefs != NULL) @@ -400,9 +399,9 @@ int test_atomic_function(cl_device_id deviceID, cl_context context, /* Use the verify function to also check the results */ if (dataType == kFloat) { - cl_float *outValue = (cl_float *)destItems; - if (!testFns.VerifyRefsFloatFn(threadSize, (cl_float *)refValues, - *outValue) + cl_float *outValue = (cl_float *)destItems.data(); + if (!testFns.VerifyRefsFloatFn( + threadSize, (cl_float *)refValues.data(), *outValue) != 0) { log_error("ERROR: Reference values did not validate!\n"); @@ -411,8 +410,8 @@ int test_atomic_function(cl_device_id deviceID, cl_context context, } else if (typeSize == 4) { - cl_int *outValue = (cl_int *)destItems; - if (!testFns.VerifyRefsIntFn(threadSize, (cl_int *)refValues, + cl_int *outValue = (cl_int *)destItems.data(); + if (!testFns.VerifyRefsIntFn(threadSize, (cl_int *)refValues.data(), *outValue) != 0) { @@ -422,9 +421,9 @@ int test_atomic_function(cl_device_id deviceID, cl_context context, } else { - cl_long *outValue = (cl_long *)destItems; - if (!testFns.VerifyRefsLongFn(threadSize, (cl_long *)refValues, - *outValue) + cl_long *outValue = (cl_long *)destItems.data(); + if (!testFns.VerifyRefsLongFn( + threadSize, (cl_long *)refValues.data(), *outValue) != 0) { log_error("ERROR: Reference values did not validate!\n"); @@ -442,10 +441,10 @@ int test_atomic_function(cl_device_id deviceID, cl_context context, /* Re-write the starting value */ for (size_t i = 0; i < numDestItems; i++) - memcpy(destItems + i * typeSize, startValue, typeSize); - error = - clEnqueueWriteBuffer(queue, streams[0], true, 0, - typeSize * numDestItems, destItems, 0, NULL, NULL); + memcpy(destItems.data() + i * typeSize, startValue, typeSize); + error = clEnqueueWriteBuffer(queue, streams[0], true, 0, + typeSize * numDestItems, destItems.data(), 0, + NULL, NULL); test_error(error, "Unable to write starting values!"); /* Run the kernel once for a single thread, so we can verify that the @@ -455,16 +454,16 @@ int test_atomic_function(cl_device_id deviceID, cl_context context, NULL, NULL); test_error(error, "Unable to execute test kernel"); - error = clEnqueueReadBuffer(queue, streams[1], true, 0, typeSize, refValues, - 0, NULL, NULL); + error = clEnqueueReadBuffer(queue, streams[1], true, 0, typeSize, + refValues.data(), 0, NULL, NULL); test_error(error, "Unable to read reference values!"); - if (memcmp(refValues, destItems, typeSize) != 0) + if (memcmp(refValues.data(), destItems.data(), typeSize) != 0) { if (typeSize == 4) { - cl_int *s = (cl_int *)destItems; - cl_int *r = (cl_int *)refValues; + cl_int *s = (cl_int *)destItems.data(); + cl_int *r = (cl_int *)refValues.data(); log_error("ERROR: atomic function operated correctly but did NOT " "return correct 'old' value " " (should have been %d, returned %d)!\n", @@ -472,8 +471,8 @@ int test_atomic_function(cl_device_id deviceID, cl_context context, } else { - cl_long *s = (cl_long *)destItems; - cl_long *r = (cl_long *)refValues; + cl_long *s = (cl_long *)destItems.data(); + cl_long *r = (cl_long *)refValues.data(); log_error("ERROR: atomic function operated correctly but did NOT " "return correct 'old' value " " (should have been %" PRId64 ", returned %" PRId64 @@ -483,10 +482,6 @@ int test_atomic_function(cl_device_id deviceID, cl_context context, return -1; } - delete[] destItems; - free(refValues); - if (startRefValues != NULL) free(startRefValues); - return 0; } @@ -654,12 +649,11 @@ bool test_atomic_xchg_verify_int(size_t size, cl_int *refValues, { /* For xchg, each value from 0 to size - 1 should have an entry in the ref * array, and ONLY one entry */ - char *valids; + std::vector valids(sizeof(char) * size); size_t i; char originalValidCount = 0; - valids = (char *)malloc(sizeof(char) * size); - memset(valids, 0, sizeof(char) * size); + memset(valids.data(), 0, sizeof(char) * size); for (i = 0; i < size; i++) { @@ -710,7 +704,6 @@ bool test_atomic_xchg_verify_int(size_t size, cl_int *refValues, } } - free(valids); return true; } @@ -719,12 +712,11 @@ bool test_atomic_xchg_verify_long(size_t size, cl_long *refValues, { /* For xchg, each value from 0 to size - 1 should have an entry in the ref * array, and ONLY one entry */ - char *valids; + std::vector valids(sizeof(char) * size); size_t i; char originalValidCount = 0; - valids = (char *)malloc(sizeof(char) * size); - memset(valids, 0, sizeof(char) * size); + memset(valids.data(), 0, sizeof(char) * size); for (i = 0; i < size; i++) { @@ -777,7 +769,6 @@ bool test_atomic_xchg_verify_long(size_t size, cl_long *refValues, } } - free(valids); return true; } @@ -786,12 +777,11 @@ bool test_atomic_xchg_verify_float(size_t size, cl_float *refValues, { /* For xchg, each value from 0 to size - 1 should have an entry in the ref * array, and ONLY one entry */ - char *valids; + std::vector valids(sizeof(char) * size); size_t i; char originalValidCount = 0; - valids = (char *)malloc(sizeof(char) * size); - memset(valids, 0, sizeof(char) * size); + memset(valids.data(), 0, sizeof(char) * size); for (i = 0; i < size; i++) { @@ -843,7 +833,6 @@ bool test_atomic_xchg_verify_float(size_t size, cl_float *refValues, } } - free(valids); return true; }