Skip to content

Commit d25f0bd

Browse files
committed
[Code] Add mxv vxm operations into backend interface && expose it into c api
1 parent c1c49e9 commit d25f0bd

File tree

10 files changed

+238
-4
lines changed

10 files changed

+238
-4
lines changed

cubool/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ set(CUBOOL_C_API_SOURCES
8989
sources/cuBool_Vector_Reduce.cpp
9090
sources/cuBool_Vector_EWiseAdd.cpp
9191
sources/cuBool_MxM.cpp
92+
sources/cuBool_MxV.cpp
93+
sources/cuBool_VxM.cpp
9294
sources/cuBool_Kronecker.cpp)
9395

9496
set(CUBOOL_BACKEND_SOURCES

cubool/include/cubool/cubool.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,20 @@ CUBOOL_EXPORT CUBOOL_API cuBool_Status cuBool_MxM(
588588
cuBool_Hints hints
589589
);
590590

591+
CUBOOL_EXPORT CUBOOL_API cuBool_Status cuBool_MxV(
592+
cuBool_Vector result,
593+
cuBool_Matrix matrix,
594+
cuBool_Vector vector,
595+
cuBool_Hints hints
596+
);
597+
598+
CUBOOL_EXPORT CUBOOL_API cuBool_Status cuBool_VxM(
599+
cuBool_Vector result,
600+
cuBool_Vector vector,
601+
cuBool_Matrix matrix,
602+
cuBool_Hints hints
603+
);
604+
591605
/**
592606
* Performs result = left `kron` right, where `kron` is a Kronecker product for boolean semiring.
593607
*

cubool/sources/backend/vector_base.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ namespace cubool {
4646
virtual void reduceMatrix(const class MatrixBase& matrix, bool transpose, bool checkTime) = 0;
4747

4848
virtual void eWiseAdd(const VectorBase &aBase, const VectorBase &bBase, bool checkTime) = 0;
49+
virtual void multiplyVxM(const VectorBase& vBase, const class MatrixBase& mBase, bool checkTime) = 0;
50+
virtual void multiplyMxV(const class MatrixBase& mBase, const VectorBase& vBase, bool checkTime) = 0;
4951

5052
virtual index getNrows() const = 0;
5153
virtual index getNvals() const = 0;

cubool/sources/core/matrix.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ namespace cubool {
6161
index getNvals() const override;
6262

6363
private:
64-
64+
friend class Vector;
6565
void releaseCache() const;
6666
void commitCache() const;
6767

cubool/sources/core/vector.cpp

Lines changed: 109 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
/**********************************************************************************/
2424

2525
#include <core/vector.hpp>
26+
#include <core/matrix.hpp>
2627
#include <core/error.hpp>
2728
#include <core/library.hpp>
2829
#include <utils/timer.hpp>
@@ -106,11 +107,54 @@ namespace cubool {
106107
}
107108

108109
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);
110127
}
111128

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);
114158
}
115159

