On 01/02/2013 12:52 AM, Vincent R wrote:
> Thanks for your comments!
>
> ---------------------------------------
> 1) I will update a more detailed example later.
> But for example, you can type:
> MyMatrix<double, 3, 3> a(1); // A matrix of one
> MyMatrix<double, 3, 3> b(2); // A matrix of two
> MyMatrix<double, 3, 3> c = a+4*b; // The calculation is done thanks to the operators provided by static_mathematizer
Ah. It seems you're discussing the mathematical concept of vector spaces here.
In particular, you seem to be trying to offer an easy way to define
the operations on a vector space, i.e.
- addition (subtraction) of two items of a vector space is element-wise addition (subtraction)
- scalar multiplication with an item of a vector space is element-wise multiplication
Are there any other operations you'd like to support in your "static_mathematizer"?
If not, please name it "vector_space_operations" and people will actually know
what you intend to mean.
I'm always looking for generalities, so are there any other mathematical
structures that would benefit from a treatment similar to what you attempt to
offer for vector spaces?
> ---------------------------------------
> 2) I do not master SIMD optimizations enough, but in a very naive way, I thought that a loop like that (where Size is a constant, and where access is inlined) could be vectorized by a compiler.
>
> template<class Kind, std::size_t Size, template<class, Parameters...> class Crtp, class Type, Kind... Parameters>
> struct static_mathematizer
> {
> template<class OtherType>
> Crtp<Type, Parameters...>& operator+=(const static_mathematizer<Kind, Size, Crtp, OtherType, Parameters...>& rhs)
> {
> for (std::size_t i = 0; i < Size; ++i) {
> static_cast<Crtp<Type, Parameters...>&>(*this).access(i) += rhs.access(i);
> }
> return static_cast<Crtp<Type, Paramters...>&>(*this);
> }
> };
>
> I am wrong ?
I don't know. It's at least two abstraction levels away from the C-style array.
Here's another concern:
- Let's assume we're talking about a 3x3 "float" matrix. The SIMD on the machine can deal
with 4 floats in one operation. Let's assume the derived class actually implements
a 4x3 matrix, with the last column ignored, so that there's a nice match to the SIMD
data width. I don't think we can expect the compiler to "know" that it can use
SIMD instructions right away, because those would modify the fourth column, which would
(in general) be visible to other parts of the code.
Jens