From 4caac2ac9c9987b23e046a209e706d0f89581b11 Mon Sep 17 00:00:00 2001 From: Egor Orachev Date: Sun, 27 Aug 2023 16:02:15 +0300 Subject: [PATCH] gh-217: impl C bindings --- CMakeLists.txt | 8 +++ include/spla.h | 128 +++++++++++++++++++++++++++++++++++- include/spla/array.hpp | 1 + include/spla/matrix.hpp | 2 +- include/spla/vector.hpp | 2 +- python/pyspla/bridge.py | 54 +++++++-------- src/binding/c_algorithm.cpp | 51 ++++++++++++++ src/binding/c_array.cpp | 55 ++++++++++++++++ src/binding/c_config.hpp | 25 ++++--- src/binding/c_library.cpp | 29 +++++--- src/binding/c_matrix.cpp | 67 +++++++++++++++++++ src/binding/c_object.cpp | 38 +++++++++++ src/binding/c_op.cpp | 90 +++++++++++++++++++++++++ src/binding/c_scalar.cpp | 52 +++++++++++++++ src/binding/c_type.cpp | 38 +++++++++++ src/binding/c_vector.cpp | 67 +++++++++++++++++++ src/core/tarray.hpp | 6 ++ src/core/tmatrix.hpp | 4 +- src/core/tvector.hpp | 4 +- 19 files changed, 669 insertions(+), 52 deletions(-) create mode 100644 src/binding/c_algorithm.cpp create mode 100644 src/binding/c_array.cpp create mode 100644 src/binding/c_matrix.cpp create mode 100644 src/binding/c_object.cpp create mode 100644 src/binding/c_op.cpp create mode 100644 src/binding/c_scalar.cpp create mode 100644 src/binding/c_type.cpp create mode 100644 src/binding/c_vector.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 978b0c7ec..dfefb00db 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -348,8 +348,16 @@ add_library(spla SHARED src/vector.cpp # C bindings include/spla.h + src/binding/c_algorithm.cpp + src/binding/c_array.cpp src/binding/c_config.hpp src/binding/c_library.cpp + src/binding/c_matrix.cpp + src/binding/c_object.cpp + src/binding/c_op.cpp + src/binding/c_scalar.cpp + src/binding/c_type.cpp + src/binding/c_vector.cpp # C++ optional part ${SRC_OPENCL}) diff --git a/include/spla.h b/include/spla.h index 5a5f4c241..28f32b198 100644 --- a/include/spla.h +++ b/include/spla.h @@ -30,8 +30,12 @@ /** * @file spla.h + * @author Egor Orachev * - * @brief Spla library C++ API bindings for C language + * @brief Spla library C API bindings + * + * @note Bindings primary intended for exporting to other programming languages, + * such as Python, etc. For a manual usage prefer C++ library API declared in spla.hpp file. * * @see Source code: https://github.com/SparseLinearAlgebra/spla * @see Python Reference API: https://SparseLinearAlgebra.github.io/spla/docs-python/spla @@ -58,6 +62,10 @@ extern "C" { #endif +////////////////////////////////////////////////////////////////////////////////////// + +/* General definitions */ + typedef enum spla_Status { SPLA_STATUS_OK = 0, SPLA_STATUS_ERROR = 1, @@ -75,7 +83,7 @@ typedef enum spla_AcceleratorType { SPLA_ACCELERATOR_TYPE_OPENCL = 1 } spla_AcceleratorType; -#define SPLA_NULL_HND NULL +#define SPLA_NULL NULL typedef uint32_t spla_uint; @@ -94,6 +102,10 @@ typedef struct spla_OpSelect_t* spla_OpSelect; typedef void(spla_MessageCallback)(spla_Status, const char* message, const char* file, const char* function, int line, void* p_user_data); +////////////////////////////////////////////////////////////////////////////////////// + +/* Library configuration and accessors */ + SPLA_API void spla_Library_finalize(); SPLA_API spla_Status spla_Library_set_accelerator(spla_AcceleratorType accelerator); SPLA_API spla_Status spla_Library_set_platform(int index); @@ -101,14 +113,97 @@ SPLA_API spla_Status spla_Library_set_device(int index); SPLA_API spla_Status spla_Library_set_queues_count(int count); SPLA_API spla_Status spla_Library_set_message_callback(spla_MessageCallback callback, void* p_user_data); SPLA_API spla_Status spla_Library_set_default_callback(); +SPLA_API spla_Status spla_Library_get_accelerator_info(char* buffer, int length); + +////////////////////////////////////////////////////////////////////////////////////// + +/* Built-in predefined scalar values types for storage parametrization */ SPLA_API spla_Type spla_Type_int(); SPLA_API spla_Type spla_Type_uint(); SPLA_API spla_Type spla_Type_float(); +////////////////////////////////////////////////////////////////////////////////////// + +/* Built-in binary element-wise operations */ + +SPLA_API spla_OpBinary spla_OpBinary_PLUS_INT(); +SPLA_API spla_OpBinary spla_OpBinary_PLUS_UINT(); +SPLA_API spla_OpBinary spla_OpBinary_PLUS_FLOAT(); +SPLA_API spla_OpBinary spla_OpBinary_MINUS_INT(); +SPLA_API spla_OpBinary spla_OpBinary_MINUS_UINT(); +SPLA_API spla_OpBinary spla_OpBinary_MINUS_FLOAT(); +SPLA_API spla_OpBinary spla_OpBinary_MULT_INT(); +SPLA_API spla_OpBinary spla_OpBinary_MULT_UINT(); +SPLA_API spla_OpBinary spla_OpBinary_MULT_FLOAT(); +SPLA_API spla_OpBinary spla_OpBinary_DIV_INT(); +SPLA_API spla_OpBinary spla_OpBinary_DIV_UINT(); +SPLA_API spla_OpBinary spla_OpBinary_DIV_FLOAT(); +SPLA_API spla_OpBinary spla_OpBinary_MINUS_POW2_INT(); +SPLA_API spla_OpBinary spla_OpBinary_MINUS_POW2_UINT(); +SPLA_API spla_OpBinary spla_OpBinary_MINUS_POW2_FLOAT(); +SPLA_API spla_OpBinary spla_OpBinary_FIRST_INT(); +SPLA_API spla_OpBinary spla_OpBinary_FIRST_UINT(); +SPLA_API spla_OpBinary spla_OpBinary_FIRST_FLOAT(); +SPLA_API spla_OpBinary spla_OpBinary_SECOND_INT(); +SPLA_API spla_OpBinary spla_OpBinary_SECOND_UINT(); +SPLA_API spla_OpBinary spla_OpBinary_SECOND_FLOAT(); +SPLA_API spla_OpBinary spla_OpBinary_ONE_INT(); +SPLA_API spla_OpBinary spla_OpBinary_ONE_UINT(); +SPLA_API spla_OpBinary spla_OpBinary_ONE_FLOAT(); +SPLA_API spla_OpBinary spla_OpBinary_MIN_INT(); +SPLA_API spla_OpBinary spla_OpBinary_MIN_UINT(); +SPLA_API spla_OpBinary spla_OpBinary_MIN_FLOAT(); +SPLA_API spla_OpBinary spla_OpBinary_MAX_INT(); +SPLA_API spla_OpBinary spla_OpBinary_MAX_UINT(); +SPLA_API spla_OpBinary spla_OpBinary_MAX_FLOAT(); +SPLA_API spla_OpBinary spla_OpBinary_BOR_INT(); +SPLA_API spla_OpBinary spla_OpBinary_BOR_UINT(); +SPLA_API spla_OpBinary spla_OpBinary_BAND_INT(); +SPLA_API spla_OpBinary spla_OpBinary_BAND_UINT(); +SPLA_API spla_OpBinary spla_OpBinary_BXOR_INT(); +SPLA_API spla_OpBinary spla_OpBinary_BXOR_UINT(); + +////////////////////////////////////////////////////////////////////////////////////// + +/* Built-in selection operations */ + +SPLA_API spla_OpSelect spla_OpSelect_EQZERO_INT(); +SPLA_API spla_OpSelect spla_OpSelect_EQZERO_UINT(); +SPLA_API spla_OpSelect spla_OpSelect_EQZERO_FLOAT(); +SPLA_API spla_OpSelect spla_OpSelect_NQZERO_INT(); +SPLA_API spla_OpSelect spla_OpSelect_NQZERO_UINT(); +SPLA_API spla_OpSelect spla_OpSelect_NQZERO_FLOAT(); +SPLA_API spla_OpSelect spla_OpSelect_GTZERO_INT(); +SPLA_API spla_OpSelect spla_OpSelect_GTZERO_UINT(); +SPLA_API spla_OpSelect spla_OpSelect_GTZERO_FLOAT(); +SPLA_API spla_OpSelect spla_OpSelect_GEZERO_INT(); +SPLA_API spla_OpSelect spla_OpSelect_GEZERO_UINT(); +SPLA_API spla_OpSelect spla_OpSelect_GEZERO_FLOAT(); +SPLA_API spla_OpSelect spla_OpSelect_LTZERO_INT(); +SPLA_API spla_OpSelect spla_OpSelect_LTZERO_UINT(); +SPLA_API spla_OpSelect spla_OpSelect_LTZERO_FLOAT(); +SPLA_API spla_OpSelect spla_OpSelect_LEZERO_INT(); +SPLA_API spla_OpSelect spla_OpSelect_LEZERO_UINT(); +SPLA_API spla_OpSelect spla_OpSelect_LEZERO_FLOAT(); +SPLA_API spla_OpSelect spla_OpSelect_ALWAYS_INT(); +SPLA_API spla_OpSelect spla_OpSelect_ALWAYS_UINT(); +SPLA_API spla_OpSelect spla_OpSelect_ALWAYS_FLOAT(); +SPLA_API spla_OpSelect spla_OpSelect_NEVER_INT(); +SPLA_API spla_OpSelect spla_OpSelect_NEVER_UINT(); +SPLA_API spla_OpSelect spla_OpSelect_NEVER_FLOAT(); + +////////////////////////////////////////////////////////////////////////////////////// + +/* 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); +////////////////////////////////////////////////////////////////////////////////////// + +/* Scala container creation and manipulation */ + SPLA_API spla_Status spla_Scalar_make(spla_Scalar* scalar, spla_Type type); SPLA_API spla_Status spla_Scalar_set_int(spla_Scalar s, int value); SPLA_API spla_Status spla_Scalar_set_uint(spla_Scalar s, unsigned int value); @@ -117,6 +212,10 @@ SPLA_API spla_Status spla_Scalar_get_int(spla_Scalar s, int* value); SPLA_API spla_Status spla_Scalar_get_uint(spla_Scalar s, unsigned int* value); SPLA_API spla_Status spla_Scalar_get_float(spla_Scalar s, float* value); +////////////////////////////////////////////////////////////////////////////////////// + +/* Array container creation and manipulation */ + SPLA_API spla_Status spla_Array_make(spla_Array* v, spla_uint n_values, spla_Type type); SPLA_API spla_Status spla_Array_set_int(spla_Array a, spla_uint i, int value); SPLA_API spla_Status spla_Array_set_uint(spla_Array a, spla_uint i, unsigned int value); @@ -126,6 +225,10 @@ SPLA_API spla_Status spla_Array_get_uint(spla_Array a, spla_uint i, unsigned int SPLA_API spla_Status spla_Array_get_float(spla_Array a, spla_uint i, float* value); SPLA_API spla_Status spla_Array_clear(spla_Array a); +////////////////////////////////////////////////////////////////////////////////////// + +/* Vector container creation and manipulation */ + SPLA_API spla_Status spla_Vector_make(spla_Vector* v, spla_uint n_rows, spla_Type type); SPLA_API spla_Status spla_Vector_set_fill_value(spla_Vector v, spla_Scalar value); SPLA_API spla_Status spla_Vector_set_reduce(spla_Vector v, spla_OpBinary reduce); @@ -135,9 +238,15 @@ 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_clear(spla_Vector v); -SPLA_API spla_Status spla_Matrix_make(spla_Matrix* M, spla_uint n_rows, spla_Type type); +////////////////////////////////////////////////////////////////////////////////////// + +/* Matrix container creation and manipulation */ + +SPLA_API spla_Status spla_Matrix_make(spla_Matrix* M, spla_uint n_rows, spla_uint n_cols, spla_Type type); SPLA_API spla_Status spla_Matrix_set_fill_value(spla_Matrix M, spla_Scalar value); SPLA_API spla_Status spla_Matrix_set_reduce(spla_Matrix M, spla_OpBinary reduce); SPLA_API spla_Status spla_Matrix_set_int(spla_Matrix M, spla_uint row_id, spla_uint col_id, int value); @@ -146,8 +255,21 @@ 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_clear(spla_Matrix M); +////////////////////////////////////////////////////////////////////////////////////// + +/* Implemented some common graph algorithms using spla library */ + +SPLA_API spla_Status spla_Algorithm_bfs(spla_Vector v, spla_Matrix A, spla_uint s, spla_Descriptor descriptor); +SPLA_API spla_Status spla_Algorithm_sssp(spla_Vector v, spla_Matrix A, spla_uint s, spla_Descriptor descriptor); +SPLA_API spla_Status spla_Algorithm_pr(spla_Vector* p, spla_Matrix A, float alpha, float eps, spla_Descriptor descriptor); +SPLA_API spla_Status spla_Algorithm_tc(int* ntrins, spla_Matrix A, spla_Matrix B, spla_Descriptor descriptor); + +////////////////////////////////////////////////////////////////////////////////////// + #if defined(__cplusplus) } #endif diff --git a/include/spla/array.hpp b/include/spla/array.hpp index d225c092d..e987dd9f8 100644 --- a/include/spla/array.hpp +++ b/include/spla/array.hpp @@ -58,6 +58,7 @@ namespace spla { 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 static ref_ptr make(uint n_values, const ref_ptr& type); }; diff --git a/include/spla/matrix.hpp b/include/spla/matrix.hpp index 8f6947aaf..a6617485e 100644 --- a/include/spla/matrix.hpp +++ b/include/spla/matrix.hpp @@ -61,7 +61,7 @@ namespace spla { 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& keys1, const ref_ptr& keys2, const ref_ptr& values) = 0; - SPLA_API virtual Status read(ref_ptr& keys1, ref_ptr& keys2, ref_ptr& values) = 0; + SPLA_API virtual Status read(const ref_ptr& keys1, const ref_ptr& keys2, const ref_ptr& values) = 0; SPLA_API virtual Status clear() = 0; /** diff --git a/include/spla/vector.hpp b/include/spla/vector.hpp index 90db5120d..d267257a9 100644 --- a/include/spla/vector.hpp +++ b/include/spla/vector.hpp @@ -62,7 +62,7 @@ namespace spla { SPLA_API virtual Status fill_noize(uint seed) = 0; SPLA_API virtual Status fill_with(const ref_ptr& value) = 0; SPLA_API virtual Status build(const ref_ptr& keys, const ref_ptr& values) = 0; - SPLA_API virtual Status read(ref_ptr& keys, ref_ptr& values) = 0; + SPLA_API virtual Status read(const ref_ptr& keys, const ref_ptr& values) = 0; SPLA_API virtual Status clear() = 0; /** diff --git a/python/pyspla/bridge.py b/python/pyspla/bridge.py index 4ea69d295..9b6cac637 100644 --- a/python/pyspla/bridge.py +++ b/python/pyspla/bridge.py @@ -42,8 +42,8 @@ TARGET_SUFFIX = {'macos': '.dylib', 'linux': '.so', 'windows': '.dll'}[SYSTEM] TARGET = {'macos': 'libspla', 'linux': 'libspla', 'windows': 'spla'}[SYSTEM] + "_" + ARCH + TARGET_SUFFIX -_spla_lib_path = None -_spla_lib = None +_spla_path = None +_spla = None _int = None _enum_t = None _status_t = None @@ -66,7 +66,7 @@ def load_library(lib_path): - global _spla_lib + global _spla global _int global _enum_t global _status_t @@ -74,7 +74,7 @@ def load_library(lib_path): global _p_object_t global _callback_t - _spla_lib = ctypes.cdll.LoadLibrary(str(lib_path)) + _spla = ctypes.cdll.LoadLibrary(str(lib_path)) _int = ctypes.c_int _enum_t = ctypes.c_uint _status_t = ctypes.c_uint @@ -88,26 +88,26 @@ def load_library(lib_path): ctypes.c_int, ctypes.c_void_p) - _spla_lib.spla_Library_finalize.restype = _status_t - _spla_lib.spla_Library_finalize.argtypes = [] + _spla.spla_Library_finalize.restype = _status_t + _spla.spla_Library_finalize.argtypes = [] - _spla_lib.spla_Library_set_accelerator.restype = _status_t - _spla_lib.spla_Library_set_accelerator.argtypes = [_enum_t] + _spla.spla_Library_set_accelerator.restype = _status_t + _spla.spla_Library_set_accelerator.argtypes = [_enum_t] - _spla_lib.spla_Library_set_platform.restype = _status_t - _spla_lib.spla_Library_set_platform.argtypes = [_int] + _spla.spla_Library_set_platform.restype = _status_t + _spla.spla_Library_set_platform.argtypes = [_int] - _spla_lib.spla_Library_set_device.restype = _status_t - _spla_lib.spla_Library_set_device.argtypes = [_int] + _spla.spla_Library_set_device.restype = _status_t + _spla.spla_Library_set_device.argtypes = [_int] - _spla_lib.spla_Library_set_queues_count.restype = _status_t - _spla_lib.spla_Library_set_queues_count.argtypes = [_int] + _spla.spla_Library_set_queues_count.restype = _status_t + _spla.spla_Library_set_queues_count.argtypes = [_int] - _spla_lib.spla_Library_set_message_callback.restype = _status_t - _spla_lib.spla_Library_set_message_callback.argtypes = [_callback_t, ctypes.c_void_p] + _spla.spla_Library_set_message_callback.restype = _status_t + _spla.spla_Library_set_message_callback.argtypes = [_callback_t, ctypes.c_void_p] - _spla_lib.spla_Library_set_default_callback.restype = _status_t - _spla_lib.spla_Library_set_default_callback.argtypes = [] + _spla.spla_Library_set_default_callback.restype = _status_t + _spla.spla_Library_set_default_callback.argtypes = [] def default_callback(status, msg, file, function, line, user_data): @@ -117,13 +117,13 @@ def default_callback(status, msg, file, function, line, user_data): def finalize(): - if _spla_lib: - _spla_lib.spla_Library_finalize() + if _spla: + _spla.spla_Library_finalize() def initialize(): - global _spla_lib - global _spla_lib_path + global _spla + global _spla_path global _callback_t global _default_callback @@ -134,26 +134,26 @@ def initialize(): except KeyError: pass - _spla_lib_path = pathlib.Path(__file__).resolve().parent / TARGET + _spla_path = pathlib.Path(__file__).resolve().parent / TARGET try: # Override library path from ENV variable (for debug & custom build) if os.environ["SPLA_PATH"]: - _spla_lib_path = pathlib.Path(os.environ["SPLA_PATH"]) + _spla_path = pathlib.Path(os.environ["SPLA_PATH"]) except KeyError: pass - if not _spla_lib_path.is_file(): + if not _spla_path.is_file(): # Validate file before loading raise Exception(f"no compiled spla file {TARGET} to load") - load_library(_spla_lib_path) + load_library(_spla_path) _default_callback = _callback_t(default_callback) try: # If debug enable in ENV, setup default callback for messages on init if int(os.environ["SPLA_DEBUG"]): - _spla_lib.spla_Library_set_message_callback( + _spla.spla_Library_set_message_callback( _default_callback, ctypes.c_void_p(0)) except KeyError: pass diff --git a/src/binding/c_algorithm.cpp b/src/binding/c_algorithm.cpp new file mode 100644 index 000000000..41fbe5c81 --- /dev/null +++ b/src/binding/c_algorithm.cpp @@ -0,0 +1,51 @@ +/**********************************************************************************/ +/* 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. */ +/**********************************************************************************/ + +#include "c_config.hpp" + +spla_Status spla_Algorithm_bfs(spla_Vector v, spla_Matrix A, spla_uint s, spla_Descriptor descriptor) { + return to_c_status(spla::bfs(as_ref(v), as_ref(A), s, as_ref(descriptor))); +} +spla_Status spla_Algorithm_sssp(spla_Vector v, spla_Matrix A, spla_uint s, spla_Descriptor descriptor) { + return to_c_status(spla::sssp(as_ref(v), as_ref(A), s, as_ref(descriptor))); +} +spla_Status spla_Algorithm_pr(spla_Vector* p, spla_Matrix A, float alpha, float eps, spla_Descriptor descriptor) { + spla::ref_ptr p_inout; + p_inout.reset(as_ptr(*p)); + + spla::Status status = spla::pr(p_inout, as_ref(A), alpha, eps, as_ref(descriptor)); + + if (status == spla::Status::Ok) { + *p = as_ptr(p_inout.release()); + return SPLA_STATUS_OK; + } + + return to_c_status(status); +} +spla_Status spla_Algorithm_tc(int* ntrins, spla_Matrix A, spla_Matrix B, spla_Descriptor descriptor) { + return to_c_status(spla::tc(*ntrins, as_ref(A), as_ref(B), as_ref(descriptor))); +} \ No newline at end of file diff --git a/src/binding/c_array.cpp b/src/binding/c_array.cpp new file mode 100644 index 000000000..960cae882 --- /dev/null +++ b/src/binding/c_array.cpp @@ -0,0 +1,55 @@ +/**********************************************************************************/ +/* 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. */ +/**********************************************************************************/ + +#include "c_config.hpp" + +spla_Status spla_Array_make(spla_Array* v, spla_uint n_values, spla_Type type) { + auto array = spla::Array::make(n_values, as_ref(type)); + *v = as_ptr(array.release()); + return SPLA_STATUS_OK; +} +spla_Status spla_Array_set_int(spla_Array a, spla_uint i, int value) { + return to_c_status(as_ptr(a)->set_int(i, value)); +} +spla_Status spla_Array_set_uint(spla_Array a, spla_uint i, unsigned int value) { + return to_c_status(as_ptr(a)->set_uint(i, value)); +} +spla_Status spla_Array_set_float(spla_Array a, spla_uint i, float value) { + return to_c_status(as_ptr(a)->set_float(i, value)); +} +spla_Status spla_Array_get_int(spla_Array a, spla_uint i, int* value) { + return to_c_status(as_ptr(a)->get_int(i, *value)); +} +spla_Status spla_Array_get_uint(spla_Array a, spla_uint i, unsigned int* value) { + return to_c_status(as_ptr(a)->get_uint(i, *value)); +} +spla_Status spla_Array_get_float(spla_Array a, spla_uint i, float* value) { + return to_c_status(as_ptr(a)->get_float(i, *value)); +} +spla_Status spla_Array_clear(spla_Array a) { + return to_c_status(as_ptr(a)->clear()); +} \ No newline at end of file diff --git a/src/binding/c_config.hpp b/src/binding/c_config.hpp index f2496156c..4e1302570 100644 --- a/src/binding/c_config.hpp +++ b/src/binding/c_config.hpp @@ -31,16 +31,25 @@ #include #include -namespace spla { +#include +#include - static spla_Status to_c_status(Status status) { - return static_cast(status); - } +template +static T* as_ptr(S* s) { + return (T*) s; +} - static AcceleratorType from_c_accelerator_type(spla_AcceleratorType accelerator) { - return static_cast(accelerator); - } +template +static spla::ref_ptr as_ref(S* s) { + return spla::ref_ptr((T*) s); +} -}// namespace spla +static spla_Status to_c_status(spla::Status status) { + return static_cast(status); +} + +static spla::AcceleratorType from_c_accelerator_type(spla_AcceleratorType accelerator) { + return static_cast(accelerator); +} #endif//SPLA_C_CONFIG_HPP diff --git a/src/binding/c_library.cpp b/src/binding/c_library.cpp index 6ef033ef6..74123d30e 100644 --- a/src/binding/c_library.cpp +++ b/src/binding/c_library.cpp @@ -32,19 +32,19 @@ void spla_Library_finalize() { } spla_Status spla_Library_set_accelerator(spla_AcceleratorType accelerator) { - return spla::to_c_status(spla::Library::get()->set_accelerator(spla::from_c_accelerator_type(accelerator))); + return to_c_status(spla::Library::get()->set_accelerator(from_c_accelerator_type(accelerator))); } spla_Status spla_Library_set_platform(int index) { - return spla::to_c_status(spla::Library::get()->set_platform(index)); + return to_c_status(spla::Library::get()->set_platform(index)); } spla_Status spla_Library_set_device(int index) { - return spla::to_c_status(spla::Library::get()->set_device(index)); + return to_c_status(spla::Library::get()->set_device(index)); } spla_Status spla_Library_set_queues_count(int count) { - return spla::to_c_status(spla::Library::get()->set_queues_count(count)); + return to_c_status(spla::Library::get()->set_queues_count(count)); } spla_Status spla_Library_set_message_callback(spla_MessageCallback callback, void* p_user_data) { @@ -53,16 +53,29 @@ spla_Status spla_Library_set_message_callback(spla_MessageCallback callback, voi const std::string& file, const std::string& function, int line) { - callback(spla::to_c_status(status), + callback(to_c_status(status), msg.c_str(), file.c_str(), function.c_str(), line, p_user_data); }; - return spla::to_c_status(spla::Library::get()->set_message_callback(wrapped_callback)); + return to_c_status(spla::Library::get()->set_message_callback(wrapped_callback)); } spla_Status spla_Library_set_default_callback() { - return spla::to_c_status(spla::Library::get()->set_default_callback()); -} \ No newline at end of file + return to_c_status(spla::Library::get()->set_default_callback()); +} + +spla_Status spla_Library_get_accelerator_info(char* buffer, int length) { + std::string info; + + auto status = spla::Library::get()->get_accelerator_info(info); + + if (length > 0) { + std::memcpy(buffer, info.c_str(), std::min(length, int(info.length()))); + buffer[length - 1] = '\0'; + } + + return to_c_status(status); +} diff --git a/src/binding/c_matrix.cpp b/src/binding/c_matrix.cpp new file mode 100644 index 000000000..4df2c3beb --- /dev/null +++ b/src/binding/c_matrix.cpp @@ -0,0 +1,67 @@ +/**********************************************************************************/ +/* 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. */ +/**********************************************************************************/ + +#include "c_config.hpp" + +spla_Status spla_Matrix_make(spla_Matrix* M, spla_uint n_rows, spla_uint n_cols, spla_Type type) { + auto matrix = spla::Matrix::make(n_rows, n_cols, as_ref(type)); + *M = as_ptr(matrix.release()); + return SPLA_STATUS_OK; +} +spla_Status spla_Matrix_set_fill_value(spla_Matrix M, spla_Scalar value) { + return to_c_status(as_ptr(M)->set_fill_value(as_ref(value))); +} +spla_Status spla_Matrix_set_reduce(spla_Matrix M, spla_OpBinary reduce) { + return to_c_status(as_ptr(M)->set_reduce(as_ref(reduce))); +} +spla_Status spla_Matrix_set_int(spla_Matrix M, spla_uint row_id, spla_uint col_id, int value) { + return to_c_status(as_ptr(M)->set_int(row_id, col_id, value)); +} +spla_Status spla_Matrix_set_uint(spla_Matrix M, spla_uint row_id, spla_uint col_id, unsigned int value) { + return to_c_status(as_ptr(M)->set_uint(row_id, col_id, value)); +} +spla_Status spla_Matrix_set_float(spla_Matrix M, spla_uint row_id, spla_uint col_id, float value) { + return to_c_status(as_ptr(M)->set_float(row_id, col_id, value)); +} +spla_Status spla_Matrix_get_int(spla_Matrix M, spla_uint row_id, spla_uint col_id, int* value) { + return to_c_status(as_ptr(M)->get_int(row_id, col_id, *value)); +} +spla_Status spla_Matrix_get_uint(spla_Matrix M, spla_uint row_id, spla_uint col_id, unsigned int* value) { + return to_c_status(as_ptr(M)->get_uint(row_id, col_id, *value)); +} +spla_Status spla_Matrix_get_float(spla_Matrix M, spla_uint row_id, spla_uint col_id, float* value) { + return to_c_status(as_ptr(M)->get_float(row_id, col_id, *value)); +} +spla_Status spla_Matrix_build(spla_Matrix M, spla_Array keys1, spla_Array keys2, spla_Array values) { + return to_c_status(as_ptr(M)->build(as_ref(keys1), as_ref(keys2), as_ref(values))); +} +spla_Status spla_Matrix_read(spla_Matrix M, spla_Array keys1, spla_Array keys2, spla_Array values) { + return to_c_status(as_ptr(M)->read(as_ref(keys1), as_ref(keys2), as_ref(values))); +} +spla_Status spla_Matrix_clear(spla_Matrix M) { + return to_c_status(as_ptr(M)->clear()); +} \ No newline at end of file diff --git a/src/binding/c_object.cpp b/src/binding/c_object.cpp new file mode 100644 index 000000000..5f7351c45 --- /dev/null +++ b/src/binding/c_object.cpp @@ -0,0 +1,38 @@ +/**********************************************************************************/ +/* 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. */ +/**********************************************************************************/ + +#include "c_config.hpp" + +spla_Status spla_Object_ref(spla_Object object) { + spla::safe_ref(as_ptr(object)); + return SPLA_STATUS_OK; +} + +spla_Status spla_Object_unref(spla_Object object) { + spla::unref(as_ptr(object)); + return SPLA_STATUS_OK; +} \ No newline at end of file diff --git a/src/binding/c_op.cpp b/src/binding/c_op.cpp new file mode 100644 index 000000000..dcc5a0ff7 --- /dev/null +++ b/src/binding/c_op.cpp @@ -0,0 +1,90 @@ +/**********************************************************************************/ +/* 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. */ +/**********************************************************************************/ + +#include "c_config.hpp" + +spla_OpBinary spla_OpBinary_PLUS_INT() { return as_ptr(spla::PLUS_INT.get()); } +spla_OpBinary spla_OpBinary_PLUS_UINT() { return as_ptr(spla::PLUS_UINT.get()); } +spla_OpBinary spla_OpBinary_PLUS_FLOAT() { return as_ptr(spla::PLUS_FLOAT.get()); } +spla_OpBinary spla_OpBinary_MINUS_INT() { return as_ptr(spla::MINUS_INT.get()); } +spla_OpBinary spla_OpBinary_MINUS_UINT() { return as_ptr(spla::MINUS_UINT.get()); } +spla_OpBinary spla_OpBinary_MINUS_FLOAT() { return as_ptr(spla::MINUS_FLOAT.get()); } +spla_OpBinary spla_OpBinary_MULT_INT() { return as_ptr(spla::MULT_INT.get()); } +spla_OpBinary spla_OpBinary_MULT_UINT() { return as_ptr(spla::MULT_UINT.get()); } +spla_OpBinary spla_OpBinary_MULT_FLOAT() { return as_ptr(spla::MULT_FLOAT.get()); } +spla_OpBinary spla_OpBinary_DIV_INT() { return as_ptr(spla::DIV_INT.get()); } +spla_OpBinary spla_OpBinary_DIV_UINT() { return as_ptr(spla::DIV_UINT.get()); } +spla_OpBinary spla_OpBinary_DIV_FLOAT() { return as_ptr(spla::DIV_FLOAT.get()); } +spla_OpBinary spla_OpBinary_MINUS_POW2_INT() { return as_ptr(spla::MINUS_POW2_INT.get()); } +spla_OpBinary spla_OpBinary_MINUS_POW2_UINT() { return as_ptr(spla::MINUS_POW2_UINT.get()); } +spla_OpBinary spla_OpBinary_MINUS_POW2_FLOAT() { return as_ptr(spla::MINUS_POW2_FLOAT.get()); } +spla_OpBinary spla_OpBinary_FIRST_INT() { return as_ptr(spla::FIRST_INT.get()); } +spla_OpBinary spla_OpBinary_FIRST_UINT() { return as_ptr(spla::FIRST_UINT.get()); } +spla_OpBinary spla_OpBinary_FIRST_FLOAT() { return as_ptr(spla::FIRST_FLOAT.get()); } +spla_OpBinary spla_OpBinary_SECOND_INT() { return as_ptr(spla::SECOND_INT.get()); } +spla_OpBinary spla_OpBinary_SECOND_UINT() { return as_ptr(spla::SECOND_UINT.get()); } +spla_OpBinary spla_OpBinary_SECOND_FLOAT() { return as_ptr(spla::SECOND_FLOAT.get()); } +spla_OpBinary spla_OpBinary_ONE_INT() { return as_ptr(spla::ONE_INT.get()); } +spla_OpBinary spla_OpBinary_ONE_UINT() { return as_ptr(spla::ONE_UINT.get()); } +spla_OpBinary spla_OpBinary_ONE_FLOAT() { return as_ptr(spla::ONE_FLOAT.get()); } +spla_OpBinary spla_OpBinary_MIN_INT() { return as_ptr(spla::MIN_INT.get()); } +spla_OpBinary spla_OpBinary_MIN_UINT() { return as_ptr(spla::MIN_UINT.get()); } +spla_OpBinary spla_OpBinary_MIN_FLOAT() { return as_ptr(spla::MIN_FLOAT.get()); } +spla_OpBinary spla_OpBinary_MAX_INT() { return as_ptr(spla::MAX_INT.get()); } +spla_OpBinary spla_OpBinary_MAX_UINT() { return as_ptr(spla::MAX_UINT.get()); } +spla_OpBinary spla_OpBinary_MAX_FLOAT() { return as_ptr(spla::MAX_FLOAT.get()); } +spla_OpBinary spla_OpBinary_BOR_INT() { return as_ptr(spla::BOR_INT.get()); } +spla_OpBinary spla_OpBinary_BOR_UINT() { return as_ptr(spla::BOR_UINT.get()); } +spla_OpBinary spla_OpBinary_BAND_INT() { return as_ptr(spla::BAND_INT.get()); } +spla_OpBinary spla_OpBinary_BAND_UINT() { return as_ptr(spla::BAND_UINT.get()); } +spla_OpBinary spla_OpBinary_BXOR_INT() { return as_ptr(spla::BXOR_INT.get()); } +spla_OpBinary spla_OpBinary_BXOR_UINT() { return as_ptr(spla::BXOR_UINT.get()); } + +spla_OpSelect spla_OpSelect_EQZERO_INT() { return as_ptr(spla::EQZERO_INT.get()); } +spla_OpSelect spla_OpSelect_EQZERO_UINT() { return as_ptr(spla::EQZERO_UINT.get()); } +spla_OpSelect spla_OpSelect_EQZERO_FLOAT() { return as_ptr(spla::EQZERO_FLOAT.get()); } +spla_OpSelect spla_OpSelect_NQZERO_INT() { return as_ptr(spla::NQZERO_INT.get()); } +spla_OpSelect spla_OpSelect_NQZERO_UINT() { return as_ptr(spla::NQZERO_UINT.get()); } +spla_OpSelect spla_OpSelect_NQZERO_FLOAT() { return as_ptr(spla::NQZERO_FLOAT.get()); } +spla_OpSelect spla_OpSelect_GTZERO_INT() { return as_ptr(spla::GTZERO_INT.get()); } +spla_OpSelect spla_OpSelect_GTZERO_UINT() { return as_ptr(spla::GTZERO_UINT.get()); } +spla_OpSelect spla_OpSelect_GTZERO_FLOAT() { return as_ptr(spla::GTZERO_FLOAT.get()); } +spla_OpSelect spla_OpSelect_GEZERO_INT() { return as_ptr(spla::GEZERO_INT.get()); } +spla_OpSelect spla_OpSelect_GEZERO_UINT() { return as_ptr(spla::GEZERO_UINT.get()); } +spla_OpSelect spla_OpSelect_GEZERO_FLOAT() { return as_ptr(spla::GEZERO_FLOAT.get()); } +spla_OpSelect spla_OpSelect_LTZERO_INT() { return as_ptr(spla::LTZERO_INT.get()); } +spla_OpSelect spla_OpSelect_LTZERO_UINT() { return as_ptr(spla::LTZERO_UINT.get()); } +spla_OpSelect spla_OpSelect_LTZERO_FLOAT() { return as_ptr(spla::LTZERO_FLOAT.get()); } +spla_OpSelect spla_OpSelect_LEZERO_INT() { return as_ptr(spla::LEZERO_INT.get()); } +spla_OpSelect spla_OpSelect_LEZERO_UINT() { return as_ptr(spla::LEZERO_UINT.get()); } +spla_OpSelect spla_OpSelect_LEZERO_FLOAT() { return as_ptr(spla::LEZERO_FLOAT.get()); } +spla_OpSelect spla_OpSelect_ALWAYS_INT() { return as_ptr(spla::ALWAYS_INT.get()); } +spla_OpSelect spla_OpSelect_ALWAYS_UINT() { return as_ptr(spla::ALWAYS_UINT.get()); } +spla_OpSelect spla_OpSelect_ALWAYS_FLOAT() { return as_ptr(spla::ALWAYS_FLOAT.get()); } +spla_OpSelect spla_OpSelect_NEVER_INT() { return as_ptr(spla::NEVER_INT.get()); } +spla_OpSelect spla_OpSelect_NEVER_UINT() { return as_ptr(spla::NEVER_UINT.get()); } +spla_OpSelect spla_OpSelect_NEVER_FLOAT() { return as_ptr(spla::NEVER_FLOAT.get()); } \ No newline at end of file diff --git a/src/binding/c_scalar.cpp b/src/binding/c_scalar.cpp new file mode 100644 index 000000000..f43cb3291 --- /dev/null +++ b/src/binding/c_scalar.cpp @@ -0,0 +1,52 @@ +/**********************************************************************************/ +/* 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. */ +/**********************************************************************************/ + +#include "c_config.hpp" + +spla_Status spla_Scalar_make(spla_Scalar* s, spla_Type type) { + auto scalar = spla::Scalar::make(as_ref(type)); + *s = as_ptr(scalar.release()); + return SPLA_STATUS_OK; +} +spla_Status spla_Scalar_set_int(spla_Scalar s, int value) { + return to_c_status(as_ptr(s)->set_int(value)); +} +spla_Status spla_Scalar_set_uint(spla_Scalar s, unsigned int value) { + return to_c_status(as_ptr(s)->set_uint(value)); +} +spla_Status spla_Scalar_set_float(spla_Scalar s, float value) { + return to_c_status(as_ptr(s)->set_float(value)); +} +spla_Status spla_Scalar_get_int(spla_Scalar s, int* value) { + return to_c_status(as_ptr(s)->get_int(*value)); +} +spla_Status spla_Scalar_get_uint(spla_Scalar s, unsigned int* value) { + return to_c_status(as_ptr(s)->get_uint(*value)); +} +spla_Status spla_Scalar_get_float(spla_Scalar s, float* value) { + return to_c_status(as_ptr(s)->get_float(*value)); +} \ No newline at end of file diff --git a/src/binding/c_type.cpp b/src/binding/c_type.cpp new file mode 100644 index 000000000..1aaf134e6 --- /dev/null +++ b/src/binding/c_type.cpp @@ -0,0 +1,38 @@ +/**********************************************************************************/ +/* 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. */ +/**********************************************************************************/ + +#include "c_config.hpp" + +spla_Type spla_Type_int() { + return as_ptr(spla::INT.get()); +} +spla_Type spla_Type_uint() { + return as_ptr(spla::UINT.get()); +} +spla_Type spla_Type_float() { + return as_ptr(spla::FLOAT.get()); +} \ No newline at end of file diff --git a/src/binding/c_vector.cpp b/src/binding/c_vector.cpp new file mode 100644 index 000000000..8313740b7 --- /dev/null +++ b/src/binding/c_vector.cpp @@ -0,0 +1,67 @@ +/**********************************************************************************/ +/* 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. */ +/**********************************************************************************/ + +#include "c_config.hpp" + +spla_Status spla_Vector_make(spla_Vector* v, spla_uint n_rows, spla_Type type) { + auto vector = spla::Vector::make(n_rows, as_ref(type)); + *v = as_ptr(vector.release()); + return SPLA_STATUS_OK; +} +spla_Status spla_Vector_set_fill_value(spla_Vector v, spla_Scalar value) { + return to_c_status(as_ptr(v)->set_fill_value(as_ref(value))); +} +spla_Status spla_Vector_set_reduce(spla_Vector v, spla_OpBinary reduce) { + return to_c_status(as_ptr(v)->set_reduce(as_ref(reduce))); +} +spla_Status spla_Vector_set_int(spla_Vector v, spla_uint row_id, int value) { + return to_c_status(as_ptr(v)->set_int(row_id, value)); +} +spla_Status spla_Vector_set_uint(spla_Vector v, spla_uint row_id, unsigned int value) { + return to_c_status(as_ptr(v)->set_uint(row_id, value)); +} +spla_Status spla_Vector_set_float(spla_Vector v, spla_uint row_id, float value) { + return to_c_status(as_ptr(v)->set_float(row_id, value)); +} +spla_Status spla_Vector_get_int(spla_Vector v, spla_uint row_id, int* value) { + return to_c_status(as_ptr(v)->get_int(row_id, *value)); +} +spla_Status spla_Vector_get_uint(spla_Vector v, spla_uint row_id, unsigned int* value) { + return to_c_status(as_ptr(v)->get_uint(row_id, *value)); +} +spla_Status spla_Vector_get_float(spla_Vector v, spla_uint row_id, float* value) { + return to_c_status(as_ptr(v)->get_float(row_id, *value)); +} +spla_Status spla_Vector_build(spla_Vector v, spla_Array keys, spla_Array values) { + return to_c_status(as_ptr(v)->build(as_ref(keys), as_ref(values))); +} +spla_Status spla_Vector_read(spla_Vector v, spla_Array keys, spla_Array values) { + return to_c_status(as_ptr(v)->read(as_ref(keys), as_ref(values))); +} +spla_Status spla_Vector_clear(spla_Vector v) { + return to_c_status(as_ptr(v)->clear()); +} \ No newline at end of file diff --git a/src/core/tarray.hpp b/src/core/tarray.hpp index 086aa1508..4bfd10fab 100644 --- a/src/core/tarray.hpp +++ b/src/core/tarray.hpp @@ -66,6 +66,7 @@ namespace spla { Status get_uint(uint i, T_UINT& value) override; Status get_float(uint i, T_FLOAT& value) override; Status resize(uint n_values) override; + Status clear() override; void set_label(std::string label) override; const std::string& get_label() const override; @@ -134,6 +135,11 @@ namespace spla { m_data.resize(n_values); return Status::Ok; } + template + Status TArray::clear() { + m_data.clear(); + return Status::Ok; + } template void TArray::set_label(std::string label) { diff --git a/src/core/tmatrix.hpp b/src/core/tmatrix.hpp index 8dd2c0f3e..a8e7f8efe 100644 --- a/src/core/tmatrix.hpp +++ b/src/core/tmatrix.hpp @@ -74,7 +74,7 @@ namespace spla { Status get_uint(uint row_id, uint col_id, uint32_t& value) override; Status get_float(uint row_id, uint col_id, float& value) override; Status build(const ref_ptr& keys1, const ref_ptr& keys2, const ref_ptr& values) override; - Status read(ref_ptr& keys1, ref_ptr& keys2, ref_ptr& values) override; + Status read(const ref_ptr& keys1, const ref_ptr& keys2, const ref_ptr& values) override; Status clear() override; template @@ -253,7 +253,7 @@ namespace spla { return Status::Ok; } template - Status TMatrix::read(ref_ptr& keys1, ref_ptr& keys2, ref_ptr& values) { + Status TMatrix::read(const ref_ptr& keys1, const ref_ptr& keys2, const ref_ptr& values) { if (!keys1 && !keys2 && !values) { return Status::InvalidArgument; } diff --git a/src/core/tvector.hpp b/src/core/tvector.hpp index 4d06cb2ef..18f34f37d 100644 --- a/src/core/tvector.hpp +++ b/src/core/tvector.hpp @@ -78,7 +78,7 @@ namespace spla { Status fill_noize(uint seed) override; Status fill_with(const ref_ptr& value) override; Status build(const ref_ptr& keys, const ref_ptr& values) override; - Status read(ref_ptr& keys, ref_ptr& values) override; + Status read(const ref_ptr& keys, const ref_ptr& values) override; Status clear() override; template @@ -295,7 +295,7 @@ namespace spla { return Status::Ok; } template - Status TVector::read(ref_ptr& keys, ref_ptr& values) { + Status TVector::read(const ref_ptr& keys, const ref_ptr& values) { if (!keys && !values) { return Status::InvalidArgument; }