Efficient Matrix-based Calculation

57 views
Skip to first unread message

Masoud Ahmadi

unread,
Oct 12, 2021, 12:33:39 PM10/12/21
to deal.II User Group

Dear all,

I'm trying to use a Matrix-based solution for a general 3D FE elastic problem (just like example-8). In that example the authors used a linear scaler solution for calculating the stiffness matrix of each element; but, due to some reasons I want to use a matrix-based solution so that the stiffness matrix of each element can be calculated as follows:
K_cell = B^T . D . B,
where B matrix follows from the differential operator for strain calculation and D is the usual elasticity matrix for 3D isotropic problems. The size of the B and D matrices are 6X3 and 6X6 respectively. I defined these matrices as "vector" of "FullMatrix<double>" and try to do operate on them by FullMatrix operators as follows:
B[i].Tmmult(tmpMat,D);
tmpMat.mmult(BDB,B[j]);
and it works correctly; however, it works rather slowly.
My question is, is "FullMatrix" the most efficient and fast way in dealii to evaluate such like matrix calculations? Are there any alternatives?

Best regards,
Masoud




Wolfgang Bangerth

unread,
Oct 13, 2021, 11:25:54 AM10/13/21
to dea...@googlegroups.com
Masoud,
it's difficult to say without seeing the actual code. As a general rule,
it is impossible to tell for even good programmers where the "slow"
parts of a code are unless one actually uses a profiling tool to measure
each line of the code. So I would suggest that you figure out which
line(s) is actually slow in your code, and then asking how that one part
can be improved.

My best guess is that it is not actually the FullMatrix itself, but how
you build the matrix. But as I said, it's not possible to say so without
actual benchmarking.

Best
W.

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

Masoud Ahmadi

unread,
Oct 15, 2021, 6:58:15 PM10/15/21
to deal.II User Group

Dear Prof. Bangerth,

Here is a part of the tutorial code for example 8 to calculate stiffness matrix of an element:
Screenshot 2021-10-15 at 12.48.39.png

And here is my equivalent code for the same purpose with a matrix-based viewpoint:
Screenshot 2021-10-15 at 23.12.13.png

it is based on the following formulation for a 3D elastic problem:
thumbnail_image.png
By "slow", I mean in comparison with the same code in tutorial (example 8). I measured the lines of the code, and I am sure the difference is for the mentioned part of the code. To be more accurate: the operations on matrices, as follow in the second image:
B[I].Tmmult(tmpM,D);
tmpM.mmult(BDB,B[J]);

Best regards,
Masoud

Wolfgang Bangerth

unread,
Oct 19, 2021, 2:24:11 PM10/19/21
to dea...@googlegroups.com

Masoud,

> Here is a part of the tutorial code for example 8 to calculate stiffness
> matrix of an element:
> Screenshot 2021-10-15 at 12.48.39.png
>
> And here is my equivalent code for the same purpose with a matrix-based viewpoint:
> Screenshot 2021-10-15 at 23.12.13.png
>
> it is based on the following formulation for a 3D elastic problem:
> thumbnail_image.png
> By "slow", I mean in comparison with the same code in tutorial (example 8). I
> measured the lines of the code, and I am sure the difference is for the
> mentioned part of the code. To be more accurate: the operations on matrices,
> as follow in the second image:
> B[I].Tmmult(tmpM,D);
> tmpM.mmult(BDB,B[J]);

You could probably make the code substantially faster already if you moved the
declaration of the B, tmpM, and BDB matrices out of the loop over the
quadrature points, and simply set them to zero inside the loop. Creating these
variables requires allocating memory every time you do one iteration over the
quadrature points, which is expensive.

But in the end, compare what work you are doing in your loop with what the
equivalent code in step-8 is doing, and you shouldn't be surprised by your
observation. step-8 only does 2 (and if i==j, then 3) tensor contractions for
each index i,j,q. You are allocating and releasing memory, and doing two
products between 6x3 and 3x3 matrices, plus a lot of index work. It does not
surprise me that this is slow.

Masoud Ahmadi

unread,
Oct 20, 2021, 8:08:24 AM10/20/21
to deal.II User Group
Dear Prof. Bangerth,

Thanks for your answer. 
Indeed, I took the declaration of matrices out of the loop; but still, as I mentioned, the part that is substantially reducing the speed is the product between the matrices.
As you mentioned, we expect this to be slower than the equivalent code in step-8; however, it is very slower than the same code that I wrote before in Matlab.
My question is, I want to be sure that "FullMatrix" is the most efficient and fast way in dealii to evaluate such like matrix calculations (the products). Or should I be looking for alternatives?

Best regards

Jean-Paul Pelteret

unread,
Oct 22, 2021, 5:25:23 PM10/22/21
to dea...@googlegroups.com
Dear Masoud,

As you mentioned, we expect this to be slower than the equivalent code in step-8; however, it is very slower than the same code that I wrote before in Matlab.

A quick question: are you sure that you’re running your problem in release mode, rather than debug mode? I had a colleague who adopted this BDB approach that you’ve described, and he never mentioned performance issues to the degree that you’re seeing here.

Best,
Jean-Paul

--
The deal.II project is located at http://www.dealii.org/
For mailing list/forum options, see https://groups.google.com/d/forum/dealii?hl=en
---
You received this message because you are subscribed to the Google Groups "deal.II User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dealii+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/dealii/00c2608c-7489-4538-8bd7-09c989d844c4n%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages