1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
|
// =================================================================================================
// 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 <www.cedricnugteren.nl>
//
// This file implements the TestBlas class (see the header for information about the class).
//
// =================================================================================================
#include <algorithm>
#include <iostream>
#include <random>
#include "utilities/utilities.hpp"
#include "test/correctness/testblas.hpp"
namespace clblast {
// =================================================================================================
// The transpose configurations to test with: template parameter dependent
template <> const std::vector<Transpose> TestBlas<half,half>::kTransposes = {Transpose::kNo, Transpose::kYes};
template <> const std::vector<Transpose> TestBlas<float,float>::kTransposes = {Transpose::kNo, Transpose::kYes};
template <> const std::vector<Transpose> TestBlas<double,double>::kTransposes = {Transpose::kNo, Transpose::kYes};
template <> const std::vector<Transpose> TestBlas<float2,float2>::kTransposes = {Transpose::kNo, Transpose::kYes, Transpose::kConjugate};
template <> const std::vector<Transpose> TestBlas<double2,double2>::kTransposes = {Transpose::kNo, Transpose::kYes, Transpose::kConjugate};
template <> const std::vector<Transpose> TestBlas<float2,float>::kTransposes = {Transpose::kNo, Transpose::kConjugate};
template <> const std::vector<Transpose> TestBlas<double2,double>::kTransposes = {Transpose::kNo, Transpose::kConjugate};
// Constructor, initializes the base class tester and input data
template <typename T, typename U>
TestBlas<T,U>::TestBlas(const std::vector<std::string> &arguments, const bool silent,
const std::string &name, const std::vector<std::string> &options,
const DataPrepare prepare_data,
const Routine run_routine,
const Routine run_reference1, const Routine run_reference2,
const ResultGet get_result, const ResultIndex get_index,
const ResultIterator get_id1, const ResultIterator get_id2):
Tester<T,U>(arguments, silent, name, options),
kOffsets(GetOffsets()),
kAlphaValues(GetExampleScalars<U>(full_test_)),
kBetaValues(GetExampleScalars<U>(full_test_)),
prepare_data_(prepare_data),
run_routine_(run_routine),
run_reference1_(run_reference1),
run_reference2_(run_reference2),
get_result_(get_result),
get_index_(get_index),
get_id1_(get_id1),
get_id2_(get_id2) {
// Sanity check
if (!compare_clblas_ && !compare_cblas_) {
throw std::runtime_error("Invalid configuration: no reference to test against");
}
// Computes the maximum sizes. This allows for a single set of input/output buffers.
const auto max_vec = *std::max_element(kVectorDims.begin(), kVectorDims.end());
const auto max_inc = *std::max_element(kIncrements.begin(), kIncrements.end());
const auto max_mat = *std::max_element(kMatrixDims.begin(), kMatrixDims.end());
const auto max_ld = *std::max_element(kMatrixDims.begin(), kMatrixDims.end());
const auto max_matvec = *std::max_element(kMatrixVectorDims.begin(), kMatrixVectorDims.end());
const auto max_offset = *std::max_element(kOffsets.begin(), kOffsets.end());
const auto max_batch_count = *std::max_element(kBatchCounts.begin(), kBatchCounts.end());
// Creates test input data. Adds a 'canary' region to detect buffer overflows
x_source_.resize(max_batch_count * std::max(max_vec, max_matvec)*max_inc + max_offset + kCanarySize);
y_source_.resize(max_batch_count * std::max(max_vec, max_matvec)*max_inc + max_offset + kCanarySize);
a_source_.resize(max_batch_count * std::max(max_mat, max_matvec)*std::max(max_ld, max_matvec) + max_offset + kCanarySize);
b_source_.resize(max_batch_count * std::max(max_mat, max_matvec)*std::max(max_ld, max_matvec) + max_offset + kCanarySize);
c_source_.resize(max_batch_count * std::max(max_mat, max_matvec)*std::max(max_ld, max_matvec) + max_offset + kCanarySize);
ap_source_.resize(max_batch_count * std::max(max_mat, max_matvec)*std::max(max_mat, max_matvec) + max_offset + kCanarySize);
scalar_source_.resize(max_batch_count * std::max(max_mat, max_matvec) + max_offset + kCanarySize);
std::mt19937 mt(kSeed);
std::uniform_real_distribution<double> dist(kTestDataLowerLimit, kTestDataUpperLimit);
PopulateVector(x_source_, mt, dist);
PopulateVector(y_source_, mt, dist);
PopulateVector(a_source_, mt, dist);
PopulateVector(b_source_, mt, dist);
PopulateVector(c_source_, mt, dist);
PopulateVector(ap_source_, mt, dist);
PopulateVector(scalar_source_, mt, dist);
}
// ===============================================================================================
// Tests the routine for a wide variety of parameters
template <typename T, typename U>
void TestBlas<T,U>::TestRegular(std::vector<Arguments<U>> &test_vector, const std::string &name) {
if (!PrecisionSupported<T>(device_)) { return; }
TestStart("regular behaviour", name);
// Iterates over all the to-be-tested combinations of arguments
for (auto &args: test_vector) {
// Adds a 'canary' region to detect buffer overflows
args.x_size += kCanarySize;
args.y_size += kCanarySize;
args.a_size += kCanarySize;
args.b_size += kCanarySize;
args.c_size += kCanarySize;
args.ap_size += kCanarySize;
args.scalar_size += kCanarySize;
// Prints the current test configuration
if (verbose_) {
fprintf(stdout, " Testing: %s", GetOptionsString(args).c_str());
std::cout << std::flush;
}
// Optionally prepares the input data
prepare_data_(args, queue_, kSeed,
x_source_, y_source_, a_source_, b_source_, c_source_,
ap_source_, scalar_source_);
// Set-up for the CLBlast run
auto x_vec2 = Buffer<T>(context_, args.x_size);
auto y_vec2 = Buffer<T>(context_, args.y_size);
auto a_mat2 = Buffer<T>(context_, args.a_size);
auto b_mat2 = Buffer<T>(context_, args.b_size);
auto c_mat2 = Buffer<T>(context_, args.c_size);
auto ap_mat2 = Buffer<T>(context_, args.ap_size);
auto scalar2 = Buffer<T>(context_, args.scalar_size);
auto scalar_uint2 = Buffer<unsigned int>(context_, args.scalar_size);
x_vec2.Write(queue_, args.x_size, x_source_);
y_vec2.Write(queue_, args.y_size, y_source_);
a_mat2.Write(queue_, args.a_size, a_source_);
b_mat2.Write(queue_, args.b_size, b_source_);
c_mat2.Write(queue_, args.c_size, c_source_);
ap_mat2.Write(queue_, args.ap_size, ap_source_);
scalar2.Write(queue_, args.scalar_size, scalar_source_);
auto buffers2 = Buffers<T>{x_vec2, y_vec2, a_mat2, b_mat2, c_mat2, ap_mat2, scalar2, scalar_uint2};
// Runs CLBlast
if (verbose_) {
fprintf(stdout, "[CLBlast]");
std::cout << std::flush;
}
const auto status2 = run_routine_(args, buffers2, queue_);
// Don't continue with CBLAS if there are incorrect parameters
if (compare_cblas_ && status2 != StatusCode::kSuccess) {
if (verbose_) {
fprintf(stdout, " -> %d -> ", static_cast<int>(status2));
std::cout << std::flush;
}
TestErrorCodes(status2, status2, args);
continue;
}
// Set-up for the reference run
auto x_vec1 = Buffer<T>(context_, args.x_size);
auto y_vec1 = Buffer<T>(context_, args.y_size);
auto a_mat1 = Buffer<T>(context_, args.a_size);
auto b_mat1 = Buffer<T>(context_, args.b_size);
auto c_mat1 = Buffer<T>(context_, args.c_size);
auto ap_mat1 = Buffer<T>(context_, args.ap_size);
auto scalar1 = Buffer<T>(context_, args.scalar_size);
auto scalar_uint1 = Buffer<unsigned int>(context_, args.scalar_size);
x_vec1.Write(queue_, args.x_size, x_source_);
y_vec1.Write(queue_, args.y_size, y_source_);
a_mat1.Write(queue_, args.a_size, a_source_);
b_mat1.Write(queue_, args.b_size, b_source_);
c_mat1.Write(queue_, args.c_size, c_source_);
ap_mat1.Write(queue_, args.ap_size, ap_source_);
scalar1.Write(queue_, args.scalar_size, scalar_source_);
auto buffers1 = Buffers<T>{x_vec1, y_vec1, a_mat1, b_mat1, c_mat1, ap_mat1, scalar1, scalar_uint1};
// Runs the reference code
if (verbose_) {
if (compare_clblas_) { fprintf(stdout, " [clBLAS]"); }
else if (compare_cblas_) { fprintf(stdout, " [CPU BLAS]"); }
std::cout << std::flush;
}
auto status1 = StatusCode::kSuccess;
if (compare_clblas_) { status1 = run_reference1_(args, buffers1, queue_); }
else if (compare_cblas_) { status1 = run_reference2_(args, buffers1, queue_); }
// Tests for equality of the two status codes
if (verbose_) { fprintf(stdout, " -> "); std::cout << std::flush; }
if (status1 != StatusCode::kSuccess || status2 != StatusCode::kSuccess) {
TestErrorCodes(status1, status2, args);
continue;
}
// Downloads the results
auto result1 = get_result_(args, buffers1, queue_);
auto result2 = get_result_(args, buffers2, queue_);
// Computes the L2 error
auto l2error = 0.0;
const auto kErrorMarginL2 = getL2ErrorMargin<T>();
for (auto id1=size_t{0}; id1<get_id1_(args); ++id1) {
for (auto id2=size_t{0}; id2<get_id2_(args); ++id2) {
auto index = get_index_(args, id1, id2);
l2error += SquaredDifference(result1[index], result2[index]);
}
}
l2error /= static_cast<double>(get_id1_(args) * get_id2_(args));
// Checks for differences in the output
auto errors = size_t{0};
for (auto id1=size_t{0}; id1<get_id1_(args); ++id1) {
for (auto id2=size_t{0}; id2<get_id2_(args); ++id2) {
auto index = get_index_(args, id1, id2);
if (!TestSimilarity(result1[index], result2[index])) {
if (l2error >= kErrorMarginL2) { errors++; }
if (verbose_) {
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) {
std::cout << " - error suppressed by a low total L2 error" << std::endl;
}
}
}
}
}
// Checks for differences in the 'canary' region to detect buffer overflows
for (auto canary_id=size_t{0}; canary_id<kCanarySize; ++canary_id) {
auto index = get_index_(args, get_id1_(args) - 1, get_id2_(args) - 1) + canary_id;
if (index >= result1.size() || index >= result2.size()) {
continue;
}
if (!TestSimilarity(result1[index], result2[index])) {
errors++;
if (verbose_) {
if (get_id2_(args) == 1) { std::cout << std::endl << " Buffer overflow index " << index << ": "; }
else { std::cout << std::endl << " Buffer overflow " << index << ": "; }
std::cout << " " << ToString(result1[index]) << " (reference) versus ";
std::cout << " " << ToString(result2[index]) << " (CLBlast)";
}
}
}
// Report the results
if (verbose_ && errors > 0) {
fprintf(stdout, "\n Combined average L2 error: %.2e\n ", l2error);
}
// Tests the error count (should be zero)
TestErrorCount(errors, get_id1_(args)*get_id2_(args) + kCanarySize, args);
}
TestEnd();
}
// =================================================================================================
// Tests the routine for cases with invalid OpenCL memory buffer sizes. Tests only on return-types,
// does not test for results (if any).
template <typename T, typename U>
void TestBlas<T,U>::TestInvalid(std::vector<Arguments<U>> &test_vector, const std::string &name) {
if (!PrecisionSupported<T>(device_)) { return; }
if (!compare_clblas_) { return; } // not supported for CPU BLAS routines
if (std::is_same<T, half>::value) { return; } // not supported for half-precision
TestStart("invalid buffer sizes", name);
// Iterates over all the to-be-tested combinations of arguments
for (const auto &args: test_vector) {
// Prints the current test configuration
if (verbose_) {
fprintf(stdout, " Testing: %s", GetSizesString(args).c_str());
std::cout << std::flush;
}
// Creates the buffers. Note: we are not using the cxpp11.h C++ version since we explicitly
// want to be able to create invalid buffers (no error checking here).
auto x_vec1 = CreateInvalidBuffer<T>(context_, args.x_size);
auto y_vec1 = CreateInvalidBuffer<T>(context_, args.y_size);
auto a_mat1 = CreateInvalidBuffer<T>(context_, args.a_size);
auto b_mat1 = CreateInvalidBuffer<T>(context_, args.b_size);
auto c_mat1 = CreateInvalidBuffer<T>(context_, args.c_size);
auto ap_mat1 = CreateInvalidBuffer<T>(context_, args.ap_size);
auto scalar1 = CreateInvalidBuffer<T>(context_, args.scalar_size);
auto scalar_uint1 = CreateInvalidBuffer<unsigned int>(context_, args.scalar_size);
auto x_vec2 = CreateInvalidBuffer<T>(context_, args.x_size);
auto y_vec2 = CreateInvalidBuffer<T>(context_, args.y_size);
auto a_mat2 = CreateInvalidBuffer<T>(context_, args.a_size);
auto b_mat2 = CreateInvalidBuffer<T>(context_, args.b_size);
auto c_mat2 = CreateInvalidBuffer<T>(context_, args.c_size);
auto ap_mat2 = CreateInvalidBuffer<T>(context_, args.ap_size);
auto scalar2 = CreateInvalidBuffer<T>(context_, args.scalar_size);
auto scalar_uint2 = CreateInvalidBuffer<unsigned int>(context_, args.scalar_size);
auto buffers1 = Buffers<T>{x_vec1, y_vec1, a_mat1, b_mat1, c_mat1, ap_mat1, scalar1, scalar_uint1};
auto buffers2 = Buffers<T>{x_vec2, y_vec2, a_mat2, b_mat2, c_mat2, ap_mat2, scalar2, scalar_uint2};
// Runs CLBlast
if (verbose_) {
fprintf(stdout, "[CLBlast]");
std::cout << std::flush;
}
const auto status2 = run_routine_(args, buffers2, queue_);
// Runs the reference code
if (verbose_) {
if (compare_clblas_) { fprintf(stdout, " [clBLAS]"); }
else if (compare_cblas_) { fprintf(stdout, " [CPU BLAS]"); }
std::cout << std::flush;
}
auto status1 = StatusCode::kSuccess;
if (compare_clblas_) { status1 = run_reference1_(args, buffers1, queue_); }
else if (compare_cblas_) { status1 = run_reference2_(args, buffers1, queue_); }
// Tests for equality of the two status codes
if (verbose_) { fprintf(stdout, " -> "); std::cout << std::flush; }
TestErrorCodes(status1, status2, args);
}
TestEnd();
}
// =================================================================================================
// Compiles the templated class
template class TestBlas<half, half>;
template class TestBlas<float, float>;
template class TestBlas<double, double>;
template class TestBlas<float2, float2>;
template class TestBlas<double2, double2>;
template class TestBlas<float2, float>;
template class TestBlas<double2, double>;
// =================================================================================================
} // namespace clblast
|