Skip to content

Commit e97f951

Browse files
committed
[Code] Add matrix debug markers and expose this feature into Python API
1 parent 9098085 commit e97f951

File tree

13 files changed

+289
-23
lines changed

13 files changed

+289
-23
lines changed

README.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,25 @@ prototyping algorithms on a local computer for later running on a powerful serve
2929

3030
- [X] Library C interface
3131
- [X] Library setup
32-
- [X] CSR matrix
33-
- [X] CSR multiplication
34-
- [X] CSR element-wise addition
35-
- [X] CSR kronecker
36-
- [X] CSR transpose
37-
- [X] CSR submatrix
38-
- [X] CSR matrix reduce
39-
- [X] CSR slicing
40-
- [ ] Matrix cached filling
32+
- [X] Sparse matrix
33+
- [X] Sparse matrix multiplication
34+
- [X] Sparse matrix element-wise addition
35+
- [X] Sparse matrix kronecker
36+
- [X] Sparse matrix transpose
37+
- [X] Sparse matrix submatrix
38+
- [X] Sparse matrix reduce
39+
- [X] Sparse matrix slicing
40+
- [X] Matrix cached filling
4141
- [X] Sequential fallback backend for CPU
4242
- [X] Device capabilities query
4343
- [ ] IO matrix loading from mtx file
4444
- [ ] IO matrix saving into mtx file
4545
- [ ] IO matrix saving into gviz format
4646
- [X] IO user-defined file logging
4747
- [X] Wrapper for Python API
48-
- [ ] Wrapper syntax sugar
48+
- [X] Wrapper syntax sugar
4949
- [ ] Tests for Python wrapper
50-
- [ ] Pip package
50+
- [X] Pip package
5151
- [ ] Code examples
5252
- [ ] User guide
5353
- [X] Unit Tests collection

