-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Restructure material models with Eigen library #297
Comments
I consolidated get_pk2cc() and get_pk2cc_dev(), and also consolidated mat_models.cpp and mat_models_carray.h (I found it more challenging to do these steps sequentially, so I just did them all together). I decided to remove the carray implementations (in mat_models_carray.h) and make everything use Array templates (in mat_models.cpp). The material unit tests and the integration tests all pass. You can find the latest branch here. Next, I think we should use this branch to test out how to use Eigen arrays. After some preliminary thinking, I think a way to organize this would be to decide to use Eigen arrays "below" some level in the code. For example, we could convert all arrays in get_pk2cc() to Eigen arrays, but in struct3d() still use Array templates, and have some code to convert appropriately. Also, I think we should still use templated functions to deal with 2 or 3 spatial dimensions, so that the code is suitably performant. chatGPT suggested a nice way to implement this using a helper function. |
Started very preliminary testing of the conversion to Eigen on the same branch. |
Btw, |
I got I decide to use fixed-size Eigen Matrices (e.g. It should be relatively straightforward to translate the other material models. We will probably have to reimplement some tensor operations (e.g. |
@aabrown100-git Very nice! I believe using fixed-size Eigen Matrices is the way to go. Do you think the performance is better or about the same? Have you replaced loops with expressions? |
I ran https://github.com/SimVascular/svFSIplus/tree/main/tests/cases/struct/block_compression for 10 timesteps as a basic timing test. Looks like the new implementation with Eigen is roughly 50% slower than the old implementation with carrays. @ktbolt Can you recommend any profiling techniques? Yes, at least for the NeoHookean implementation, there are no for loops. Also, any idea why Ubuntu can't find the Eigen library? |
@aabrown100-git You can use gprof to profile the code on Ubuntu. It might be easier and more informative to write some simple programs to model the differences in efficiency of C-style arrays versus how you are using Eigen. Eigen is installed in ThirdParty but I bet the svFSIplus CMake is not finding it. We have probably been using an already installed Eigen when we test. |
@ktbolt Got it thanks. Will work with @lpapamanolis and @msbazzi on these things! |
I finished converting One thing that may improve performance is optimizing Also, just want to mention a couple things that gave me trouble.
if I replace
|
@aabrown100-git Very nice! For a discussion of using |
Great thanks for that link. Just replaced most of the |
I profiled my latest Eigen implementation as well as the current C-array implementation using Xcode Instruments on my Mac. (As an aside, I tried a whole bunch of other profiler tools, like gprof, Valgrind, Easy Profiler, and could not get any of them to work on my Mac. Instruments worked with little effort, so I recommend using it) See revised numbers in next comment I found that the Eigen implementation is much slower than the C-array implementation. To give an example, on the Holzapfel-Ogden Modified-Anisotropy test case, This slow down for small Eigen Tensors seems to be somewhat known. The last response from this stack overflow question provides this comparison for matrix-matrix multiplication (I am currently using EigenTensorFixed) I think we should reconsider using Eigen, or at least Eigen Tensors. Eigen Tensors are technically unsupported by the Eigen project. We could 1) Try to only use Eigen Matrices, 2) Go back to C-arrays for these performance critical tensor operations, 3) Try other tensor libraries as listed here |
UPDATE ON EIGEN PERFORMANCE TESTS I just realized I was using the Debug CMAKE_BUILD_TYPE, which seems to slow down Eigen a lot. With the default CMAKE_BUILD_TYPE=RelWithDebInfo, the slow down is less, but still significant. I also increased the number of timesteps in the test case to 5, to give a longer total simulation time. The total simulation took 7.3 s with C-arrays and 8.72 s with Eigen. We should do some more timing, but in light of these new numbers, perhaps Eigen Tensors are performant enough. Although, in some preliminary testing with ustruct (which in the latest version of main branch uses the custom Array class), Eigen performance is on par with the custom Array performance. |
@aabrown100-git There are two primary goals of converting the
These goals are often conflicting so we need to decide which is more important. I would choose 1) with an acceptable performance hit. Goal 1)
A compiler might also be able to optimize the indexing or we could do our own loop unrolling. Goal 2) We also need to understand how best to use Eigen, how to organize statements that can be optimized. For example, is it better to have one large algebraic statement or is it better to break it up into several statements. |
Problem
mat_models.cpp
andmat_models_carray.h
are basically redundant. They both implement material models, exceptmat_models.cpp
uses customArray
s andmat_models_carray.h
uses native carrays. Currently, theget_pk2cc_dev()
function frommat_models.cpp
is used forustruct
, while theget_pk2cc()
function frommat_models_carray.h
is used forstruct
. Also, there is a lot of redundancy betweenget_pk2cc()
andget_pk2cc_dev()
.In addition, both
Array
s and carrays have drawbacks.Array
s allow to clean syntax, but lead to slower code. carrays are faster, but require clunky syntax for basic operations and are memory unsafe.All of this makes it confusing and leads to extra work when implementing or restructuring materials models.
Solution
mat_models.cpp
andmat_models_carray.h
and use onlyArray
sget_pk2cc()
andget_pk2cc_dev()
Array
s with Eigen arrays.Additional context
This is a useful first step for subsequent restructuring and optimizations of the material models.
Code of Conduct
The text was updated successfully, but these errors were encountered: