Skip to content

Commit 22aee64

Browse files
committed
[Code] Update:
- Add set element c api and expose it into python - Add more doc intp python - Minor readme fixes
1 parent dc9aaae commit 22aee64

35 files changed

+718
-169
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
[![JB Research](https://jb.gg/badges/research-flat-square.svg)](https://research.jetbrains.org/)
66
[![Ubuntu](https://github.com/JetBrains-Research/cuBool/workflows/Ubuntu/badge.svg?branch=master)](https://github.com/JetBrains-Research/cuBool/actions)
77
[![License](https://img.shields.io/badge/license-MIT-orange)](https://github.com/JetBrains-Research/cuBool/blob/master/LICENSE)
8+
[![Package](https://img.shields.io/badge/pip-cybool-%233776ab)](https://test.pypi.org/project/pycubool/)
89

910
**cuBool** is a linear Boolean algebra library primitives and operations for
1011
work with sparse matrices written on the NVIDIA CUDA platform. The primary
@@ -36,6 +37,7 @@ prototyping algorithms on a local computer for later running on a powerful serve
3637
- [X] CSR submatrix
3738
- [X] CSR matrix reduce
3839
- [X] CSR slicing
40+
- [ ] Matrix cached filling
3941
- [X] Sequential fallback backend for CPU
4042
- [X] Device capabilities query
4143
- [ ] IO matrix loading from mtx file
@@ -206,7 +208,7 @@ wrapper can be used to compute the same transitive closure problem for the
206208
directed graph within python environment:
207209
208210
```python
209-
from python import pycubool
211+
import pycubool
210212
211213
def transitive_closure(a: pycubool.Matrix):
212214
"""

cubool/CMakeLists.txt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ set(CUBOOL_SOURCES
2323
sources/core/matrix.cpp
2424
sources/core/matrix.hpp
2525
sources/io/logger.cpp
26-
sources/io/logger.hpp)
26+
sources/io/logger.hpp
27+
sources/utils/exclusive_scan.hpp
28+
)
2729

2830
set(CUBOOL_C_API_SOURCES
2931
include/cubool/cubool.h
@@ -36,6 +38,7 @@ set(CUBOOL_C_API_SOURCES
3638
sources/cuBool_SetupLogger.cpp
3739
sources/cuBool_Matrix_New.cpp
3840
sources/cuBool_Matrix_Build.cpp
41+
sources/cuBool_Matrix_SetElement.cpp
3942
sources/cuBool_Matrix_ExtractPairs.cpp
4043
sources/cuBool_Matrix_ExtractSubMatrix.cpp
4144
sources/cuBool_Matrix_Duplicate.cpp
@@ -101,8 +104,7 @@ if (CUBOOL_WITH_SEQUENTIAL)
101104
sources/sequential/sq_reduce.cpp
102105
sources/sequential/sq_reduce.hpp
103106
sources/sequential/sq_submatrix.cpp
104-
sources/sequential/sq_submatrix.hpp
105-
sources/sequential/sq_exclusive_scan.hpp)
107+
sources/sequential/sq_submatrix.hpp)
106108
endif()
107109

108110
# Shared library object config

cubool/include/cubool/cubool.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ typedef enum cuBool_Hint {
9090
CUBOOL_HINT_LOG_WARNING = 0x64,
9191
/** Logging hint: log all messages */
9292
CUBOOL_HINT_LOG_ALL = 0x128,
93+
/** No duplicates in the build data */
94+
CUBOOL_HINT_NO_DUPLICATES = 0x256
9395
} cuBool_Hint;
9496

9597
/** Hit mask */
@@ -225,7 +227,9 @@ CUBOOL_EXPORT CUBOOL_API cuBool_Status cuBool_Matrix_New(
225227
* Build sparse matrix from provided pairs array. Pairs are supposed to be stored
226228
* as (rows[i],cols[i]) for pair with i-th index.
227229
*
230+
* @note This function automatically reduces duplicates
228231
* @note Pass CUBOOL_HINT_VALUES_SORTED if values already in the row-col order.
232+
* @note Pass CUBOOL_HINT_NO_DUPLICATES if values has no duplicates
229233
*
230234
* @param matrix Matrix handle to perform operation on
231235
* @param rows Array of pairs row indices
@@ -243,6 +247,23 @@ CUBOOL_EXPORT CUBOOL_API cuBool_Status cuBool_Matrix_Build(
243247
cuBool_Hints hints
244248
);
245249

250+
/**
251+
* Sets specified (i, j) value of the matrix to True.
252+
*
253+
* @note This function automatically reduces duplicates
254+
*
255+
* @param matrix Matrix handle to perform operation on
256+
* @param i Row index
257+
* @param j Column Index
258+
*
259+
* @return Error code on this operation
260+
*/
261+
CUBOOL_EXPORT CUBOOL_API cuBool_Status cuBool_Matrix_SetElement(
262+
cuBool_Matrix matrix,
263+
cuBool_Index i,
264+
cuBool_Index j
265+
);
266+
246267
/**
247268
* Reads matrix data to the host visible CPU buffer as an array of values pair.
248269
*

cubool/sources/backend/matrix_base.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ namespace cubool {
3737
public:
3838
virtual ~MatrixBase() = default;
3939

40-
virtual void build(const index *rows, const index *cols, size_t nvals, bool isSorted) = 0;
40+
virtual void setElement(index i, index j) = 0;
41+
virtual void build(const index *rows, const index *cols, size_t nvals, bool isSorted, bool hasDuplicates) = 0;
4142
virtual void extract(index* rows, index* cols, size_t &nvals) = 0;
4243
virtual void extractSubMatrix(const MatrixBase& otherBase, index i, index j, index nrows, index ncols) = 0;
4344

cubool/sources/core/library.cpp

Lines changed: 49 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
namespace cubool {
4646

4747
std::unordered_set<class MatrixBase*> Library::mAllocated;
48-
BackendBase* Library::mBackend = nullptr;
48+
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;
5151

@@ -57,75 +57,61 @@ namespace cubool {
5757
// If user do not force the cpu backend usage
5858
if (!preferCpu) {
5959
#ifdef CUBOOL_WITH_CUDA
60-
mBackend = new CudaBackend();
60+
mBackend = std::make_shared<CudaBackend>();
6161
mBackend->initialize(initHints);
6262

6363
// Failed to setup cuda, release backend and go to try cpu
6464
if (!mBackend->isInitialized()) {
65-
delete mBackend;
6665
mBackend = nullptr;
67-
6866
mLogger->logWarning("Failed to initialize Cuda backend");
6967
}
7068
#endif
7169
}
7270

7371
#ifdef CUBOOL_WITH_SEQUENTIAL
7472
if (mBackend == nullptr) {
75-
mBackend = new SqBackend();
73+
mBackend = std::make_shared<SqBackend>();
7674
mBackend->initialize(initHints);
7775

7876
// Failed somehow setup
7977
if (!mBackend->isInitialized()) {
80-
delete mBackend;
8178
mBackend = nullptr;
82-
8379
mLogger->logWarning("Failed to initialize Cpu fallback backend");
8480
}
8581
}
8682
#endif
8783

8884
CHECK_RAISE_ERROR(mBackend != nullptr, BackendError, "Failed to select backend");
89-
mRelaxedRelease = initHints & CUBOOL_HINT_RELAXED_FINALIZE;
90-
91-
// Log device caps
92-
cuBool_DeviceCaps caps;
93-
queryCapabilities(caps);
9485

95-
std::stringstream ss;
96-
97-
if (caps.cudaSupported) {
98-
ss << "Cuda device capabilities" << std::endl
99-
<< " name: " << caps.name << std::endl
100-
<< " major: " << caps.major << std::endl
101-
<< " minor: " << caps.minor << std::endl
102-
<< " warp size: " << caps.warp << std::endl
103-
<< " globalMemoryKiBs: " << caps.globalMemoryKiBs << std::endl
104-
<< " sharedMemoryPerMultiProcKiBs: " << caps.sharedMemoryPerMultiProcKiBs << std::endl
105-
<< " sharedMemoryPerBlockKiBs: " << caps.sharedMemoryPerBlockKiBs << std::endl;
106-
}
107-
else {
108-
ss << "Cuda device is not presented";
109-
}
110-
111-
mLogger->logInfo(ss.str());
86+
// If initialized, post-init actions
87+
mRelaxedRelease = initHints & CUBOOL_HINT_RELAXED_FINALIZE;
88+
logDeviceInfo();
11289
}
11390

11491
void Library::finalize() {
11592
if (mBackend) {
11693
// Release all allocated resources implicitly
11794
if (mRelaxedRelease) {
11895
for (auto m: mAllocated) {
96+
std::stringstream s;
97+
s << "Implicitly release matrix instance " << m;
98+
99+
mLogger->logWarning(s.str());
119100
delete m;
120101
}
121102

122103
mAllocated.clear();
123104
}
124105

106+
// Some final message
107+
mLogger->logInfo("** cuBool:Finalize backend **");
108+
125109
// Remember to finalize backend
126110
mBackend->finalize();
127-
delete mBackend;
128111
mBackend = nullptr;
112+
113+
// Release (possibly setup text logger) logger, reassign dummy
114+
mLogger = std::make_shared<DummyLogger>();
129115
}
130116
}
131117

@@ -179,14 +165,18 @@ namespace cubool {
179165
file << "Level::Always";
180166
}
181167
file << std::setw(-1) << "] ";
182-
file << message << std::endl << std::endl;
168+
file << message << std::endl;
183169
});
184170

185171
// Assign new text logger
186172
mLogger = textLogger;
187173

188174
// Initial message
189175
mLogger->logInfo("*** cuBool::Logger file ***");
176+
177+
// Also log device capabilities
178+
if (isBackedInitialized())
179+
logDeviceInfo();
190180
}
191181

192182
MatrixBase *Library::createMatrix(size_t nrows, size_t ncols) {
@@ -222,6 +212,34 @@ namespace cubool {
222212
mBackend->queryCapabilities(caps);
223213
}
224214

215+
void Library::logDeviceInfo() {
216+
// Log device caps
217+
cuBool_DeviceCaps caps;
218+
queryCapabilities(caps);
219+
220+
std::stringstream ss;
221+
222+
if (caps.cudaSupported) {
223+
ss << "Cuda device capabilities" << std::endl
224+
<< " name: " << caps.name << std::endl
225+
<< " major: " << caps.major << std::endl
226+
<< " minor: " << caps.minor << std::endl
227+
<< " warp size: " << caps.warp << std::endl
228+
<< " globalMemoryKiBs: " << caps.globalMemoryKiBs << std::endl
229+
<< " sharedMemoryPerMultiProcKiBs: " << caps.sharedMemoryPerMultiProcKiBs << std::endl
230+
<< " sharedMemoryPerBlockKiBs: " << caps.sharedMemoryPerBlockKiBs << std::endl;
231+
}
232+
else {
233+
ss << "Cuda device is not presented";
234+
}
235+
236+
mLogger->logInfo(ss.str());
237+
}
238+
239+
bool Library::isBackedInitialized() {
240+
return mBackend != nullptr;
241+
}
242+
225243
class Logger * Library::getLogger() {
226244
return mLogger.get();
227245
}

cubool/sources/core/library.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,13 @@ namespace cubool {
4242
static void releaseMatrix(class MatrixBase *matrixBase);
4343
static void handleError(const std::exception& error);
4444
static void queryCapabilities(cuBool_DeviceCaps& caps);
45+
static void logDeviceInfo();
46+
static bool isBackedInitialized();
4547
static class Logger* getLogger();
4648

4749
private:
4850
static std::unordered_set<class MatrixBase*> mAllocated;
49-
static class BackendBase* mBackend;
51+
static std::shared_ptr<class BackendBase> mBackend;
5052
static std::shared_ptr<class Logger> mLogger;
5153
static bool mRelaxedRelease;
5254
};

0 commit comments

Comments
 (0)