From 7d686aed08c03e06093197ec583ddf08a1978737 Mon Sep 17 00:00:00 2001 From: "George G. Vega Yon" Date: Fri, 15 Sep 2023 10:54:47 -0600 Subject: [PATCH] Verifying the data is preserved --- .vscode/c_cpp_properties.json | 16 +++++++++++---- pyproject.toml | 2 +- src/main.cpp | 29 +++++++++++++++++++++++++--- src/scikit_build_example/__init__.py | 4 ++-- tests/test_basic.py | 15 ++++++++++---- 5 files changed, 52 insertions(+), 14 deletions(-) diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index 5cc3da4..7f374b1 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -2,15 +2,23 @@ "configurations": [ { "name": "Linux", + "compilerPathInCppPropertiesJson": "/usr/bin/clang", "includePath": [ - "${workspaceFolder}/**" - "include/**" + "/home/george/Documents/development/pydefm/**" ], "defines": [], - "compilerPath": "/usr/bin/clang", "cStandard": "c17", "cppStandard": "c++14", - "intelliSenseMode": "linux-clang-x64" + "intelliSenseMode": "linux-clang-x64", + "mergeConfigurations": false, + "compilerPath": "/usr/bin/clang", + "configurationProvider": "ms-vscode.makefile-tools", + "browse": { + "path": [ + "/home/george/Documents/development/pydefm" + ], + "limitSymbolsToIncludedHeaders": true + } } ], "version": 4 diff --git a/pyproject.toml b/pyproject.toml index 5d2508b..44116e4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "scikit_build_core.build" [project] -name = "pydefm" +name = "scikit_build_example" version = "0.0.1" description="Python bindings for defm" readme = "README.md" diff --git a/src/main.cpp b/src/main.cpp index edd0eb2..33a28d7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -24,14 +24,19 @@ std::shared_ptr< defm::DEFM > new_defm( // std::vector y = {1, 2, 3, 4, 5}; // std::vector x = {1.0, 2.0, 3.0, 4.0, 5.0}; + // Accessing the data buffer + auto id_buff = id.request(); + auto y_buff = y.request(); + auto x_buff = x.request(); + int n_id = id.size(); int n_y = y.size(); int n_x = x.size(); std::shared_ptr< defm::DEFM > object(new defm::DEFM( - id.mutable_data(0u), - y.mutable_data(0u), - x.mutable_data(0u), + static_cast< int * >(id_buff.ptr), + static_cast< int * >(y_buff.ptr), + static_cast< double * >(x_buff.ptr), static_cast< size_t >(n_id), static_cast< size_t >(n_y), static_cast< size_t >(n_x), @@ -42,8 +47,20 @@ std::shared_ptr< defm::DEFM > new_defm( return object; } +/** + * @brief Print the y vector + * @param object The DEFM object +*/ +void print_y(const std::shared_ptr< defm::DEFM > & object) { + + auto Y = object->get_Y(); + for (size_t i = 0u; i < object->get_n_y(); ++i) + std::cout << (*(Y + i)) << " "; + std::cout << std::endl; + return; +} PYBIND11_MODULE(_core, m) { m.doc() = R"pbdoc( @@ -88,6 +105,12 @@ PYBIND11_MODULE(_core, m) { Some other explanation about the new_defm function. )pbdoc"); + m.def("print_y", &print_y, R"pbdoc( + Print the y vector + + Some other explanation about the print_y function.") + )pbdoc"); + #ifdef VERSION_INFO m.attr("__version__") = MACRO_STRINGIFY(VERSION_INFO); #else diff --git a/src/scikit_build_example/__init__.py b/src/scikit_build_example/__init__.py index a81b80b..608ec81 100644 --- a/src/scikit_build_example/__init__.py +++ b/src/scikit_build_example/__init__.py @@ -1,5 +1,5 @@ from __future__ import annotations -from ._core import __doc__, __version__, add, subtract, new_defm +from ._core import __doc__, __version__, add, subtract, new_defm, print_y -__all__ = ["__doc__", "__version__", "add", "subtract", "new_defm"] +__all__ = ["__doc__", "__version__", "add", "subtract", "new_defm", "print_y"] diff --git a/tests/test_basic.py b/tests/test_basic.py index 7ce9a1a..6fab9e9 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -3,11 +3,16 @@ import scikit_build_example as m import numpy as np -y = np.array([1, 2, 3]) -x = np.array([1, 2, 3]) -id = np.array([1, 2, 3]) +y = np.array([0, 10, 3]) +x = np.array([1, 2.0, 3.4]) +id = np.array([11, 2, 3]) -m.new_defm(y, x, id) +obj = m.new_defm(id, y, x) + +obj.print() + +# Just testing whether the function works +m.print_y(obj) def test_version(): @@ -20,3 +25,5 @@ def test_add(): def test_sub(): assert m.subtract(1, 2) == -1 + +print("Everything passed") \ No newline at end of file