From 4b415bdf3c7ca4a97047f23de07df446d4843752 Mon Sep 17 00:00:00 2001 From: Cedric Nugteren Date: Sun, 9 Jul 2017 20:04:13 +0200 Subject: Disabled UNIX-style terminal color printing under Windows --- test/correctness/tester.hpp | 42 ++++++++++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 12 deletions(-) (limited to 'test') diff --git a/test/correctness/tester.hpp b/test/correctness/tester.hpp index e544f776..dfbefdfd 100644 --- a/test/correctness/tester.hpp +++ b/test/correctness/tester.hpp @@ -153,20 +153,38 @@ template const size_t Tester::kResultsPerLine = si template const float Tester::kStatusError = -1.0f; // Constants holding start and end strings for terminal-output in colour -template const std::string Tester::kPrintError = "\x1b[31m"; -template const std::string Tester::kPrintSuccess = "\x1b[32m"; -template const std::string Tester::kPrintWarning = "\x1b[35m"; -template const std::string Tester::kPrintMessage = "\x1b[1m"; -template const std::string Tester::kPrintEnd = "\x1b[0m"; +#if defined(_WIN32) + template const std::string Tester::kPrintError = ""; + template const std::string Tester::kPrintSuccess = ""; + template const std::string Tester::kPrintWarning = ""; + template const std::string Tester::kPrintMessage = ""; + template const std::string Tester::kPrintEnd = ""; +#else + template const std::string Tester::kPrintError = "\x1b[31m"; + template const std::string Tester::kPrintSuccess = "\x1b[32m"; + template const std::string Tester::kPrintWarning = "\x1b[35m"; + template const std::string Tester::kPrintMessage = "\x1b[1m"; + template const std::string Tester::kPrintEnd = "\x1b[0m"; +#endif // Sets the output error coding -template const std::string Tester::kSuccessData = "\x1b[32m:\x1b[0m"; // success -template const std::string Tester::kSuccessStatus = "\x1b[32m.\x1b[0m"; // success -template const std::string Tester::kErrorData = "\x1b[31mX\x1b[0m"; // error -template const std::string Tester::kErrorStatus = "\x1b[31m/\x1b[0m"; // error -template const std::string Tester::kSkippedCompilation = "\x1b[35m\\\x1b[0m"; // warning -template const std::string Tester::kUnsupportedPrecision = "\x1b[35mo\x1b[0m"; // warning -template const std::string Tester::kUnsupportedReference = "\x1b[35m-\x1b[0m"; // warning +#if defined(_WIN32) + template const std::string Tester::kSuccessData = ":"; // success + template const std::string Tester::kSuccessStatus = "."; // success + template const std::string Tester::kErrorData = "X"; // error + template const std::string Tester::kErrorStatus = "/"; // error + template const std::string Tester::kSkippedCompilation = "\\"; // warning + template const std::string Tester::kUnsupportedPrecision = "o"; // warning + template const std::string Tester::kUnsupportedReference = "-"; // warning +#else + template const std::string Tester::kSuccessData = "\x1b[32m:\x1b[0m"; // success + template const std::string Tester::kSuccessStatus = "\x1b[32m.\x1b[0m"; // success + template const std::string Tester::kErrorData = "\x1b[31mX\x1b[0m"; // error + template const std::string Tester::kErrorStatus = "\x1b[31m/\x1b[0m"; // error + template const std::string Tester::kSkippedCompilation = "\x1b[35m\\\x1b[0m"; // warning + template const std::string Tester::kUnsupportedPrecision = "\x1b[35mo\x1b[0m"; // warning + template const std::string Tester::kUnsupportedReference = "\x1b[35m-\x1b[0m"; // warning +#endif // ================================================================================================= // Below are the non-member functions (separated because of otherwise required partial class -- cgit v1.2.3 From d4c8a7c8b0dcf1e4f6e8e5edb85ebfc72d595189 Mon Sep 17 00:00:00 2001 From: Cedric Nugteren Date: Sun, 9 Jul 2017 20:19:08 +0200 Subject: Changed printf-statements with %zu into std::cout to fix MSVC 2013 compatibility --- test/correctness/misc/override_parameters.cpp | 7 ++++--- test/correctness/testblas.cpp | 10 +++++----- test/correctness/tester.cpp | 24 ++++++++++++------------ 3 files changed, 21 insertions(+), 20 deletions(-) (limited to 'test') diff --git a/test/correctness/misc/override_parameters.cpp b/test/correctness/misc/override_parameters.cpp index 4283c039..535d9286 100644 --- a/test/correctness/misc/override_parameters.cpp +++ b/test/correctness/misc/override_parameters.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include "utilities/utilities.hpp" #include "test/routines/level3/xgemm.hpp" @@ -120,9 +121,9 @@ size_t RunOverrideTests(int argc, char *argv[], const bool silent, const std::st } // Prints and returns the statistics - fprintf(stdout, " %zu test(s) passed\n", passed); - fprintf(stdout, " %zu test(s) failed\n", errors); - fprintf(stdout, "\n"); + std::cout << " " << passed << " test(s) passed" << std::endl; + std::cout << " " << errors << " test(s) failed" << std::endl; + std::cout << std::endl; return errors; } diff --git a/test/correctness/testblas.cpp b/test/correctness/testblas.cpp index edd9d4ea..659131c5 100644 --- a/test/correctness/testblas.cpp +++ b/test/correctness/testblas.cpp @@ -198,12 +198,12 @@ void TestBlas::TestRegular(std::vector> &test_vector, const st if (!TestSimilarity(result1[index], result2[index])) { if (l2error >= kErrorMarginL2) { errors++; } if (verbose_) { - if (get_id2_(args) == 1) { fprintf(stdout, "\n Error at index %zu: ", id1); } - else { fprintf(stdout, "\n Error at %zu,%zu: ", id1, id2); } - fprintf(stdout, " %s (reference) versus ", ToString(result1[index]).c_str()); - fprintf(stdout, " %s (CLBlast)", ToString(result2[index]).c_str()); + if (get_id2_(args) == 1) { std::cout << std::endl << " Error at index " << id1 << ": "; } + else { std::cout << std::endl << " Error at " << id1 << "," << id2 << ": "; } + std::cout << " " << ToString(result1[index]) << " (reference) versus "; + std::cout << " " << ToString(result2[index]) << " (CLBlast)"; if (l2error < kErrorMarginL2) { - fprintf(stdout, " - error suppressed by a low total L2 error\n"); + std::cout << " - error suppressed by a low total L2 error" << std::endl; } } } diff --git a/test/correctness/tester.cpp b/test/correctness/tester.cpp index fb85e609..700a86f7 100644 --- a/test/correctness/tester.cpp +++ b/test/correctness/tester.cpp @@ -185,14 +185,14 @@ Tester::Tester(const std::vector &arguments, const bool silent template Tester::~Tester() { if (PrecisionSupported(device_)) { - fprintf(stdout, "* Completed all test-cases for this routine. Results:\n"); - fprintf(stdout, " %zu test(s) passed\n", tests_passed_); - if (tests_skipped_ > 0) { fprintf(stdout, "%s", kPrintWarning.c_str()); } - fprintf(stdout, " %zu test(s) skipped%s\n", tests_skipped_, kPrintEnd.c_str()); - if (tests_failed_ > 0) { fprintf(stdout, "%s", kPrintError.c_str()); } - fprintf(stdout, " %zu test(s) failed%s\n", tests_failed_, kPrintEnd.c_str()); + std::cout << "* Completed all test-cases for this routine. Results:" << std::endl; + std::cout << " " << tests_passed_ << " test(s) passed" << std::endl; + if (tests_skipped_ > 0) { std::cout << kPrintWarning; } + std::cout << " " << tests_skipped_ << " test(s) skipped" << kPrintEnd << std::endl; + if (tests_failed_ > 0) { std::cout << kPrintError; } + std::cout << " " << tests_failed_ << " test(s) failed" << kPrintEnd << std::endl; } - fprintf(stdout, "\n"); + std::cout << std::endl; // Cleans-up clBLAS #ifdef CLBLAST_REF_CLBLAS @@ -238,18 +238,18 @@ void Tester::TestEnd() { // Prints a test summary auto pass_rate = 100*num_passed_ / static_cast(num_passed_ + num_skipped_ + num_failed_); fprintf(stdout, " Pass rate %s%5.1lf%%%s:", kPrintMessage.c_str(), pass_rate, kPrintEnd.c_str()); - fprintf(stdout, " %zu passed /", num_passed_); + std::cout << " " << num_passed_ << " passed /"; if (num_skipped_ != 0) { - fprintf(stdout, " %s%zu skipped%s /", kPrintWarning.c_str(), num_skipped_, kPrintEnd.c_str()); + std::cout << " " << kPrintWarning << num_skipped_ << " skipped" << kPrintEnd << " /"; } else { - fprintf(stdout, " %zu skipped /", num_skipped_); + std::cout << " " << num_skipped_ << " skipped /"; } if (num_failed_ != 0) { - fprintf(stdout, " %s%zu failed%s\n", kPrintError.c_str(), num_failed_, kPrintEnd.c_str()); + std::cout << " " << kPrintError << num_failed_ << " failed" << kPrintEnd << std::endl; } else { - fprintf(stdout, " %zu failed\n", num_failed_); + std::cout << " " << num_failed_ << " failed" << std::endl; } } -- cgit v1.2.3 From f77b48692b5dbc9f13f5f93a8242ea546a39236e Mon Sep 17 00:00:00 2001 From: Cedric Nugteren Date: Wed, 12 Jul 2017 21:53:39 +0200 Subject: Relaxed requirement on a_ld and b_ld for batched GEMM --- src/routines/levelx/xgemmbatched.cpp | 4 ++-- src/utilities/buffer_test.hpp | 8 ++++---- test/routines/levelx/xgemmbatched.hpp | 9 +++++++++ 3 files changed, 15 insertions(+), 6 deletions(-) (limited to 'test') diff --git a/src/routines/levelx/xgemmbatched.cpp b/src/routines/levelx/xgemmbatched.cpp index 0fea1922..ee8448d2 100644 --- a/src/routines/levelx/xgemmbatched.cpp +++ b/src/routines/levelx/xgemmbatched.cpp @@ -94,8 +94,8 @@ void XgemmBatched::DoGemmBatched(const Layout layout, const Transpose a_trans // Tests the matrices for validity for (auto batch = size_t{0}; batch < batch_count; ++batch) { - TestMatrixA(a_one, a_two, a_buffer, a_offsets[batch], a_ld); - TestMatrixB(b_one, b_two, b_buffer, b_offsets[batch], b_ld); + TestMatrixA(a_one, a_two, a_buffer, a_offsets[batch], a_ld, false); // don't test for invalid LD + TestMatrixB(b_one, b_two, b_buffer, b_offsets[batch], b_ld, false); // don't test for invalid LD TestMatrixC(c_one, c_two, c_buffer, c_offsets[batch], c_ld); } diff --git a/src/utilities/buffer_test.hpp b/src/utilities/buffer_test.hpp index 652ab8c6..b5693181 100644 --- a/src/utilities/buffer_test.hpp +++ b/src/utilities/buffer_test.hpp @@ -23,8 +23,8 @@ namespace clblast { // Tests matrix 'A' for validity template void TestMatrixA(const size_t one, const size_t two, const Buffer &buffer, - const size_t offset, const size_t ld) { - if (ld < one) { throw BLASError(StatusCode::kInvalidLeadDimA); } + const size_t offset, const size_t ld, const bool test_lead_dim = true) { + if (test_lead_dim && ld < one) { throw BLASError(StatusCode::kInvalidLeadDimA); } try { const auto required_size = (ld * (two - 1) + one + offset) * sizeof(T); if (buffer.GetSize() < required_size) { throw BLASError(StatusCode::kInsufficientMemoryA); } @@ -34,8 +34,8 @@ void TestMatrixA(const size_t one, const size_t two, const Buffer &buffer, // Tests matrix 'B' for validity template void TestMatrixB(const size_t one, const size_t two, const Buffer &buffer, - const size_t offset, const size_t ld) { - if (ld < one) { throw BLASError(StatusCode::kInvalidLeadDimB); } + const size_t offset, const size_t ld, const bool test_lead_dim = true) { + if (test_lead_dim && ld < one) { throw BLASError(StatusCode::kInvalidLeadDimB); } try { const auto required_size = (ld * (two - 1) + one + offset) * sizeof(T); if (buffer.GetSize() < required_size) { throw BLASError(StatusCode::kInsufficientMemoryB); } diff --git a/test/routines/levelx/xgemmbatched.hpp b/test/routines/levelx/xgemmbatched.hpp index 56823e47..704d0578 100644 --- a/test/routines/levelx/xgemmbatched.hpp +++ b/test/routines/levelx/xgemmbatched.hpp @@ -110,6 +110,15 @@ class TestXgemmBatched { static StatusCode RunRoutine(const Arguments &args, Buffers &buffers, Queue &queue) { auto queue_plain = queue(); auto event = cl_event{}; + // Relaxed requirement on ld_a and ld_b within the library, this is here to match clBLAS + auto a_rotated = (args.layout == Layout::kColMajor && args.a_transpose != Transpose::kNo) || + (args.layout == Layout::kRowMajor && args.a_transpose == Transpose::kNo); + auto b_rotated = (args.layout == Layout::kColMajor && args.b_transpose != Transpose::kNo) || + (args.layout == Layout::kRowMajor && args.b_transpose == Transpose::kNo); + auto a_one = (!a_rotated) ? args.m : args.k; + auto b_one = (!b_rotated) ? args.k : args.n; + if (args.a_ld < a_one) { return StatusCode::kInvalidLeadDimA; } + if (args.b_ld < b_one) { return StatusCode::kInvalidLeadDimB; } auto status = GemmBatched(args.layout, args.a_transpose, args.b_transpose, args.m, args.n, args.k, args.alphas.data(), buffers.a_mat(), args.a_offsets.data(), args.a_ld, -- cgit v1.2.3 From de9ed9d4ea5ccc771013cbefe70501c62b98cb05 Mon Sep 17 00:00:00 2001 From: Cedric Nugteren Date: Wed, 12 Jul 2017 21:54:16 +0200 Subject: Fixed batched tests when testing for invalid sizes against clBLAS --- test/correctness/testblas.hpp | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'test') diff --git a/test/correctness/testblas.hpp b/test/correctness/testblas.hpp index a553687d..b377be47 100644 --- a/test/correctness/testblas.hpp +++ b/test/correctness/testblas.hpp @@ -343,6 +343,12 @@ size_t RunTests(int argc, char *argv[], const bool silent, const std::string &na auto i_args = args; i_args.m = i_args.n = i_args.k = i_args.kl = i_args.ku = tester.kBufferSize; i_args.a_ld = i_args.b_ld = i_args.c_ld = tester.kBufferSize; + i_args.batch_count = 3; + i_args.alphas = std::vector(i_args.batch_count); + i_args.betas = std::vector(i_args.batch_count); + i_args.a_offsets = std::vector(i_args.batch_count); + i_args.b_offsets = std::vector(i_args.batch_count); + i_args.c_offsets = std::vector(i_args.batch_count); for (auto &x_size: x_sizes) { i_args.x_size = x_size; for (auto &y_size: y_sizes) { i_args.y_size = y_size; for (auto &a_size: a_sizes) { i_args.a_size = a_size; -- cgit v1.2.3 From 844e68853e14e5cf5ba67a8b376bd59d27383290 Mon Sep 17 00:00:00 2001 From: Cedric Nugteren Date: Sat, 12 Aug 2017 15:38:17 +0200 Subject: Moved some utility functions to a test-specific utility compilation-unit --- CMakeLists.txt | 6 ++- src/utilities/utilities.cpp | 97 +------------------------------------ src/utilities/utilities.hpp | 67 ++------------------------ test/correctness/tester.hpp | 2 +- test/performance/client.hpp | 2 +- test/routines/common.hpp | 1 + test/test_utilities.cpp | 114 ++++++++++++++++++++++++++++++++++++++++++++ test/test_utilities.hpp | 99 ++++++++++++++++++++++++++++++++++++++ 8 files changed, 224 insertions(+), 164 deletions(-) create mode 100644 test/test_utilities.cpp create mode 100644 test/test_utilities.hpp (limited to 'test') diff --git a/CMakeLists.txt b/CMakeLists.txt index ae259fad..9abdadf3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -380,7 +380,8 @@ endif() # Section for the performance tests (i.e. the client). These compare against optionally a reference # library, either clBLAS, a CPU BLAS, or CUDA's cuBLAS. if(CLIENTS) - set(CLIENTS_COMMON ${WRAPPERS} test/performance/client.hpp test/routines/common.hpp) + set(CLIENTS_COMMON ${WRAPPERS} test/test_utilities.cpp test/test_utilities.hpp + test/performance/client.hpp test/routines/common.hpp) # Visual Studio requires the sources of non-exported objects/libraries if(MSVC) @@ -431,7 +432,8 @@ endif() # CPU BLAS library, and/or cuBLAS to act as a reference. if(TESTS) enable_testing() - set(TESTS_COMMON ${WRAPPERS} test/correctness/testblas.hpp test/correctness/tester.hpp + set(TESTS_COMMON ${WRAPPERS} test/test_utilities.cpp test/test_utilities.hpp + test/correctness/testblas.hpp test/correctness/tester.hpp test/routines/common.hpp) # Visual Studio requires the sources of non-exported objects/libraries diff --git a/src/utilities/utilities.cpp b/src/utilities/utilities.cpp index 95b70cd5..0cd00438 100644 --- a/src/utilities/utilities.cpp +++ b/src/utilities/utilities.cpp @@ -7,7 +7,7 @@ // Author(s): // Cedric Nugteren // -// This file implements the common (test) utility functions. +// This file implements the common utility functions. // // ================================================================================================= @@ -85,14 +85,6 @@ template <> double AbsoluteValue(const double2 value) { return std::sqrt(value.real() * value.real() + value.imag() * value.imag()); } -// Returns whether a scalar is close to zero -template bool IsCloseToZero(const T value) { return (value > -SmallConstant()) && (value < SmallConstant()); } -template bool IsCloseToZero(const float); -template bool IsCloseToZero(const double); -template <> bool IsCloseToZero(const half value) { return IsCloseToZero(HalfToFloat(value)); } -template <> bool IsCloseToZero(const float2 value) { return IsCloseToZero(value.real()) || IsCloseToZero(value.imag()); } -template <> bool IsCloseToZero(const double2 value) { return IsCloseToZero(value.real()) || IsCloseToZero(value.imag()); } - // ================================================================================================= // Implements the string conversion using std::to_string if possible @@ -319,12 +311,6 @@ bool CheckArgument(const std::vector &arguments, std::string &help, // ================================================================================================= -// Returns a random seed. This used to be implemented using 'std::random_device', but that doesn't -// always work. The chrono-timers are more reliable in that sense, but perhaps less random. -unsigned int GetRandomSeed() { - return static_cast(std::chrono::system_clock::now().time_since_epoch().count()); -} - // Create a random number generator and populates a vector with samples from a random distribution template void PopulateVector(std::vector &vector, std::mt19937 &mt, std::uniform_real_distribution &dist) { @@ -354,87 +340,6 @@ void PopulateVector(std::vector &vector, std::mt19937 &mt, std::uniform_re // ================================================================================================= -template -void DeviceToHost(const Arguments &args, Buffers &buffers, BuffersHost &buffers_host, - Queue &queue, const std::vector &names) { - for (auto &name: names) { - if (name == kBufVecX) {buffers_host.x_vec = std::vector(args.x_size, static_cast(0)); buffers.x_vec.Read(queue, args.x_size, buffers_host.x_vec); } - else if (name == kBufVecY) { buffers_host.y_vec = std::vector(args.y_size, static_cast(0)); buffers.y_vec.Read(queue, args.y_size, buffers_host.y_vec); } - else if (name == kBufMatA) { buffers_host.a_mat = std::vector(args.a_size, static_cast(0)); buffers.a_mat.Read(queue, args.a_size, buffers_host.a_mat); } - else if (name == kBufMatB) { buffers_host.b_mat = std::vector(args.b_size, static_cast(0)); buffers.b_mat.Read(queue, args.b_size, buffers_host.b_mat); } - else if (name == kBufMatC) { buffers_host.c_mat = std::vector(args.c_size, static_cast(0)); buffers.c_mat.Read(queue, args.c_size, buffers_host.c_mat); } - else if (name == kBufMatAP) { buffers_host.ap_mat = std::vector(args.ap_size, static_cast(0)); buffers.ap_mat.Read(queue, args.ap_size, buffers_host.ap_mat); } - else if (name == kBufScalar) { buffers_host.scalar = std::vector(args.scalar_size, static_cast(0)); buffers.scalar.Read(queue, args.scalar_size, buffers_host.scalar); } - else { throw std::runtime_error("Invalid buffer name"); } - } -} - -template -void HostToDevice(const Arguments &args, Buffers &buffers, BuffersHost &buffers_host, - Queue &queue, const std::vector &names) { - for (auto &name: names) { - if (name == kBufVecX) { buffers.x_vec.Write(queue, args.x_size, buffers_host.x_vec); } - else if (name == kBufVecY) { buffers.y_vec.Write(queue, args.y_size, buffers_host.y_vec); } - else if (name == kBufMatA) { buffers.a_mat.Write(queue, args.a_size, buffers_host.a_mat); } - else if (name == kBufMatB) { buffers.b_mat.Write(queue, args.b_size, buffers_host.b_mat); } - else if (name == kBufMatC) { buffers.c_mat.Write(queue, args.c_size, buffers_host.c_mat); } - else if (name == kBufMatAP) { buffers.ap_mat.Write(queue, args.ap_size, buffers_host.ap_mat); } - else if (name == kBufScalar) { buffers.scalar.Write(queue, args.scalar_size, buffers_host.scalar); } - else { throw std::runtime_error("Invalid buffer name"); } - } -} - -// Compiles the above functions -template void DeviceToHost(const Arguments&, Buffers&, BuffersHost&, Queue&, const std::vector&); -template void DeviceToHost(const Arguments&, Buffers&, BuffersHost&, Queue&, const std::vector&); -template void DeviceToHost(const Arguments&, Buffers&, BuffersHost&, Queue&, const std::vector&); -template void DeviceToHost(const Arguments&, Buffers&, BuffersHost&, Queue&, const std::vector&); -template void DeviceToHost(const Arguments&, Buffers&, BuffersHost&, Queue&, const std::vector&); -template void DeviceToHost(const Arguments&, Buffers&, BuffersHost&, Queue&, const std::vector&); -template void DeviceToHost(const Arguments&, Buffers&, BuffersHost&, Queue&, const std::vector&); -template void HostToDevice(const Arguments&, Buffers&, BuffersHost&, Queue&, const std::vector&); -template void HostToDevice(const Arguments&, Buffers&, BuffersHost&, Queue&, const std::vector&); -template void HostToDevice(const Arguments&, Buffers&, BuffersHost&, Queue&, const std::vector&); -template void HostToDevice(const Arguments&, Buffers&, BuffersHost&, Queue&, const std::vector&); -template void HostToDevice(const Arguments&, Buffers&, BuffersHost&, Queue&, const std::vector&); -template void HostToDevice(const Arguments&, Buffers&, BuffersHost&, Queue&, const std::vector&); -template void HostToDevice(const Arguments&, Buffers&, BuffersHost&, Queue&, const std::vector&); - -// ================================================================================================= - -// Conversion between half and single-precision -std::vector HalfToFloatBuffer(const std::vector& source) { - auto result = std::vector(source.size()); - for (auto i = size_t(0); i < source.size(); ++i) { result[i] = HalfToFloat(source[i]); } - return result; -} -void FloatToHalfBuffer(std::vector& result, const std::vector& source) { - for (auto i = size_t(0); i < source.size(); ++i) { result[i] = FloatToHalf(source[i]); } -} - -// As above, but now for OpenCL data-types instead of std::vectors -Buffer HalfToFloatBuffer(const Buffer& source, cl_command_queue queue_raw) { - const auto size = source.GetSize() / sizeof(half); - auto queue = Queue(queue_raw); - auto context = queue.GetContext(); - auto source_cpu = std::vector(size); - source.Read(queue, size, source_cpu); - auto result_cpu = HalfToFloatBuffer(source_cpu); - auto result = Buffer(context, size); - result.Write(queue, size, result_cpu); - return result; -} -void FloatToHalfBuffer(Buffer& result, const Buffer& source, cl_command_queue queue_raw) { - const auto size = source.GetSize() / sizeof(float); - auto queue = Queue(queue_raw); - auto context = queue.GetContext(); - auto source_cpu = std::vector(size); - source.Read(queue, size, source_cpu); - auto result_cpu = std::vector(size); - FloatToHalfBuffer(result_cpu, source_cpu); - result.Write(queue, size, result_cpu); -} - // Converts a 'real' value to a 'real argument' value to be passed to a kernel. Normally there is // no conversion, but half-precision is not supported as kernel argument so it is converted to float. template <> typename RealArg::Type GetRealArg(const half value) { return HalfToFloat(value); } diff --git a/src/utilities/utilities.hpp b/src/utilities/utilities.hpp index 03051354..72997d7f 100644 --- a/src/utilities/utilities.hpp +++ b/src/utilities/utilities.hpp @@ -7,10 +7,9 @@ // Author(s): // Cedric Nugteren // -// This file provides declarations for the common (test) utility functions such as a command-line +// This file provides declarations for the common utility functions such as a command-line // argument parser. On top of this, it serves as the 'common' header, including the C++ OpenCL -// wrapper. These utilities are not only used for CLBlast, but also included as part of the tuners, -// the performance client and the correctness testers. +// wrapper. // // ================================================================================================= @@ -81,19 +80,6 @@ constexpr auto kArgBatchCount = "batch_num"; // The tuner-specific arguments in string form constexpr auto kArgFraction = "fraction"; -// The client-specific arguments in string form -constexpr auto kArgCompareclblas = "clblas"; -constexpr auto kArgComparecblas = "cblas"; -constexpr auto kArgComparecublas = "cublas"; -constexpr auto kArgStepSize = "step"; -constexpr auto kArgNumSteps = "num_steps"; -constexpr auto kArgNumRuns = "runs"; -constexpr auto kArgWarmUp = "warm_up"; - -// The test-specific arguments in string form -constexpr auto kArgFullTest = "full_test"; -constexpr auto kArgVerbose = "verbose"; - // The common arguments in string form constexpr auto kArgPlatform = "platform"; constexpr auto kArgDevice = "device"; @@ -101,6 +87,7 @@ constexpr auto kArgPrecision = "precision"; constexpr auto kArgHelp = "h"; constexpr auto kArgQuiet = "q"; constexpr auto kArgNoAbbreviations = "no_abbrv"; +constexpr auto kArgNumRuns = "runs"; // The buffer names constexpr auto kBufVecX = "X"; @@ -133,9 +120,6 @@ template T SmallConstant(); // Returns the absolute value of a scalar (modulus in case of complex numbers) template typename BaseType::Type AbsoluteValue(const T value); -// Returns whether a scalar is close to zero -template bool IsCloseToZero(const T value); - // ================================================================================================= // Structure containing all possible arguments for test clients, including their default values @@ -208,28 +192,6 @@ struct Arguments { bool no_abbrv = false; }; -// Structure containing all possible buffers for test clients -template -struct Buffers { - Buffer x_vec; - Buffer y_vec; - Buffer a_mat; - Buffer b_mat; - Buffer c_mat; - Buffer ap_mat; - Buffer scalar; -}; -template -struct BuffersHost { - std::vector x_vec; - std::vector y_vec; - std::vector a_mat; - std::vector b_mat; - std::vector c_mat; - std::vector ap_mat; - std::vector scalar; -}; - // ================================================================================================= // Converts a value (e.g. an integer) to a string. This also covers special cases for CLBlast @@ -264,9 +226,6 @@ bool CheckArgument(const std::vector &arguments, std::string &help, // ================================================================================================= -// Returns a random number to be used as a seed -unsigned int GetRandomSeed(); - // Test/example data lower and upper limit constexpr auto kTestDataLowerLimit = -2.0; constexpr auto kTestDataUpperLimit = 2.0; @@ -277,26 +236,6 @@ void PopulateVector(std::vector &vector, std::mt19937 &mt, std::uniform_real_ // ================================================================================================= -// Copies buffers from the OpenCL device to the host -template -void DeviceToHost(const Arguments &args, Buffers &buffers, BuffersHost &buffers_host, - Queue &queue, const std::vector &names); - -// Copies buffers from the host to the OpenCL device -template -void HostToDevice(const Arguments &args, Buffers &buffers, BuffersHost &buffers_host, - Queue &queue, const std::vector &names); - -// ================================================================================================= - -// Conversion between half and single-precision -std::vector HalfToFloatBuffer(const std::vector& source); -void FloatToHalfBuffer(std::vector& result, const std::vector& source); - -// As above, but now for OpenCL data-types instead of std::vectors -Buffer HalfToFloatBuffer(const Buffer& source, cl_command_queue queue_raw); -void FloatToHalfBuffer(Buffer& result, const Buffer& source, cl_command_queue queue_raw); - // Converts a 'real' value to a 'real argument' value to be passed to a kernel. Normally there is // no conversion, but half-precision is not supported as kernel argument so it is converted to float. template struct RealArg { using Type = T; }; diff --git a/test/correctness/tester.hpp b/test/correctness/tester.hpp index dfbefdfd..caf03787 100644 --- a/test/correctness/tester.hpp +++ b/test/correctness/tester.hpp @@ -22,7 +22,7 @@ #include #include -#include "utilities/utilities.hpp" +#include "test/test_utilities.hpp" // The libraries #ifdef CLBLAST_REF_CLBLAS diff --git a/test/performance/client.hpp b/test/performance/client.hpp index 47a13017..2ba09cb9 100644 --- a/test/performance/client.hpp +++ b/test/performance/client.hpp @@ -25,7 +25,7 @@ #include #include -#include "utilities/utilities.hpp" +#include "test/test_utilities.hpp" // The libraries to test #ifdef CLBLAST_REF_CLBLAS diff --git a/test/routines/common.hpp b/test/routines/common.hpp index 9708288a..47c8f8d7 100644 --- a/test/routines/common.hpp +++ b/test/routines/common.hpp @@ -18,6 +18,7 @@ #include #include "utilities/utilities.hpp" +#include "test/test_utilities.hpp" #ifdef CLBLAST_REF_CLBLAS #include "test/wrapper_clblas.hpp" diff --git a/test/test_utilities.cpp b/test/test_utilities.cpp new file mode 100644 index 00000000..b8fd94a9 --- /dev/null +++ b/test/test_utilities.cpp @@ -0,0 +1,114 @@ + +// ================================================================================================= +// This file is part of the CLBlast project. The project is licensed under Apache Version 2.0. This +// project loosely follows the Google C++ styleguide and uses a tab-size of two spaces and a max- +// width of 100 characters per line. +// +// Author(s): +// Cedric Nugteren +// +// This file implements the test utility functions. +// +// ================================================================================================= + +#include "test/test_utilities.hpp" + +#include +#include + +namespace clblast { +// ================================================================================================= + +// Returns whether a scalar is close to zero +template bool IsCloseToZero(const T value) { return (value > -SmallConstant()) && (value < SmallConstant()); } +template bool IsCloseToZero(const float); +template bool IsCloseToZero(const double); +template <> bool IsCloseToZero(const half value) { return IsCloseToZero(HalfToFloat(value)); } +template <> bool IsCloseToZero(const float2 value) { return IsCloseToZero(value.real()) || IsCloseToZero(value.imag()); } +template <> bool IsCloseToZero(const double2 value) { return IsCloseToZero(value.real()) || IsCloseToZero(value.imag()); } + +// ================================================================================================= + +template +void DeviceToHost(const Arguments &args, Buffers &buffers, BuffersHost &buffers_host, + Queue &queue, const std::vector &names) { + for (auto &name: names) { + if (name == kBufVecX) {buffers_host.x_vec = std::vector(args.x_size, static_cast(0)); buffers.x_vec.Read(queue, args.x_size, buffers_host.x_vec); } + else if (name == kBufVecY) { buffers_host.y_vec = std::vector(args.y_size, static_cast(0)); buffers.y_vec.Read(queue, args.y_size, buffers_host.y_vec); } + else if (name == kBufMatA) { buffers_host.a_mat = std::vector(args.a_size, static_cast(0)); buffers.a_mat.Read(queue, args.a_size, buffers_host.a_mat); } + else if (name == kBufMatB) { buffers_host.b_mat = std::vector(args.b_size, static_cast(0)); buffers.b_mat.Read(queue, args.b_size, buffers_host.b_mat); } + else if (name == kBufMatC) { buffers_host.c_mat = std::vector(args.c_size, static_cast(0)); buffers.c_mat.Read(queue, args.c_size, buffers_host.c_mat); } + else if (name == kBufMatAP) { buffers_host.ap_mat = std::vector(args.ap_size, static_cast(0)); buffers.ap_mat.Read(queue, args.ap_size, buffers_host.ap_mat); } + else if (name == kBufScalar) { buffers_host.scalar = std::vector(args.scalar_size, static_cast(0)); buffers.scalar.Read(queue, args.scalar_size, buffers_host.scalar); } + else { throw std::runtime_error("Invalid buffer name"); } + } +} + +template +void HostToDevice(const Arguments &args, Buffers &buffers, BuffersHost &buffers_host, + Queue &queue, const std::vector &names) { + for (auto &name: names) { + if (name == kBufVecX) { buffers.x_vec.Write(queue, args.x_size, buffers_host.x_vec); } + else if (name == kBufVecY) { buffers.y_vec.Write(queue, args.y_size, buffers_host.y_vec); } + else if (name == kBufMatA) { buffers.a_mat.Write(queue, args.a_size, buffers_host.a_mat); } + else if (name == kBufMatB) { buffers.b_mat.Write(queue, args.b_size, buffers_host.b_mat); } + else if (name == kBufMatC) { buffers.c_mat.Write(queue, args.c_size, buffers_host.c_mat); } + else if (name == kBufMatAP) { buffers.ap_mat.Write(queue, args.ap_size, buffers_host.ap_mat); } + else if (name == kBufScalar) { buffers.scalar.Write(queue, args.scalar_size, buffers_host.scalar); } + else { throw std::runtime_error("Invalid buffer name"); } + } +} + +// Compiles the above functions +template void DeviceToHost(const Arguments&, Buffers&, BuffersHost&, Queue&, const std::vector&); +template void DeviceToHost(const Arguments&, Buffers&, BuffersHost&, Queue&, const std::vector&); +template void DeviceToHost(const Arguments&, Buffers&, BuffersHost&, Queue&, const std::vector&); +template void DeviceToHost(const Arguments&, Buffers&, BuffersHost&, Queue&, const std::vector&); +template void DeviceToHost(const Arguments&, Buffers&, BuffersHost&, Queue&, const std::vector&); +template void DeviceToHost(const Arguments&, Buffers&, BuffersHost&, Queue&, const std::vector&); +template void DeviceToHost(const Arguments&, Buffers&, BuffersHost&, Queue&, const std::vector&); +template void HostToDevice(const Arguments&, Buffers&, BuffersHost&, Queue&, const std::vector&); +template void HostToDevice(const Arguments&, Buffers&, BuffersHost&, Queue&, const std::vector&); +template void HostToDevice(const Arguments&, Buffers&, BuffersHost&, Queue&, const std::vector&); +template void HostToDevice(const Arguments&, Buffers&, BuffersHost&, Queue&, const std::vector&); +template void HostToDevice(const Arguments&, Buffers&, BuffersHost&, Queue&, const std::vector&); +template void HostToDevice(const Arguments&, Buffers&, BuffersHost&, Queue&, const std::vector&); +template void HostToDevice(const Arguments&, Buffers&, BuffersHost&, Queue&, const std::vector&); + +// ================================================================================================= + +// Conversion between half and single-precision +std::vector HalfToFloatBuffer(const std::vector& source) { + auto result = std::vector(source.size()); + for (auto i = size_t(0); i < source.size(); ++i) { result[i] = HalfToFloat(source[i]); } + return result; +} +void FloatToHalfBuffer(std::vector& result, const std::vector& source) { + for (auto i = size_t(0); i < source.size(); ++i) { result[i] = FloatToHalf(source[i]); } +} + +// As above, but now for OpenCL data-types instead of std::vectors +Buffer HalfToFloatBuffer(const Buffer& source, cl_command_queue queue_raw) { + const auto size = source.GetSize() / sizeof(half); + auto queue = Queue(queue_raw); + auto context = queue.GetContext(); + auto source_cpu = std::vector(size); + source.Read(queue, size, source_cpu); + auto result_cpu = HalfToFloatBuffer(source_cpu); + auto result = Buffer(context, size); + result.Write(queue, size, result_cpu); + return result; +} +void FloatToHalfBuffer(Buffer& result, const Buffer& source, cl_command_queue queue_raw) { + const auto size = source.GetSize() / sizeof(float); + auto queue = Queue(queue_raw); + auto context = queue.GetContext(); + auto source_cpu = std::vector(size); + source.Read(queue, size, source_cpu); + auto result_cpu = std::vector(size); + FloatToHalfBuffer(result_cpu, source_cpu); + result.Write(queue, size, result_cpu); +} + +// ================================================================================================= +} // namespace clblast diff --git a/test/test_utilities.hpp b/test/test_utilities.hpp new file mode 100644 index 00000000..fc50a754 --- /dev/null +++ b/test/test_utilities.hpp @@ -0,0 +1,99 @@ + +// ================================================================================================= +// This file is part of the CLBlast project. The project is licensed under Apache Version 2.0. This +// project loosely follows the Google C++ styleguide and uses a tab-size of two spaces and a max- +// width of 100 characters per line. +// +// Author(s): +// Cedric Nugteren +// +// This file provides declarations for the common test utility functions (performance clients and +// correctness testers). +// +// ================================================================================================= + +#ifndef CLBLAST_TEST_UTILITIES_H_ +#define CLBLAST_TEST_UTILITIES_H_ + +#include + +#include "utilities/utilities.hpp" + +namespace clblast { +// ================================================================================================= + +// The client-specific arguments in string form +constexpr auto kArgCompareclblas = "clblas"; +constexpr auto kArgComparecblas = "cblas"; +constexpr auto kArgComparecublas = "cublas"; +constexpr auto kArgStepSize = "step"; +constexpr auto kArgNumSteps = "num_steps"; +constexpr auto kArgWarmUp = "warm_up"; + +// The test-specific arguments in string form +constexpr auto kArgFullTest = "full_test"; +constexpr auto kArgVerbose = "verbose"; + +// ================================================================================================= + +// Returns whether a scalar is close to zero +template bool IsCloseToZero(const T value); + +// ================================================================================================= + +// Structure containing all possible buffers for test clients +template +struct Buffers { + Buffer x_vec; + Buffer y_vec; + Buffer a_mat; + Buffer b_mat; + Buffer c_mat; + Buffer ap_mat; + Buffer scalar; +}; +template +struct BuffersHost { + std::vector x_vec; + std::vector y_vec; + std::vector a_mat; + std::vector b_mat; + std::vector c_mat; + std::vector ap_mat; + std::vector scalar; +}; + +// ================================================================================================= + +// Converts a value (e.g. an integer) to a string. This also covers special cases for CLBlast +// data-types such as the Layout and Transpose data-types. +template +std::string ToString(T value); + +// ================================================================================================= + +// Copies buffers from the OpenCL device to the host +template +void DeviceToHost(const Arguments &args, Buffers &buffers, BuffersHost &buffers_host, + Queue &queue, const std::vector &names); + +// Copies buffers from the host to the OpenCL device +template +void HostToDevice(const Arguments &args, Buffers &buffers, BuffersHost &buffers_host, + Queue &queue, const std::vector &names); + +// ================================================================================================= + +// Conversion between half and single-precision +std::vector HalfToFloatBuffer(const std::vector& source); +void FloatToHalfBuffer(std::vector& result, const std::vector& source); + +// As above, but now for OpenCL data-types instead of std::vectors +Buffer HalfToFloatBuffer(const Buffer& source, cl_command_queue queue_raw); +void FloatToHalfBuffer(Buffer& result, const Buffer& source, cl_command_queue queue_raw); + +// ================================================================================================= +} // namespace clblast + +// CLBLAST_TEST_UTILITIES_H_ +#endif -- cgit v1.2.3