|
23 | 23 | /**********************************************************************************/ |
24 | 24 |
|
25 | 25 | #include <core/vector.hpp> |
| 26 | +#include <core/matrix.hpp> |
26 | 27 | #include <core/error.hpp> |
27 | 28 | #include <core/library.hpp> |
28 | 29 | #include <utils/timer.hpp> |
@@ -106,11 +107,54 @@ namespace cubool { |
106 | 107 | } |
107 | 108 |
|
108 | 109 | void Vector::reduce(index &result, bool checkTime) { |
109 | | - RAISE_ERROR(NotImplemented, "This function is not implemented"); |
| 110 | + this->commitCache(); |
| 111 | + |
| 112 | + if (checkTime) { |
| 113 | + TIMER_ACTION(timer, mHnd->reduce(result, false)); |
| 114 | + |
| 115 | + LogStream stream(*Library::getLogger()); |
| 116 | + stream << Logger::Level::Info |
| 117 | + << "Time: " << timer.getElapsedTimeMs() << " ms " |
| 118 | + << "Vector::reduce: " |
| 119 | + << "index" << " =reduce" |
| 120 | + << this->getDebugMarker() |
| 121 | + << LogStream::cmt; |
| 122 | + |
| 123 | + return; |
| 124 | + } |
| 125 | + |
| 126 | + mHnd->reduce(result, false); |
110 | 127 | } |
111 | 128 |
|
112 | | - void Vector::reduceMatrix(const MatrixBase &matrix, bool transpose, bool checkTime) { |
113 | | - RAISE_ERROR(NotImplemented, "This function is not implemented"); |
| 129 | + void Vector::reduceMatrix(const MatrixBase &matrixBase, bool transpose, bool checkTime) { |
| 130 | + const auto* matrix = dynamic_cast<const Matrix*>(&matrixBase); |
| 131 | + |
| 132 | + CHECK_RAISE_ERROR(matrix != nullptr, InvalidArgument, "Passed matrix does not belong to core matrix class"); |
| 133 | + |
| 134 | + if (transpose) { |
| 135 | + CHECK_RAISE_ERROR(matrix->getNcols() == this->getNrows(), InvalidArgument, "Passed matrix has incompatible size"); |
| 136 | + } |
| 137 | + else { |
| 138 | + CHECK_RAISE_ERROR(matrix->getNrows() == this->getNrows(), InvalidArgument, "Passed matrix has incompatible size"); |
| 139 | + } |
| 140 | + |
| 141 | + matrix->commitCache(); |
| 142 | + this->releaseCache(); |
| 143 | + |
| 144 | + if (checkTime) { |
| 145 | + TIMER_ACTION(timer, mHnd->reduceMatrix(*matrix->mHnd, transpose, false)); |
| 146 | + |
| 147 | + LogStream stream(*Library::getLogger()); |
| 148 | + stream << Logger::Level::Info |
| 149 | + << "Time: " << timer.getElapsedTimeMs() << " ms " |
| 150 | + << "Vector::reduceMatrix: " |
| 151 | + << this->getDebugMarker() << " =reduce(trsp=" << transpose << ") " |
| 152 | + << matrix->getDebugMarker() << LogStream::cmt; |
| 153 | + |
| 154 | + return; |
| 155 | + } |
| 156 | + |
| 157 | + mHnd->reduceMatrix(*matrix->mHnd, transpose, false); |
114 | 158 | } |
115 | 159 |
|
116 | 160 | void Vector::eWiseAdd(const VectorBase &aBase, const VectorBase &bBase, bool checkTime) { |
@@ -146,6 +190,68 @@ namespace cubool { |
146 | 190 | mHnd->eWiseAdd(*a->mHnd, *b->mHnd, false); |
147 | 191 | } |
148 | 192 |
|
| 193 | + void Vector::multiplyVxM(const VectorBase &vBase, const class MatrixBase &mBase, bool checkTime) { |
| 194 | + const auto* v = dynamic_cast<const Vector*>(&vBase); |
| 195 | + const auto* m = dynamic_cast<const Matrix*>(&mBase); |
| 196 | + |
| 197 | + CHECK_RAISE_ERROR(v != nullptr, InvalidArgument, "Passed vector does not belong to core vector class"); |
| 198 | + CHECK_RAISE_ERROR(m != nullptr, InvalidArgument, "Passed matrix does not belong to core matrix class"); |
| 199 | + |
| 200 | + CHECK_RAISE_ERROR(v->getNrows() == m->getNrows(), InvalidArgument, "Provided vector and matrix have incompatible size for operation"); |
| 201 | + CHECK_RAISE_ERROR(this->getNrows() == v->getNrows(), InvalidArgument, "This vector has incompatible size for operation result"); |
| 202 | + |
| 203 | + v->commitCache(); |
| 204 | + m->commitCache(); |
| 205 | + this->releaseCache(); |
| 206 | + |
| 207 | + if (checkTime) { |
| 208 | + TIMER_ACTION(timer, mHnd->multiplyVxM(*v->mHnd, *m->mHnd, false)); |
| 209 | + |
| 210 | + LogStream stream(*Library::getLogger()); |
| 211 | + stream << Logger::Level::Info |
| 212 | + << "Time: " << timer.getElapsedTimeMs() << " ms " |
| 213 | + << "Vector::multiplyVxM: " |
| 214 | + << this->getDebugMarker() << " = " |
| 215 | + << v->getDebugMarker() << " x " |
| 216 | + << m->getDebugMarker() << LogStream::cmt; |
| 217 | + |
| 218 | + return; |
| 219 | + } |
| 220 | + |
| 221 | + mHnd->multiplyVxM(*v->mHnd, *m->mHnd, false); |
| 222 | + } |
| 223 | + |
| 224 | + void Vector::multiplyMxV(const class MatrixBase &mBase, const VectorBase &vBase, bool checkTime) { |
| 225 | + const auto* v = dynamic_cast<const Vector*>(&vBase); |
| 226 | + const auto* m = dynamic_cast<const Matrix*>(&mBase); |
| 227 | + |
| 228 | + CHECK_RAISE_ERROR(v != nullptr, InvalidArgument, "Passed vector does not belong to core vector class"); |
| 229 | + CHECK_RAISE_ERROR(m != nullptr, InvalidArgument, "Passed matrix does not belong to core matrix class"); |
| 230 | + |
| 231 | + CHECK_RAISE_ERROR(v->getNrows() == m->getNcols(), InvalidArgument, "Provided vector and matrix have incompatible size for operation"); |
| 232 | + CHECK_RAISE_ERROR(this->getNrows() == v->getNrows(), InvalidArgument, "This vector has incompatible size for operation result"); |
| 233 | + |
| 234 | + v->commitCache(); |
| 235 | + m->commitCache(); |
| 236 | + this->releaseCache(); |
| 237 | + |
| 238 | + if (checkTime) { |
| 239 | + TIMER_ACTION(timer, mHnd->multiplyMxV(*m->mHnd, *v->mHnd, false)); |
| 240 | + |
| 241 | + LogStream stream(*Library::getLogger()); |
| 242 | + stream << Logger::Level::Info |
| 243 | + << "Time: " << timer.getElapsedTimeMs() << " ms " |
| 244 | + << "Vector::multiplyMxV: " |
| 245 | + << this->getDebugMarker() << " = " |
| 246 | + << m->getDebugMarker() << " x " |
| 247 | + << v->getDebugMarker() << LogStream::cmt; |
| 248 | + |
| 249 | + return; |
| 250 | + } |
| 251 | + |
| 252 | + mHnd->multiplyMxV(*m->mHnd, *v->mHnd, false); |
| 253 | + } |
| 254 | + |
149 | 255 | index Vector::getNrows() const { |
150 | 256 | return mHnd->getNrows(); |
151 | 257 | } |
|
0 commit comments