Topic: Vectorizer helper class


Author: VinceRev <vince.rev@gmail.com>
Date: Sun, 2 Dec 2012 14:37:07 -0800 (PST)
Raw View
------=_Part_276_15504290.1354487827545
Content-Type: text/plain; charset=ISO-8859-1

Hello.

This discussion follows this one :
*Standardization of optimized constant multidimensionnal math array : need
reviews*
https://groups.google.com/a/isocpp.org/forum/?fromgroups=#!topic/std-proposals/yNEhgsQE03s

Since the last discussion, I have coded a tool that I needed by the library
that I currently design, and the idea proposed here come from that.
Many libraries implement they own constant (or dynamic) mathematical
arrays. So the question is : is it possible to have an unified tool for all
these implementations ?

The idea, I propose here (before writing a proposal) is the following.
The philosophy would be to have a generic abstract helper class (like
std::iterator) that vectorize all standard operations on each element of a
mathematical container.
A design equivalent to what I use in my library would be (example only for
operator+) (the destructor should be protected to avoid any direct use)

--------------------------------------------------
template<
typename Type,
unsigned int Size,
typename Kind,
template<typename, Kind...> class Derived,
Kind... Dim
>
class std::vectorizer
{

public:
    // Addition operation (rhs vectorizer):
    template<
    typename OtherType,
    class = typename std::enable_if<std::is_convertible<OtherType,
Type>::value>::type>
    inline Derived<typename std::common_type<Type, OtherType>::type,
Dim...>
    operator+(const std::vectorizer<OtherType, Size, Kind, Derived,
Dim...>& rhs) const;

    // Addition operation (rhs value):
    template<
    typename OtherType,
    class = typename std::enable_if<std::is_convertible<OtherType,
Type>::value>::type>
    inline Derived<typename std::common_type<Type, OtherType>::type,
Dim...>
    operator+(const OtherType& rhs) const;

    // Addition operation (lhs value):
    template<
    typename OtherType,
    typename SelfType,
    unsigned int SelfSize,
    typename SelfKind,
    template<typename, SelfKind...> class SelfDerived,
    SelfKind... SelfDim,
    class>
    friend inline SelfDerived<typename std::common_type<SelfType,
OtherType>::type, SelfDim...>
    operator+(const OtherType& lhs, const std::vectorizer<SelfType,
SelfSize, SelfKind, SelfDerived, SelfDim...>& rhs);

protected:
    // Data members
    Type _data[Size];
}

--------------------------------------------------

With all operators and some functions (like apply, reduce... ) implemented
in the same way, it provides the following syntax to create automatically
vectorized classes :
template<typename T, unsigned int N> class MyVector : public
std::vectorizer<T, N, unsigned int, MyVector, N>
template<typename T, unsigned int N, unsigned int M> class MyMatrix :
public std::vectorizer<T, N*M, unsigned int, MyMatrix, N, M>.

The implementation could be optimized though expression templates, but with
this kind of tool, we avoid the never-ending debate about sparsity and
other tools needed for numerically-intensive calculation and provide an
easy-to-use unified tool that vectorizes basic operations.
I don't know if this kind of design would be the best for the
standardization, but I like very much the idea to have a standardized
generic vectorizer helper class.

I'm waiting for remarks and comments.

--
Vincent Reverdy
Phd Student @ Laboratory Universe and Theories
Cosmology and General Relativity Group
Observatory of Paris-Meudon, France

--




------=_Part_276_15504290.1354487827545
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

Hello.<br><br>This discussion follows this one : <br><b>Standardization of =
optimized constant multidimensionnal math array : need reviews</b><br>https=
://groups.google.com/a/isocpp.org/forum/?fromgroups=3D#!topic/std-proposals=
/yNEhgsQE03s<br><br>Since the last discussion, I have coded a tool that I n=
eeded by the library that I currently design, and the idea proposed here co=
me from that. <br>Many libraries implement they own constant (or dynamic) m=
athematical arrays. So the question is : is it possible to have an unified =
tool for all these implementations ?<br><br>The idea, I propose here (befor=
e writing a proposal) is the following. <br>The philosophy would be to have=
 a generic abstract helper class (like std::iterator) that vectorize all st=
