10000 fix segfault on exit with python3 by amadeus84 · Pull Request #301 · lava/matplotlib-cpp · GitHub
[go: up one dir, main page]

Skip to content

fix segfault on exit with python3 #301

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
matplotlib.h: add support for contiguous_range (C++20) and ranges.cpp…
… example
  • Loading branch information
amadeus84 committed Mar 14, 2022
commit 3f5b27618cf79c0fbc0219646b298218a3aa9220
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ if(Python3_NumPy_FOUND)
target_link_libraries(span PRIVATE matplotlib_cpp)
target_compile_features(span PRIVATE cxx_std_20)
set_target_properties(span PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")

add_executable(ranges examples/ranges.cpp)
target_link_libraries(ranges PRIVATE matplotlib_cpp)
target_compile_features(ranges PRIVATE cxx_std_20)
set_target_properties(ranges PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")

endif()


Expand Down
105 changes: 105 additions & 0 deletions examples/ranges.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
//
// g++ -std=c++20 -g -Wall -o ranges $(python-config --includes) ranges.cpp $(python-config --ldflags --embed)
//
// g++ -std=c++17 -g -Wall -o ranges $(python-config --includes) ranges.cpp $(python-config --ldflags --embed)
//
#include "../matplotlibcpp.h"

#include <iostream>
#include <list>
#include <map>
#include <span>
#include <string>
#include <vector>

#define UNUSED(x) (void)(x)

using namespace std;
namespace plt = matplotlibcpp;

int main()
{
plt::detail::_interpreter::get();


#if __cplusplus >= CPP20

// C-style arrays with multiple rows.

// Care with column-major vs row-major!
// C and Python are row-major, but usually a time series is column-major
// and we want to plot the columns.
// In the example below, these columns are [3,1,4,5] and [5,4,1,3], so
// the data must be stored like this:
time_t t[]={1, 2, 3, 4};
double data [] = {
3, 5,
1, 4,
4, 1,
5, 3
};

// Use std::span() to convert to a contiguous range (O(1)).
// Data won't be copied, but passed as a pointer to Python.
plt::plot(span(t, 4), span(data, 8));
plt::grid(true);
plt::title("C-arrays, with multiple columns");
plt::show();

#else

cerr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << ": "
<< "No support for contiguous ranges." << endl;

#endif

// vectors

// Vectors are also contiguous ranges.
// In C++20, as with span, plot resolves to plot(contiguous_range).
// In C++ < 20 plot resolves to plot(vector).
vector<double> x={1, 2, 3, 4, 5};
vector<double> y={0, 1, 0, -1, 0};
plt::plot(x, y);
plt::grid(true);
plt::title("vectors");
plt::show();


// lists

// By contrast, lists are non-contiguous (but iterable) containers, so
// plot resolves to plot(iterable).
list<double> u { 1, 2, 3, 4, 5};
list<double> v { 0, -1, 0, 1, 0};
plt::plot(u, v, "");
plt::grid(true);
plt::title("Lists (non-contiguous ranges)");
plt::show();


// All together now
#if __cplusplus >= CPP20

// If span is not last, plot resolves to plot(iterable), which copies data.
// That's because in the dispatch plot() we have plot_impl() && plot()
// (i.e. plot_impl() comes first), and we only have iterable and
// callable plot_impl(). That sends us to the iterable plot_impl(),
// rather than to plot(contiguous_range).
//
// TODO: have 3 tags plot_impl(): iterable, callable and contiguous range.
plt::plot(span(t, 4), span(data, 8), "", x, y, "b", u, v, "r");

// This resolves to plot(contiguous_range) and does not copy data.
// plt::plot(x, y, "b", u, v, "r", span(t, 4), span(data, 8));
#else
plt::plot(x, y, "b", u, v, "r");
#endif
plt::grid(true);
plt::title("Variadic templates recursion");
plt::show();

plt::detail::_interpreter::kill();

return 0;
}
Loading
0