cubool/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ set(CUBOOL_C_API_SOURCES
3939
sources/cuBool_Matrix_New.cpp
4040
sources/cuBool_Matrix_Build.cpp
4141
sources/cuBool_Matrix_SetElement.cpp
42+
sources/cuBool_Matrix_SetMarker.cpp
43+
sources/cuBool_Matrix_GetMarker.cpp
4244
sources/cuBool_Matrix_ExtractPairs.cpp
4345
sources/cuBool_Matrix_ExtractSubMatrix.cpp
4446
sources/cuBool_Matrix_Duplicate.cpp

cubool/include/cubool/cubool.h

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ CUBOOL_EXPORT CUBOOL_API cuBool_Status cuBool_GetVersion(
159159
* @note Pass `CUBOOL_HINT_LOG_WARNING` to include warning messages into log
160160
* @note Pass `CUBOOL_HINT_LOG_ALL` to include all messages into log
161161
*
162-
* @param logFileName UTF-8 encoded file name and path.
162+
* @param logFileName UTF-8 encoded null-terminated file name and path string.
163163
* @param hints Logging hints to filter messages.
164164
*
165165
* @return Error code on this operation
@@ -264,6 +264,38 @@ CUBOOL_EXPORT CUBOOL_API cuBool_Status cuBool_Matrix_SetElement(
264264
cuBool_Index j
265265
);
266266

267+
/**
268+
* Sets to the matrix specific debug string marker.
269+
* This marker will appear in the log messages as string identifier of the matrix.
270+
*
271+
* @param matrix Matrix handle to perform operation on
272+
* @param marker UTF-8 encoded null-terminated string marker name.
273+
*
274+
* @return Error code on this operation
275+
*/
276+
CUBOOL_EXPORT CUBOOL_API cuBool_Status cuBool_Matrix_SetMarker(
277+
cuBool_Matrix matrix,
278+
const char* marker
279+
);
280+
281+
/**
282+
* Allows to get matrix debug string marker.
283+
*
284+
* @note Pass null marker if you want to retrieve only the required marker buffer size.
285+
* @note After the function call the actual size of the marker is stored in the size variable.
286+
*
287+
* @param matrix Matrix handle to perform operation on
288+
* @param[in,out] marker Where to store null-terminated UTF-8 encoded marker string.
289+
* @param[in,out] size Size of the provided buffer in bytes to save marker string.
290+
*
291+
* @return Error code on this operation
292+
*/
293+
CUBOOL_EXPORT CUBOOL_API cuBool_Status cuBool_Matrix_GetMarker(
294+
cuBool_Matrix matrix,
295+
char* marker,
296+
cuBool_Index* size
297+
);
298+
267299
/**
268300
* Reads matrix data to the host visible CPU buffer as an array of values pair.
269301
*

cubool/sources/core/library.cpp

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444

4545
namespace cubool {
4646

47-
std::unordered_set<class MatrixBase*> Library::mAllocated;
47+
std::unordered_set<class Matrix*> Library::mAllocated;
4848
std::shared_ptr<class BackendBase> Library::mBackend = nullptr;
4949
std::shared_ptr<class Logger> Library::mLogger = std::make_shared<DummyLogger>();
5050
bool Library::mRelaxedRelease = false;
@@ -92,11 +92,11 @@ namespace cubool {
9292
if (mBackend) {
9393
// Release all allocated resources implicitly
9494
if (mRelaxedRelease) {
95-
for (auto m: mAllocated) {
96-
std::stringstream s;
97-
s << "Implicitly release matrix instance " << m;
95+
LogStream stream(*getLogger());
96+
stream << Logger::Level::Info << "Enabled relaxed library finalize" << LogStream::cmt;
9897

99-
mLogger->logWarning(s.str());
98+
for (auto m: mAllocated) {
99+
stream << Logger::Level::Warning << "Implicitly release matrix " << m->getDebugMarker() << LogStream::cmt;
100100
delete m;
101101
}
102102

@@ -185,14 +185,25 @@ namespace cubool {
185185

186186
auto m = new Matrix(nrows, ncols, *mBackend);
187187
mAllocated.emplace(m);
188+
189+
LogStream stream(*getLogger());
190+
stream << Logger::Level::Info << "Create Matrix " << m->getDebugMarker()
191+
<< " (" << nrows << "," << ncols << ")" << LogStream::cmt;
192+
188193
return m;
189194
}
190195

191196
void Library::releaseMatrix(MatrixBase *matrixBase) {
192197
if (mRelaxedRelease && !mBackend) return;
193198

194-
mAllocated.erase(matrixBase);
195-
delete matrixBase;
199+
auto m = (Matrix*)(matrixBase);
200+
CHECK_RAISE_ERROR(mAllocated.find(m) != mAllocated.end(), InvalidArgument, "No such matrix was allocated");
201+
202+
LogStream stream(*getLogger());
203+
stream << Logger::Level::Info << "Release Matrix " << m->getDebugMarker() << LogStream::cmt;
204+
205+
mAllocated.erase(m);
206+
delete m;
196207
}
197208

198209
void Library::handleError(const std::exception& error) {
@@ -227,7 +238,7 @@ namespace cubool {
227238
<< " warp size: " << caps.warp << std::endl
228239
<< " globalMemoryKiBs: " << caps.globalMemoryKiBs << std::endl
229240
<< " sharedMemoryPerMultiProcKiBs: " << caps.sharedMemoryPerMultiProcKiBs << std::endl
230-
<< " sharedMemoryPerBlockKiBs: " << caps.sharedMemoryPerBlockKiBs << std::endl;
241+
<< " sharedMemoryPerBlockKiBs: " << caps.sharedMemoryPerBlockKiBs;
231242
}
232243
else {
233244
ss << "Cuda device is not presented";

cubool/sources/core/library.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ namespace cubool {
4747
static class Logger* getLogger();
4848

4949
private:
50-
static std::unordered_set<class MatrixBase*> mAllocated;
50+
static std::unordered_set<class Matrix*> mAllocated;
5151
static std::shared_ptr<class BackendBase> mBackend;
5252
static std::shared_ptr<class Logger> mLogger;
5353
static bool mRelaxedRelease;

cubool/sources/core/matrix.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ namespace cubool {
3333
Matrix::Matrix(size_t nrows, size_t ncols, BackendBase &backend) {
3434
mHnd = backend.createMatrix(nrows, ncols);
3535
mProvider = &backend;
36+
37+
// By default marker is the address of the matrix
38+
std::stringstream s;
39+
s << this;
40+
mMarker = s.str();
3641
}
3742

3843
Matrix::~Matrix() {
@@ -207,13 +212,21 @@ namespace cubool {
207212

208213
void Matrix::setDebugMarker(const char *marker) {
209214
CHECK_RAISE_ERROR(marker, InvalidArgument, "Null pointer marker string");
210-
mMarker = marker;
215+
216+
// Marker = "$marker (address)"
217+
std::stringstream s;
218+
s << marker << " (" << this << ")";
219+
mMarker = s.str();
211220
}
212221

213222
const char * Matrix::getDebugMarker() const {
214223
return mMarker.c_str();
215224
}
216225

226+
index Matrix::getDebugMarkerSizeWithNullT() const {
227+
return mMarker.length() + 1;
228+
}
229+
217230
void Matrix::releaseCache() const {
218231
mCachedI.clear();
219232
mCachedJ.clear();

cubool/sources/core/matrix.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232

3333
namespace cubool {
3434

35+
/**
36+
* Proxy matrix for the actual backend matrix implementation.
37+
* Behaves as validation/auxiliary layer.
38+
*/
3539
class Matrix final: public MatrixBase {
3640
public:
3741
Matrix(size_t nrows, size_t ncols, BackendBase& backend);
@@ -56,6 +60,7 @@ namespace cubool {
5660

5761
void setDebugMarker(const char* marker);
5862
const char* getDebugMarker() const;
63+
index getDebugMarkerSizeWithNullT() const;
5964

6065
private:
6166

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
#include <cstring>
27+
28+
cuBool_Status cuBool_Matrix_GetMarker(
29+
cuBool_Matrix matrix,
30+
char* marker,
31+
cuBool_Index* size
32+
) {
33+
CUBOOL_BEGIN_BODY
34+
CUBOOL_VALIDATE_LIBRARY
35+
CUBOOL_ARG_NOT_NULL(matrix)
36+
CUBOOL_ARG_NOT_NULL(size)
37+
38+
auto m = (cubool::Matrix*) matrix;
39+
auto actualSize = m->getDebugMarkerSizeWithNullT();
40+
41+
if (marker != nullptr && *size > 0) {
42+
const auto* text = m->getDebugMarker();
43+
std::memcpy(marker, text, *size);
44+
marker[*size - 1] = '\0';
45+
}
46+
47+
*size = actualSize;
48+
CUBOOL_END_BODY
49+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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_Matrix_SetMarker(
28+
cuBool_Matrix matrix,
29+
const char* marker
30+
) {
31+
CUBOOL_BEGIN_BODY
32+
CUBOOL_VALIDATE_LIBRARY
33+
CUBOOL_ARG_NOT_NULL(matrix)
34+
CUBOOL_ARG_NOT_NULL(marker)
35+
auto m = (cubool::Matrix*) matrix;
36+
m->setDebugMarker(marker);
37+
CUBOOL_END_BODY
38+
}

cubool/sources/io/logger.hpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <string>
2929
#include <vector>
3030
#include <functional>
31+
#include <sstream>
3132

3233
namespace cubool {
3334

@@ -98,6 +99,47 @@ namespace cubool {
9899
size_t getMessagesCount() const override;
99100
};
100101

102+
/**
103+
* Utility to build and log fancy messages.
104+
*/
105+
class LogStream {
106+
public:
107+
struct Commit {};
108+
constexpr static const Commit cmt = Commit{};
109+
110+
explicit LogStream(Logger& logger)
111+
: mLogger(logger), mLevel(Logger::Level::Info) {
112+
};
113+
LogStream(LogStream&& other) noexcept = default;
114+
~LogStream() = default;
115+
116+
void commit() {
117+
mLogger.log(mLevel, mStream.str());
118+
mStream.str(std::string());
119+
}
120+
121+
friend LogStream& operator<<(LogStream& stream, Logger::Level level) {
122+
stream.mLevel = level;
123+
return stream;
124+
}
125+
126+
friend LogStream& operator<<(LogStream& stream, Commit commit) {
127+
stream.commit();
128+
return stream;
129+
}
130+
131+
template<typename T>
132+
friend LogStream& operator<<(LogStream& stream, T&& t) {
133+
stream.mStream << std::forward<T>(t);
134+
return stream;
135+
}
136+
137+
private:
138+
Logger& mLogger;
139+
Logger::Level mLevel;
140+
std::stringstream mStream;
141+
};
142+
101143
}
102144

103145
#endif //CUBOOL_LOGGER_HPP

0 commit comments

Comments
 (0)