diff options
author | Cedric Nugteren <web@cedricnugteren.nl> | 2017-08-12 20:50:00 +0200 |
---|---|---|
committer | Cedric Nugteren <web@cedricnugteren.nl> | 2017-08-12 20:50:00 +0200 |
commit | 777681dcbdf18493320dd7b94fccd5c6faee9455 (patch) | |
tree | b8597f5d79f8ef33bffbf33f3de2548cc51d4c5c /src/utilities | |
parent | 97bcf77d4bc9b31e32a8785787e0497ac5440e44 (diff) | |
parent | d67fd6604b4a6584c4f9e856057fcc8076ce377d (diff) |
Merge branch 'master' into im_to_col
Diffstat (limited to 'src/utilities')
-rw-r--r-- | src/utilities/buffer_test.hpp | 8 | ||||
-rw-r--r-- | src/utilities/utilities.cpp | 97 | ||||
-rw-r--r-- | src/utilities/utilities.hpp | 67 |
3 files changed, 8 insertions, 164 deletions
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 <typename T> void TestMatrixA(const size_t one, const size_t two, const Buffer<T> &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<T> &buffer, // Tests matrix 'B' for validity template <typename T> void TestMatrixB(const size_t one, const size_t two, const Buffer<T> &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/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 <www.cedricnugteren.nl> // -// 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 <typename T> bool IsCloseToZero(const T value) { return (value > -SmallConstant<T>()) && (value < SmallConstant<T>()); } -template bool IsCloseToZero<float>(const float); -template bool IsCloseToZero<double>(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<std::string> &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<unsigned int>(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 <typename T> void PopulateVector(std::vector<T> &vector, std::mt19937 &mt, std::uniform_real_distribution<double> &dist) { @@ -354,87 +340,6 @@ void PopulateVector(std::vector<half> &vector, std::mt19937 &mt, std::uniform_re // ================================================================================================= -template <typename T, typename U> -void DeviceToHost(const Arguments<U> &args, Buffers<T> &buffers, BuffersHost<T> &buffers_host, - Queue &queue, const std::vector<std::string> &names) { - for (auto &name: names) { - if (name == kBufVecX) {buffers_host.x_vec = std::vector<T>(args.x_size, static_cast<T>(0)); buffers.x_vec.Read(queue, args.x_size, buffers_host.x_vec); } - else if (name == kBufVecY) { buffers_host.y_vec = std::vector<T>(args.y_size, static_cast<T>(0)); buffers.y_vec.Read(queue, args.y_size, buffers_host.y_vec); } - else if (name == kBufMatA) { buffers_host.a_mat = std::vector<T>(args.a_size, static_cast<T>(0)); buffers.a_mat.Read(queue, args.a_size, buffers_host.a_mat); } - else if (name == kBufMatB) { buffers_host.b_mat = std::vector<T>(args.b_size, static_cast<T>(0)); buffers.b_mat.Read(queue, args.b_size, buffers_host.b_mat); } - else if (name == kBufMatC) { buffers_host.c_mat = std::vector<T>(args.c_size, static_cast<T>(0)); buffers.c_mat.Read(queue, args.c_size, buffers_host.c_mat); } - else if (name == kBufMatAP) { buffers_host.ap_mat = std::vector<T>(args.ap_size, static_cast<T>(0)); buffers.ap_mat.Read(queue, args.ap_size, buffers_host.ap_mat); } - else if (name == kBufScalar) { buffers_host.scalar = std::vector<T>(args.scalar_size, static_cast<T>(0)); buffers.scalar.Read(queue, args.scalar_size, buffers_host.scalar); } - else { throw std::runtime_error("Invalid buffer name"); } - } -} - -template <typename T, typename U> -void HostToDevice(const Arguments<U> &args, Buffers<T> &buffers, BuffersHost<T> &buffers_host, - Queue &queue, const std::vector<std::string> &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<half>&, Buffers<half>&, BuffersHost<half>&, Queue&, const std::vector<std::string>&); -template void DeviceToHost(const Arguments<float>&, Buffers<float>&, BuffersHost<float>&, Queue&, const std::vector<std::string>&); -template void DeviceToHost(const Arguments<double>&, Buffers<double>&, BuffersHost<double>&, Queue&, const std::vector<std::string>&); -template void DeviceToHost(const Arguments<float>&, Buffers<float2>&, BuffersHost<float2>&, Queue&, const std::vector<std::string>&); -template void DeviceToHost(const Arguments<double>&, Buffers<double2>&, BuffersHost<double2>&, Queue&, const std::vector<std::string>&); -template void DeviceToHost(const Arguments<float2>&, Buffers<float2>&, BuffersHost<float2>&, Queue&, const std::vector<std::string>&); -template void DeviceToHost(const Arguments<double2>&, Buffers<double2>&, BuffersHost<double2>&, Queue&, const std::vector<std::string>&); -template void HostToDevice(const Arguments<half>&, Buffers<half>&, BuffersHost<half>&, Queue&, const std::vector<std::string>&); -template void HostToDevice(const Arguments<float>&, Buffers<float>&, BuffersHost<float>&, Queue&, const std::vector<std::string>&); -template void HostToDevice(const Arguments<double>&, Buffers<double>&, BuffersHost<double>&, Queue&, const std::vector<std::string>&); -template void HostToDevice(const Arguments<float>&, Buffers<float2>&, BuffersHost<float2>&, Queue&, const std::vector<std::string>&); -template void HostToDevice(const Arguments<double>&, Buffers<double2>&, BuffersHost<double2>&, Queue&, const std::vector<std::string>&); -template void HostToDevice(const Arguments<float2>&, Buffers<float2>&, BuffersHost<float2>&, Queue&, const std::vector<std::string>&); -template void HostToDevice(const Arguments<double2>&, Buffers<double2>&, BuffersHost<double2>&, Queue&, const std::vector<std::string>&); - -// ================================================================================================= - -// Conversion between half and single-precision -std::vector<float> HalfToFloatBuffer(const std::vector<half>& source) { - auto result = std::vector<float>(source.size()); - for (auto i = size_t(0); i < source.size(); ++i) { result[i] = HalfToFloat(source[i]); } - return result; -} -void FloatToHalfBuffer(std::vector<half>& result, const std::vector<float>& 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<float> HalfToFloatBuffer(const Buffer<half>& 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<half>(size); - source.Read(queue, size, source_cpu); - auto result_cpu = HalfToFloatBuffer(source_cpu); - auto result = Buffer<float>(context, size); - result.Write(queue, size, result_cpu); - return result; -} -void FloatToHalfBuffer(Buffer<half>& result, const Buffer<float>& 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<float>(size); - source.Read(queue, size, source_cpu); - auto result_cpu = std::vector<half>(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<half>::Type GetRealArg(const half value) { return HalfToFloat(value); } diff --git a/src/utilities/utilities.hpp b/src/utilities/utilities.hpp index 8daeda08..784e0324 100644 --- a/src/utilities/utilities.hpp +++ b/src/utilities/utilities.hpp @@ -7,10 +7,9 @@ // Author(s): // Cedric Nugteren <www.cedricnugteren.nl> // -// 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. // // ================================================================================================= @@ -94,19 +93,6 @@ constexpr auto kArgDilationW = "dilationw"; // 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"; @@ -114,6 +100,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"; @@ -146,9 +133,6 @@ template <typename T> T SmallConstant(); // Returns the absolute value of a scalar (modulus in case of complex numbers) template <typename T> typename BaseType<T>::Type AbsoluteValue(const T value); -// Returns whether a scalar is close to zero -template <typename T> bool IsCloseToZero(const T value); - // ================================================================================================= // Structure containing all possible arguments for test clients, including their default values @@ -233,28 +217,6 @@ struct Arguments { bool no_abbrv = false; }; -// Structure containing all possible buffers for test clients -template <typename T> -struct Buffers { - Buffer<T> x_vec; - Buffer<T> y_vec; - Buffer<T> a_mat; - Buffer<T> b_mat; - Buffer<T> c_mat; - Buffer<T> ap_mat; - Buffer<T> scalar; -}; -template <typename T> -struct BuffersHost { - std::vector<T> x_vec; - std::vector<T> y_vec; - std::vector<T> a_mat; - std::vector<T> b_mat; - std::vector<T> c_mat; - std::vector<T> ap_mat; - std::vector<T> scalar; -}; - // ================================================================================================= // Converts a value (e.g. an integer) to a string. This also covers special cases for CLBlast @@ -289,9 +251,6 @@ bool CheckArgument(const std::vector<std::string> &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; @@ -302,26 +261,6 @@ void PopulateVector(std::vector<T> &vector, std::mt19937 &mt, std::uniform_real_ // ================================================================================================= -// Copies buffers from the OpenCL device to the host -template <typename T, typename U> -void DeviceToHost(const Arguments<U> &args, Buffers<T> &buffers, BuffersHost<T> &buffers_host, - Queue &queue, const std::vector<std::string> &names); - -// Copies buffers from the host to the OpenCL device -template <typename T, typename U> -void HostToDevice(const Arguments<U> &args, Buffers<T> &buffers, BuffersHost<T> &buffers_host, - Queue &queue, const std::vector<std::string> &names); - -// ================================================================================================= - -// Conversion between half and single-precision -std::vector<float> HalfToFloatBuffer(const std::vector<half>& source); -void FloatToHalfBuffer(std::vector<half>& result, const std::vector<float>& source); - -// As above, but now for OpenCL data-types instead of std::vectors -Buffer<float> HalfToFloatBuffer(const Buffer<half>& source, cl_command_queue queue_raw); -void FloatToHalfBuffer(Buffer<half>& result, const Buffer<float>& 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 <typename T> struct RealArg { using Type = T; }; |