Boost logo

Boost :

From: Toon Knapen (toon.knapen_at_[hidden])
Date: 2001-12-28 02:48:18


rgarcia1771 wrote:

> --- In boost_at_y..., Toon Knapen <toon.knapen_at_s...> wrote:
>
>>>What problems were you having with gcc-2.95.3?
>>>
>>My small 'performance test' for instance (in attach. a fresh copy).
>>
>>
> ...
>
>>>I have had some great preliminary success with KCC, but
>>>more investigation of this is necessary.
>>>
>>
>>Can you elaborate on the performance with KCC ?
>>
>
> I received your small performance test. Thanks a lot. I ran it on a
> few compilers. The example works under gcc 2.95.3, but the trick is
> that you must supply "-ftemplate-depth-50" for it to compile. The
> default depth of 17 isn't adequate. I will add some notes on
> compiler support to the documentation.

Confirmed : it builds succesfully with -ftemplate-depts-50
BTW : I just tried gcc-3.0.3 and the build was succesfull.

>
> In short, I'm finding that both KCC and gcc seem to optimize the
> abstraction layer away. I haven't done anything comprehensive, but
> here are my single-run results:
> (note: Toon's test code times some operations with a flat C-style
> array of data, then the same code using multi_array, followed by the
> C-style array again).
>
> # GCC 2.95.3 (Yellow Dog Linux (on my laptop, Powerbook G3 500Mhz)
> # c++ -O2 -ftemplate-depth-50 ...
> 1.46
> 2.34
> 1.51
>
> # KCC <version?> (Solaris - SunBlade 1000)
> # KCC +K3 -O2 ...
> 1.28
> 1.53
> 1.21
>
> # GCC 3.0.2 (Solaris - SunBlade 1000)
> # c++ -O2
> 1.04
> 3.52
> 1.31
>

Below you can find a new test program. This time the allocation is not
inside the timing anymore and all tests are run twice to compensate for
startup times.
But next to a 3D test, it also contains a 1D test. The reason I included
this is that I suspect that proxy-handling for the indexing in 3D
generates the extra time compared to the C-array.
As you can see in the timings, in 1D performance of the multi_array and
the plain C array are equal. In 3D I don't think that the abstraction
layer is compiled away since the multi_array is 50% slower (on
gcc-2.95.3) or more (on gcc-3.0.x).

my command line for all three compilers looks like :
g++ -I/usr/local/boost -I/home/tk/multi_array -ftemplate-depth-50 -O2
multiarray.cpp

for gcc-2.95.3 :
----------------
3D C array : 0.97
3D multiarray : 1.55
3D C array : 0.95
3D multiarray : 1.53
1D C array : 2.5
1D multiarray : 0.99
1D C array : 0.95
1D multiarray : 0.97

for gcc-3.0.2 :
---------------
3D C array : 0.97
3D multiarray : 2.71
3D C array : 0.96
3D multiarray : 2.69
1D C array : 3.62
1D multiarray : 1.01
1D C array : 0.92
1D multiarray : 1

for gcc-3.0.3 :
---------------
3D C array : 0.97
3D multiarray : 2.73
3D C array : 0.97
3D multiarray : 2.71
1D C array : 3.65
1D multiarray : 1.01
1D C array : 0.93
1D multiarray : 0.99

test program multi-array.cpp :
------------------------------

#include "boost/multi_array.hpp"
#include <boost/timer.hpp>
#include <iostream>
#include <cassert>

static const int size = 100;
static const int total_1D_size = size*size*size;

typedef boost::multi_array<double, 1> MultiArray1D;
typedef boost::multi_array<double, 3> MultiArray3D;

int multiarray_1D(MultiArray1D& A) {

   // Assign values to the elements
   int values = 0;
   for(MultiArray1D::index i = 0; i != total_1D_size; ++i) A[i] = values++;

   int verify = 0;
   for(MultiArray1D::index i = 0; i != total_1D_size; ++i) assert(A[i]
== verify++);

   return 0;
}

int carray_1D(double* A)
{
   // Assign values to the elements
   int values = 0;
   for(int i = 0; i != total_1D_size; ++i) A[i] = values++;

   // Verify values
   int verify = 0;
   for(int i = 0; i != total_1D_size; ++i) assert( A[i] == verify++ );

   return 0;
}

int multiarray_3D (MultiArray3D& A) {
   // Assign values to the elements
   int values = 0;
   for(MultiArray3D::index i = 0; i != size; ++i)
     for(MultiArray3D::index j = 0; j != size; ++j)
       for(MultiArray3D::index k = 0; k != size; ++k)
         A[i][j][k] = values++;

   // Verify values
   int verify = 0;
   for(MultiArray3D::index i = 0; i != size; ++i)
     for(MultiArray3D::index j = 0; j != size; ++j)
       for(MultiArray3D::index k = 0; k != size; ++k)
         assert(A[i][j][k] == verify++);

   return 0;
}

int carray_3D(double* A)
{
   // Assign values to the elements
   int values = 0;
   for(int i = 0; i != size; ++i)
     for(int j = 0; j != size; ++j)
       for(int k = 0; k != size; ++k)
         A[(((i*size)+j)*size)+k] = values++;

   // Verify values
   int verify = 0;
   for(int i = 0; i != size; ++i)
     for(int j = 0; j != size; ++j)
       for(int k = 0; k != size; ++k)
         assert( A[(((i*size)+j)*size)+k] == verify++ );

   return 0;
}

int main()
{
   boost::timer t;

   { // 3D tests
     MultiArray3D multiarray(boost::extents[size][size][size]);
     double* carray = new double[size*size*size];
     assert( carray );

     t.restart();
     for(int i = 0; i < 10 ; ++i ) carray_3D(carray);
     std::cout << "3D C array : " << t.elapsed() << std::endl;

     t.restart();
     for(int i = 0; i < 10 ; ++i ) multiarray_3D(multiarray);
     std::cout << "3D multiarray : " << t.elapsed() << std::endl;

     t.restart();
     for(int i = 0; i < 10 ; ++i ) carray_3D(carray);
     std::cout << "3D C array : " << t.elapsed() << std::endl;

     t.restart();
     for(int i = 0; i < 10 ; ++i ) multiarray_3D(multiarray);
     std::cout << "3D multiarray : " << t.elapsed() << std::endl;

     delete carray;
   }

   {
     MultiArray1D multiarray(boost::extents[total_1D_size]);
     double* carray = new double[total_1D_size];

     for(int i = 0; i < 10 ; ++i ) carray_1D(carray);
     std::cout << "1D C array : " << t.elapsed() << std::endl;

     t.restart();
     for(int i = 0; i < 10 ; ++i ) multiarray_1D(multiarray);
     std::cout << "1D multiarray : " << t.elapsed() << std::endl;

     t.restart();
     for(int i = 0; i < 10 ; ++i ) carray_1D(carray);
     std::cout << "1D C array : " << t.elapsed() << std::endl;

     t.restart();
     for(int i = 0; i < 10 ; ++i ) multiarray_1D(multiarray);
     std::cout << "1D multiarray : " << t.elapsed() << std::endl;

     delete carray;
   }

   return 0;
}


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk