Deduplicate test_basic int2float/float2int (#1537)

Merge int2float,float2int.

Signed-off-by: John Kesapides <john.kesapides@arm.com>
This commit is contained in:
John Kesapides
2023-05-24 16:55:25 +01:00
committed by GitHub
parent 957e3b3985
commit 3e8898ffeb
2 changed files with 95 additions and 98 deletions

View File

@@ -11,7 +11,7 @@ set(${MODULE_NAME}_SOURCES
test_multireadimageonefmt.cpp test_multireadimagemultifmt.cpp
test_imagedim.cpp
test_vloadstore.cpp
test_int2float.cpp test_float2int.cpp
test_int2float.cpp
test_createkernelsinprogram.cpp
test_hostptr.cpp
test_explicit_s2v.cpp

View File

@@ -1,6 +1,6 @@
//
// 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
@@ -21,123 +21,120 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <algorithm>
#include <vector>
#include "procs.h"
const char *int2float_kernel_code =
"__kernel void test_int2float(__global int *src, __global float *dst)\n"
"{\n"
" int tid = get_global_id(0);\n"
"\n"
" dst[tid] = (float)src[tid];\n"
"\n"
"}\n";
int
verify_int2float(cl_int *inptr, cl_float *outptr, int n)
namespace {
const char *int2float_kernel_code = R"(
__kernel void test_X2Y(__global TYPE_X *src, __global TYPE_Y *dst)
{
int i;
int tid = get_global_id(0);
for (i=0; i<n; i++)
{
if (outptr[i] != (float)inptr[i])
{
log_error("INT2FLOAT test failed\n");
return -1;
}
}
dst[tid] = (TYPE_Y)src[tid];
log_info("INT2FLOAT test passed\n");
return 0;
})";
template <typename T> const char *Type2str() { return ""; }
template <> const char *Type2str<cl_int>() { return "int"; }
template <> const char *Type2str<cl_float>() { return "float"; }
template <typename T> void generate_random_inputs(std::vector<T> &v)
{
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);
};
std::generate(v.begin(), v.end(), random_generator);
}
int
test_int2float(cl_device_id device, cl_context context, cl_command_queue queue, int num_elements)
template <typename Tx, typename Ty> bool equal_value(Tx a, Ty b)
{
cl_mem streams[2];
cl_int *input_ptr;
cl_float *output_ptr;
cl_program program;
cl_kernel kernel;
size_t threads[1];
int err;
int i;
MTdata d;
return a == (Tx)b;
}
template <typename Tx, typename Ty>
int verify_X2Y(std::vector<Tx> input, std::vector<Ty> output,
const char *test_name)
{
if (!std::equal(output.begin(), output.end(), input.begin(),
equal_value<Tx, Ty>))
{
log_error("%s test failed\n", test_name);
return -1;
}
log_info("%s test passed\n", test_name);
return 0;
}
template <typename Tx, typename Ty>
int test_X2Y(cl_device_id device, cl_context context, cl_command_queue queue,
int num_elements, const char *test_name)
{
clMemWrapper streams[2];
clProgramWrapper program;
clKernelWrapper kernel;
int err;
std::vector<Tx> input(num_elements);
std::vector<Ty> output(num_elements);
input_ptr = (cl_int*)malloc(sizeof(cl_int) * num_elements);
output_ptr = (cl_float*)malloc(sizeof(cl_float) * num_elements);
streams[0] = clCreateBuffer(context, CL_MEM_READ_WRITE,
sizeof(cl_int) * num_elements, NULL, NULL);
if (!streams[0])
{
log_error("clCreateBuffer failed\n");
return -1;
}
sizeof(Tx) * num_elements, nullptr, &err);
test_error(err, "clCreateBuffer failed.");
streams[1] = clCreateBuffer(context, CL_MEM_READ_WRITE,
sizeof(cl_float) * num_elements, NULL, NULL);
if (!streams[1])
{
log_error("clCreateBuffer failed\n");
return -1;
}
sizeof(Ty) * num_elements, nullptr, &err);
test_error(err, "clCreateBuffer failed.");
d = init_genrand( gRandomSeed );
for (i=0; i<num_elements; i++)
input_ptr[i] = (cl_int)get_random_float(-MAKE_HEX_FLOAT( 0x1.0p31f, 0x1, 31), MAKE_HEX_FLOAT( 0x1.0p31f, 0x1, 31), d);
free_mtdata(d); d = NULL;
generate_random_inputs(input);
err = clEnqueueWriteBuffer(queue, streams[0], CL_TRUE, 0, sizeof(cl_int)*num_elements, (void *)input_ptr, 0, NULL, NULL);
if (err != CL_SUCCESS)
{
log_error("clWriteArray failed\n");
return -1;
}
err = clEnqueueWriteBuffer(queue, streams[0], CL_TRUE, 0,
sizeof(Tx) * num_elements, input.data(), 0,
nullptr, nullptr);
test_error(err, "clEnqueueWriteBuffer failed.");
err = create_single_kernel_helper(context, &program, &kernel, 1, &int2float_kernel_code, "test_int2float");
if (err != CL_SUCCESS)
{
log_error("create_single_kernel_helper failed\n");
return -1;
}
std::string build_options;
build_options.append("-DTYPE_X=").append(Type2str<Tx>());
build_options.append(" -DTYPE_Y=").append(Type2str<Ty>());
err = create_single_kernel_helper(context, &program, &kernel, 1,
&int2float_kernel_code, "test_X2Y",
build_options.c_str());
test_error(err, "create_single_kernel_helper failed.");
err = clSetKernelArg(kernel, 0, sizeof streams[0], &streams[0]);
err |= clSetKernelArg(kernel, 1, sizeof streams[1], &streams[1]);
if (err != CL_SUCCESS)
{
log_error("clSetKernelArgs failed\n");
return -1;
}
test_error(err, "clSetKernelArg failed.");
threads[0] = (size_t)num_elements;
err = clEnqueueNDRangeKernel( queue, kernel, 1, NULL, threads, NULL, 0, NULL, NULL );
if (err != CL_SUCCESS)
{
log_error("clEnqueueNDRangeKernel failed\n");
return -1;
}
size_t threads[] = { (size_t)num_elements };
err = clEnqueueNDRangeKernel(queue, kernel, 1, nullptr, threads, nullptr, 0,
nullptr, nullptr);
test_error(err, "clEnqueueNDRangeKernel failed.");
err = clEnqueueReadBuffer( queue, streams[1], true, 0, sizeof(cl_float)*num_elements, (void *)output_ptr, 0, NULL, NULL );
if (err != CL_SUCCESS)
{
log_error("clEnqueueReadBuffer failed\n");
return -1;
}
err = clEnqueueReadBuffer(queue, streams[1], CL_TRUE, 0,
sizeof(Ty) * num_elements, output.data(), 0,
nullptr, nullptr);
test_error(err, "clEnqueueReadBuffer failed.");
err = verify_int2float(input_ptr, output_ptr, num_elements);
// cleanup
clReleaseMemObject(streams[0]);
clReleaseMemObject(streams[1]);
clReleaseKernel(kernel);
clReleaseProgram(program);
free(input_ptr);
free(output_ptr);
err = verify_X2Y(input, output, test_name);
return err;
}
}
int test_int2float(cl_device_id device, cl_context context,
cl_command_queue queue, int num_elements)
{
return test_X2Y<cl_int, cl_float>(device, context, queue, num_elements,
"INT2FLOAT");
}
int test_float2int(cl_device_id device, cl_context context,
cl_command_queue queue, int num_elements)
{
return test_X2Y<cl_float, cl_int>(device, context, queue, num_elements,
"FLOAT2INT");
}