mirror of
https://github.com/KhronosGroup/OpenCL-CTS.git
synced 2026-03-19 06:09:01 +00:00
Added cl_khr_fp16 extension support for test_commonfns (#1695)
* Added cl_khr_fp16 extension support for commonfns test (issue #142, commonfns) * Added missing header due to presubmit check * Corrected radians/degrees ulp calculations + cosmetic fixes * Corrected presubmit code format * Corrections related to code review * Moved string format helper to test_common in separate header * Added clang format for last commit * Corrections related to code review * Modified mix verification procedure for half type to only report max error * Removed redundant condition for logging mix verification * Corrected generator limits for half tests
This commit is contained in:
@@ -14,8 +14,8 @@
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
#ifndef BASIC_UTILS_H
|
||||
#define BASIC_UTILS_H
|
||||
#ifndef STRING_HELPERS_H
|
||||
#define STRING_HELPERS_H
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
@@ -38,4 +38,4 @@ inline std::string str_sprintf(const std::string &str, Args... args)
|
||||
return std::string(buffer.get(), buffer.get() + s - 1);
|
||||
}
|
||||
|
||||
#endif // BASIC_UTIL_H
|
||||
#endif // STRING_HELPERS_H
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
//
|
||||
// Copyright (c) 2017 The Khronos Group Inc.
|
||||
// Copyright (c) 2023 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.
|
||||
@@ -18,8 +18,10 @@
|
||||
#include <string.h>
|
||||
#include "procs.h"
|
||||
#include "test_base.h"
|
||||
#include "harness/kernelHelpers.h"
|
||||
|
||||
std::map<size_t, std::string> BaseFunctionTest::type2name;
|
||||
cl_half_rounding_mode BaseFunctionTest::halfRoundingMode = CL_HALF_RTE;
|
||||
|
||||
int g_arrVecSizes[kVectorSizeCount + kStrangeVectorSizeCount];
|
||||
int g_arrStrangeVectorSizes[kStrangeVectorSizeCount] = {3};
|
||||
@@ -45,17 +47,38 @@ test_definition test_list[] = {
|
||||
|
||||
const int test_num = ARRAY_SIZE( test_list );
|
||||
|
||||
test_status InitCL(cl_device_id device)
|
||||
{
|
||||
if (is_extension_available(device, "cl_khr_fp16"))
|
||||
{
|
||||
const cl_device_fp_config fpConfigHalf =
|
||||
get_default_rounding_mode(device, CL_DEVICE_HALF_FP_CONFIG);
|
||||
if ((fpConfigHalf & CL_FP_ROUND_TO_NEAREST) != 0)
|
||||
{
|
||||
BaseFunctionTest::halfRoundingMode = CL_HALF_RTE;
|
||||
}
|
||||
else if ((fpConfigHalf & CL_FP_ROUND_TO_ZERO) != 0)
|
||||
{
|
||||
BaseFunctionTest::halfRoundingMode = CL_HALF_RTZ;
|
||||
}
|
||||
else
|
||||
{
|
||||
log_error("Error while acquiring half rounding mode");
|
||||
return TEST_FAIL;
|
||||
}
|
||||
}
|
||||
|
||||
return TEST_PASS;
|
||||
}
|
||||
|
||||
int main(int argc, const char *argv[])
|
||||
{
|
||||
initVecSizes();
|
||||
|
||||
if (BaseFunctionTest::type2name.empty())
|
||||
{
|
||||
BaseFunctionTest::type2name[sizeof(half)] = "half";
|
||||
BaseFunctionTest::type2name[sizeof(float)] = "float";
|
||||
BaseFunctionTest::type2name[sizeof(double)] = "double";
|
||||
}
|
||||
|
||||
return runTestHarness(argc, argv, test_num, test_list, false, 0);
|
||||
return runTestHarnessWithCheck(argc, argv, test_num, test_list, false, 0,
|
||||
InitCL);
|
||||
}
|
||||
|
||||
|
||||
@@ -19,27 +19,23 @@
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <cmath>
|
||||
|
||||
#include <CL/cl_half.h>
|
||||
#include <CL/cl_ext.h>
|
||||
|
||||
#include "harness/deviceInfo.h"
|
||||
#include "harness/testHarness.h"
|
||||
#include "harness/typeWrappers.h"
|
||||
|
||||
|
||||
template <typename T>
|
||||
using VerifyFuncBinary = int (*)(const T *const, const T *const, const T *const,
|
||||
const int num, const int vs, const int vp);
|
||||
|
||||
|
||||
template <typename T>
|
||||
using VerifyFuncUnary = int (*)(const T *const, const T *const, const int num);
|
||||
|
||||
|
||||
using half = cl_half;
|
||||
|
||||
|
||||
struct BaseFunctionTest
|
||||
{
|
||||
BaseFunctionTest(cl_device_id device, cl_context context,
|
||||
@@ -61,9 +57,9 @@ struct BaseFunctionTest
|
||||
bool vecParam;
|
||||
|
||||
static std::map<size_t, std::string> type2name;
|
||||
static cl_half_rounding_mode halfRoundingMode;
|
||||
};
|
||||
|
||||
|
||||
struct MinTest : BaseFunctionTest
|
||||
{
|
||||
MinTest(cl_device_id device, cl_context context, cl_command_queue queue,
|
||||
@@ -74,7 +70,6 @@ struct MinTest : BaseFunctionTest
|
||||
cl_int Run() override;
|
||||
};
|
||||
|
||||
|
||||
struct MaxTest : BaseFunctionTest
|
||||
{
|
||||
MaxTest(cl_device_id device, cl_context context, cl_command_queue queue,
|
||||
@@ -85,7 +80,6 @@ struct MaxTest : BaseFunctionTest
|
||||
cl_int Run() override;
|
||||
};
|
||||
|
||||
|
||||
struct ClampTest : BaseFunctionTest
|
||||
{
|
||||
ClampTest(cl_device_id device, cl_context context, cl_command_queue queue,
|
||||
@@ -96,7 +90,6 @@ struct ClampTest : BaseFunctionTest
|
||||
cl_int Run() override;
|
||||
};
|
||||
|
||||
|
||||
struct DegreesTest : BaseFunctionTest
|
||||
{
|
||||
DegreesTest(cl_device_id device, cl_context context, cl_command_queue queue,
|
||||
@@ -107,7 +100,6 @@ struct DegreesTest : BaseFunctionTest
|
||||
cl_int Run() override;
|
||||
};
|
||||
|
||||
|
||||
struct RadiansTest : BaseFunctionTest
|
||||
{
|
||||
RadiansTest(cl_device_id device, cl_context context, cl_command_queue queue,
|
||||
@@ -118,7 +110,6 @@ struct RadiansTest : BaseFunctionTest
|
||||
cl_int Run() override;
|
||||
};
|
||||
|
||||
|
||||
struct SignTest : BaseFunctionTest
|
||||
{
|
||||
SignTest(cl_device_id device, cl_context context, cl_command_queue queue,
|
||||
@@ -129,7 +120,6 @@ struct SignTest : BaseFunctionTest
|
||||
cl_int Run() override;
|
||||
};
|
||||
|
||||
|
||||
struct SmoothstepTest : BaseFunctionTest
|
||||
{
|
||||
SmoothstepTest(cl_device_id device, cl_context context,
|
||||
@@ -141,7 +131,6 @@ struct SmoothstepTest : BaseFunctionTest
|
||||
cl_int Run() override;
|
||||
};
|
||||
|
||||
|
||||
struct StepTest : BaseFunctionTest
|
||||
{
|
||||
StepTest(cl_device_id device, cl_context context, cl_command_queue queue,
|
||||
@@ -152,7 +141,6 @@ struct StepTest : BaseFunctionTest
|
||||
cl_int Run() override;
|
||||
};
|
||||
|
||||
|
||||
struct MixTest : BaseFunctionTest
|
||||
{
|
||||
MixTest(cl_device_id device, cl_context context, cl_command_queue queue,
|
||||
@@ -163,19 +151,71 @@ struct MixTest : BaseFunctionTest
|
||||
cl_int Run() override;
|
||||
};
|
||||
|
||||
|
||||
template <typename... Args>
|
||||
std::string string_format(const std::string &format, Args... args)
|
||||
template <typename T> float UlpFn(const T &val, const double &r)
|
||||
{
|
||||
int sformat = std::snprintf(nullptr, 0, format.c_str(), args...) + 1;
|
||||
if (sformat <= 0)
|
||||
throw std::runtime_error("string_format: string processing error.");
|
||||
auto format_size = static_cast<size_t>(sformat);
|
||||
std::unique_ptr<char[]> buffer(new char[format_size]);
|
||||
std::snprintf(buffer.get(), format_size, format.c_str(), args...);
|
||||
return std::string(buffer.get(), buffer.get() + format_size - 1);
|
||||
if (std::is_same<T, half>::value)
|
||||
{
|
||||
return Ulp_Error_Half(val, r);
|
||||
}
|
||||
else if (std::is_same<T, float>::value)
|
||||
{
|
||||
return Ulp_Error(val, r);
|
||||
}
|
||||
else if (std::is_same<T, double>::value)
|
||||
{
|
||||
return Ulp_Error_Double(val, r);
|
||||
}
|
||||
else
|
||||
{
|
||||
log_error("UlpFn: unsupported data type\n");
|
||||
}
|
||||
|
||||
return -1.f; // wrong val
|
||||
}
|
||||
|
||||
template <typename T> inline double conv_to_dbl(const T &val)
|
||||
{
|
||||
if (std::is_same<T, half>::value)
|
||||
return (double)cl_half_to_float(val);
|
||||
else
|
||||
return (double)val;
|
||||
}
|
||||
|
||||
template <typename T> inline double conv_to_flt(const T &val)
|
||||
{
|
||||
if (std::is_same<T, half>::value)
|
||||
return (float)cl_half_to_float(val);
|
||||
else
|
||||
return (float)val;
|
||||
}
|
||||
|
||||
template <typename T> inline half conv_to_half(const T &val)
|
||||
{
|
||||
if (std::is_floating_point<T>::value)
|
||||
return cl_half_from_float(val, BaseFunctionTest::halfRoundingMode);
|
||||
return 0;
|
||||
}
|
||||
|
||||
template <typename T> bool isfinite_fp(const T &v)
|
||||
{
|
||||
if (std::is_same<T, half>::value)
|
||||
{
|
||||
// Extract FP16 exponent and mantissa
|
||||
uint16_t h_exp = (((half)v) >> (CL_HALF_MANT_DIG - 1)) & 0x1F;
|
||||
uint16_t h_mant = ((half)v) & 0x3FF;
|
||||
|
||||
// !Inf test
|
||||
return !(h_exp == 0x1F && h_mant == 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
#if !defined(_WIN32)
|
||||
return std::isfinite(v);
|
||||
#else
|
||||
return isfinite(v);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
template <class T>
|
||||
int MakeAndRunTest(cl_device_id device, cl_context context,
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
//
|
||||
// Copyright (c) 2017 The Khronos Group Inc.
|
||||
// Copyright (c) 2023 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.
|
||||
@@ -22,6 +22,7 @@
|
||||
|
||||
#include "harness/deviceInfo.h"
|
||||
#include "harness/typeWrappers.h"
|
||||
#include "harness/stringHelpers.h"
|
||||
|
||||
#include "procs.h"
|
||||
#include "test_base.h"
|
||||
@@ -53,7 +54,6 @@ const char *binary_fn_code_pattern_v3_scalar =
|
||||
" vstore3(%s(vload3(tid,x), y[tid] ), tid, dst);\n"
|
||||
"}\n";
|
||||
|
||||
|
||||
template <typename T>
|
||||
int test_binary_fn(cl_device_id device, cl_context context,
|
||||
cl_command_queue queue, int n_elems,
|
||||
@@ -105,6 +105,16 @@ int test_binary_fn(cl_device_id device, cl_context context,
|
||||
input_ptr[1][j] = get_random_double(-0x20000000, 0x20000000, d);
|
||||
}
|
||||
}
|
||||
else if (std::is_same<T, half>::value)
|
||||
{
|
||||
const float fval = CL_HALF_MAX;
|
||||
pragma_str = "#pragma OPENCL EXTENSION cl_khr_fp16 : enable\n";
|
||||
for (int j = 0; j < num_elements; j++)
|
||||
{
|
||||
input_ptr[0][j] = conv_to_half(get_random_float(-fval, fval, d));
|
||||
input_ptr[1][j] = conv_to_half(get_random_float(-fval, fval, d));
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < 2; i++)
|
||||
{
|
||||
@@ -125,14 +135,14 @@ int test_binary_fn(cl_device_id device, cl_context context,
|
||||
{
|
||||
std::string str = binary_fn_code_pattern_v3;
|
||||
kernelSource =
|
||||
string_format(str, pragma_str.c_str(), tname.c_str(),
|
||||
str_sprintf(str, pragma_str.c_str(), tname.c_str(),
|
||||
tname.c_str(), tname.c_str(), fnName.c_str());
|
||||
}
|
||||
else
|
||||
{
|
||||
std::string str = binary_fn_code_pattern_v3_scalar;
|
||||
kernelSource =
|
||||
string_format(str, pragma_str.c_str(), tname.c_str(),
|
||||
str_sprintf(str, pragma_str.c_str(), tname.c_str(),
|
||||
tname.c_str(), tname.c_str(), fnName.c_str());
|
||||
}
|
||||
}
|
||||
@@ -140,7 +150,7 @@ int test_binary_fn(cl_device_id device, cl_context context,
|
||||
{
|
||||
// do regular
|
||||
std::string str = binary_fn_code_pattern;
|
||||
kernelSource = string_format(
|
||||
kernelSource = str_sprintf(
|
||||
str, pragma_str.c_str(), tname.c_str(), vecSizeNames[i],
|
||||
tname.c_str(), vecSecParam ? vecSizeNames[i] : "",
|
||||
tname.c_str(), vecSizeNames[i], fnName.c_str());
|
||||
@@ -203,11 +213,18 @@ int max_verify(const T* const x, const T* const y, const T* const out,
|
||||
{
|
||||
int k = i * vecSize + j;
|
||||
int l = (k * vecParam + i * (1 - vecParam));
|
||||
T v = (x[k] < y[l]) ? y[l] : x[k];
|
||||
T v = (conv_to_dbl(x[k]) < conv_to_dbl(y[l])) ? y[l] : x[k];
|
||||
if (v != out[k])
|
||||
{
|
||||
log_error(
|
||||
"x[%d]=%g y[%d]=%g out[%d]=%g, expected %g. (index %d is "
|
||||
if (std::is_same<T, half>::value)
|
||||
log_error("x[%d]=%g y[%d]=%g out[%d]=%g, expected %g. "
|
||||
"(index %d is "
|
||||
"vector %d, element %d, for vector size %d)\n",
|
||||
k, conv_to_flt(x[k]), l, conv_to_flt(y[l]), k,
|
||||
conv_to_flt(out[k]), v, k, i, j, vecSize);
|
||||
else
|
||||
log_error("x[%d]=%g y[%d]=%g out[%d]=%g, expected %g. "
|
||||
"(index %d is "
|
||||
"vector %d, element %d, for vector size %d)\n",
|
||||
k, x[k], l, y[l], k, out[k], v, k, i, j, vecSize);
|
||||
return -1;
|
||||
@@ -227,11 +244,18 @@ int min_verify(const T* const x, const T* const y, const T* const out,
|
||||
{
|
||||
int k = i * vecSize + j;
|
||||
int l = (k * vecParam + i * (1 - vecParam));
|
||||
T v = (x[k] > y[l]) ? y[l] : x[k];
|
||||
T v = (conv_to_dbl(x[k]) > conv_to_dbl(y[l])) ? y[l] : x[k];
|
||||
if (v != out[k])
|
||||
{
|
||||
log_error(
|
||||
"x[%d]=%g y[%d]=%g out[%d]=%g, expected %g. (index %d is "
|
||||
if (std::is_same<T, half>::value)
|
||||
log_error("x[%d]=%g y[%d]=%g out[%d]=%g, expected %g. "
|
||||
"(index %d is "
|
||||
"vector %d, element %d, for vector size %d)\n",
|
||||
k, conv_to_flt(x[k]), l, conv_to_flt(y[l]), k,
|
||||
conv_to_flt(out[k]), v, k, i, j, vecSize);
|
||||
else
|
||||
log_error("x[%d]=%g y[%d]=%g out[%d]=%g, expected %g. "
|
||||
"(index %d is "
|
||||
"vector %d, element %d, for vector size %d)\n",
|
||||
k, x[k], l, y[l], k, out[k], v, k, i, j, vecSize);
|
||||
return -1;
|
||||
@@ -246,6 +270,13 @@ int min_verify(const T* const x, const T* const y, const T* const out,
|
||||
cl_int MaxTest::Run()
|
||||
{
|
||||
cl_int error = CL_SUCCESS;
|
||||
if (is_extension_available(device, "cl_khr_fp16"))
|
||||
{
|
||||
error = test_binary_fn<cl_half>(device, context, queue, num_elems,
|
||||
fnName.c_str(), vecParam,
|
||||
max_verify<cl_half>);
|
||||
test_error(error, "MaxTest::Run<cl_half> failed");
|
||||
}
|
||||
|
||||
error = test_binary_fn<float>(device, context, queue, num_elems,
|
||||
fnName.c_str(), vecParam, max_verify<float>);
|
||||
@@ -265,6 +296,13 @@ cl_int MaxTest::Run()
|
||||
cl_int MinTest::Run()
|
||||
{
|
||||
cl_int error = CL_SUCCESS;
|
||||
if (is_extension_available(device, "cl_khr_fp16"))
|
||||
{
|
||||
error = test_binary_fn<cl_half>(device, context, queue, num_elems,
|
||||
fnName.c_str(), vecParam,
|
||||
min_verify<cl_half>);
|
||||
test_error(error, "MinTest::Run<cl_half> failed");
|
||||
}
|
||||
|
||||
error = test_binary_fn<float>(device, context, queue, num_elems,
|
||||
fnName.c_str(), vecParam, min_verify<float>);
|
||||
|
||||
@@ -26,12 +26,10 @@
|
||||
#include "procs.h"
|
||||
#include "test_base.h"
|
||||
|
||||
|
||||
#ifndef M_PI
|
||||
#define M_PI 3.14159265358979323846264338327950288
|
||||
#endif
|
||||
|
||||
|
||||
#define CLAMP_KERNEL(type) \
|
||||
const char *clamp_##type##_kernel_code = EMIT_PRAGMA_DIRECTIVE \
|
||||
"__kernel void test_clamp(__global " #type " *x, __global " #type \
|
||||
@@ -64,6 +62,14 @@
|
||||
"vload3(tid,maxval)), tid, dst);\n" \
|
||||
"}\n";
|
||||
|
||||
#define EMIT_PRAGMA_DIRECTIVE "#pragma OPENCL EXTENSION cl_khr_fp16 : enable\n"
|
||||
CLAMP_KERNEL(half)
|
||||
CLAMP_KERNEL_V(half, 2)
|
||||
CLAMP_KERNEL_V(half, 4)
|
||||
CLAMP_KERNEL_V(half, 8)
|
||||
CLAMP_KERNEL_V(half, 16)
|
||||
CLAMP_KERNEL_V3(half, 3)
|
||||
#undef EMIT_PRAGMA_DIRECTIVE
|
||||
|
||||
#define EMIT_PRAGMA_DIRECTIVE " "
|
||||
CLAMP_KERNEL(float)
|
||||
@@ -83,6 +89,10 @@ CLAMP_KERNEL_V(double, 16)
|
||||
CLAMP_KERNEL_V3(double, 3)
|
||||
#undef EMIT_PRAGMA_DIRECTIVE
|
||||
|
||||
const char *clamp_half_codes[] = {
|
||||
clamp_half_kernel_code, clamp_half2_kernel_code, clamp_half4_kernel_code,
|
||||
clamp_half8_kernel_code, clamp_half16_kernel_code, clamp_half3_kernel_code
|
||||
};
|
||||
const char *clamp_float_codes[] = {
|
||||
clamp_float_kernel_code, clamp_float2_kernel_code,
|
||||
clamp_float4_kernel_code, clamp_float8_kernel_code,
|
||||
@@ -96,11 +106,31 @@ const char *clamp_double_codes[] = {
|
||||
|
||||
namespace {
|
||||
|
||||
|
||||
template <typename T>
|
||||
int verify_clamp(const T *const x, const T *const minval, const T *const maxval,
|
||||
const T *const outptr, int n)
|
||||
{
|
||||
if (std::is_same<T, half>::value)
|
||||
{
|
||||
float t;
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
t = std::min(
|
||||
std::max(cl_half_to_float(x[i]), cl_half_to_float(minval[i])),
|
||||
cl_half_to_float(maxval[i]));
|
||||
if (t != cl_half_to_float(outptr[i]))
|
||||
{
|
||||
log_error(
|
||||
"%d) verification error: clamp( %a, %a, %a) = *%a vs. %a\n",
|
||||
i, cl_half_to_float(x[i]), cl_half_to_float(minval[i]),
|
||||
cl_half_to_float(maxval[i]), t,
|
||||
cl_half_to_float(outptr[i]));
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
T t;
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
@@ -108,17 +138,17 @@ int verify_clamp(const T *const x, const T *const minval, const T *const maxval,
|
||||
if (t != outptr[i])
|
||||
{
|
||||
log_error(
|
||||
"%d) verification error: clamp( %a, %a, %a) = *%a vs. %a\n", i,
|
||||
x[i], minval[i], maxval[i], t, outptr[i]);
|
||||
"%d) verification error: clamp( %a, %a, %a) = *%a vs. %a\n",
|
||||
i, x[i], minval[i], maxval[i], t, outptr[i]);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template <typename T>
|
||||
int test_clamp_fn(cl_device_id device, cl_context context,
|
||||
cl_command_queue queue, int n_elems)
|
||||
@@ -169,6 +199,17 @@ int test_clamp_fn(cl_device_id device, cl_context context,
|
||||
input_ptr[2][j] = get_random_double(input_ptr[1][j], 0x20000000, d);
|
||||
}
|
||||
}
|
||||
else if (std::is_same<T, half>::value)
|
||||
{
|
||||
const float fval = CL_HALF_MAX;
|
||||
for (j = 0; j < num_elements; j++)
|
||||
{
|
||||
input_ptr[0][j] = conv_to_half(get_random_float(-fval, fval, d));
|
||||
input_ptr[1][j] = conv_to_half(get_random_float(-fval, fval, d));
|
||||
input_ptr[2][j] = conv_to_half(
|
||||
get_random_float(conv_to_flt(input_ptr[1][j]), fval, d));
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < 3; i++)
|
||||
{
|
||||
@@ -194,9 +235,16 @@ int test_clamp_fn(cl_device_id device, cl_context context,
|
||||
"test_clamp");
|
||||
test_error(err, "Unable to create kernel");
|
||||
}
|
||||
else if (std::is_same<T, half>::value)
|
||||
{
|
||||
err = create_single_kernel_helper(
|
||||
context, &programs[i], &kernels[i], 1, &clamp_half_codes[i],
|
||||
"test_clamp");
|
||||
test_error(err, "Unable to create kernel");
|
||||
}
|
||||
|
||||
log_info("Just made a program for float, i=%d, size=%d, in slot %d\n",
|
||||
i, g_arrVecSizes[i], i);
|
||||
log_info("Just made a program for %s, i=%d, size=%d, in slot %d\n",
|
||||
tname.c_str(), i, g_arrVecSizes[i], i);
|
||||
fflush(stdout);
|
||||
|
||||
for (j = 0; j < 4; j++)
|
||||
@@ -239,10 +287,14 @@ int test_clamp_fn(cl_device_id device, cl_context context,
|
||||
return err;
|
||||
}
|
||||
|
||||
|
||||
cl_int ClampTest::Run()
|
||||
{
|
||||
cl_int error = CL_SUCCESS;
|
||||
if (is_extension_available(device, "cl_khr_fp16"))
|
||||
{
|
||||
error = test_clamp_fn<cl_half>(device, context, queue, num_elems);
|
||||
test_error(error, "ClampTest::Run<cl_half> failed");
|
||||
}
|
||||
|
||||
error = test_clamp_fn<float>(device, context, queue, num_elems);
|
||||
test_error(error, "ClampTest::Run<float> failed");
|
||||
@@ -256,7 +308,6 @@ cl_int ClampTest::Run()
|
||||
return error;
|
||||
}
|
||||
|
||||
|
||||
int test_clamp(cl_device_id device, cl_context context, cl_command_queue queue,
|
||||
int n_elems)
|
||||
{
|
||||
|
||||
@@ -18,6 +18,8 @@
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include "harness/stringHelpers.h"
|
||||
|
||||
#include "procs.h"
|
||||
#include "test_base.h"
|
||||
|
||||
@@ -52,35 +54,44 @@ const char *mix_fn_code_pattern_v3_scalar =
|
||||
" vstore3(mix(vload3(tid, x), vload3(tid, y), a[tid]), tid, dst);\n"
|
||||
"}\n";
|
||||
|
||||
|
||||
#define MAX_ERR 1e-3
|
||||
|
||||
namespace {
|
||||
|
||||
|
||||
template <typename T>
|
||||
int verify_mix(const T *const inptrX, const T *const inptrY,
|
||||
const T *const inptrA, const T *const outptr, const int n,
|
||||
const int veclen, const bool vecParam)
|
||||
{
|
||||
T r;
|
||||
float delta = 0.0f;
|
||||
double r, o;
|
||||
float delta = 0.f, max_delta = 0.f;
|
||||
int i;
|
||||
|
||||
if (vecParam)
|
||||
{
|
||||
for (i = 0; i < n * veclen; i++)
|
||||
{
|
||||
r = inptrX[i] + ((inptrY[i] - inptrX[i]) * inptrA[i]);
|
||||
delta = fabs(double(r - outptr[i])) / r;
|
||||
r = conv_to_dbl(inptrX[i])
|
||||
+ ((conv_to_dbl(inptrY[i]) - conv_to_dbl(inptrX[i]))
|
||||
* conv_to_dbl(inptrA[i]));
|
||||
|
||||
o = conv_to_dbl(outptr[i]);
|
||||
delta = fabs(double(r - o)) / r;
|
||||
if (!std::is_same<T, half>::value)
|
||||
{
|
||||
if (delta > MAX_ERR)
|
||||
{
|
||||
log_error(
|
||||
"%d) verification error: mix(%a, %a, %a) = *%a vs. %a\n", i,
|
||||
inptrX[i], inptrY[i], inptrA[i], r, outptr[i]);
|
||||
log_error("%d) verification error: mix(%a, %a, %a) = *%a "
|
||||
"vs. %a\n",
|
||||
i, inptrX[i], inptrY[i], inptrA[i], r, outptr[i]);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
max_delta = std::max(max_delta, delta);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -90,25 +101,40 @@ int verify_mix(const T *const inptrX, const T *const inptrY,
|
||||
int vi = i * veclen;
|
||||
for (int j = 0; j < veclen; ++j, ++vi)
|
||||
{
|
||||
r = inptrX[vi] + ((inptrY[vi] - inptrX[vi]) * inptrA[i]);
|
||||
delta = fabs(double(r - outptr[vi])) / r;
|
||||
r = conv_to_dbl(inptrX[vi])
|
||||
+ ((conv_to_dbl(inptrY[vi]) - conv_to_dbl(inptrX[vi]))
|
||||
* conv_to_dbl(inptrA[i]));
|
||||
delta = fabs(double(r - conv_to_dbl(outptr[vi]))) / r;
|
||||
if (!std::is_same<T, half>::value)
|
||||
{
|
||||
if (delta > MAX_ERR)
|
||||
{
|
||||
log_error("{%d, element %d}) verification error: mix(%a, "
|
||||
log_error(
|
||||
"{%d, element %d}) verification error: mix(%a, "
|
||||
"%a, %a) = *%a vs. %a\n",
|
||||
ii, j, inptrX[vi], inptrY[vi], inptrA[i], r,
|
||||
outptr[vi]);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
max_delta = std::max(max_delta, delta);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// due to the fact that accuracy of mix for cl_khr_fp16 is implementation
|
||||
// defined this test only reports maximum error without testing maximum
|
||||
// error threshold
|
||||
if (std::is_same<T, half>::value)
|
||||
log_error("mix half verification result, max delta: %a\n", max_delta);
|
||||
|
||||
return 0;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
|
||||
template <typename T>
|
||||
int test_mix_fn(cl_device_id device, cl_context context, cl_command_queue queue,
|
||||
int n_elems, bool vecParam)
|
||||
@@ -120,7 +146,7 @@ int test_mix_fn(cl_device_id device, cl_context context, cl_command_queue queue,
|
||||
std::vector<clKernelWrapper> kernels;
|
||||
|
||||
int err, i;
|
||||
MTdataHolder d = MTdataHolder(gRandomSeed);
|
||||
MTdataHolder d(gRandomSeed);
|
||||
|
||||
assert(BaseFunctionTest::type2name.find(sizeof(T))
|
||||
!= BaseFunctionTest::type2name.end());
|
||||
@@ -142,17 +168,30 @@ int test_mix_fn(cl_device_id device, cl_context context, cl_command_queue queue,
|
||||
test_error(err, "clCreateBuffer failed");
|
||||
}
|
||||
|
||||
std::string pragma_str;
|
||||
if (std::is_same<T, double>::value)
|
||||
{
|
||||
pragma_str = "#pragma OPENCL EXTENSION cl_khr_fp64 : enable\n";
|
||||
}
|
||||
|
||||
if (std::is_same<T, half>::value)
|
||||
{
|
||||
pragma_str = "#pragma OPENCL EXTENSION cl_khr_fp16 : enable\n";
|
||||
for (i = 0; i < num_elements; i++)
|
||||
{
|
||||
input_ptr[0][i] = conv_to_half((float)genrand_real1(d));
|
||||
input_ptr[1][i] = conv_to_half((float)genrand_real1(d));
|
||||
input_ptr[2][i] = conv_to_half((float)genrand_real1(d));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (i = 0; i < num_elements; i++)
|
||||
{
|
||||
input_ptr[0][i] = (T)genrand_real1(d);
|
||||
input_ptr[1][i] = (T)genrand_real1(d);
|
||||
input_ptr[2][i] = (T)genrand_real1(d);
|
||||
}
|
||||
|
||||
std::string pragma_str;
|
||||
if (std::is_same<T, double>::value)
|
||||
{
|
||||
pragma_str = "#pragma OPENCL EXTENSION cl_khr_fp64 : enable\n";
|
||||
}
|
||||
|
||||
for (i = 0; i < 3; i++)
|
||||
@@ -164,7 +203,6 @@ int test_mix_fn(cl_device_id device, cl_context context, cl_command_queue queue,
|
||||
}
|
||||
|
||||
char vecSizeNames[][3] = { "", "2", "4", "8", "16", "3" };
|
||||
|
||||
for (i = 0; i < kTotalVecCount; i++)
|
||||
{
|
||||
std::string kernelSource;
|
||||
@@ -174,14 +212,14 @@ int test_mix_fn(cl_device_id device, cl_context context, cl_command_queue queue,
|
||||
{
|
||||
std::string str = mix_fn_code_pattern_v3;
|
||||
kernelSource =
|
||||
string_format(str, pragma_str.c_str(), tname.c_str(),
|
||||
str_sprintf(str, pragma_str.c_str(), tname.c_str(),
|
||||
tname.c_str(), tname.c_str(), tname.c_str());
|
||||
}
|
||||
else
|
||||
{
|
||||
std::string str = mix_fn_code_pattern_v3_scalar;
|
||||
kernelSource =
|
||||
string_format(str, pragma_str.c_str(), tname.c_str(),
|
||||
str_sprintf(str, pragma_str.c_str(), tname.c_str(),
|
||||
tname.c_str(), tname.c_str(), tname.c_str());
|
||||
}
|
||||
}
|
||||
@@ -190,7 +228,7 @@ int test_mix_fn(cl_device_id device, cl_context context, cl_command_queue queue,
|
||||
// regular path
|
||||
std::string str = mix_fn_code_pattern;
|
||||
kernelSource =
|
||||
string_format(str, pragma_str.c_str(), tname.c_str(),
|
||||
str_sprintf(str, pragma_str.c_str(), tname.c_str(),
|
||||
vecSizeNames[i], tname.c_str(), vecSizeNames[i],
|
||||
tname.c_str(), vecParam ? vecSizeNames[i] : "",
|
||||
tname.c_str(), vecSizeNames[i]);
|
||||
@@ -242,10 +280,14 @@ int test_mix_fn(cl_device_id device, cl_context context, cl_command_queue queue,
|
||||
return err;
|
||||
}
|
||||
|
||||
|
||||
cl_int MixTest::Run()
|
||||
{
|
||||
cl_int error = CL_SUCCESS;
|
||||
if (is_extension_available(device, "cl_khr_fp16"))
|
||||
{
|
||||
error = test_mix_fn<half>(device, context, queue, num_elems, vecParam);
|
||||
test_error(error, "MixTest::Run<cl_half> failed");
|
||||
}
|
||||
|
||||
error = test_mix_fn<float>(device, context, queue, num_elems, vecParam);
|
||||
test_error(error, "MixTest::Run<float> failed");
|
||||
@@ -260,7 +302,6 @@ cl_int MixTest::Run()
|
||||
return error;
|
||||
}
|
||||
|
||||
|
||||
int test_mix(cl_device_id device, cl_context context, cl_command_queue queue,
|
||||
int n_elems)
|
||||
{
|
||||
@@ -268,7 +309,6 @@ int test_mix(cl_device_id device, cl_context context, cl_command_queue queue,
|
||||
true);
|
||||
}
|
||||
|
||||
|
||||
int test_mixf(cl_device_id device, cl_context context, cl_command_queue queue,
|
||||
int n_elems)
|
||||
{
|
||||
|
||||
@@ -18,10 +18,11 @@
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include "harness/stringHelpers.h"
|
||||
|
||||
#include "procs.h"
|
||||
#include "test_base.h"
|
||||
|
||||
|
||||
const char *smoothstep_fn_code_pattern =
|
||||
"%s\n" /* optional pragma */
|
||||
"__kernel void test_fn(__global %s%s *e0, __global %s%s *e1, __global %s%s "
|
||||
@@ -53,39 +54,44 @@ const char *smoothstep_fn_code_pattern_v3_scalar =
|
||||
" vstore3(smoothstep(e0[tid], e1[tid], vload3(tid,x)), tid, dst);\n"
|
||||
"}\n";
|
||||
|
||||
|
||||
#define MAX_ERR (1e-5f)
|
||||
|
||||
namespace {
|
||||
|
||||
|
||||
template <typename T>
|
||||
int verify_smoothstep(const T *const edge0, const T *const edge1,
|
||||
const T *const x, const T *const outptr, const int n,
|
||||
const int veclen, const bool vecParam)
|
||||
{
|
||||
T r, t;
|
||||
float delta = 0;
|
||||
double r, t;
|
||||
float delta = 0, max_delta = 0;
|
||||
|
||||
if (vecParam)
|
||||
{
|
||||
for (int i = 0; i < n * veclen; i++)
|
||||
{
|
||||
t = (x[i] - edge0[i]) / (edge1[i] - edge0[i]);
|
||||
if (t < 0.0f)
|
||||
t = 0.0f;
|
||||
else if (t > 1.0f)
|
||||
t = 1.0f;
|
||||
r = t * t * (3.0f - 2.0f * t);
|
||||
delta = (float)fabs(r - outptr[i]);
|
||||
t = (conv_to_dbl(x[i]) - conv_to_dbl(edge0[i]))
|
||||
/ (conv_to_dbl(edge1[i]) - conv_to_dbl(edge0[i]));
|
||||
if (t < 0.0)
|
||||
t = 0.0;
|
||||
else if (t > 1.0)
|
||||
t = 1.0;
|
||||
r = t * t * (3.0 - 2.0 * t);
|
||||
delta = (float)fabs(r - conv_to_dbl(outptr[i]));
|
||||
if (!std::is_same<T, half>::value)
|
||||
{
|
||||
if (delta > MAX_ERR)
|
||||
{
|
||||
log_error("%d) verification error: smoothstep(%a, %a, %a) = "
|
||||
log_error(
|
||||
"%d) verification error: smoothstep(%a, %a, %a) = "
|
||||
"*%a vs. %a\n",
|
||||
i, x[i], edge0[i], edge1[i], r, outptr[i]);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
else
|
||||
max_delta = std::max(max_delta, delta);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -95,32 +101,48 @@ int verify_smoothstep(const T *const edge0, const T *const edge1,
|
||||
int vi = i * veclen;
|
||||
for (int j = 0; j < veclen; ++j, ++vi)
|
||||
{
|
||||
t = (x[vi] - edge0[i]) / (edge1[i] - edge0[i]);
|
||||
if (t < 0.0f)
|
||||
t = 0.0f;
|
||||
else if (t > 1.0f)
|
||||
t = 1.0f;
|
||||
r = t * t * (3.0f - 2.0f * t);
|
||||
delta = (float)fabs(r - outptr[vi]);
|
||||
t = (conv_to_dbl(x[vi]) - conv_to_dbl(edge0[i]))
|
||||
/ (conv_to_dbl(edge1[i]) - conv_to_dbl(edge0[i]));
|
||||
if (t < 0.0)
|
||||
t = 0.0;
|
||||
else if (t > 1.0)
|
||||
t = 1.0;
|
||||
r = t * t * (3.0 - 2.0 * t);
|
||||
delta = (float)fabs(r - conv_to_dbl(outptr[vi]));
|
||||
|
||||
if (!std::is_same<T, half>::value)
|
||||
{
|
||||
if (delta > MAX_ERR)
|
||||
{
|
||||
log_error("{%d, element %d}) verification error: "
|
||||
"smoothstep(%a, %a, %a) = *%a vs. %a\n",
|
||||
ii, j, x[vi], edge0[i], edge1[i], r, outptr[vi]);
|
||||
ii, j, x[vi], edge0[i], edge1[i], r,
|
||||
outptr[vi]);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
else
|
||||
max_delta = std::max(max_delta, delta);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// due to the fact that accuracy of smoothstep for cl_khr_fp16 is
|
||||
// implementation defined this test only reports maximum error without
|
||||
// testing maximum error threshold
|
||||
if (std::is_same<T, half>::value)
|
||||
log_error("smoothstep half verification result, max delta: %a\n",
|
||||
max_delta);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
template <typename T>
|
||||
int test_smoothstep_fn(cl_device_id device, cl_context context,
|
||||
cl_command_queue queue, int n_elems, bool vecParam)
|
||||
cl_command_queue queue, const int n_elems,
|
||||
const bool vecParam)
|
||||
{
|
||||
clMemWrapper streams[4];
|
||||
std::vector<T> input_ptr[3], output_ptr;
|
||||
@@ -170,6 +192,17 @@ int test_smoothstep_fn(cl_device_id device, cl_context context,
|
||||
input_ptr[2][i] = get_random_double(-0x20000000, 0x20000000, d);
|
||||
}
|
||||
}
|
||||
else if (std::is_same<T, half>::value)
|
||||
{
|
||||
pragma_str = "#pragma OPENCL EXTENSION cl_khr_fp16 : enable\n";
|
||||
for (i = 0; i < num_elements; i++)
|
||||
{
|
||||
input_ptr[0][i] = conv_to_half(get_random_float(-65503, 65503, d));
|
||||
input_ptr[1][i] = conv_to_half(
|
||||
get_random_float(conv_to_flt(input_ptr[0][i]), 65503, d));
|
||||
input_ptr[2][i] = conv_to_half(get_random_float(-65503, 65503, d));
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < 3; i++)
|
||||
{
|
||||
@@ -179,7 +212,7 @@ int test_smoothstep_fn(cl_device_id device, cl_context context,
|
||||
test_error(err, "Unable to write input buffer");
|
||||
}
|
||||
|
||||
char vecSizeNames[][3] = { "", "2", "4", "8", "16", "3" };
|
||||
const char vecSizeNames[][3] = { "", "2", "4", "8", "16", "3" };
|
||||
|
||||
for (i = 0; i < kTotalVecCount; i++)
|
||||
{
|
||||
@@ -190,14 +223,14 @@ int test_smoothstep_fn(cl_device_id device, cl_context context,
|
||||
{
|
||||
std::string str = smoothstep_fn_code_pattern_v3;
|
||||
kernelSource =
|
||||
string_format(str, pragma_str.c_str(), tname.c_str(),
|
||||
str_sprintf(str, pragma_str.c_str(), tname.c_str(),
|
||||
tname.c_str(), tname.c_str(), tname.c_str());
|
||||
}
|
||||
else
|
||||
{
|
||||
std::string str = smoothstep_fn_code_pattern_v3_scalar;
|
||||
kernelSource =
|
||||
string_format(str, pragma_str.c_str(), tname.c_str(),
|
||||
str_sprintf(str, pragma_str.c_str(), tname.c_str(),
|
||||
tname.c_str(), tname.c_str(), tname.c_str());
|
||||
}
|
||||
}
|
||||
@@ -206,11 +239,12 @@ int test_smoothstep_fn(cl_device_id device, cl_context context,
|
||||
// regular path
|
||||
std::string str = smoothstep_fn_code_pattern;
|
||||
kernelSource =
|
||||
string_format(str, pragma_str.c_str(), tname.c_str(),
|
||||
str_sprintf(str, pragma_str.c_str(), tname.c_str(),
|
||||
vecParam ? vecSizeNames[i] : "", tname.c_str(),
|
||||
vecParam ? vecSizeNames[i] : "", tname.c_str(),
|
||||
vecSizeNames[i], tname.c_str(), vecSizeNames[i]);
|
||||
}
|
||||
|
||||
const char *programPtr = kernelSource.c_str();
|
||||
err =
|
||||
create_single_kernel_helper(context, &programs[i], &kernels[i], 1,
|
||||
@@ -259,10 +293,15 @@ int test_smoothstep_fn(cl_device_id device, cl_context context,
|
||||
return err;
|
||||
}
|
||||
|
||||
|
||||
cl_int SmoothstepTest::Run()
|
||||
{
|
||||
cl_int error = CL_SUCCESS;
|
||||
if (is_extension_available(device, "cl_khr_fp16"))
|
||||
{
|
||||
error = test_smoothstep_fn<half>(device, context, queue, num_elems,
|
||||
vecParam);
|
||||
test_error(error, "SmoothstepTest::Run<cl_half> failed");
|
||||
}
|
||||
|
||||
error =
|
||||
test_smoothstep_fn<float>(device, context, queue, num_elems, vecParam);
|
||||
@@ -278,7 +317,6 @@ cl_int SmoothstepTest::Run()
|
||||
return error;
|
||||
}
|
||||
|
||||
|
||||
int test_smoothstep(cl_device_id device, cl_context context,
|
||||
cl_command_queue queue, int n_elems)
|
||||
{
|
||||
@@ -286,7 +324,6 @@ int test_smoothstep(cl_device_id device, cl_context context,
|
||||
"smoothstep", true);
|
||||
}
|
||||
|
||||
|
||||
int test_smoothstepf(cl_device_id device, cl_context context,
|
||||
cl_command_queue queue, int n_elems)
|
||||
{
|
||||
|
||||
@@ -18,10 +18,11 @@
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include "harness/stringHelpers.h"
|
||||
|
||||
#include "procs.h"
|
||||
#include "test_base.h"
|
||||
|
||||
|
||||
const char *step_fn_code_pattern = "%s\n" /* optional pragma */
|
||||
"__kernel void test_fn(__global %s%s *edge, "
|
||||
"__global %s%s *x, __global %s%s *dst)\n"
|
||||
@@ -48,7 +49,6 @@ const char *step_fn_code_pattern_v3_scalar =
|
||||
" vstore3(step(edge[tid], vload3(tid,x)), tid, dst);\n"
|
||||
"}\n";
|
||||
|
||||
|
||||
namespace {
|
||||
|
||||
template <typename T>
|
||||
@@ -62,8 +62,8 @@ int verify_step(const T *const inptrA, const T *const inptrB,
|
||||
{
|
||||
for (int i = 0; i < n * veclen; i++)
|
||||
{
|
||||
r = (inptrB[i] < inptrA[i]) ? 0.0 : 1.0;
|
||||
if (r != outptr[i]) return -1;
|
||||
r = (conv_to_dbl(inptrB[i]) < conv_to_dbl(inptrA[i])) ? 0.0 : 1.0;
|
||||
if (r != conv_to_dbl(outptr[i])) return -1;
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -73,10 +73,19 @@ int verify_step(const T *const inptrA, const T *const inptrB,
|
||||
int ii = i / veclen;
|
||||
for (int j = 0; j < veclen && i < n; ++j, ++i)
|
||||
{
|
||||
r = (inptrB[i] < inptrA[ii]) ? 0.0f : 1.0f;
|
||||
if (r != outptr[i])
|
||||
r = (conv_to_dbl(inptrB[i]) < conv_to_dbl(inptrA[ii])) ? 0.0f
|
||||
: 1.0f;
|
||||
if (r != conv_to_dbl(outptr[i]))
|
||||
{
|
||||
log_error("Failure @ {%d, element %d}: step(%a,%a) -> *%a "
|
||||
if (std::is_same<T, half>::value)
|
||||
log_error(
|
||||
"Failure @ {%d, element %d}: step(%a,%a) -> *%a "
|
||||
"vs %a\n",
|
||||
ii, j, conv_to_flt(inptrA[ii]),
|
||||
conv_to_flt(inptrB[i]), r, conv_to_flt(outptr[i]));
|
||||
else
|
||||
log_error(
|
||||
"Failure @ {%d, element %d}: step(%a,%a) -> *%a "
|
||||
"vs %a\n",
|
||||
ii, j, inptrA[ii], inptrB[i], r, outptr[i]);
|
||||
return -1;
|
||||
@@ -84,13 +93,11 @@ int verify_step(const T *const inptrA, const T *const inptrB,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
template <typename T>
|
||||
int test_step_fn(cl_device_id device, cl_context context,
|
||||
cl_command_queue queue, int n_elems, bool vecParam)
|
||||
@@ -140,6 +147,16 @@ int test_step_fn(cl_device_id device, cl_context context,
|
||||
input_ptr[1][i] = get_random_double(-0x40000000, 0x40000000, d);
|
||||
}
|
||||
}
|
||||
else if (std::is_same<T, half>::value)
|
||||
{
|
||||
const float fval = CL_HALF_MAX;
|
||||
pragma_str = "#pragma OPENCL EXTENSION cl_khr_fp16 : enable\n";
|
||||
for (i = 0; i < num_elements; i++)
|
||||
{
|
||||
input_ptr[0][i] = conv_to_half(get_random_float(-fval, fval, d));
|
||||
input_ptr[1][i] = conv_to_half(get_random_float(-fval, fval, d));
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < 2; i++)
|
||||
{
|
||||
@@ -160,14 +177,14 @@ int test_step_fn(cl_device_id device, cl_context context,
|
||||
{
|
||||
std::string str = step_fn_code_pattern_v3;
|
||||
kernelSource =
|
||||
string_format(str, pragma_str.c_str(), tname.c_str(),
|
||||
str_sprintf(str, pragma_str.c_str(), tname.c_str(),
|
||||
tname.c_str(), tname.c_str());
|
||||
}
|
||||
else
|
||||
{
|
||||
std::string str = step_fn_code_pattern_v3_scalar;
|
||||
kernelSource =
|
||||
string_format(str, pragma_str.c_str(), tname.c_str(),
|
||||
str_sprintf(str, pragma_str.c_str(), tname.c_str(),
|
||||
tname.c_str(), tname.c_str());
|
||||
}
|
||||
}
|
||||
@@ -176,7 +193,7 @@ int test_step_fn(cl_device_id device, cl_context context,
|
||||
// regular path
|
||||
std::string str = step_fn_code_pattern;
|
||||
kernelSource =
|
||||
string_format(str, pragma_str.c_str(), tname.c_str(),
|
||||
str_sprintf(str, pragma_str.c_str(), tname.c_str(),
|
||||
vecParam ? vecSizeNames[i] : "", tname.c_str(),
|
||||
vecSizeNames[i], tname.c_str(), vecSizeNames[i]);
|
||||
}
|
||||
@@ -229,10 +246,14 @@ int test_step_fn(cl_device_id device, cl_context context,
|
||||
return err;
|
||||
}
|
||||
|
||||
|
||||
cl_int StepTest::Run()
|
||||
{
|
||||
cl_int error = CL_SUCCESS;
|
||||
if (is_extension_available(device, "cl_khr_fp16"))
|
||||
{
|
||||
error = test_step_fn<half>(device, context, queue, num_elems, vecParam);
|
||||
test_error(error, "StepTest::Run<cl_half> failed");
|
||||
}
|
||||
|
||||
error = test_step_fn<float>(device, context, queue, num_elems, vecParam);
|
||||
test_error(error, "StepTest::Run<float> failed");
|
||||
@@ -247,7 +268,6 @@ cl_int StepTest::Run()
|
||||
return error;
|
||||
}
|
||||
|
||||
|
||||
int test_step(cl_device_id device, cl_context context, cl_command_queue queue,
|
||||
int n_elems)
|
||||
{
|
||||
@@ -255,7 +275,6 @@ int test_step(cl_device_id device, cl_context context, cl_command_queue queue,
|
||||
true);
|
||||
}
|
||||
|
||||
|
||||
int test_stepf(cl_device_id device, cl_context context, cl_command_queue queue,
|
||||
int n_elems)
|
||||
{
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
#include <vector>
|
||||
|
||||
#include "harness/deviceInfo.h"
|
||||
#include "harness/stringHelpers.h"
|
||||
#include "harness/typeWrappers.h"
|
||||
|
||||
#include "procs.h"
|
||||
@@ -30,7 +31,6 @@
|
||||
#define M_PI 3.14159265358979323846264338327950288
|
||||
#endif
|
||||
|
||||
|
||||
// clang-format off
|
||||
const char *unary_fn_code_pattern =
|
||||
"%s\n" /* optional pragma */
|
||||
@@ -51,23 +51,10 @@ const char *unary_fn_code_pattern_v3 =
|
||||
"}\n";
|
||||
// clang-format on
|
||||
|
||||
|
||||
#define MAX_ERR 2.0f
|
||||
|
||||
namespace {
|
||||
|
||||
|
||||
template <typename T> float UlpFn(const T &val, const double &r)
|
||||
{
|
||||
if (std::is_same<T, double>::value)
|
||||
return Ulp_Error_Double(val, r);
|
||||
else if (std::is_same<T, float>::value)
|
||||
return Ulp_Error(val, r);
|
||||
else if (std::is_same<T, half>::value)
|
||||
return Ulp_Error(val, r);
|
||||
}
|
||||
|
||||
|
||||
template <typename T>
|
||||
int verify_degrees(const T *const inptr, const T *const outptr, int n)
|
||||
{
|
||||
@@ -77,7 +64,11 @@ int verify_degrees(const T *const inptr, const T *const outptr, int n)
|
||||
|
||||
for (int i = 0, j = 0; i < n; i++, j++)
|
||||
{
|
||||
r = (180.0 / M_PI) * inptr[i];
|
||||
r = (180.0 / M_PI) * conv_to_dbl(inptr[i]);
|
||||
|
||||
if (std::is_same<T, half>::value)
|
||||
if (!isfinite_fp(conv_to_half(r)) && !isfinite_fp(outptr[i]))
|
||||
continue;
|
||||
|
||||
error = UlpFn(outptr[i], r);
|
||||
|
||||
@@ -88,13 +79,25 @@ int verify_degrees(const T *const inptr, const T *const outptr, int n)
|
||||
max_val = r;
|
||||
if (fabsf(error) > MAX_ERR)
|
||||
{
|
||||
log_error("%d) Error @ %a: *%a vs %a (*%g vs %g) ulps: %f\n",
|
||||
i, inptr[i], r, outptr[i], r, outptr[i], error);
|
||||
if (std::is_same<T, half>::value)
|
||||
log_error(
|
||||
"%d) Error @ %a: *%a vs %a (*%g vs %g) ulps: %f\n", i,
|
||||
conv_to_flt(inptr[i]), r, conv_to_flt(outptr[i]), r,
|
||||
conv_to_flt(outptr[i]), error);
|
||||
else
|
||||
log_error(
|
||||
"%d) Error @ %a: *%a vs %a (*%g vs %g) ulps: %f\n", i,
|
||||
inptr[i], r, outptr[i], r, outptr[i], error);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (std::is_same<T, half>::value)
|
||||
log_info("degrees: Max error %f ulps at %d: *%a vs %a (*%g vs %g)\n",
|
||||
max_error, max_index, max_val, conv_to_flt(outptr[max_index]),
|
||||
max_val, conv_to_flt(outptr[max_index]));
|
||||
else
|
||||
log_info("degrees: Max error %f ulps at %d: *%a vs %a (*%g vs %g)\n",
|
||||
max_error, max_index, max_val, outptr[max_index], max_val,
|
||||
outptr[max_index]);
|
||||
@@ -102,7 +105,6 @@ int verify_degrees(const T *const inptr, const T *const outptr, int n)
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
template <typename T>
|
||||
int verify_radians(const T *const inptr, const T *const outptr, int n)
|
||||
{
|
||||
@@ -112,8 +114,14 @@ int verify_radians(const T *const inptr, const T *const outptr, int n)
|
||||
|
||||
for (int i = 0, j = 0; i < n; i++, j++)
|
||||
{
|
||||
r = (M_PI / 180.0) * inptr[i];
|
||||
error = Ulp_Error(outptr[i], r);
|
||||
r = (M_PI / 180.0) * conv_to_dbl(inptr[i]);
|
||||
|
||||
if (std::is_same<T, half>::value)
|
||||
if (!isfinite_fp(conv_to_half(r)) && !isfinite_fp(outptr[i]))
|
||||
continue;
|
||||
|
||||
error = UlpFn(outptr[i], r);
|
||||
|
||||
if (fabsf(error) > max_error)
|
||||
{
|
||||
max_error = error;
|
||||
@@ -121,13 +129,25 @@ int verify_radians(const T *const inptr, const T *const outptr, int n)
|
||||
max_val = r;
|
||||
if (fabsf(error) > MAX_ERR)
|
||||
{
|
||||
log_error("%d) Error @ %a: *%a vs %a (*%g vs %g) ulps: %f\n",
|
||||
i, inptr[i], r, outptr[i], r, outptr[i], error);
|
||||
if (std::is_same<T, half>::value)
|
||||
log_error(
|
||||
"%d) Error @ %a: *%a vs %a (*%g vs %g) ulps: %f\n", i,
|
||||
conv_to_flt(inptr[i]), r, conv_to_flt(outptr[i]), r,
|
||||
conv_to_flt(outptr[i]), error);
|
||||
else
|
||||
log_error(
|
||||
"%d) Error @ %a: *%a vs %a (*%g vs %g) ulps: %f\n", i,
|
||||
inptr[i], r, outptr[i], r, outptr[i], error);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (std::is_same<T, half>::value)
|
||||
log_info("radians: Max error %f ulps at %d: *%a vs %a (*%g vs %g)\n",
|
||||
max_error, max_index, max_val, conv_to_flt(outptr[max_index]),
|
||||
max_val, conv_to_flt(outptr[max_index]));
|
||||
else
|
||||
log_info("radians: Max error %f ulps at %d: *%a vs %a (*%g vs %g)\n",
|
||||
max_error, max_index, max_val, outptr[max_index], max_val,
|
||||
outptr[max_index]);
|
||||
@@ -135,27 +155,25 @@ int verify_radians(const T *const inptr, const T *const outptr, int n)
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
template <typename T>
|
||||
int verify_sign(const T *const inptr, const T *const outptr, int n)
|
||||
{
|
||||
T r = 0;
|
||||
double r = 0;
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
if (inptr[i] > 0.0f)
|
||||
if (conv_to_dbl(inptr[i]) > 0.0f)
|
||||
r = 1.0;
|
||||
else if (inptr[i] < 0.0f)
|
||||
else if (conv_to_dbl(inptr[i]) < 0.0f)
|
||||
r = -1.0;
|
||||
else
|
||||
r = 0.0;
|
||||
if (r != outptr[i]) return -1;
|
||||
if (r != conv_to_dbl(outptr[i])) return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
template <typename T>
|
||||
int test_unary_fn(cl_device_id device, cl_context context,
|
||||
cl_command_queue queue, int n_elems,
|
||||
@@ -207,31 +225,36 @@ int test_unary_fn(cl_device_id device, cl_context context,
|
||||
get_random_double(-100000.0 * M_PI, 100000.0 * M_PI, d);
|
||||
}
|
||||
}
|
||||
else if (std::is_same<T, half>::value)
|
||||
{
|
||||
pragma_str = "#pragma OPENCL EXTENSION cl_khr_fp16 : enable\n";
|
||||
for (int j = 0; j < num_elements; j++)
|
||||
{
|
||||
input_ptr[j] = conv_to_half(get_random_float(
|
||||
(float)(-10000.f * M_PI), (float)(10000.f * M_PI), d));
|
||||
}
|
||||
}
|
||||
|
||||
err = clEnqueueWriteBuffer(queue, streams[0], true, 0,
|
||||
sizeof(T) * num_elements, &input_ptr.front(), 0,
|
||||
NULL, NULL);
|
||||
if (err != CL_SUCCESS)
|
||||
{
|
||||
log_error("clEnqueueWriteBuffer failed\n");
|
||||
return -1;
|
||||
}
|
||||
test_error(err, "clEnqueueWriteBuffer failed\n");
|
||||
|
||||
for (i = 0; i < kTotalVecCount; i++)
|
||||
{
|
||||
std::string kernelSource;
|
||||
char vecSizeNames[][3] = { "", "2", "4", "8", "16", "3" };
|
||||
const char vecSizeNames[][3] = { "", "2", "4", "8", "16", "3" };
|
||||
|
||||
if (i >= kVectorSizeCount)
|
||||
{
|
||||
std::string str = unary_fn_code_pattern_v3;
|
||||
kernelSource = string_format(str, pragma_str.c_str(), tname.c_str(),
|
||||
kernelSource = str_sprintf(str, pragma_str.c_str(), tname.c_str(),
|
||||
tname.c_str(), fnName.c_str());
|
||||
}
|
||||
else
|
||||
{
|
||||
std::string str = unary_fn_code_pattern;
|
||||
kernelSource = string_format(str, pragma_str.c_str(), tname.c_str(),
|
||||
kernelSource = str_sprintf(str, pragma_str.c_str(), tname.c_str(),
|
||||
vecSizeNames[i], tname.c_str(),
|
||||
vecSizeNames[i], fnName.c_str());
|
||||
}
|
||||
@@ -290,10 +313,17 @@ int test_unary_fn(cl_device_id device, cl_context context,
|
||||
return err;
|
||||
}
|
||||
|
||||
|
||||
cl_int DegreesTest::Run()
|
||||
{
|
||||
cl_int error = test_unary_fn<float>(device, context, queue, num_elems,
|
||||
cl_int error = CL_SUCCESS;
|
||||
if (is_extension_available(device, "cl_khr_fp16"))
|
||||
{
|
||||
error = test_unary_fn<half>(device, context, queue, num_elems,
|
||||
fnName.c_str(), verify_degrees<half>);
|
||||
test_error(error, "DegreesTest::Run<cl_half> failed");
|
||||
}
|
||||
|
||||
error = test_unary_fn<float>(device, context, queue, num_elems,
|
||||
fnName.c_str(), verify_degrees<float>);
|
||||
test_error(error, "DegreesTest::Run<float> failed");
|
||||
|
||||
@@ -307,10 +337,17 @@ cl_int DegreesTest::Run()
|
||||
return error;
|
||||
}
|
||||
|
||||
|
||||
cl_int RadiansTest::Run()
|
||||
{
|
||||
cl_int error = test_unary_fn<float>(device, context, queue, num_elems,
|
||||
cl_int error = CL_SUCCESS;
|
||||
if (is_extension_available(device, "cl_khr_fp16"))
|
||||
{
|
||||
error = test_unary_fn<half>(device, context, queue, num_elems,
|
||||
fnName.c_str(), verify_radians<half>);
|
||||
test_error(error, "RadiansTest::Run<cl_half> failed");
|
||||
}
|
||||
|
||||
error = test_unary_fn<float>(device, context, queue, num_elems,
|
||||
fnName.c_str(), verify_radians<float>);
|
||||
test_error(error, "RadiansTest::Run<float> failed");
|
||||
|
||||
@@ -324,10 +361,17 @@ cl_int RadiansTest::Run()
|
||||
return error;
|
||||
}
|
||||
|
||||
|
||||
cl_int SignTest::Run()
|
||||
{
|
||||
cl_int error = test_unary_fn<float>(device, context, queue, num_elems,
|
||||
cl_int error = CL_SUCCESS;
|
||||
if (is_extension_available(device, "cl_khr_fp16"))
|
||||
{
|
||||
error = test_unary_fn<half>(device, context, queue, num_elems,
|
||||
fnName.c_str(), verify_sign<half>);
|
||||
test_error(error, "SignTest::Run<cl_half> failed");
|
||||
}
|
||||
|
||||
error = test_unary_fn<float>(device, context, queue, num_elems,
|
||||
fnName.c_str(), verify_sign<float>);
|
||||
test_error(error, "SignTest::Run<float> failed");
|
||||
|
||||
@@ -341,7 +385,6 @@ cl_int SignTest::Run()
|
||||
return error;
|
||||
}
|
||||
|
||||
|
||||
int test_degrees(cl_device_id device, cl_context context,
|
||||
cl_command_queue queue, int n_elems)
|
||||
{
|
||||
@@ -349,7 +392,6 @@ int test_degrees(cl_device_id device, cl_context context,
|
||||
"degrees");
|
||||
}
|
||||
|
||||
|
||||
int test_radians(cl_device_id device, cl_context context,
|
||||
cl_command_queue queue, int n_elems)
|
||||
{
|
||||
@@ -357,7 +399,6 @@ int test_radians(cl_device_id device, cl_context context,
|
||||
"radians");
|
||||
}
|
||||
|
||||
|
||||
int test_sign(cl_device_id device, cl_context context, cl_command_queue queue,
|
||||
int n_elems)
|
||||
{
|
||||
|
||||
@@ -22,6 +22,8 @@
|
||||
#include <stdexcept>
|
||||
#include <vector>
|
||||
|
||||
#include "harness/stringHelpers.h"
|
||||
|
||||
#include <CL/cl_half.h>
|
||||
|
||||
#include "test_comparisons_fp.h"
|
||||
@@ -83,29 +85,6 @@ extension,
|
||||
// clang-format on
|
||||
|
||||
|
||||
std::string concat_kernel(const char* sstr[], int num)
|
||||
{
|
||||
std::string res;
|
||||
for (int i = 0; i < num; i++) res += std::string(sstr[i]);
|
||||
return res;
|
||||
}
|
||||
|
||||
template <typename... Args>
|
||||
std::string string_format(const std::string& format, Args... args)
|
||||
{
|
||||
int size_s = std::snprintf(nullptr, 0, format.c_str(), args...)
|
||||
+ 1; // Extra space for '\0'
|
||||
if (size_s <= 0)
|
||||
{
|
||||
throw std::runtime_error("Error during formatting.");
|
||||
}
|
||||
auto size = static_cast<size_t>(size_s);
|
||||
std::unique_ptr<char[]> buf(new char[size]);
|
||||
std::snprintf(buf.get(), size, format.c_str(), args...);
|
||||
return std::string(buf.get(),
|
||||
buf.get() + size - 1); // We don't want the '\0' inside
|
||||
}
|
||||
|
||||
template <typename T, typename F> bool verify(const T& A, const T& B)
|
||||
{
|
||||
return F()(A, B);
|
||||
@@ -226,14 +205,14 @@ int RelationalsFPTest::test_equiv_kernel(unsigned int vecSize,
|
||||
auto str =
|
||||
concat_kernel(equivTestKerPat_3,
|
||||
sizeof(equivTestKerPat_3) / sizeof(const char*));
|
||||
kernelSource = string_format(str, fnName.c_str(), opName.c_str());
|
||||
kernelSource = str_sprintf(str, fnName.c_str(), opName.c_str());
|
||||
}
|
||||
else
|
||||
{
|
||||
auto str = concat_kernel(equivTestKerPatLessGreater_3,
|
||||
sizeof(equivTestKerPatLessGreater_3)
|
||||
/ sizeof(const char*));
|
||||
kernelSource = string_format(str, fnName.c_str());
|
||||
kernelSource = str_sprintf(str, fnName.c_str());
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -243,14 +222,14 @@ int RelationalsFPTest::test_equiv_kernel(unsigned int vecSize,
|
||||
auto str =
|
||||
concat_kernel(equivTestKernPat,
|
||||
sizeof(equivTestKernPat) / sizeof(const char*));
|
||||
kernelSource = string_format(str, fnName.c_str(), opName.c_str());
|
||||
kernelSource = str_sprintf(str, fnName.c_str(), opName.c_str());
|
||||
}
|
||||
else
|
||||
{
|
||||
auto str = concat_kernel(equivTestKernPatLessGreater,
|
||||
sizeof(equivTestKernPatLessGreater)
|
||||
/ sizeof(const char*));
|
||||
kernelSource = string_format(str, fnName.c_str());
|
||||
kernelSource = str_sprintf(str, fnName.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user