andard operations on each element of a mathematical container.<br>A design =
equivalent to what I use in my library would be (example only for operator+=
) (the destructor should be protected to avoid any direct use)<br><span sty=
le=3D"font-family: courier new,monospace;"><br>----------------------------=
----------------------</span><br><span style=3D"font-family: courier new,mo=
nospace;">template&lt;<br>typename Type,<br>unsigned int Size,<br>typename =
Kind,<br>template&lt;typename, Kind...&gt; class Derived,<br>Kind... Dim<br=
>&gt;<br>class std::vectorizer<br>{<br><br>public:<br>&nbsp;&nbsp;&nbsp; //=
 Addition operation (rhs vectorizer):<br>&nbsp;&nbsp;&nbsp; template&lt;<br=
>&nbsp;&nbsp;&nbsp; typename OtherType, <br>&nbsp;&nbsp;&nbsp; class =3D ty=
pename std::enable_if&lt;std::is_convertible&lt;OtherType, Type&gt;::value&=
gt;::type&gt; <br>&nbsp;&nbsp;&nbsp; inline Derived&lt;typename std::common=
_type&lt;Type, OtherType&gt;::type, Dim...&gt; <br>&nbsp;&nbsp;&nbsp; opera=
tor+(const std::vectorizer&lt;OtherType, Size, Kind, Derived, Dim...&gt;&am=
p; rhs) const;<br><br>&nbsp;&nbsp;&nbsp; // Addition operation (rhs value):=
<br>&nbsp;&nbsp;&nbsp; template&lt;<br>&nbsp;&nbsp;&nbsp; typename OtherTyp=
e, <br>&nbsp;&nbsp;&nbsp; class =3D typename std::enable_if&lt;std::is_conv=
ertible&lt;OtherType, Type&gt;::value&gt;::type&gt; <br>&nbsp;&nbsp;&nbsp; =
inline Derived&lt;typename std::common_type&lt;Type, OtherType&gt;::type, D=
im...&gt; <br>&nbsp;&nbsp;&nbsp; operator+(const OtherType&amp; rhs) const;=
<br><br>&nbsp;&nbsp;&nbsp; // Addition operation (lhs value):<br>&nbsp;&nbs=
p;&nbsp; template&lt;<br>&nbsp;&nbsp;&nbsp; typename OtherType, <br>&nbsp;&=
nbsp;&nbsp; typename SelfType, <br>&nbsp;&nbsp;&nbsp; unsigned int SelfSize=
, <br>&nbsp;&nbsp;&nbsp; typename SelfKind, <br>&nbsp;&nbsp;&nbsp; template=
&lt;typename, SelfKind...&gt; class SelfDerived, <br>&nbsp;&nbsp;&nbsp; Sel=
fKind... SelfDim, <br>&nbsp;&nbsp;&nbsp; class&gt; <br>&nbsp;&nbsp;&nbsp; f=
riend inline SelfDerived&lt;typename std::common_type&lt;SelfType, OtherTyp=
e&gt;::type, SelfDim...&gt; <br>&nbsp;&nbsp;&nbsp; operator+(const OtherTyp=
e&amp; lhs, const std::vectorizer&lt;SelfType, SelfSize, SelfKind, SelfDeri=
ved, SelfDim...&gt;&amp; rhs);<br><br>protected:<br>&nbsp;&nbsp;&nbsp; // D=
ata members<br>&nbsp;&nbsp;&nbsp; Type _data[Size];<br>}<br></span><br><spa=
n style=3D"font-family: courier new,monospace;">---------------------------=
-----------------------<br><br><span style=3D"font-family: arial,sans-serif=
;">With all operators and some functions (like apply, reduce... ) implement=
ed in the same way, it provides the following syntax to create automaticall=
y vectorized classes :<br><span style=3D"font-family: courier new,monospace=
;">template&lt;typename T, unsigned int N&gt; class MyVector : public std::=
vectorizer&lt;T, N, unsigned int, MyVector, N&gt;<br>template&lt;typename T=
, unsigned int N, unsigned int M&gt; class MyMatrix : public std::vectorize=
r&lt;T, N*M, unsigned int, MyMatrix, N, M&gt;</span>.<br><br>The implementa=
tion could be optimized though expression templates, but with this kind of =
tool, we avoid the never-ending debate about sparsity and other tools neede=
d for numerically-intensive calculation and provide an easy-to-use unified =
tool that vectorizes basic operations.<br>I don't know if this kind of desi=
gn would be the best for the standardization, but I like very much the idea=
 to have a standardized generic vectorizer helper class.<br><br>I'm waiting=
 for remarks and comments.<br></span></span><br><span style=3D"font-family:=
 courier new,monospace;"><span style=3D"font-family: arial,sans-serif;">-- =
<br>Vincent Reverdy<br>Phd Student @ Laboratory Universe and Theories<br>Co=
smology and General Relativity Group<br>Observatory of Paris-Meudon, France=
<br><br></span></span>

<p></p>

-- <br />
&nbsp;<br />
&nbsp;<br />
&nbsp;<br />

------=_Part_276_15504290.1354487827545--

.