summaryrefslogtreecommitdiff
path: root/src/utilities/buffer_test.hpp
blob: 4a2a2c95fb5539d545330428d47d7d71d17f7311 (plain)
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
// =================================================================================================
// 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 tests for the OpenCL buffers (matrices and vectors). These tests are
// templated and thus header-only.
//
// =================================================================================================

#ifndef CLBLAST_BUFFER_TEST_H_
#define CLBLAST_BUFFER_TEST_H_

#include <algorithm>
#include <vector>

#include "utilities/utilities.hpp"

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, 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); }
  } catch (const Error<std::runtime_error> &e) { throw BLASError(StatusCode::kInvalidMatrixA, e.what()); }
}

// 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, 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); }
  } catch (const Error<std::runtime_error> &e) { throw BLASError(StatusCode::kInvalidMatrixB, e.what()); }
}

// Tests matrix 'C' for validity
template <typename T>
void TestMatrixC(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::kInvalidLeadDimC); }
  try {
    const auto required_size = (ld * (two - 1) + one + offset) * sizeof(T);
    if (buffer.GetSize() < required_size) { throw BLASError(StatusCode::kInsufficientMemoryC); }
  } catch (const Error<std::runtime_error> &e) { throw BLASError(StatusCode::kInvalidMatrixC, e.what()); }
}

// Tests matrix 'AP' for validity
template <typename T>
void TestMatrixAP(const size_t n, const Buffer<T> &buffer, const size_t offset) {
  try {
    const auto required_size = (((n * (n + 1)) / 2) + offset) * sizeof(T);
    if (buffer.GetSize() < required_size) { throw BLASError(StatusCode::kInsufficientMemoryA); }
  } catch (const Error<std::runtime_error> &e) { throw BLASError(StatusCode::kInvalidMatrixA, e.what()); }
}

// =================================================================================================

// Tests vector 'X' for validity
template <typename T>
void TestVectorX(const size_t n, const Buffer<T> &buffer, const size_t offset, const size_t inc) {
  if (inc == 0) { throw BLASError(StatusCode::kInvalidIncrementX); }
  try {
    const auto required_size = ((n - 1) * inc + 1 + offset) * sizeof(T);
    if (buffer.GetSize() < required_size) { throw BLASError(StatusCode::kInsufficientMemoryX); }
  } catch (const Error<std::runtime_error> &e) { throw BLASError(StatusCode::kInvalidVectorX, e.what()); }
}

// Tests vector 'Y' for validity
template <typename T>
void TestVectorY(const size_t n, const Buffer<T> &buffer, const size_t offset, const size_t inc) {
  if (inc == 0) { throw BLASError(StatusCode::kInvalidIncrementY); }
  try {
    const auto required_size = ((n - 1) * inc + 1 + offset) * sizeof(T);
    if (buffer.GetSize() < required_size) { throw BLASError(StatusCode::kInsufficientMemoryY); }
  } catch (const Error<std::runtime_error> &e) { throw BLASError(StatusCode::kInvalidVectorY, e.what()); }
}

// =================================================================================================

// Tests vector 'scalar' for validity
template <typename T>
void TestVectorScalar(const size_t n, const Buffer<T> &buffer, const size_t offset) {
  try {
    const auto required_size = (n + offset) * sizeof(T);
    if (buffer.GetSize() < required_size) { throw BLASError(StatusCode::kInsufficientMemoryScalar); }
  } catch (const Error<std::runtime_error> &e) { throw BLASError(StatusCode::kInvalidVectorScalar, e.what()); }
}

// Tests vector 'index' for validity
template <typename T>
void TestVectorIndex(const size_t n, const Buffer<T> &buffer, const size_t offset) {
  try {
    const auto required_size = (n + offset) * sizeof(T);
    if (buffer.GetSize() < required_size) { throw BLASError(StatusCode::kInsufficientMemoryScalar); }
  } catch (const Error<std::runtime_error> &e) { throw BLASError(StatusCode::kInvalidVectorScalar, e.what()); }
}

// =================================================================================================

// Tests matrix 'A' for validity in a batched setting
template <typename T>
void TestBatchedMatrixA(const size_t one, const size_t two, const Buffer<T>& buffer,
                        const std::vector<size_t> &offsets, const size_t ld, const bool test_lead_dim = true) {
  const auto max_offset = *std::max_element(offsets.begin(), offsets.end());
  TestMatrixA(one, two, buffer, max_offset, ld, test_lead_dim);
}

// Tests matrix 'B' for validity in a batched setting
template <typename T>
void TestBatchedMatrixB(const size_t one, const size_t two, const Buffer<T>& buffer,
                        const std::vector<size_t>& offsets, const size_t ld, const bool test_lead_dim = true) {
  const auto max_offset = *std::max_element(offsets.begin(), offsets.end());
  TestMatrixB(one, two, buffer, max_offset, ld, test_lead_dim);
}

// Tests matrix 'C' for validity in a batched setting
template <typename T>
void TestBatchedMatrixC(const size_t one, const size_t two, const Buffer<T>& buffer,
                        const std::vector<size_t>& offsets, const size_t ld) {
  const auto max_offset = *std::max_element(offsets.begin(), offsets.end());
  TestMatrixC(one, two, buffer, max_offset, ld);
}

// =================================================================================================

// Tests matrix 'A' for validity in a strided batched setting
template <typename T>
void TestStridedBatchedMatrixA(const size_t one, const size_t two, const Buffer<T>& buffer,
                               const size_t offset, const size_t stride, const size_t batch_count,
                               const size_t ld, const bool test_lead_dim = true) {
  const auto last_batch_offset = (batch_count - 1) * stride;
  TestMatrixA(one, two, buffer, offset + last_batch_offset, ld, test_lead_dim);
}

// Tests matrix 'B' for validity in a strided batched setting
template <typename T>
void TestStridedBatchedMatrixB(const size_t one, const size_t two, const Buffer<T>& buffer,
                               const size_t offset, const size_t stride, const size_t batch_count,
                               const size_t ld, const bool test_lead_dim = true) {
  const auto last_batch_offset = (batch_count - 1) * stride;
  TestMatrixB(one, two, buffer, offset + last_batch_offset, ld, test_lead_dim);
}

// Tests matrix 'C' for validity in a strided batched setting
template <typename T>
void TestStridedBatchedMatrixC(const size_t one, const size_t two, const Buffer<T>& buffer,
                               const size_t offset, const size_t stride, const size_t batch_count,
                               const size_t ld) {
  const auto last_batch_offset = (batch_count - 1) * stride;
  TestMatrixC(one, two, buffer, offset + last_batch_offset, ld);
}

// =================================================================================================
} // namespace clblast

// CLBLAST_BUFFER_TEST_H_
#endif