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<<br>typename Type,<br>unsigned int Size,<br>typename =
Kind,<br>template<typename, Kind...> class Derived,<br>Kind... Dim<br=
>><br>class std::vectorizer<br>{<br><br>public:<br> //=
Addition operation (rhs vectorizer):<br> template<<br=
> typename OtherType, <br> class =3D ty=
pename std::enable_if<std::is_convertible<OtherType, Type>::value&=
gt;::type> <br> inline Derived<typename std::common=
_type<Type, OtherType>::type, Dim...> <br> opera=
tor+(const std::vectorizer<OtherType, Size, Kind, Derived, Dim...>&am=
p; rhs) const;<br><br> // Addition operation (rhs value):=
<br> template<<br> typename OtherTyp=
e, <br> class =3D typename std::enable_if<std::is_conv=
ertible<OtherType, Type>::value>::type> <br> =
inline Derived<typename std::common_type<Type, OtherType>::type, D=
im...> <br> operator+(const OtherType& rhs) const;=
<br><br> // Addition operation (lhs value):<br> &nbs=
p; template<<br> typename OtherType, <br> &=
nbsp; typename SelfType, <br> unsigned int SelfSize=
, <br> typename SelfKind, <br> template=
<typename, SelfKind...> class SelfDerived, <br> Sel=
fKind... SelfDim, <br> class> <br> f=
riend inline SelfDerived<typename std::common_type<SelfType, OtherTyp=
e>::type, SelfDim...> <br> operator+(const OtherTyp=
e& lhs, const std::vectorizer<SelfType, SelfSize, SelfKind, SelfDeri=
ved, SelfDim...>& rhs);<br><br>protected:<br> // D=
ata members<br> 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<typename T, unsigned int N> class MyVector : public std::=
vectorizer<T, N, unsigned int, MyVector, N><br>template<typename T=
, unsigned int N, unsigned int M> class MyMatrix : public std::vectorize=
r<T, N*M, unsigned int, MyMatrix, N, M></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 />
<br />
<br />
<br />
------=_Part_276_15504290.1354487827545--
.