[vsipl++] Matlab IO

Stefan Seefeld stefan at codesourcery.com
Fri Jun 16 15:47:06 UTC 2006


Assem,

I just checked in my png bits, so your patch may already get a bit smaller.

Assem Salama wrote:

> +  // generic reader that allows us to read a generic type and cast to another
> +  
> +  // the read function for real or complex depending of the view that was
> +  // passed in
> +  template <typename T1,
> +            typename T2,
> +	    typename ViewT>
> +  void read(std::istream& is,ViewT v)
> +  {
> +    vsip::dimension_type const View_dim = ViewT::dim;
> +    vsip::Index<View_dim> my_index;
> +    vsip::impl::Length<View_dim> v_extent = extent(v);
> +    T1 data;
> +
> +    // get num_points
> +    vsip::length_type num_points = v.size();
> +
> +    // read all the points
> +    for(vsip::index_type i=0;i<num_points;i++) {
> +      is.read(reinterpret_cast<char*>(&data),sizeof(data));
> +      put(v,my_index,T2(data));
> +
> +      // increment index
> +      my_index = vsip::impl::next(v_extent,my_index);
> +    }
> +
> +  }

What's the reason for you using the 'put' function here, instead of
some v method ? And, why are you casting to T2 in that call, instead
of casting to ViewT::value_type ? As it stands, the above read function
takes three distinct type parameters: T1, T2, and ViewT::value_type.
I think only two are required (and the first may default to ViewT::value_type,
actually).

How portable is the above code ? It doesn't appear to care for endianness,
i.e. you cast between 'char *' and T2 without any endianness check. If you
write on a big-endian machine, and read it back on a little-endian
machine, you will thus get different values.

> +  // a write function to output a view to a matlab file.
> +  template <typename T,
> +	    typename ViewT>
> +  void write(std::ostream& os,ViewT v)
> +  {
> +    vsip::dimension_type const View_dim = ViewT::dim;
> +    vsip::Index<View_dim> my_index;
> +    vsip::impl::Length<View_dim> v_extent = extent(v);
> +    T data;
> +
> +    // get num_points
> +    vsip::length_type num_points = v.size();
> +
> +    // write all the points
> +    for(vsip::index_type i=0;i<num_points;i++) {
> +      data = get(v,my_index);
> +      os.write(reinterpret_cast<char*>(&data),sizeof(data));

The same point about endianness applies here.


> +
> +      // increment index
> +      my_index = vsip::impl::next(v_extent,my_index);
> +    }
> +
> +  }

Thanks,
		Stefan

-- 
Stefan Seefeld
CodeSourcery
stefan at codesourcery.com
(650) 331-3385 x718



More information about the vsipl++ mailing list