diff --git a/test_conformance/basic/CMakeLists.txt b/test_conformance/basic/CMakeLists.txt index 3f84d7a4..d73b84a2 100644 --- a/test_conformance/basic/CMakeLists.txt +++ b/test_conformance/basic/CMakeLists.txt @@ -3,8 +3,7 @@ set(MODULE_NAME BASIC) set(${MODULE_NAME}_SOURCES main.cpp test_fpmath_float.cpp test_fpmath_float2.cpp test_fpmath_float4.cpp - test_intmath_int.cpp test_intmath_int2.cpp test_intmath_int4.cpp - test_intmath_long.cpp test_intmath_long2.cpp test_intmath_long4.cpp + test_intmath.cpp test_hiloeo.cpp test_local.cpp test_pointercast.cpp test_if.cpp test_loop.cpp test_readimage.cpp test_readimage_int16.cpp test_readimage_fp32.cpp diff --git a/test_conformance/basic/test_intmath.cpp b/test_conformance/basic/test_intmath.cpp new file mode 100644 index 00000000..6fd41abb --- /dev/null +++ b/test_conformance/basic/test_intmath.cpp @@ -0,0 +1,240 @@ +// +// Copyright (c) 2020 The Khronos Group Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#include +#include +#include + +#include "procs.h" + +template struct TestDef +{ + const char *name; + const char *kernel_code; + std::function ref; +}; + +template +int test_intmath(cl_device_id device, cl_context context, + cl_command_queue queue, int num_elements, std::string typestr) +{ + TestDef tests[] = { + // Test addition + { + "test_add", + R"( + __kernel void test_add(__global TYPE *srcA, + __global TYPE *srcB, + __global TYPE *srcC, + __global TYPE *dst) + { + int tid = get_global_id(0); + dst[tid] = srcA[tid] + srcB[tid]; + }; +)", + [](T a, T b, T c) { return a + b; }, + }, + + // Test subtraction + { + "test_sub", + R"( + __kernel void test_sub(__global TYPE *srcA, + __global TYPE *srcB, + __global TYPE *srcC, + __global TYPE *dst) + { + int tid = get_global_id(0); + dst[tid] = srcA[tid] - srcB[tid]; + }; +)", + [](T a, T b, T c) { return a - b; }, + }, + + // Test multiplication + { + "test_mul", + R"( + __kernel void test_mul(__global TYPE *srcA, + __global TYPE *srcB, + __global TYPE *srcC, + __global TYPE *dst) + { + int tid = get_global_id(0); + dst[tid] = srcA[tid] * srcB[tid]; + }; +)", + [](T a, T b, T c) { return a * b; }, + }, + + // Test multiply-accumulate + { + "test_mad", + R"( + __kernel void test_mad(__global TYPE *srcA, + __global TYPE *srcB, + __global TYPE *srcC, + __global TYPE *dst) + { + int tid = get_global_id(0); + dst[tid] = srcA[tid] * srcB[tid] + srcC[tid]; + }; +)", + [](T a, T b, T c) { return a * b + c; }, + }, + }; + + clMemWrapper streams[4]; + cl_int err; + + if (std::is_same::value && !gHasLong) + { + log_info("64-bit integers are not supported on this device. Skipping " + "test.\n"); + return TEST_SKIPPED_ITSELF; + } + + // Create host buffers and fill with random data. + std::vector inputA(num_elements * N); + std::vector inputB(num_elements * N); + std::vector inputC(num_elements * N); + std::vector output(num_elements * N); + MTdataHolder d(gRandomSeed); + for (int i = 0; i < num_elements; i++) + { + inputA[i] = (T)genrand_int64(d); + inputB[i] = (T)genrand_int64(d); + inputC[i] = (T)genrand_int64(d); + } + + size_t datasize = sizeof(T) * num_elements * N; + + // Create device buffers. + for (int i = 0; i < ARRAY_SIZE(streams); i++) + { + streams[i] = + clCreateBuffer(context, CL_MEM_READ_WRITE, datasize, NULL, &err); + test_error(err, "clCreateBuffer failed"); + } + + // Copy input data to device. + err = clEnqueueWriteBuffer(queue, streams[0], CL_TRUE, 0, datasize, + inputA.data(), 0, NULL, NULL); + test_error(err, "clEnqueueWriteBuffer failed\n"); + err = clEnqueueWriteBuffer(queue, streams[1], CL_TRUE, 0, datasize, + inputB.data(), 0, NULL, NULL); + test_error(err, "clEnqueueWriteBuffer failed\n"); + err = clEnqueueWriteBuffer(queue, streams[2], CL_TRUE, 0, datasize, + inputC.data(), 0, NULL, NULL); + test_error(err, "clEnqueueWriteBuffer failed\n"); + + std::string build_options = "-DTYPE="; + build_options += typestr; + + // Run test for each operation + for (auto test : tests) + { + log_info("%s... ", test.name); + + // Create kernel and set args + clProgramWrapper program; + clKernelWrapper kernel; + err = create_single_kernel_helper(context, &program, &kernel, 1, + &test.kernel_code, test.name, + build_options.c_str()); + test_error(err, "create_single_kernel_helper failed\n"); + + err = clSetKernelArg(kernel, 0, sizeof(cl_mem), &streams[0]); + err |= clSetKernelArg(kernel, 1, sizeof(cl_mem), &streams[1]); + err |= clSetKernelArg(kernel, 2, sizeof(cl_mem), &streams[2]); + err |= clSetKernelArg(kernel, 3, sizeof(cl_mem), &streams[3]); + test_error(err, "clSetKernelArgs failed\n"); + + // Run kernel + size_t threads[1] = { static_cast(num_elements) }; + err = clEnqueueNDRangeKernel(queue, kernel, 1, NULL, threads, NULL, 0, + NULL, NULL); + test_error(err, "clEnqueueNDRangeKernel failed\n"); + + // Read results + err = clEnqueueReadBuffer(queue, streams[3], CL_TRUE, 0, datasize, + output.data(), 0, NULL, NULL); + test_error(err, "clEnqueueReadBuffer failed\n"); + + // Verify results + for (int i = 0; i < num_elements * N; i++) + { + T r = test.ref(inputA[i], inputB[i], inputC[i]); + if (r != output[i]) + { + log_error("\n\nverification failed at index %d\n", i); + log_error("-> inputs: %llu, %llu, %llu\n", + static_cast(inputA[i]), + static_cast(inputB[i]), + static_cast(inputC[i])); + log_error("-> expected %llu, got %llu\n\n", + static_cast(r), + static_cast(output[i])); + return TEST_FAIL; + } + } + log_info("passed\n"); + } + + return TEST_PASS; +} + +int test_intmath_int(cl_device_id device, cl_context context, + cl_command_queue queue, int num_elements) +{ + return test_intmath(device, context, queue, num_elements, + "uint"); +} + +int test_intmath_int2(cl_device_id device, cl_context context, + cl_command_queue queue, int num_elements) +{ + return test_intmath(device, context, queue, num_elements, + "uint2"); +} + +int test_intmath_int4(cl_device_id device, cl_context context, + cl_command_queue queue, int num_elements) +{ + return test_intmath(device, context, queue, num_elements, + "uint4"); +} + +int test_intmath_long(cl_device_id device, cl_context context, + cl_command_queue queue, int num_elements) +{ + return test_intmath(device, context, queue, num_elements, + "ulong"); +} + +int test_intmath_long2(cl_device_id device, cl_context context, + cl_command_queue queue, int num_elements) +{ + return test_intmath(device, context, queue, num_elements, + "ulong2"); +} + +int test_intmath_long4(cl_device_id device, cl_context context, + cl_command_queue queue, int num_elements) +{ + return test_intmath(device, context, queue, num_elements, + "ulong4"); +} diff --git a/test_conformance/basic/test_intmath_int.cpp b/test_conformance/basic/test_intmath_int.cpp deleted file mode 100644 index 76676617..00000000 --- a/test_conformance/basic/test_intmath_int.cpp +++ /dev/null @@ -1,334 +0,0 @@ -// -// Copyright (c) 2017 The Khronos Group Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -#include "harness/compat.h" - -#include -#include -#include -#include -#include - - -#include "procs.h" - -const char *int_add_kernel_code = -"__kernel void test_int_add(__global int *srcA, __global int *srcB, __global int *dst)\n" -"{\n" -" int tid = get_global_id(0);\n" -"\n" -" dst[tid] = srcA[tid] + srcB[tid];\n" -"}\n"; - -const char *int_sub_kernel_code = -"__kernel void test_int_sub(__global int *srcA, __global int *srcB, __global int *dst)\n" -"{\n" -" int tid = get_global_id(0);\n" -"\n" -" dst[tid] = srcA[tid] - srcB[tid];\n" -"}\n"; - -const char *int_mul_kernel_code = -"__kernel void test_int_mul(__global int *srcA, __global int *srcB, __global int *dst)\n" -"{\n" -" int tid = get_global_id(0);\n" -"\n" -" dst[tid] = srcA[tid] * srcB[tid];\n" -"}\n"; - -const char *int_mad_kernel_code = -"__kernel void test_int_mad(__global int *srcA, __global int *srcB, __global int *srcC, __global int *dst)\n" -"{\n" -" int tid = get_global_id(0);\n" -"\n" -" dst[tid] = srcA[tid] * srcB[tid] + srcC[tid];\n" -"}\n"; - -static const float MAX_ERR = 1e-5f; - -int -verify_int_add(int *inptrA, int *inptrB, int *outptr, int n) -{ - int r; - int i; - - for (i=0; i -#include -#include -#include -#include - - -#include "procs.h" - -const char *int_add2_kernel_code = -"__kernel void test_int_add2(__global int2 *srcA, __global int2 *srcB, __global int2 *dst)\n" -"{\n" -" int tid = get_global_id(0);\n" -"\n" -" dst[tid] = srcA[tid] + srcB[tid];\n" -"}\n"; - -const char *int_sub2_kernel_code = -"__kernel void test_int_sub2(__global int2 *srcA, __global int2 *srcB, __global int2 *dst)\n" -"{\n" -" int tid = get_global_id(0);\n" -"\n" -" dst[tid] = srcA[tid] - srcB[tid];\n" -"}\n"; - -const char *int_mul2_kernel_code = -"__kernel void test_int_mul2(__global int2 *srcA, __global int2 *srcB, __global int2 *dst)\n" -"{\n" -" int tid = get_global_id(0);\n" -"\n" -" dst[tid] = srcA[tid] * srcB[tid];\n" -"}\n"; - -const char *int_mad2_kernel_code = -"__kernel void test_int_mad2(__global int2 *srcA, __global int2 *srcB, __global int2 *srcC, __global int2 *dst)\n" -"{\n" -" int tid = get_global_id(0);\n" -"\n" -" dst[tid] = srcA[tid] * srcB[tid] + srcC[tid];\n" -"}\n"; - -int -verify_int_add2(int *inptrA, int *inptrB, int *outptr, int n) -{ - int r; - int i; - - for (i=0; i -#include -#include -#include -#include - - -#include "procs.h" - -const char *int_add4_kernel_code = -"__kernel void test_int_add4(__global int4 *srcA, __global int4 *srcB, __global int4 *dst)\n" -"{\n" -" int tid = get_global_id(0);\n" -"\n" -" dst[tid] = srcA[tid] + srcB[tid];\n" -"}\n"; - -const char *int_sub4_kernel_code = -"__kernel void test_int_sub4(__global int4 *srcA, __global int4 *srcB, __global int4 *dst)\n" -"{\n" -" int tid = get_global_id(0);\n" -"\n" -" dst[tid] = srcA[tid] - srcB[tid];\n" -"}\n"; - -const char *int_mul4_kernel_code = -"__kernel void test_int_mul4(__global int4 *srcA, __global int4 *srcB, __global int4 *dst)\n" -"{\n" -" int tid = get_global_id(0);\n" -"\n" -" dst[tid] = srcA[tid] * srcB[tid];\n" -"}\n"; - -const char *int_mad4_kernel_code = -"__kernel void test_int_mad4(__global int4 *srcA, __global int4 *srcB, __global int4 *srcC, __global int4 *dst)\n" -"{\n" -" int tid = get_global_id(0);\n" -"\n" -" dst[tid] = srcA[tid] * srcB[tid] + srcC[tid];\n" -"}\n"; - -int -verify_int_add4(int *inptrA, int *inptrB, int *outptr, int n) -{ - int r; - int i; - - for (i=0; i -#include -#include -#include -#include - - -#include "procs.h" - -const char *long_add_kernel_code = -"__kernel void test_long_add(__global long *srcA, __global long *srcB, __global long *dst)\n" -"{\n" -" int tid = get_global_id(0);\n" -"\n" -" dst[tid] = srcA[tid] + srcB[tid];\n" -"}\n"; - -const char *long_sub_kernel_code = -"__kernel void test_long_sub(__global long *srcA, __global long *srcB, __global long *dst)\n" -"{\n" -" int tid = get_global_id(0);\n" -"\n" -" dst[tid] = srcA[tid] - srcB[tid];\n" -"}\n"; - -const char *long_mul_kernel_code = -"__kernel void test_long_mul(__global long *srcA, __global long *srcB, __global long *dst)\n" -"{\n" -" int tid = get_global_id(0);\n" -"\n" -" dst[tid] = srcA[tid] * srcB[tid];\n" -"}\n"; - -const char *long_mad_kernel_code = -"__kernel void test_long_mad(__global long *srcA, __global long *srcB, __global long *srcC, __global long *dst)\n" -"{\n" -" int tid = get_global_id(0);\n" -"\n" -" dst[tid] = srcA[tid] * srcB[tid] + srcC[tid];\n" -"}\n"; - -static const float MAX_ERR = 1e-5f; - -int -verify_long_add(cl_long *inptrA, cl_long *inptrB, cl_long *outptr, int n) -{ - cl_long r; - int i; - - for (i=0; i -#include -#include -#include -#include - - -#include "procs.h" - -const char *long_add2_kernel_code = -"__kernel void test_long_add2(__global long2 *srcA, __global long2 *srcB, __global long2 *dst)\n" -"{\n" -" int tid = get_global_id(0);\n" -"\n" -" dst[tid] = srcA[tid] + srcB[tid];\n" -"}\n"; - -const char *long_sub2_kernel_code = -"__kernel void test_long_sub2(__global long2 *srcA, __global long2 *srcB, __global long2 *dst)\n" -"{\n" -" int tid = get_global_id(0);\n" -"\n" -" dst[tid] = srcA[tid] - srcB[tid];\n" -"}\n"; - -const char *long_mul2_kernel_code = -"__kernel void test_long_mul2(__global long2 *srcA, __global long2 *srcB, __global long2 *dst)\n" -"{\n" -" int tid = get_global_id(0);\n" -"\n" -" dst[tid] = srcA[tid] * srcB[tid];\n" -"}\n"; - -const char *long_mad2_kernel_code = -"__kernel void test_long_mad2(__global long2 *srcA, __global long2 *srcB, __global long2 *srcC, __global long2 *dst)\n" -"{\n" -" int tid = get_global_id(0);\n" -"\n" -" dst[tid] = srcA[tid] * srcB[tid] + srcC[tid];\n" -"}\n"; - -int -verify_long_add2(cl_long *inptrA, cl_long *inptrB, cl_long *outptr, int n) -{ - cl_long r; - int i; - - for (i=0; i -#include -#include -#include -#include - - -#include "procs.h" - -const char *long_add4_kernel_code = -"__kernel void test_long_add4(__global long4 *srcA, __global long4 *srcB, __global long4 *dst)\n" -"{\n" -" int tid = get_global_id(0);\n" -"\n" -" dst[tid] = srcA[tid] + srcB[tid];\n" -"}\n"; - -const char *long_sub4_kernel_code = -"__kernel void test_long_sub4(__global long4 *srcA, __global long4 *srcB, __global long4 *dst)\n" -"{\n" -" int tid = get_global_id(0);\n" -"\n" -" dst[tid] = srcA[tid] - srcB[tid];\n" -"}\n"; - -const char *long_mul4_kernel_code = -"__kernel void test_long_mul4(__global long4 *srcA, __global long4 *srcB, __global long4 *dst)\n" -"{\n" -" int tid = get_global_id(0);\n" -"\n" -" dst[tid] = srcA[tid] * srcB[tid];\n" -"}\n"; - -const char *long_mad4_kernel_code = -"__kernel void test_long_mad4(__global long4 *srcA, __global long4 *srcB, __global long4 *srcC, __global long4 *dst)\n" -"{\n" -" int tid = get_global_id(0);\n" -"\n" -" dst[tid] = srcA[tid] * srcB[tid] + srcC[tid];\n" -"}\n"; - -int -verify_long_add4(cl_long *inptrA, cl_long *inptrB, cl_long *outptr, int n) -{ - cl_long r; - int i; - - for (i=0; i