xtensor-stack / xtensor

C++ tensors with broadcasting and lazy computing
BSD 3-Clause "New" or "Revised" License
3.33k stars 398 forks source link

Implement pretty-printing for xtensor containers in GDB #1780

Open xiuliren opened 4 years ago

xiuliren commented 4 years ago

is there a way to print out the values in a xtensor?

this is the output I get now. Note that the coords variable is a xtensor rather than a xexpression.

(gdb) p coords
$7 = {<xt::xview_semantic<xt::xview<xt::xtensor_container<xt::uvector<float, std::allocator<float> >, 2, (xt::layout_t
ype)1, xt::xtensor_expression_tag> const&, xt::xall<unsigned long>, unsigned long> >> = {<xt::xsemantic_base<xt::xview
<xt::xtensor_container<xt::uvector<float, std::allocator<float> >, 2, (xt::layout_type)1, xt::xtensor_expression_tag> 
const&, xt::xall<unsigned long>, unsigned long> >> = {<xt::xexpression<xt::xview<xt::xtensor_container<xt::uvector<flo
at, std::allocator<float> >, 2, (xt::layout_type)1, xt::xtensor_expression_tag> const&, xt::xall<unsigned long>, unsig
ned long> >> = {
        p_shared = {<std::__shared_ptr<xt::xview<xt::xtensor_container<xt::uvector<float, std::allocator<float> >, 2, 
(xt::layout_type)1, xt::xtensor_expression_tag> const&, xt::xall<unsigned long>, unsigned long>, (__gnu_cxx::_Lock_pol
icy)2>> = {<std::__shared_ptr_access<xt::xview<xt::xtensor_container<xt::uvector<float, std::allocator<float> >, 2, (x
t::layout_type)1, xt::xtensor_expression_tag> const&, xt::xall<unsigned long>, unsigned long>, (__gnu_cxx::_Lock_polic
y)2, false, false>> = {<No data fields>}, _M_ptr = 0x0, _M_refcount = {
              _M_pi = 0x0}}, <No data fields>}}, <No data fields>}, <No data fields>}, <xt::xiterable<xt::xview<xt::xt
ensor_container<xt::uvector<float, std::allocator<float> >, 2, (xt::layout_type)1, xt::xtensor_expression_tag> const&,
 xt::xall<unsigned long>, unsigned long> >> = {<xt::xconst_iterable<xt::xview<xt::xtensor_container<xt::uvector<float,
 std::allocator<float> >, 2, (xt::layout_type)1, xt::xtensor_expression_tag> const&, xt::xall<unsigned long>, unsigned
 long> >> = {<No data fields>}, <No data fields>}, <xt::xaccessible<xt::xview<xt::xtensor_container<xt::uvector<float,
 std::allocator<float> >, 2, (xt::layout_type)1, xt::xtensor_expression_tag> const&, xt::xall<unsigned long>, unsigned
 long> >> = {<xt::xconst_accessible<xt::xview<xt::xtensor_container<xt::uvector<float, std::allocator<float> >, 2, (xt
::layout_type)1, xt::xtensor_expression_tag> const&, xt::xall<unsigned long>, unsigned long> >> = {<No data fields>}, 
<No data fields>}, <xt::extension::xtensor_empty_base> = {<No data fields>}, static is_const = true, 
  static static_layout = xt::layout_type::dynamic, static contiguous_layout = false, static is_strided_view = true, 
  static is_contiguous_view = false, static has_trivial_strides = false, m_e = @0x555555d72020, 
  m_slices = {<std::_Tuple_impl<0, xt::xall<unsigned long>, unsigned long>> = {<std::_Tuple_impl<1, unsigned long>> = 
{<std::_Head_base<1, unsigned long, false>> = {
          _M_head_impl = 1}, <No data fields>}, <std::_Head_base<0, xt::xall<unsigned long>, false>> = {
        _M_head_impl = {<xt::xslice<xt::xall<unsigned long> >> = {<No data fields>}, 
          m_size = 8}}, <No data fields>}, <No data fields>}, m_shape = {_M_elems = {8}}, m_strides = {_M_elems = {
      140737488340848}}, m_backstrides = {_M_elems = {140737488340848}}, m_data_offset = 93825000193232, 
  m_strides_computed = false}
JohanMabille commented 4 years ago

I don't use gdb so I'm not sure, can yo try to print coords.data() ? it should print the underlying 1-D buffer.

wolfv commented 4 years ago

I think there are ways to implement pretty printing for GDB ...

Here seems to be some good introduction on how to do it: http://www.kurokatta.org/grumble/2018/05/gdb-pretty

If someone wants to do that that would be awesome!

christopherburke commented 2 years ago

I made an initial version of converting the xarray, xtensor, and xtensor_fixed containers for use in gdb's python interface. https://github.com/christopherburke/gdb_xt2np It actually goes beyond a pretty printer because it actually copies the array data into a numpy array, thus allowing one to use numpy or matplotlib plotting within gdb's python interpreter to examining the array data. It opens up development for any kind of data visualization one can think up in python to aid in debugging xtensor containers.

JohanMabille commented 2 years ago

Awesome!