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_fpmath from basic (#1718)
* Added half and double support for fpmath test from basic (issue #142, basic) * Cosmetic corrections due to code review * Removed unnecessary casting * Added corrections due to code review * Tuning range of input generation to avoid hitting infinity * Moved string helpers procedures due to request from test_commonfns PR #1695
This commit is contained in:
@@ -2,7 +2,7 @@ set(MODULE_NAME BASIC)
|
||||
|
||||
set(${MODULE_NAME}_SOURCES
|
||||
main.cpp
|
||||
test_fpmath_float.cpp
|
||||
test_fpmath.cpp
|
||||
test_intmath.cpp
|
||||
test_hiloeo.cpp test_local.cpp test_pointercast.cpp
|
||||
test_if.cpp test_loop.cpp
|
||||
|
||||
@@ -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,14 +22,15 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <CL/cl_half.h>
|
||||
|
||||
#include "harness/testHarness.h"
|
||||
#include "procs.h"
|
||||
|
||||
test_definition test_list[] = {
|
||||
ADD_TEST(hostptr),
|
||||
ADD_TEST(fpmath_float),
|
||||
ADD_TEST(fpmath_float2),
|
||||
ADD_TEST(fpmath_float4),
|
||||
ADD_TEST(fpmath),
|
||||
ADD_TEST(intmath_int),
|
||||
ADD_TEST(intmath_int2),
|
||||
ADD_TEST(intmath_int4),
|
||||
@@ -164,9 +165,35 @@ test_definition test_list[] = {
|
||||
};
|
||||
|
||||
const int test_num = ARRAY_SIZE( test_list );
|
||||
cl_half_rounding_mode halfRoundingMode = CL_HALF_RTE;
|
||||
|
||||
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)
|
||||
{
|
||||
halfRoundingMode = CL_HALF_RTE;
|
||||
}
|
||||
else if ((fpConfigHalf & CL_FP_ROUND_TO_ZERO) != 0)
|
||||
{
|
||||
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[])
|
||||
{
|
||||
return runTestHarness(argc, argv, test_num, test_list, false, 0);
|
||||
return runTestHarnessWithCheck(argc, argv, test_num, test_list, false, 0,
|
||||
InitCL);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
//
|
||||
// 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.
|
||||
// You may obtain a copy of the License at
|
||||
@@ -13,6 +13,7 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
#include "harness/kernelHelpers.h"
|
||||
#include "harness/testHarness.h"
|
||||
#include "harness/errorHelpers.h"
|
||||
@@ -21,9 +22,8 @@
|
||||
#include "harness/rounding_mode.h"
|
||||
|
||||
extern int test_hostptr(cl_device_id deviceID, cl_context context, cl_command_queue queue, int num_elements);
|
||||
extern int test_fpmath_float(cl_device_id deviceID, cl_context context, cl_command_queue queue, int num_elements);
|
||||
extern int test_fpmath_float2(cl_device_id deviceID, cl_context context, cl_command_queue queue, int num_elements);
|
||||
extern int test_fpmath_float4(cl_device_id deviceID, cl_context context, cl_command_queue queue, int num_elements);
|
||||
extern int test_fpmath(cl_device_id deviceID, cl_context context,
|
||||
cl_command_queue queue, int num_elements);
|
||||
extern int test_intmath_int(cl_device_id deviceID, cl_context context, cl_command_queue queue, int num_elements);
|
||||
extern int test_intmath_int2(cl_device_id deviceID, cl_context context, cl_command_queue queue, int num_elements);
|
||||
extern int test_intmath_int4(cl_device_id deviceID, cl_context context, cl_command_queue queue, int num_elements);
|
||||
|
||||
@@ -14,6 +14,9 @@
|
||||
// limitations under the License.
|
||||
//
|
||||
#include "harness/compat.h"
|
||||
#include "harness/conversions.h"
|
||||
#include "harness/stringHelpers.h"
|
||||
#include "harness/typeWrappers.h"
|
||||
|
||||
#include <limits.h>
|
||||
#include <stdio.h>
|
||||
@@ -22,11 +25,7 @@
|
||||
#include <sys/stat.h>
|
||||
#include <vector>
|
||||
|
||||
#include "harness/conversions.h"
|
||||
#include "harness/typeWrappers.h"
|
||||
|
||||
#include "procs.h"
|
||||
#include "utils.h"
|
||||
|
||||
// clang-format off
|
||||
|
||||
|
||||
386
test_conformance/basic/test_fpmath.cpp
Normal file
386
test_conformance/basic/test_fpmath.cpp
Normal file
@@ -0,0 +1,386 @@
|
||||
//
|
||||
// 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.
|
||||
// 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 "harness/rounding_mode.h"
|
||||
#include "harness/stringHelpers.h"
|
||||
|
||||
#include <CL/cl_half.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "procs.h"
|
||||
|
||||
static const char *fp_kernel_code = R"(
|
||||
%s
|
||||
__kernel void test_fp(__global TYPE *srcA, __global TYPE *srcB, __global TYPE *dst)
|
||||
{
|
||||
int tid = get_global_id(0);
|
||||
|
||||
dst[tid] = srcA[tid] OP srcB[tid];
|
||||
})";
|
||||
|
||||
extern cl_half_rounding_mode halfRoundingMode;
|
||||
|
||||
#define HFF(num) cl_half_from_float(num, halfRoundingMode)
|
||||
#define HTF(num) cl_half_to_float(num)
|
||||
|
||||
template <typename T> double toDouble(T val)
|
||||
{
|
||||
if (std::is_same<cl_half, T>::value)
|
||||
return HTF(val);
|
||||
else
|
||||
return val;
|
||||
}
|
||||
|
||||
bool isHalfNan(cl_half v)
|
||||
{
|
||||
// Extract FP16 exponent and mantissa
|
||||
uint16_t h_exp = (v >> (CL_HALF_MANT_DIG - 1)) & 0x1F;
|
||||
uint16_t h_mant = v & 0x3FF;
|
||||
|
||||
// NaN test
|
||||
return (h_exp == 0x1F && h_mant != 0);
|
||||
}
|
||||
|
||||
cl_half half_plus(cl_half a, cl_half b)
|
||||
{
|
||||
return HFF(std::plus<float>()(HTF(a), HTF(b)));
|
||||
}
|
||||
|
||||
cl_half half_minus(cl_half a, cl_half b)
|
||||
{
|
||||
return HFF(std::minus<float>()(HTF(a), HTF(b)));
|
||||
}
|
||||
|
||||
cl_half half_mult(cl_half a, cl_half b)
|
||||
{
|
||||
return HFF(std::multiplies<float>()(HTF(a), HTF(b)));
|
||||
}
|
||||
|
||||
template <typename T> struct TestDef
|
||||
{
|
||||
const char op;
|
||||
std::function<T(T, T)> ref;
|
||||
std::string type_str;
|
||||
size_t vec_size;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
int verify_fp(std::vector<T> (&input)[2], std::vector<T> &output,
|
||||
const TestDef<T> &test)
|
||||
{
|
||||
auto &inA = input[0];
|
||||
auto &inB = input[1];
|
||||
for (int i = 0; i < output.size(); i++)
|
||||
{
|
||||
bool nan_test = false;
|
||||
|
||||
T r = test.ref(inA[i], inB[i]);
|
||||
|
||||
if (std::is_same<T, cl_half>::value)
|
||||
nan_test = !(isHalfNan(r) && isHalfNan(output[i]));
|
||||
|
||||
if (r != output[i] && nan_test)
|
||||
{
|
||||
log_error("FP math test for type: %s, vec size: %zu, failed at "
|
||||
"index %d, %a '%c' %a, expected %a, get %a\n",
|
||||
test.type_str.c_str(), test.vec_size, i, toDouble(inA[i]),
|
||||
test.op, toDouble(inB[i]), toDouble(r),
|
||||
toDouble(output[i]));
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
template <typename T> void generate_random_inputs(std::vector<T> (&input)[2])
|
||||
{
|
||||
RandomSeed seed(gRandomSeed);
|
||||
|
||||
if (std::is_same<T, float>::value)
|
||||
{
|
||||
auto random_generator = [&seed]() {
|
||||
return get_random_float(-MAKE_HEX_FLOAT(0x1.0p31f, 0x1, 31),
|
||||
MAKE_HEX_FLOAT(0x1.0p31f, 0x1, 31), seed);
|
||||
};
|
||||
for (auto &v : input)
|
||||
std::generate(v.begin(), v.end(), random_generator);
|
||||
}
|
||||
else if (std::is_same<T, double>::value)
|
||||
{
|
||||
auto random_generator = [&seed]() {
|
||||
return get_random_double(-MAKE_HEX_DOUBLE(0x1.0p63, 0x1LL, 63),
|
||||
MAKE_HEX_DOUBLE(0x1.0p63, 0x1LL, 63),
|
||||
seed);
|
||||
};
|
||||
for (auto &v : input)
|
||||
std::generate(v.begin(), v.end(), random_generator);
|
||||
}
|
||||
else
|
||||
{
|
||||
auto random_generator = [&seed]() {
|
||||
return HFF(get_random_float(-MAKE_HEX_FLOAT(0x1.0p8f, 0x1, 8),
|
||||
MAKE_HEX_FLOAT(0x1.0p8f, 0x1, 8),
|
||||
seed));
|
||||
};
|
||||
for (auto &v : input)
|
||||
std::generate(v.begin(), v.end(), random_generator);
|
||||
}
|
||||
}
|
||||
|
||||
struct TypesIterator
|
||||
{
|
||||
using TypeIter = std::tuple<cl_float, cl_half, cl_double>;
|
||||
|
||||
TypesIterator(cl_device_id deviceID, cl_context context,
|
||||
cl_command_queue queue, int num_elems)
|
||||
: context(context), queue(queue), fpConfigHalf(0), fpConfigFloat(0),
|
||||
num_elements(num_elems)
|
||||
{
|
||||
// typeid().name one day
|
||||
type2name[sizeof(cl_half)] = "half";
|
||||
type2name[sizeof(cl_float)] = "float";
|
||||
type2name[sizeof(cl_double)] = "double";
|
||||
|
||||
fp16Support = is_extension_available(deviceID, "cl_khr_fp16");
|
||||
fp64Support = is_extension_available(deviceID, "cl_khr_fp64");
|
||||
|
||||
fpConfigFloat = get_default_rounding_mode(deviceID);
|
||||
|
||||
if (fp16Support)
|
||||
fpConfigHalf =
|
||||
get_default_rounding_mode(deviceID, CL_DEVICE_HALF_FP_CONFIG);
|
||||
|
||||
for_each_elem(it);
|
||||
}
|
||||
|
||||
template <typename T> int test_fpmath(TestDef<T> &test)
|
||||
{
|
||||
constexpr size_t vecSizes[] = { 1, 2, 4, 8, 16 };
|
||||
cl_int err = CL_SUCCESS;
|
||||
|
||||
std::ostringstream sstr;
|
||||
if (std::is_same<T, double>::value)
|
||||
sstr << "#pragma OPENCL EXTENSION cl_khr_fp64 : enable\n";
|
||||
|
||||
if (std::is_same<T, cl_half>::value)
|
||||
sstr << "#pragma OPENCL EXTENSION cl_khr_fp16 : enable\n";
|
||||
|
||||
std::string program_source =
|
||||
str_sprintf(std::string(fp_kernel_code), sstr.str().c_str());
|
||||
|
||||
for (unsigned i = 0; i < ARRAY_SIZE(vecSizes); i++)
|
||||
{
|
||||
test.vec_size = vecSizes[i];
|
||||
|
||||
std::ostringstream vecNameStr;
|
||||
vecNameStr << test.type_str;
|
||||
if (test.vec_size != 1) vecNameStr << test.vec_size;
|
||||
|
||||
clMemWrapper streams[3];
|
||||
clProgramWrapper program;
|
||||
clKernelWrapper kernel;
|
||||
|
||||
size_t length = sizeof(T) * num_elements * test.vec_size;
|
||||
|
||||
bool isRTZ = false;
|
||||
RoundingMode oldMode = kDefaultRoundingMode;
|
||||
|
||||
|
||||
// If we only support rtz mode
|
||||
if (std::is_same<T, cl_half>::value)
|
||||
{
|
||||
if (CL_FP_ROUND_TO_ZERO == fpConfigHalf)
|
||||
{
|
||||
isRTZ = true;
|
||||
oldMode = get_round();
|
||||
}
|
||||
}
|
||||
else if (std::is_same<T, float>::value)
|
||||
{
|
||||
if (CL_FP_ROUND_TO_ZERO == fpConfigFloat)
|
||||
{
|
||||
isRTZ = true;
|
||||
oldMode = get_round();
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<T> inputs[]{
|
||||
std::vector<T>(test.vec_size * num_elements),
|
||||
std::vector<T>(test.vec_size * num_elements)
|
||||
};
|
||||
std::vector<T> output =
|
||||
std::vector<T>(test.vec_size * num_elements);
|
||||
|
||||
generate_random_inputs<T>(inputs);
|
||||
|
||||
for (int i = 0; i < ARRAY_SIZE(streams); i++)
|
||||
{
|
||||
streams[i] = clCreateBuffer(context, CL_MEM_READ_WRITE, length,
|
||||
NULL, &err);
|
||||
test_error(err, "clCreateBuffer failed.");
|
||||
}
|
||||
for (int i = 0; i < ARRAY_SIZE(inputs); i++)
|
||||
{
|
||||
err =
|
||||
clEnqueueWriteBuffer(queue, streams[i], CL_TRUE, 0, length,
|
||||
inputs[i].data(), 0, NULL, NULL);
|
||||
test_error(err, "clEnqueueWriteBuffer failed.");
|
||||
}
|
||||
|
||||
std::string build_options = "-DTYPE=";
|
||||
build_options.append(vecNameStr.str())
|
||||
.append(" -DOP=")
|
||||
.append(1, test.op);
|
||||
|
||||
const char *ptr = program_source.c_str();
|
||||
err =
|
||||
create_single_kernel_helper(context, &program, &kernel, 1, &ptr,
|
||||
"test_fp", build_options.c_str());
|
||||
|
||||
test_error(err, "create_single_kernel_helper failed");
|
||||
|
||||
for (int i = 0; i < ARRAY_SIZE(streams); i++)
|
||||
{
|
||||
err =
|
||||
clSetKernelArg(kernel, i, sizeof(streams[i]), &streams[i]);
|
||||
test_error(err, "clSetKernelArgs failed.");
|
||||
}
|
||||
|
||||
size_t threads[] = { static_cast<size_t>(num_elements) };
|
||||
err = clEnqueueNDRangeKernel(queue, kernel, 1, NULL, threads, NULL,
|
||||
0, NULL, NULL);
|
||||
test_error(err, "clEnqueueNDRangeKernel failed.");
|
||||
|
||||
err = clEnqueueReadBuffer(queue, streams[2], CL_TRUE, 0, length,
|
||||
output.data(), 0, NULL, NULL);
|
||||
test_error(err, "clEnqueueReadBuffer failed.");
|
||||
|
||||
if (isRTZ) set_round(kRoundTowardZero, kfloat);
|
||||
|
||||
err = verify_fp(inputs, output, test);
|
||||
|
||||
if (isRTZ) set_round(oldMode, kfloat);
|
||||
|
||||
test_error(err, "test verification failed");
|
||||
log_info("FP '%c' '%s' test passed\n", test.op,
|
||||
vecNameStr.str().c_str());
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
template <typename T> int test_fpmath_common()
|
||||
{
|
||||
int err = TEST_PASS;
|
||||
if (std::is_same<cl_half, T>::value)
|
||||
{
|
||||
TestDef<T> tests[] = { { '+', half_plus, type2name[sizeof(T)] },
|
||||
{ '-', half_minus, type2name[sizeof(T)] },
|
||||
{ '*', half_mult, type2name[sizeof(T)] } };
|
||||
for (auto &test : tests) err |= test_fpmath<T>(test);
|
||||
}
|
||||
else
|
||||
{
|
||||
TestDef<T> tests[] = {
|
||||
{ '+', std::plus<T>(), type2name[sizeof(T)] },
|
||||
{ '-', std::minus<T>(), type2name[sizeof(T)] },
|
||||
{ '*', std::multiplies<T>(), type2name[sizeof(T)] }
|
||||
};
|
||||
for (auto &test : tests) err |= test_fpmath<T>(test);
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
template <typename T> bool skip_type()
|
||||
{
|
||||
if (std::is_same<double, T>::value && !fp64Support)
|
||||
return true;
|
||||
else if (std::is_same<cl_half, T>::value && !fp16Support)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
template <std::size_t Cnt = 0, typename Type>
|
||||
void iterate_type(const Type &t)
|
||||
{
|
||||
bool doTest = !skip_type<Type>();
|
||||
|
||||
if (doTest)
|
||||
{
|
||||
if (test_fpmath_common<Type>())
|
||||
{
|
||||
throw std::runtime_error("test_fpmath_common failed\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <std::size_t Cnt = 0, typename... Tp>
|
||||
inline typename std::enable_if<Cnt == sizeof...(Tp), void>::type
|
||||
for_each_elem(
|
||||
const std::tuple<Tp...> &) // Unused arguments are given no names.
|
||||
{}
|
||||
|
||||
template <std::size_t Cnt = 0, typename... Tp>
|
||||
inline typename std::enable_if < Cnt<sizeof...(Tp), void>::type
|
||||
for_each_elem(const std::tuple<Tp...> &t)
|
||||
{
|
||||
iterate_type<Cnt>(std::get<Cnt>(t));
|
||||
for_each_elem<Cnt + 1, Tp...>(t);
|
||||
}
|
||||
|
||||
protected:
|
||||
TypeIter it;
|
||||
|
||||
cl_context context;
|
||||
cl_command_queue queue;
|
||||
|
||||
cl_device_fp_config fpConfigHalf;
|
||||
cl_device_fp_config fpConfigFloat;
|
||||
|
||||
bool fp16Support;
|
||||
bool fp64Support;
|
||||
|
||||
int num_elements;
|
||||
std::map<size_t, std::string> type2name;
|
||||
};
|
||||
|
||||
int test_fpmath(cl_device_id device, cl_context context, cl_command_queue queue,
|
||||
int num_elements)
|
||||
{
|
||||
try
|
||||
{
|
||||
TypesIterator(device, context, queue, num_elements);
|
||||
} catch (const std::runtime_error &e)
|
||||
{
|
||||
log_error("%s", e.what());
|
||||
return TEST_FAIL;
|
||||
}
|
||||
|
||||
return TEST_PASS;
|
||||
}
|
||||
@@ -1,196 +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 <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include "harness/rounding_mode.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "procs.h"
|
||||
|
||||
struct TestDef
|
||||
{
|
||||
const char op;
|
||||
std::function<float(float, float)> ref;
|
||||
};
|
||||
|
||||
static const char *fp_kernel_code = R"(
|
||||
__kernel void test_fp(__global TYPE *srcA, __global TYPE *srcB, __global TYPE *dst)
|
||||
{
|
||||
int tid = get_global_id(0);
|
||||
|
||||
dst[tid] = srcA[tid] OP srcB[tid];
|
||||
})";
|
||||
|
||||
static int verify_fp(std::vector<float> (&input)[2], std::vector<float> &output,
|
||||
const TestDef &test)
|
||||
{
|
||||
|
||||
auto &inA = input[0];
|
||||
auto &inB = input[1];
|
||||
for (int i = 0; i < output.size(); i++)
|
||||
{
|
||||
float r = test.ref(inA[i], inB[i]);
|
||||
if (r != output[i])
|
||||
{
|
||||
log_error("FP '%c' float test failed\n", test.op);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
log_info("FP '%c' float test passed\n", test.op);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void generate_random_inputs(std::vector<cl_float> (&input)[2])
|
||||
{
|
||||
RandomSeed seed(gRandomSeed);
|
||||
|
||||
auto random_generator = [&seed]() {
|
||||
return get_random_float(-MAKE_HEX_FLOAT(0x1.0p31f, 0x1, 31),
|
||||
MAKE_HEX_FLOAT(0x1.0p31f, 0x1, 31), seed);
|
||||
};
|
||||
|
||||
for (auto &v : input)
|
||||
{
|
||||
std::generate(v.begin(), v.end(), random_generator);
|
||||
}
|
||||
}
|
||||
|
||||
template <size_t N>
|
||||
int test_fpmath(cl_device_id device, cl_context context, cl_command_queue queue,
|
||||
int num_elements, const std::string type_str,
|
||||
const TestDef &test)
|
||||
{
|
||||
clMemWrapper streams[3];
|
||||
clProgramWrapper program;
|
||||
clKernelWrapper kernel;
|
||||
|
||||
int err;
|
||||
|
||||
size_t length = sizeof(cl_float) * num_elements * N;
|
||||
|
||||
int isRTZ = 0;
|
||||
RoundingMode oldMode = kDefaultRoundingMode;
|
||||
|
||||
// If we only support rtz mode
|
||||
if (CL_FP_ROUND_TO_ZERO == get_default_rounding_mode(device))
|
||||
{
|
||||
isRTZ = 1;
|
||||
oldMode = get_round();
|
||||
}
|
||||
|
||||
|
||||
std::vector<cl_float> inputs[]{ std::vector<cl_float>(N * num_elements),
|
||||
std::vector<cl_float>(N * num_elements) };
|
||||
std::vector<cl_float> output = std::vector<cl_float>(N * num_elements);
|
||||
|
||||
generate_random_inputs(inputs);
|
||||
|
||||
for (int i = 0; i < ARRAY_SIZE(streams); i++)
|
||||
{
|
||||
streams[i] =
|
||||
clCreateBuffer(context, CL_MEM_READ_WRITE, length, NULL, &err);
|
||||
test_error(err, "clCreateBuffer failed.");
|
||||
}
|
||||
for (int i = 0; i < ARRAY_SIZE(inputs); i++)
|
||||
{
|
||||
err = clEnqueueWriteBuffer(queue, streams[i], CL_TRUE, 0, length,
|
||||
inputs[i].data(), 0, NULL, NULL);
|
||||
test_error(err, "clEnqueueWriteBuffer failed.");
|
||||
}
|
||||
|
||||
std::string build_options = "-DTYPE=";
|
||||
build_options.append(type_str).append(" -DOP=").append(1, test.op);
|
||||
|
||||
err = create_single_kernel_helper(context, &program, &kernel, 1,
|
||||
&fp_kernel_code, "test_fp",
|
||||
build_options.c_str());
|
||||
|
||||
test_error(err, "create_single_kernel_helper failed");
|
||||
|
||||
for (int i = 0; i < ARRAY_SIZE(streams); i++)
|
||||
{
|
||||
err = clSetKernelArg(kernel, i, sizeof(streams[i]), &streams[i]);
|
||||
test_error(err, "clSetKernelArgs failed.");
|
||||
}
|
||||
|
||||
size_t threads[] = { static_cast<size_t>(num_elements) };
|
||||
err = clEnqueueNDRangeKernel(queue, kernel, 1, NULL, threads, NULL, 0, NULL,
|
||||
NULL);
|
||||
test_error(err, "clEnqueueNDRangeKernel failed.");
|
||||
|
||||
err = clEnqueueReadBuffer(queue, streams[2], CL_TRUE, 0, length,
|
||||
output.data(), 0, NULL, NULL);
|
||||
test_error(err, "clEnqueueReadBuffer failed.");
|
||||
|
||||
if (isRTZ) set_round(kRoundTowardZero, kfloat);
|
||||
|
||||
err = verify_fp(inputs, output, test);
|
||||
|
||||
if (isRTZ) set_round(oldMode, kfloat);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
|
||||
template <size_t N>
|
||||
int test_fpmath_common(cl_device_id device, cl_context context,
|
||||
cl_command_queue queue, int num_elements,
|
||||
const std::string type_str)
|
||||
{
|
||||
TestDef tests[] = { { '+', std::plus<float>() },
|
||||
{ '-', std::minus<float>() },
|
||||
{ '*', std::multiplies<float>() } };
|
||||
int err = TEST_PASS;
|
||||
|
||||
for (const auto &test : tests)
|
||||
{
|
||||
err |= test_fpmath<N>(device, context, queue, num_elements, type_str,
|
||||
test);
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
int test_fpmath_float(cl_device_id device, cl_context context,
|
||||
cl_command_queue queue, int num_elements)
|
||||
{
|
||||
return test_fpmath_common<1>(device, context, queue, num_elements, "float");
|
||||
}
|
||||
|
||||
int test_fpmath_float2(cl_device_id device, cl_context context,
|
||||
cl_command_queue queue, int num_elements)
|
||||
{
|
||||
return test_fpmath_common<2>(device, context, queue, num_elements,
|
||||
"float2");
|
||||
}
|
||||
|
||||
int test_fpmath_float4(cl_device_id device, cl_context context,
|
||||
cl_command_queue queue, int num_elements)
|
||||
{
|
||||
return test_fpmath_common<4>(device, context, queue, num_elements,
|
||||
"float4");
|
||||
}
|
||||
Reference in New Issue
Block a user