116160
void Vector::eWiseAdd(const VectorBase &aBase, const VectorBase &bBase, bool checkTime) {
@@ -146,6 +190,68 @@ namespace cubool {
146190
mHnd->eWiseAdd(*a->mHnd, *b->mHnd, false);
147191
}
148192

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+
149255
index Vector::getNrows() const {
150256
return mHnd->getNrows();
151257
}

cubool/sources/core/vector.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ namespace cubool {
4949
void reduceMatrix(const MatrixBase &matrix, bool transpose, bool checkTime) override;
5050

5151
void eWiseAdd(const VectorBase &aBase, const VectorBase &bBase, bool checkTime) override;
52+
void multiplyVxM(const VectorBase& vBase, const class MatrixBase& mBase, bool checkTime) override;
53+
void multiplyMxV(const class MatrixBase& mBase, const VectorBase& vBase, bool checkTime) override;
5254

5355
index getNrows() const override;
5456
index getNvals() const override;

cubool/sources/cuBool_MxV.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**********************************************************************************/
2+
/* MIT License */
3+
/* */
4+
/* Copyright (c) 2020, 2021 JetBrains-Research */
5+
/* */
6+
/* Permission is hereby granted, free of charge, to any person obtaining a copy */
7+
/* of this software and associated documentation files (the "Software"), to deal */
8+
/* in the Software without restriction, including without limitation the rights */
9+
/* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell */
10+
/* copies of the Software, and to permit persons to whom the Software is */
11+
/* furnished to do so, subject to the following conditions: */
12+
/* */
13+
/* The above copyright notice and this permission notice shall be included in all */
14+
/* copies or substantial portions of the Software. */
15+
/* */
16+
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */
17+
/* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */
18+
/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */
19+
/* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */
20+
/* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
21+
/* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */
22+
/* SOFTWARE. */
23+
/**********************************************************************************/
24+
25+
#include <cuBool_Common.hpp>
26+
27+
cuBool_Status cuBool_MxV(
28+
cuBool_Vector result,
29+
cuBool_Matrix matrix,
30+
cuBool_Vector vector,
31+
cuBool_Hints hints
32+
) {
33+
CUBOOL_BEGIN_BODY
34+
CUBOOL_VALIDATE_LIBRARY
35+
CUBOOL_ARG_NOT_NULL(result)
36+
CUBOOL_ARG_NOT_NULL(matrix)
37+
CUBOOL_ARG_NOT_NULL(vector)
38+
auto resultV = (cubool::Vector *) result;
39+
auto left = (cubool::Matrix *) matrix;
40+
auto right = (cubool::Vector *) vector;
41+
resultV->multiplyMxV(*left, *right, hints & CUBOOL_HINT_TIME_CHECK);
42+
CUBOOL_END_BODY
43+
}

cubool/sources/cuBool_VxM.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**********************************************************************************/
2+
/* MIT License */
3+
/* */
4+
/* Copyright (c) 2020, 2021 JetBrains-Research */
5+
/* */
6+
/* Permission is hereby granted, free of charge, to any person obtaining a copy */
7+
/* of this software and associated documentation files (the "Software"), to deal */
8+
/* in the Software without restriction, including without limitation the rights */
9+
/* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell */
10+
/* copies of the Software, and to permit persons to whom the Software is */
11+
/* furnished to do so, subject to the following conditions: */
12+
/* */
13+
/* The above copyright notice and this permission notice shall be included in all */
14+
/* copies or substantial portions of the Software. */
15+
/* */
16+
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */
17+
/* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */
18+
/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */
19+
/* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */
20+
/* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
21+
/* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */
22+
/* SOFTWARE. */
23+
/**********************************************************************************/
24+
25+
#include <cuBool_Common.hpp>
26+
27+
cuBool_Status cuBool_VxM(
28+
cuBool_Vector result,
29+
cuBool_Vector vector,
30+
cuBool_Matrix matrix,
31+
cuBool_Hints hints
32+
) {
33+
CUBOOL_BEGIN_BODY
34+
CUBOOL_VALIDATE_LIBRARY
35+
CUBOOL_ARG_NOT_NULL(result)
36+
CUBOOL_ARG_NOT_NULL(vector)
37+
CUBOOL_ARG_NOT_NULL(matrix)
38+
auto resultV = (cubool::Vector *) result;
39+
auto left = (cubool::Vector *) vector;
40+
auto right = (cubool::Matrix *) matrix;
41+
resultV->multiplyVxM(*left, *right, hints & CUBOOL_HINT_TIME_CHECK);
42+
CUBOOL_END_BODY
43+
}

cubool/sources/sequential/sq_vector.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,26 @@ namespace cubool {
125125
this->mData = std::move(out);
126126
}
127127

128+
void SqVector::multiplyVxM(const VectorBase &vBase, const class MatrixBase &mBase, bool checkTime) {
129+
auto v = dynamic_cast<const SqVector*>(&vBase);
130+
auto m = dynamic_cast<const SqMatrix*>(&mBase);
131+
132+
CHECK_RAISE_ERROR(v != nullptr, InvalidArgument, "Provided vector does not belongs to sequential vector class");
133+
CHECK_RAISE_ERROR(m != nullptr, InvalidArgument, "Provided matrix does not belongs to sequential matrix class");
134+
135+
// todo
136+
}
137+
138+
void SqVector::multiplyMxV(const class MatrixBase &mBase, const VectorBase &vBase, bool checkTime) {
139+
auto v = dynamic_cast<const SqVector*>(&vBase);
140+
auto m = dynamic_cast<const SqMatrix*>(&mBase);
141+
142+
CHECK_RAISE_ERROR(v != nullptr, InvalidArgument, "Provided vector does not belongs to sequential vector class");
143+
CHECK_RAISE_ERROR(m != nullptr, InvalidArgument, "Provided matrix does not belongs to sequential matrix class");
144+
145+
// todo
146+
}
147+
128148
index SqVector::getNrows() const {
129149
return mData.nrows;
130150
}

cubool/sources/sequential/sq_vector.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ namespace cubool {
4545
void reduceMatrix(const class MatrixBase &matrix, bool transpose, bool checkTime) override;
4646

4747
void eWiseAdd(const VectorBase &aBase, const VectorBase &bBase, bool checkTime) override;
48+
void multiplyVxM(const VectorBase& vBase, const class MatrixBase& mBase, bool checkTime) override;
49+
void multiplyMxV(const class MatrixBase& mBase, const VectorBase& vBase, bool checkTime) override;
4850

4951
index getNrows() const override;
5052
index getNvals() const override;

0 commit comments

Comments
 (0)