L2 norm of distribution solution

27 views
Skip to first unread message

Jean Ragusa

unread,
Mar 1, 2019, 10:54:46 PM3/1/19
to deal.II User Group
Hi,

I have a solution vector with locally owned and locally relevant dofs. I want to compute its L2-norm. Using the l2_norm() on it caused a Trilinos error (I was going to compute the squared of the L2 norm, then do an MPI Allreduce on the resulting values).

So, I decided to code it "by hand" as follows: 

double norm_squared = 0.;
const unsigned int start_ = (dist_solution.local_range().first), end_ = (dist_solution.local_range().second);

for (unsigned int i = start_; i < end_; ++i)
   norm_squared += std::pow(dist_solution(i),2);

double norm_global =0.;
MPI_Allreduce(&norm_squared, &norm_global, 1, MPI_DOUBLE, MPI_SUM, mpi_communicator);
norm_global = std::sqrt(norm_global);


Even though this works, I was wondering if there is a better way to do this? 

Cheers,
--jean


Wolfgang Bangerth

unread,
Mar 2, 2019, 11:28:03 AM3/2/19
to dea...@googlegroups.com
Yes! Create a completely distributed vector, copy your ghosted vector into it,
and then compute the l2 norm of that completely distributed vector. That's
going to cost a bit of communication, but I suspect you're not doing it often
enough to actually feel the difference :-)

Cheers
W.

--
------------------------------------------------------------------------
Wolfgang Bangerth email: bang...@colostate.edu
www: http://www.math.colostate.edu/~bangerth/

Jean Ragusa

unread,
Mar 3, 2019, 2:27:24 PM3/3/19
to deal.II User Group
Thanks Wolfgang.I am just replying to add a couple of details that were not 100% obvious to me. Maybe someone else will benefit from this when the search the post archive...

As Wolfgang stated, copying the vector that contains ghosted values (relevant dofs) into a completely distributed vector allows you to use l2_norm() on it.What I didn't realize is that the l2_norm() on a distributed vector calls the MPI_Allreduce behind the scene and thus no additional action is needed when writing your own code.

Wolfgang Bangerth

unread,
Mar 3, 2019, 4:58:53 PM3/3/19
to dea...@googlegroups.com
On 3/3/19 12:27 PM, Jean Ragusa wrote:
>
> As Wolfgang stated, copying the vector that contains ghosted values (relevant
> dofs) into a completely distributed vector allows you to use l2_norm() on
> it.What I didn't realize is that the l2_norm() on a distributed vector calls
> the MPI_Allreduce behind the scene and thus no additional action is needed
> when writing your own code.

That is correct. If you think this is not obvious in the documentation, can
you point out where you looked? It's something easily fixed. (Want to write
your first pull request? ;-) )

Denis Davydov

unread,
Mar 4, 2019, 8:30:36 AM3/4/19
to deal.II User Group
Hi Jean,

Minor unrelated note to your snippet of manually written L2 norm: have a look at 
which has plenty of template wrappers for MPI functions that can make life much much easier, including sending objects that can be serialized
(which is not the case here, but still).

> If you think this is not obvious in the documentation, can 
you point out where you looked? It's something easily fixed. (Want to write 
your first pull request? ;-) ) 

+1

Regards,
Denis.
Reply all
Reply to author
Forward
0 new messages