← Back to team overview

dolfin team mailing list archive

Re: Fix for const Array<const double>

 


Anders Logg wrote:
> I think I found a solution for the const Array<const double> thing.
> It involves const_cast but with a little twist that I think makes it a
> good const_cast.
> 
> I first tried copying data instead of copying the array but that had a
> noticeable impact on run-time. I've been running the electrodynamics
> demo to check.
> 
> When copying data, the timing went from
> 
>   real 0m48.910s
>   user 0m46.150s
>   sys  0m2.070s
> 
> to
> 
>   real 0m49.756s
>   user 0m47.540s
>   sys  0m2.180s
> 
> This timing is from using a loop over the array. Using memcpy instead
> was even worse:
> 
>   real 0m50.502s
>   user 0m48.490s
>   sys  0m1.990s
> 
> I've now implemented another fix using const_cast and it runs even
> faster than before which is strange:
> 
>   real 0m47.761s
>   user 0m45.800s
>   sys  0m1.920s
>

We use boost::shared_ptr in Array, which may have non-negligible
creation overhead when handling small arrays. I can't remember if we
create Arrays repeatedly in the eval process, but if we do it might that
the shared_ptr creation dominates and that this hides the cost of copying.

I was planning at some point to check the performance of Array using
shared and plain pointers.

> Anyway, the suggested fix is the following:
> 
> 1. Make the x member variable const in Data.h:
> 
>   const Array<double> x;
> 
> 2. Use a double const_cast when copying the array:
> 
>   const_cast<Array<double>*>(&(this->x))->update(gdim, const_cast<double*>(x));
> 
> This is an honest const_cast. The member variable x is const so it
> "can't" be changed so the input data double* x remains const in
> practice.
> 
> If there are no objections, I can push the change which will change
> the x variable in all eval functions from
>

Go ahead.

Garth

>   const Array<const double>
> 
> to
> 
>   const Array<double>.
> 
> --
> Anders
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Mailing list: https://launchpad.net/~dolfin
> Post to     : dolfin@xxxxxxxxxxxxxxxxxxx
> Unsubscribe : https://launchpad.net/~dolfin
> More help   : https://help.launchpad.net/ListHelp





Follow ups

References