Skip to content

Commit

Permalink
gh-219: add memview object for storage inspection
Browse files Browse the repository at this point in the history
  • Loading branch information
EgorOrachyov committed Aug 28, 2023
1 parent 7016b3e commit 5894e9b
Show file tree
Hide file tree
Showing 23 changed files with 674 additions and 311 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ add_library(spla SHARED
include/spla/exec.hpp
include/spla/io.hpp
include/spla/library.hpp
include/spla/memview.hpp
include/spla/matrix.hpp
include/spla/object.hpp
include/spla/op.hpp
Expand Down Expand Up @@ -340,6 +341,7 @@ add_library(spla SHARED
src/exec.cpp
src/library.cpp
src/matrix.cpp
src/memview.cpp
src/op.cpp
src/scalar.cpp
src/schedule.cpp
Expand All @@ -353,8 +355,10 @@ add_library(spla SHARED
src/binding/c_config.hpp
src/binding/c_library.cpp
src/binding/c_matrix.cpp
src/binding/c_memview.cpp
src/binding/c_object.cpp
src/binding/c_op.cpp
src/binding/c_ref.cpp
src/binding/c_scalar.cpp
src/binding/c_type.cpp
src/binding/c_vector.cpp
Expand Down
33 changes: 27 additions & 6 deletions include/spla.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,12 @@ typedef enum spla_AcceleratorType {

#define SPLA_NULL NULL

typedef int32_t spla_bool;
typedef uint32_t spla_uint;
typedef size_t spla_size_t;

typedef struct spla_RefCnt_t* spla_RefCnt;
typedef struct spla_MemView_t* spla_MemView;
typedef struct spla_Object_t* spla_Object;
typedef struct spla_Type_t* spla_Type;
typedef struct spla_Descriptor_t* spla_Descriptor;
Expand Down Expand Up @@ -195,10 +199,25 @@ SPLA_API spla_OpSelect spla_OpSelect_NEVER_FLOAT();

//////////////////////////////////////////////////////////////////////////////////////

/* General base RefCnt type methods */

SPLA_API spla_Status spla_RefCnt_ref(spla_RefCnt object);
SPLA_API spla_Status spla_RefCnt_unref(spla_RefCnt object);

//////////////////////////////////////////////////////////////////////////////////////

/* General base Object type methods */

SPLA_API spla_Status spla_Object_ref(spla_Object object);
SPLA_API spla_Status spla_Object_unref(spla_Object object);
//////////////////////////////////////////////////////////////////////////////////////

/* Memory view resource methods */

SPLA_API spla_Status spla_MemView_make(spla_MemView* view, void* buffer, spla_size_t size, spla_bool is_mutable);
SPLA_API spla_Status spla_MemView_read(spla_MemView view, spla_size_t offset, spla_size_t size, void* dst);
SPLA_API spla_Status spla_MemView_write(spla_MemView view, spla_size_t offset, spla_size_t size, const void* src);
SPLA_API spla_Status spla_MemView_get_buffer(spla_MemView view, void** buffer);
SPLA_API spla_Status spla_MemView_get_size(spla_MemView view, spla_size_t* size);
SPLA_API spla_Status spla_MemView_is_mutable(spla_MemView view, spla_bool* is_mutable);

//////////////////////////////////////////////////////////////////////////////////////

Expand All @@ -225,6 +244,8 @@ SPLA_API spla_Status spla_Array_get_int(spla_Array a, spla_uint i, int* value);
SPLA_API spla_Status spla_Array_get_uint(spla_Array a, spla_uint i, unsigned int* value);
SPLA_API spla_Status spla_Array_get_float(spla_Array a, spla_uint i, float* value);
SPLA_API spla_Status spla_Array_resize(spla_Array a, spla_uint n);
SPLA_API spla_Status spla_Array_build(spla_Array a, spla_MemView view);
SPLA_API spla_Status spla_Array_read(spla_Array a, spla_MemView* view);
SPLA_API spla_Status spla_Array_clear(spla_Array a);

//////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -240,8 +261,8 @@ SPLA_API spla_Status spla_Vector_set_float(spla_Vector v, spla_uint row_id, floa
SPLA_API spla_Status spla_Vector_get_int(spla_Vector v, spla_uint row_id, int* value);
SPLA_API spla_Status spla_Vector_get_uint(spla_Vector v, spla_uint row_id, unsigned int* value);
SPLA_API spla_Status spla_Vector_get_float(spla_Vector v, spla_uint row_id, float* value);
SPLA_API spla_Status spla_Vector_build(spla_Vector v, spla_Array keys, spla_Array values);
SPLA_API spla_Status spla_Vector_read(spla_Vector v, spla_Array keys, spla_Array values);
SPLA_API spla_Status spla_Vector_build(spla_Vector v, spla_MemView keys, spla_MemView values);
SPLA_API spla_Status spla_Vector_read(spla_Vector v, spla_MemView* keys, spla_MemView* values);
SPLA_API spla_Status spla_Vector_clear(spla_Vector v);

//////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -257,8 +278,8 @@ SPLA_API spla_Status spla_Matrix_set_float(spla_Matrix M, spla_uint row_id, spla
SPLA_API spla_Status spla_Matrix_get_int(spla_Matrix M, spla_uint row_id, spla_uint col_id, int* value);
SPLA_API spla_Status spla_Matrix_get_uint(spla_Matrix M, spla_uint row_id, spla_uint col_id, unsigned int* value);
SPLA_API spla_Status spla_Matrix_get_float(spla_Matrix M, spla_uint row_id, spla_uint col_id, float* value);
SPLA_API spla_Status spla_Matrix_build(spla_Matrix M, spla_Array keys1, spla_Array keys2, spla_Array values);
SPLA_API spla_Status spla_Matrix_read(spla_Matrix M, spla_Array keys1, spla_Array keys2, spla_Array values);
SPLA_API spla_Status spla_Matrix_build(spla_Matrix M, spla_MemView keys1, spla_MemView keys2, spla_MemView values);
SPLA_API spla_Status spla_Matrix_read(spla_Matrix M, spla_MemView* keys1, spla_MemView* keys2, spla_MemView* values);
SPLA_API spla_Status spla_Matrix_clear(spla_Matrix M);

//////////////////////////////////////////////////////////////////////////////////////
Expand Down
1 change: 1 addition & 0 deletions include/spla.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "spla/io.hpp"
#include "spla/library.hpp"
#include "spla/matrix.hpp"
#include "spla/memview.hpp"
#include "spla/object.hpp"
#include "spla/op.hpp"
#include "spla/ref.hpp"
Expand Down
25 changes: 14 additions & 11 deletions include/spla/array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#define SPLA_ARRAY_HPP

#include "config.hpp"
#include "memview.hpp"
#include "object.hpp"
#include "type.hpp"

Expand All @@ -48,17 +49,19 @@ namespace spla {
*/
class Array : public Object {
public:
SPLA_API ~Array() override = default;
SPLA_API virtual uint get_n_values() = 0;
SPLA_API virtual ref_ptr<Type> get_type() = 0;
SPLA_API virtual Status set_int(uint i, T_INT value) = 0;
SPLA_API virtual Status set_uint(uint i, T_UINT value) = 0;
SPLA_API virtual Status set_float(uint i, T_FLOAT value) = 0;
SPLA_API virtual Status get_int(uint i, T_INT& value) = 0;
SPLA_API virtual Status get_uint(uint i, T_UINT& value) = 0;
SPLA_API virtual Status get_float(uint i, T_FLOAT& value) = 0;
SPLA_API virtual Status resize(uint n_values) = 0;
SPLA_API virtual Status clear() = 0;
SPLA_API ~Array() override = default;
SPLA_API virtual uint get_n_values() = 0;
SPLA_API virtual ref_ptr<Type> get_type() = 0;
SPLA_API virtual Status set_int(uint i, T_INT value) = 0;
SPLA_API virtual Status set_uint(uint i, T_UINT value) = 0;
SPLA_API virtual Status set_float(uint i, T_FLOAT value) = 0;
SPLA_API virtual Status get_int(uint i, T_INT& value) = 0;
SPLA_API virtual Status get_uint(uint i, T_UINT& value) = 0;
SPLA_API virtual Status get_float(uint i, T_FLOAT& value) = 0;
SPLA_API virtual Status resize(uint n_values) = 0;
SPLA_API virtual Status build(const ref_ptr<MemView>& view) = 0;
SPLA_API virtual Status read(ref_ptr<MemView>& view) = 0;
SPLA_API virtual Status clear() = 0;

SPLA_API static ref_ptr<Array> make(uint n_values, const ref_ptr<Type>& type);
};
Expand Down
32 changes: 16 additions & 16 deletions include/spla/matrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,22 @@ namespace spla {
*/
class Matrix : public Object {
public:
SPLA_API ~Matrix() override = default;
SPLA_API virtual uint get_n_rows() = 0;
SPLA_API virtual uint get_n_cols() = 0;
SPLA_API virtual ref_ptr<Type> get_type() = 0;
SPLA_API virtual Status set_format(FormatMatrix format) = 0;
SPLA_API virtual Status set_fill_value(const ref_ptr<Scalar>& value) = 0;
SPLA_API virtual Status set_reduce(ref_ptr<OpBinary> resolve_duplicates) = 0;
SPLA_API virtual Status set_int(uint row_id, uint col_id, std::int32_t value) = 0;
SPLA_API virtual Status set_uint(uint row_id, uint col_id, std::uint32_t value) = 0;
SPLA_API virtual Status set_float(uint row_id, uint col_id, float value) = 0;
SPLA_API virtual Status get_int(uint row_id, uint col_id, std::int32_t& value) = 0;
SPLA_API virtual Status get_uint(uint row_id, uint col_id, std::uint32_t& value) = 0;
SPLA_API virtual Status get_float(uint row_id, uint col_id, float& value) = 0;
SPLA_API virtual Status build(const ref_ptr<Array>& keys1, const ref_ptr<Array>& keys2, const ref_ptr<Array>& values) = 0;
SPLA_API virtual Status read(const ref_ptr<Array>& keys1, const ref_ptr<Array>& keys2, const ref_ptr<Array>& values) = 0;
SPLA_API virtual Status clear() = 0;
SPLA_API ~Matrix() override = default;
SPLA_API virtual uint get_n_rows() = 0;
SPLA_API virtual uint get_n_cols() = 0;
SPLA_API virtual ref_ptr<Type> get_type() = 0;
SPLA_API virtual Status set_format(FormatMatrix format) = 0;
SPLA_API virtual Status set_fill_value(const ref_ptr<Scalar>& value) = 0;
SPLA_API virtual Status set_reduce(ref_ptr<OpBinary> resolve_duplicates) = 0;
SPLA_API virtual Status set_int(uint row_id, uint col_id, std::int32_t value) = 0;
SPLA_API virtual Status set_uint(uint row_id, uint col_id, std::uint32_t value) = 0;
SPLA_API virtual Status set_float(uint row_id, uint col_id, float value) = 0;
SPLA_API virtual Status get_int(uint row_id, uint col_id, std::int32_t& value) = 0;
SPLA_API virtual Status get_uint(uint row_id, uint col_id, std::uint32_t& value) = 0;
SPLA_API virtual Status get_float(uint row_id, uint col_id, float& value) = 0;
SPLA_API virtual Status build(const ref_ptr<MemView>& keys1, const ref_ptr<MemView>& keys2, const ref_ptr<MemView>& values) = 0;
SPLA_API virtual Status read(ref_ptr<MemView>& keys1, ref_ptr<MemView>& keys2, ref_ptr<MemView>& values) = 0;
SPLA_API virtual Status clear() = 0;

/**
* @brief Make new matrix instance with specified dim and values type
Expand Down
68 changes: 68 additions & 0 deletions include/spla/memview.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**********************************************************************************/
/* This file is part of spla project */
/* https://github.com/JetBrains-Research/spla */
/**********************************************************************************/
/* MIT License */
/* */
/* Copyright (c) 2023 SparseLinearAlgebra */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining a copy */
/* of this software and associated documentation files (the "Software"), to deal */
/* in the Software without restriction, including without limitation the rights */
/* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell */
/* copies of the Software, and to permit persons to whom the Software is */
/* furnished to do so, subject to the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be included in all */
/* copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */
/* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */
/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */
/* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */
/* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
/* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */
/* SOFTWARE. */
/**********************************************************************************/

#ifndef SPLA_MEMVIEW_HPP
#define SPLA_MEMVIEW_HPP

#include "config.hpp"
#include "ref.hpp"

namespace spla {

/**
* @class MemView
* @brief View to some memory resource without life-time control
*
* Memory view allows to inspect raw structure of a particular buffer without
* explicit control over life time of without costly copy operation to
* move data from one place into another. Use views to access data and then to
* copy values or fill values from your own data source.
*
* @note Memory view does not control type and layout of data. Thus, be
* cautions when modifying data through the view.
*/
class MemView final : public RefCnt {
public:
SPLA_API ~MemView() override = default;
SPLA_API Status read(std::size_t offset, std::size_t size, void* dst);
SPLA_API Status write(std::size_t offset, std::size_t size, const void* src);
SPLA_API void* get_buffer() const;
SPLA_API std::size_t get_size() const;
SPLA_API bool is_mutable() const;

SPLA_API static ref_ptr<MemView> make(void* buffer, std::size_t size, bool is_mutable = false);
SPLA_API static ref_ptr<MemView> make();

private:
void* m_buffer = nullptr;
std::size_t m_size = 0;
bool m_is_mutable = false;
};

}// namespace spla

#endif//SPLA_MEMVIEW_HPP
34 changes: 17 additions & 17 deletions include/spla/vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,23 @@ namespace spla {
*/
class Vector : public Object {
public:
SPLA_API ~Vector() override = default;
SPLA_API virtual uint get_n_rows() = 0;
SPLA_API virtual ref_ptr<Type> get_type() = 0;
SPLA_API virtual Status set_format(FormatVector format) = 0;
SPLA_API virtual Status set_fill_value(const ref_ptr<Scalar>& value) = 0;
SPLA_API virtual Status set_reduce(ref_ptr<OpBinary> resolve_duplicates) = 0;
SPLA_API virtual Status set_int(uint row_id, T_INT value) = 0;
SPLA_API virtual Status set_uint(uint row_id, T_UINT value) = 0;
SPLA_API virtual Status set_float(uint row_id, T_FLOAT value) = 0;
SPLA_API virtual Status get_int(uint row_id, T_INT& value) = 0;
SPLA_API virtual Status get_uint(uint row_id, T_UINT& value) = 0;
SPLA_API virtual Status get_float(uint row_id, float& value) = 0;
SPLA_API virtual Status fill_noize(uint seed) = 0;
SPLA_API virtual Status fill_with(const ref_ptr<Scalar>& value) = 0;
SPLA_API virtual Status build(const ref_ptr<Array>& keys, const ref_ptr<Array>& values) = 0;
SPLA_API virtual Status read(const ref_ptr<Array>& keys, const ref_ptr<Array>& values) = 0;
SPLA_API virtual Status clear() = 0;
SPLA_API ~Vector() override = default;
SPLA_API virtual uint get_n_rows() = 0;
SPLA_API virtual ref_ptr<Type> get_type() = 0;
SPLA_API virtual Status set_format(FormatVector format) = 0;
SPLA_API virtual Status set_fill_value(const ref_ptr<Scalar>& value) = 0;
SPLA_API virtual Status set_reduce(ref_ptr<OpBinary> resolve_duplicates) = 0;
SPLA_API virtual Status set_int(uint row_id, T_INT value) = 0;
SPLA_API virtual Status set_uint(uint row_id, T_UINT value) = 0;
SPLA_API virtual Status set_float(uint row_id, T_FLOAT value) = 0;
SPLA_API virtual Status get_int(uint row_id, T_INT& value) = 0;
SPLA_API virtual Status get_uint(uint row_id, T_UINT& value) = 0;
SPLA_API virtual Status get_float(uint row_id, float& value) = 0;
SPLA_API virtual Status fill_noize(uint seed) = 0;
SPLA_API virtual Status fill_with(const ref_ptr<Scalar>& value) = 0;
SPLA_API virtual Status build(const ref_ptr<MemView>& keys, const ref_ptr<MemView>& values) = 0;
SPLA_API virtual Status read(ref_ptr<MemView>& keys, ref_ptr<MemView>& values) = 0;
SPLA_API virtual Status clear() = 0;

/**
* @brief Make new vector instance with specified dim and values type
Expand Down
6 changes: 5 additions & 1 deletion python/example.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import pyspla

print(pyspla.Array.generate(dtype=pyspla.FLOAT, shape=3, dist=[100, 300]).to_list())
a = pyspla.Array.from_list(dtype=pyspla.INT, values=[-1, 0, 1])
b = pyspla.Array.generate(dtype=pyspla.INT, shape=10, dist=(-10, 10))

print(a)
print(b)
Loading

0 comments on commit 5894e9b

Please sign in to comment.