Skip to content

Commit 62c4a6b

Browse files
committed
[Code] Setup basic vector interface && expose basic C API
1 parent ffa2125 commit 62c4a6b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1095
-53
lines changed

cubool/CMakeLists.txt

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,12 @@ set(CUBOOL_SOURCES
3838
sources/core/error.hpp
3939
sources/core/library.cpp
4040
sources/core/library.hpp
41+
sources/core/object.cpp
42+
sources/core/object.hpp
4143
sources/core/matrix.cpp
4244
sources/core/matrix.hpp
45+
sources/core/vector.cpp
46+
sources/core/vector.hpp
4347
sources/io/logger.cpp
4448
sources/io/logger.hpp
4549
sources/utils/exclusive_scan.hpp
@@ -71,12 +75,25 @@ set(CUBOOL_C_API_SOURCES
7175
sources/cuBool_Matrix_Free.cpp
7276
sources/cuBool_Matrix_Reduce.cpp
7377
sources/cuBool_Matrix_EWiseAdd.cpp
78+
sources/cuBool_Vector_New.cpp
79+
sources/cuBool_Vector_Build.cpp
80+
sources/cuBool_Vector_SetElement.cpp
81+
sources/cuBool_Vector_SetMarker.cpp
82+
sources/cuBool_Vector_Marker.cpp
83+
sources/cuBool_Vector_ExtractValues.cpp
84+
sources/cuBool_Vector_ExtractSubVector.cpp
85+
sources/cuBool_Vector_Duplicate.cpp
86+
sources/cuBool_Vector_Nvals.cpp
87+
sources/cuBool_Vector_Nrows.cpp
88+
sources/cuBool_Vector_Reduce.cpp
89+
sources/cuBool_Vector_Free.cpp
7490
sources/cuBool_MxM.cpp
7591
sources/cuBool_Kronecker.cpp)
7692

7793
set(CUBOOL_BACKEND_SOURCES
7894
sources/backend/backend_base.hpp
79-
sources/backend/matrix_base.hpp)
95+
sources/backend/matrix_base.hpp
96+
sources/backend/vector_base.hpp)
8097

8198
set(CUBOOL_CUDA_SOURCES)
8299
set(CUBOOL_SEQUENTIAL_SOURCES)
@@ -116,7 +133,9 @@ if (CUBOOL_WITH_SEQUENTIAL)
116133
sources/sequential/sq_backend.hpp
117134
sources/sequential/sq_matrix.cpp
118135
sources/sequential/sq_matrix.hpp
119-
sources/sequential/sq_csr_data.hpp
136+
sources/sequential/sq_vector.cpp
137+
sources/sequential/sq_vector.hpp
138+
sources/sequential/sq_data.hpp
120139
sources/sequential/sq_transpose.cpp
121140
sources/sequential/sq_transpose.hpp
122141
sources/sequential/sq_kronecker.cpp

cubool/include/cubool/cubool.h

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,12 @@ typedef uint32_t cuBool_Hints;
106106
/** Alias integer type for indexing operations */
107107
typedef uint32_t cuBool_Index;
108108

109-
/** Cubool sparse boolean matrix handle */
109+
/** cuBool sparse boolean matrix handle */
110110
typedef struct cuBool_Matrix_t* cuBool_Matrix;
111111

112+
/** cuBool sparse boolean vector handle */
113+
typedef struct cuBool_Vector_t* cuBool_Vector;
114+
112115
/** Cuda device capabilities */
113116
typedef struct cuBool_DeviceCaps {
114117
char name[256];
@@ -407,7 +410,7 @@ CUBOOL_EXPORT CUBOOL_API cuBool_Status cuBool_Matrix_Nvals(
407410
*
408411
* @param matrix Matrix handle to perform operation on
409412
* @param nrows[out] Pointer to the place where to store number of matrix rows
410-
*
413+
*
411414
* @return Error code on this operation
412415
*/
413416
CUBOOL_EXPORT CUBOOL_API cuBool_Status cuBool_Matrix_Nrows(
@@ -485,6 +488,73 @@ CUBOOL_EXPORT CUBOOL_API cuBool_Status cuBool_Matrix_EWiseAdd(
485488
cuBool_Hints hints
486489
);
487490

491+
CUBOOL_EXPORT CUBOOL_API cuBool_Status cuBool_Vector_New(
492+
cuBool_Vector* vector,
493+
cuBool_Index nrows
494+
);
495+
496+
CUBOOL_EXPORT CUBOOL_API cuBool_Status cuBool_Vector_Build(
497+
cuBool_Vector vector,
498+
const cuBool_Index* rows,
499+
cuBool_Index nvals,
500+
cuBool_Hints hints
501+
);
502+
503+
CUBOOL_EXPORT CUBOOL_API cuBool_Status cuBool_Vector_SetElement(
504+
cuBool_Vector vector,
505+
cuBool_Index i
506+
);
507+
508+
CUBOOL_EXPORT CUBOOL_API cuBool_Status cuBool_Vector_SetMarker(
509+
cuBool_Vector vector,
510+
const char* marker
511+
);
512+
513+
CUBOOL_EXPORT CUBOOL_API cuBool_Status cuBool_Vector_Marker(
514+
cuBool_Vector vector,
515+
char* marker,
516+
cuBool_Index* size
517+
);
518+
519+
CUBOOL_EXPORT CUBOOL_API cuBool_Status cuBool_Vector_ExtractValues(
520+
cuBool_Vector vector,
521+
cuBool_Index* rows,
522+
cuBool_Index* nvals
523+
);
524+
525+
CUBOOL_EXPORT CUBOOL_API cuBool_Status cuBool_Vector_ExtractSubVector(
526+
cuBool_Vector result,
527+
cuBool_Vector vector,
528+
cuBool_Index i,
529+
cuBool_Index nrows,
530+
cuBool_Hints hints
531+
);
532+
533+
CUBOOL_EXPORT CUBOOL_API cuBool_Status cuBool_Vector_Duplicate(
534+
cuBool_Vector vector,
535+
cuBool_Vector* duplicated
536+
);
537+
538+
CUBOOL_EXPORT CUBOOL_API cuBool_Status cuBool_Vector_Nvals(
539+
cuBool_Vector vector,
540+
cuBool_Index* nvals
541+
);
542+
543+
CUBOOL_EXPORT CUBOOL_API cuBool_Status cuBool_Vector_Nrows(
544+
cuBool_Vector vector,
545+
cuBool_Index* nrows
546+
);
547+
548+
CUBOOL_EXPORT CUBOOL_API cuBool_Status cuBool_Vector_Free(
549+
cuBool_Vector vector
550+
);
551+
552+
CUBOOL_EXPORT CUBOOL_API cuBool_Status cuBool_Vector_Reduce(
553+
cuBool_Index* result,
554+
cuBool_Vector vector,
555+
cuBool_Hints hints
556+
);
557+
488558
/**
489559
* Performs result (accum)= left x right evaluation, where source '+' and 'x' are boolean semiring operations.
490560
* If accum hint passed, the the result of the multiplication is added to the result matrix.

cubool/sources/backend/backend_base.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#define CUBOOL_BACKEND_BASE_HPP
2727

2828
#include <backend/matrix_base.hpp>
29+
#include <backend/vector_base.hpp>
2930
#include <core/config.hpp>
3031

3132
namespace cubool {
@@ -37,7 +38,9 @@ namespace cubool {
3738
virtual void finalize() = 0;
3839
virtual bool isInitialized() const = 0;
3940
virtual MatrixBase* createMatrix(size_t nrows, size_t ncols) = 0;
41+
virtual VectorBase* createVector(size_t nrows) = 0;
4042
virtual void releaseMatrix(MatrixBase* matrixBase) = 0;
43+
virtual void releaseVector(VectorBase* vectorBase) = 0;
4144
virtual void queryCapabilities(cuBool_DeviceCaps& caps) = 0;
4245
};
4346

cubool/sources/backend/matrix_base.hpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
#define CUBOOL_MATRIX_BASE_HPP
2727

2828
#include <core/config.hpp>
29-
#include <string>
3029

3130
namespace cubool {
3231

@@ -40,8 +39,7 @@ namespace cubool {
4039
virtual void setElement(index i, index j) = 0;
4140
virtual void build(const index *rows, const index *cols, size_t nvals, bool isSorted, bool noDuplicates) = 0;
4241
virtual void extract(index* rows, index* cols, size_t &nvals) = 0;
43-
virtual void
44-
extractSubMatrix(const MatrixBase &otherBase, index i, index j, index nrows, index ncols, bool checkTime) = 0;
42+
virtual void extractSubMatrix(const MatrixBase &otherBase, index i, index j, index nrows, index ncols, bool checkTime) = 0;
4543

4644
virtual void clone(const MatrixBase& otherBase) = 0;
4745
virtual void transpose(const MatrixBase &otherBase, bool checkTime) = 0;
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
#ifndef CUBOOL_VECTOR_BASE_HPP
26+
#define CUBOOL_VECTOR_BASE_HPP
27+
28+
#include <core/config.hpp>
29+
30+
namespace cubool {
31+
32+
/**
33+
* Base class for any boolean vector type
34+
*/
35+
class VectorBase {
36+
public:
37+
virtual ~VectorBase() = default;
38+
39+
virtual void setElement(index i) = 0;
40+
virtual void build(const index *rows, size_t nvals, bool isSorted, bool noDuplicates) = 0;
41+
virtual void extract(index* rows, size_t &nvals) = 0;
42+
virtual void extractSubVector(const VectorBase &otherBase, index i, index nrows, bool checkTime) = 0;
43+
44+
virtual void clone(const VectorBase& otherBase) = 0;
45+
virtual void reduce(index &result, bool checkTime) = 0;
46+
virtual void reduceMatrix(const class MatrixBase& matrix, bool transpose, bool checkTime) = 0;
47+
48+
virtual index getNrows() const = 0;
49+
virtual index getNvals() const = 0;
50+
51+
bool isZeroDim() const { return getNrows() == 0; }
52+
};
53+
54+
}
55+
56+
#endif //CUBOOL_VECTOR_BASE_HPP

cubool/sources/core/config.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
#define CUBOOL_CONFIG_HPP
2727

2828
#include <cubool/cubool.h>
29+
#include <cinttypes>
30+
#include <cstddef>
2931

3032
namespace cubool {
3133
using index = cuBool_Index;

cubool/sources/core/library.cpp

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <core/library.hpp>
2626
#include <core/error.hpp>
2727
#include <core/matrix.hpp>
28+
#include <core/vector.hpp>
2829
#include <backend/backend_base.hpp>
2930
#include <backend/matrix_base.hpp>
3031
#include <io/logger.hpp>
@@ -44,7 +45,7 @@
4445

4546
namespace cubool {
4647

47-
std::unordered_set<class Matrix*> Library::mAllocated;
48+
std::unordered_set<class Matrix*> Library::mAllocMatrices;
4849
std::shared_ptr<class BackendBase> Library::mBackend = nullptr;
4950
std::shared_ptr<class Logger> Library::mLogger = std::make_shared<DummyLogger>();
5051
bool Library::mRelaxedRelease = false;
@@ -95,16 +96,22 @@ namespace cubool {
9596
LogStream stream(*getLogger());
9697
stream << Logger::Level::Info << "Enabled relaxed library finalize" << LogStream::cmt;
9798

98-
for (auto m: mAllocated) {
99+
for (auto m: mAllocMatrices) {
99100
stream << Logger::Level::Warning << "Implicitly release matrix " << m->getDebugMarker() << LogStream::cmt;
100101
delete m;
101102
}
102103

103-
mAllocated.clear();
104+
for (auto v: mAllocVectors) {
105+
stream << Logger::Level::Warning << "Implicitly release vector " << v->getDebugMarker() << LogStream::cmt;
106+
delete v;
107+
}
108+
109+
mAllocMatrices.clear();
110+
mAllocVectors.clear();
104111
}
105112

106113
// Some final message
107-
mLogger->logInfo("** cuBool:Finalize backend **");
114+
mLogger->logInfo("*** cuBool:Finalize backend ***");
108115

109116
// Remember to finalize backend
110117
mBackend->finalize();
@@ -184,7 +191,7 @@ namespace cubool {
184191
CHECK_RAISE_ERROR(ncols > 0, InvalidArgument, "Cannot create matrix with zero dimension");
185192

186193
auto m = new Matrix(nrows, ncols, *mBackend);
187-
mAllocated.emplace(m);
194+
mAllocMatrices.emplace(m);
188195

189196
LogStream stream(*getLogger());
190197
stream << Logger::Level::Info << "Create Matrix " << m->getDebugMarker()
@@ -193,18 +200,43 @@ namespace cubool {
193200
return m;
194201
}
195202

203+
class Vector * Library::createVector(size_t nrows) {
204+
CHECK_RAISE_ERROR(nrows > 0, InvalidArgument, "Cannot create vector with zero dimension");
205+
206+
auto v = new Vector(nrows, *mBackend);
207+
mAllocVectors.emplace(v);
208+
209+
LogStream stream(*getLogger());
210+
stream << Logger::Level::Info << "Create Vector " << v->getDebugMarker()
211+
<< " (" << nrows << ")" << LogStream::cmt;
212+
213+
return v;
214+
}
215+
196216
void Library::releaseMatrix(Matrix *matrix) {
197217
if (mRelaxedRelease && !mBackend) return;
198218

199-
CHECK_RAISE_ERROR(mAllocated.find(matrix) != mAllocated.end(), InvalidArgument, "No such matrix was allocated");
219+
CHECK_RAISE_ERROR(mAllocMatrices.find(matrix) != mAllocMatrices.end(), InvalidArgument, "No such matrix was allocated");
200220

201221
LogStream stream(*getLogger());
202222
stream << Logger::Level::Info << "Release Matrix " << matrix->getDebugMarker() << LogStream::cmt;
203223

204-
mAllocated.erase(matrix);
224+
mAllocMatrices.erase(matrix);
205225
delete matrix;
206226
}
207227

228+
void Library::releaseVector(class Vector *vector) {
229+
if (mRelaxedRelease && !mBackend) return;
230+
231+
CHECK_RAISE_ERROR(mAllocVectors.find(vector) != mAllocVectors.end(), InvalidArgument, "No such vector was allocated");
232+
233+
LogStream stream(*getLogger());
234+
stream << Logger::Level::Info << "Release Vector " << vector->getDebugMarker() << LogStream::cmt;
235+
236+
mAllocVectors.erase(vector);
237+
delete vector;
238+
}
239+
208240
void Library::handleError(const std::exception& error) {
209241
mLogger->log(Logger::Level::Error, error.what());
210242
}

cubool/sources/core/library.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,18 @@ namespace cubool {
3939
static void validate();
4040
static void setupLogging(const char* logFileName, cuBool_Hints hints);
4141
static class Matrix *createMatrix(size_t nrows, size_t ncols);
42+
static class Vector *createVector(size_t nrows);
4243
static void releaseMatrix(class Matrix *matrix);
44+
static void releaseVector(class Vector *vector);
4345
static void handleError(const std::exception& error);
4446
static void queryCapabilities(cuBool_DeviceCaps& caps);
4547
static void logDeviceInfo();
4648
static bool isBackedInitialized();
4749
static class Logger* getLogger();
4850

4951
private:
50-
static std::unordered_set<class Matrix*> mAllocated;
52+
static std::unordered_set<class Matrix*> mAllocMatrices;
53+
static std::unordered_set<class Vector*> mAllocVectors;
5154
static std::shared_ptr<class BackendBase> mBackend;
5255
static std::shared_ptr<class Logger> mLogger;
5356
static bool mRelaxedRelease;

cubool/sources/core/matrix.cpp

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -330,23 +330,6 @@ namespace cubool {
330330
return mHnd->getNvals();
331331
}
332332

333-
void Matrix::setDebugMarker(const char *marker) {
334-
CHECK_RAISE_ERROR(marker, InvalidArgument, "Null pointer marker string");
335-
336-
// Marker = "$marker (address)"
337-
std::stringstream s;
338-
s << marker << " (" << this << ")";
339-
mMarker = s.str();
340-
}
341-
342-
const char * Matrix::getDebugMarker() const {
343-
return mMarker.c_str();
344-
}
345-
346-
index Matrix::getDebugMarkerSizeWithNullT() const {
347-
return mMarker.length() + 1;
348-
}
349-
350333
void Matrix::releaseCache() const {
351334
mCachedI.clear();
352335
mCachedJ.clear();

0 commit comments

Comments
 (0)