dolfin team mailing list archive
-
dolfin team
-
Mailing list archive
-
Message #17285
Fix for const Array<const double>
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
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
const Array<const double>
to
const Array<double>.
--
Anders
Attachment:
signature.asc
Description: Digital signature
Follow ups