Skip to content

Commit

Permalink
Prefixed compile-time options with NNP_
Browse files Browse the repository at this point in the history
- Also added underscores.
- Changed IMPROVED_SFD_MEMORY (enabled by default) to
FULL_SFD_MEMORY (disabled by default).
  • Loading branch information
singraber committed Dec 21, 2020
1 parent dd02fc6 commit 8650d24
Show file tree
Hide file tree
Showing 70 changed files with 284 additions and 270 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ network potentials in computational physics and chemistry.
# [Documentation](http://compphysvienna.github.io/n2p2)

This package uses automatic documentation generation via
[Sphinx](http://www.sphinx-doc.org)
[Sphinx](http://www.sphinx-doc.org),
[Breathe](https://breathe.readthedocs.io/en/latest/#) and
[doxygen](http://www.doxygen.nl/). An online version of the documentation which
is automatically updated with the master branch of the repository can be found
Expand Down
2 changes: 1 addition & 1 deletion src/application/nnp-comp2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ int main(int argc, char* argv[])
it != dataset.structures.end(); ++it)
{
it->calculateNeighborList(dataset.getMaxCutoffRadius());
#ifdef NOSFGROUPS
#ifdef NNP_NO_SF_GROUPS
dataset.calculateSymmetryFunctions((*it), useForces);
#else
dataset.calculateSymmetryFunctionGroups((*it), useForces);
Expand Down
4 changes: 2 additions & 2 deletions src/application/nnp-dataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ int main(int argc, char* argv[])
it != dataset.structures.end(); ++it)
{
it->calculateNeighborList(dataset.getMaxCutoffRadius());
#ifdef NOSFGROUPS
#ifdef NNP_NO_SF_GROUPS
dataset.calculateSymmetryFunctions((*it), useForces);
#else
dataset.calculateSymmetryFunctionGroups((*it), useForces);
Expand Down Expand Up @@ -246,7 +246,7 @@ int main(int argc, char* argv[])
it2->dEdG.clear();
vector<double>(it2->dEdG).swap(it2->dEdG);

#ifndef IMPROVED_SFD_MEMORY
#ifdef NNP_FULL_SFD_MEMORY
it2->dGdxia.clear();
vector<double>(it2->dGdxia).swap(it2->dGdxia);
#endif
Expand Down
4 changes: 2 additions & 2 deletions src/application/nnp-scaling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ int main(int argc, char* argv[])
it != dataset.structures.end(); ++it)
{
it->calculateNeighborList(dataset.getMaxCutoffRadius());
#ifdef NOSFGROUPS
#ifdef NNP_NO_SF_GROUPS
dataset.calculateSymmetryFunctions((*it), useForces);
#else
dataset.calculateSymmetryFunctionGroups((*it), useForces);
Expand All @@ -105,7 +105,7 @@ int main(int argc, char* argv[])
it2->dEdG.clear();
vector<double>(it2->dEdG).swap(it2->dEdG);

#ifndef IMPROVED_SFD_MEMORY
#ifdef NNP_FULL_SFD_MEMORY
it2->dGdxia.clear();
vector<double>(it2->dGdxia).swap(it2->dGdxia);
#endif
Expand Down
4 changes: 2 additions & 2 deletions src/application/nnp-sfclust.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ int main(int argc, char* argv[])
it != dataset.structures.end(); ++it)
{
it->calculateNeighborList(dataset.getMaxCutoffRadius());
#ifdef NOSFGROUPS
#ifdef NNP_NO_SF_GROUPS
dataset.calculateSymmetryFunctions((*it), useForces);
#else
dataset.calculateSymmetryFunctionGroups((*it), useForces);
Expand All @@ -112,7 +112,7 @@ int main(int argc, char* argv[])
it2->dEdG.clear();
vector<double>(it2->dEdG).swap(it2->dEdG);

#ifndef IMPROVED_SFD_MEMORY
#ifdef NNP_FULL_SFD_MEMORY
it2->dGdxia.clear();
vector<double>(it2->dGdxia).swap(it2->dGdxia);
#endif
Expand Down
66 changes: 40 additions & 26 deletions src/doc/sphinx/source/topics/build.rst
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ time:
Symmetry function groups
^^^^^^^^^^^^^^^^^^^^^^^^

**Flag:** ``-DNOSFGROUPS`` (default: *disabled*)
**Flag:** ``-DNNP_NO_SF_GROUPS`` (default: *disabled*)

If this flag is set the symmetry function group feature will be disabled
everywhere. This will result in a much worse performance but may be useful for
Expand All @@ -170,18 +170,31 @@ will not change results, please see details in this publication [1]_.
Improved symmetry function derivative memory
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

**Flag:** ``-DIMPROVED_SFD_MEMORY`` (default: *enabled*)

Reduces the memory usage for symmetry function derivatives when multiple
elements are present. The actual reduction depends on the symmetry function
setup, as a rough estimate expect about 30 to 50% reduction. This feature is
particularly useful for training of large data sets when symmetry function
derivatives are stored in memory (keyword ``memorize_symfunc_results``). An
additional section in the :ref:`libnnp <libnnp>` output will displayed after
the ``SETUP: SYMMETRY FUNCTIONS`` section, which indicates the amount of
still required memory for symmetry function derivatives. Here is how the
output looks like for the RPBE-D3 water example
(``examples/nnp-predict/H2O_RPBE-D3``):
**Flag:** ``-DNNP_FULL_SFD_MEMORY`` (default: *disabled*)

By default *n2p2* reduces the memory usage when multiple elements are present by
eliminating storage for symmetry function derivatives which are zero by
definition. This happens whenever a symmetry function is only sensitive to
neighbors of certain (and not all) elements. Then, there is no space required
for derivatives with respect to neighbors of all other elements and hence a
significant amount of memory allocation can be avoided. The actual benefit
depends on the symmetry function setup, as a rough estimate expect about 30 to
50% reduction. This feature is particularly useful for training of large data
sets when symmetry function derivatives are stored in memory (keyword
``memorize_symfunc_results``).

However, for debugging and development purposes it can be helpful to keep the
naive, full symmetry function derivative memory allocation. This can be achieved
by enabling the flag ``-DNNP_FULL_SFD_MEMORY``. Only in this case there is a
one-to-one correspondance between the list of symmetry functions in the
:ref:`libnnp <libnnp>` output and the symmetry function derivative vectors in
:cpp:member:`nnp::Atom::Neighbor::dGdr`.

Normally, i.e. when ``-DNNP_FULL_SFD_MEMORY`` is **disabled**, an additional
section in the :ref:`libnnp <libnnp>` output will displayed after the ``SETUP:
SYMMETRY FUNCTIONS`` section, which indicates the amount of still required
memory for symmetry function derivatives. Here is how the output looks like for
the RPBE-D3 water example (``examples/nnp-predict/H2O_RPBE-D3``):

.. code-block:: none
Expand All @@ -204,24 +217,25 @@ output looks like for the RPBE-D3 water example
Benchmarking the training program and the LAMMPS interface with the same
system gives the following results:

+---------------------------------+-------------+-----------+------------+
| ``-DIMPROVED_SFD_MEMORY`` | *disabled* | *enabled* | difference |
+=================================+=============+===========+============+
| Training (memory) | 55.2 GB | 37.8 GB | -31.5 % |
+---------------------------------+-------------+-----------+------------+
| MD with LAMMPS (memory) | 725.6 MB | 500.0 MB | -31.1 % |
+---------------------------------+-------------+-----------+------------+
| MD with LAMMPS (speed) | 33.82 s | 34.14 s | +0.9 % |
+---------------------------------+-------------+-----------+------------+
+---------------------------------+-------------+------------+------------+
| ``-DNNP_FULL_SFD_MEMORY`` | *enabled* | *disabled* | difference |
+=================================+=============+============+============+
| Training (memory) | 55.2 GB | 37.8 GB | -31.5 % |
+---------------------------------+-------------+------------+------------+
| MD with LAMMPS (memory) | 725.6 MB | 500.0 MB | -31.1 % |
+---------------------------------+-------------+------------+------------+
| MD with LAMMPS (speed) | 33.82 s | 34.14 s | +0.9 % |
+---------------------------------+-------------+------------+------------+

Given the significant reduction in memory and the negligible impact on speed
this feature is enabled by default.
the improved memory layout is used by default (``-DNNP_FULL_SFD_MEMORY``
disabled).

.. important::

If you prefer to disable this functionality please also remove the flag
``-DIMPROVED_SFD_MEMORY`` from the LAMMPS makefile in
``src/libnnpif/Makefile.lammps``!
If you need the full symmetry function derivative memory together with the
LAMMPS interface add ``-DNNP_FULL_SFD_MEMORY`` to the ``nnp_SYSINC =``
variable in the LAMMPS makefile in ``src/libnnpif/LAMMPS/Makefile.lammps``!

.. [1] Singraber, A.; Behler, J.; Dellago, C. Library-Based LAMMPS
Implementation of High-Dimensional Neural Network Potentials. J. Chem. Theory
Expand Down
28 changes: 14 additions & 14 deletions src/libnnp/Atom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Atom::Atom() : hasNeighborList (false),
{
}

#ifndef IMPROVED_SFD_MEMORY
#ifdef NNP_FULL_SFD_MEMORY
void Atom::collectDGdxia(size_t indexAtom, size_t indexComponent)
{
for (size_t i = 0; i < dGdxia.size(); i++)
Expand Down Expand Up @@ -78,7 +78,7 @@ void Atom::toNormalizedUnits(double convEnergy, double convLength)
{
dEdG.at(i) *= convEnergy;
dGdr.at(i) /= convLength;
#ifndef IMPROVED_SFD_MEMORY
#ifdef NNP_FULL_SFD_MEMORY
dGdxia.at(i) /= convLength;
#endif
}
Expand Down Expand Up @@ -116,7 +116,7 @@ void Atom::toPhysicalUnits(double convEnergy, double convLength)
{
dEdG.at(i) /= convEnergy;
dGdr.at(i) *= convLength;
#ifndef IMPROVED_SFD_MEMORY
#ifdef NNP_FULL_SFD_MEMORY
dGdxia.at(i) *= convLength;
#endif
}
Expand Down Expand Up @@ -153,14 +153,14 @@ void Atom::allocate(bool all)
// Clear all symmetry function related vectors (also for derivatives).
G.clear();
dEdG.clear();
#ifndef IMPROVED_SFD_MEMORY
#ifdef NNP_FULL_SFD_MEMORY
dGdxia.clear();
#endif
dGdr.clear();
for (vector<Neighbor>::iterator it = neighbors.begin();
it != neighbors.end(); ++it)
{
#ifndef NOSFCACHE
#ifndef NNP_NO_SF_CACHE
it->cache.clear();
#endif
it->dGdr.clear();
Expand All @@ -174,29 +174,29 @@ void Atom::allocate(bool all)
G.resize(numSymmetryFunctions, 0.0);
if (all)
{
#ifdef IMPROVED_SFD_MEMORY
#ifndef NNP_FULL_SFD_MEMORY
if (numSymmetryFunctionDerivatives.size() == 0)
{
throw range_error("ERROR: Number of symmetry function derivatives"
" unset, cannot allocate.\n");
}
#endif
dEdG.resize(numSymmetryFunctions, 0.0);
#ifndef IMPROVED_SFD_MEMORY
#ifdef NNP_FULL_SFD_MEMORY
dGdxia.resize(numSymmetryFunctions, 0.0);
#endif
dGdr.resize(numSymmetryFunctions);
}
for (vector<Neighbor>::iterator it = neighbors.begin();
it != neighbors.end(); ++it)
{
#ifndef NOSFCACHE
#ifndef NNP_NO_SF_CACHE
it->cache.resize(cacheSizePerElement.at(it->element),
-numeric_limits<double>::max());
#endif
if (all)
{
#ifdef IMPROVED_SFD_MEMORY
#ifndef NNP_FULL_SFD_MEMORY
it->dGdr.resize(numSymmetryFunctionDerivatives.at(it->element));
#else
it->dGdr.resize(numSymmetryFunctions);
Expand All @@ -214,7 +214,7 @@ void Atom::free(bool all)
G.clear();
vector<double>(G).swap(G);
hasSymmetryFunctions = false;
#ifndef NOSFCACHE
#ifndef NNP_NO_SF_CACHE
for (vector<Neighbor>::iterator it = neighbors.begin();
it != neighbors.end(); ++it)
{
Expand All @@ -226,7 +226,7 @@ void Atom::free(bool all)

dEdG.clear();
vector<double>(dEdG).swap(dEdG);
#ifndef IMPROVED_SFD_MEMORY
#ifdef NNP_FULL_SFD_MEMORY
dGdxia.clear();
vector<double>(dGdxia).swap(dGdxia);
#endif
Expand Down Expand Up @@ -350,7 +350,7 @@ vector<string> Atom::info() const
{
v.push_back(strpr("%29d : %d\n", i, numSymmetryFunctionDerivatives.at(i)));
}
#ifndef NOSFCACHE
#ifndef NNP_NO_SF_CACHE
v.push_back(strpr("--------------------------------\n"));
v.push_back(strpr("--------------------------------\n"));
v.push_back(strpr("cacheSizePerElement [*] : %d\n", cacheSizePerElement.size()));
Expand All @@ -377,7 +377,7 @@ vector<string> Atom::info() const
v.push_back(strpr("%29d : %16.8E\n", i, dEdG.at(i)));
}
v.push_back(strpr("--------------------------------\n"));
#ifndef IMPROVED_SFD_MEMORY
#ifdef NNP_FULL_SFD_MEMORY
v.push_back(strpr("--------------------------------\n"));
v.push_back(strpr("dGdxia [*] : %d\n", dGdxia.size()));
v.push_back(strpr("--------------------------------\n"));
Expand Down Expand Up @@ -446,7 +446,7 @@ vector<string> Atom::Neighbor::info() const
v.push_back(strpr("d : %16.8E\n", d));
v.push_back(strpr("dr : %16.8E %16.8E %16.8E\n", dr[0], dr[1], dr[2]));
v.push_back(strpr("--------------------------------\n"));
#ifndef NOSFCACHE
#ifndef NNP_NO_SF_CACHE
v.push_back(strpr("cache [*] : %d\n", cache.size()));
v.push_back(strpr("--------------------------------\n"));
for (size_t i = 0; i < cache.size(); ++i)
Expand Down
8 changes: 4 additions & 4 deletions src/libnnp/Atom.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ struct Atom
double d;
/// Distance vector to neighbor atom.
Vec3D dr;
#ifndef NOSFCACHE
#ifndef NNP_NO_SF_CACHE
/// Symmetry function cache (e.g. for cutoffs, compact functions).
std::vector<double> cache;
#endif
Expand Down Expand Up @@ -120,15 +120,15 @@ struct Atom
std::vector<std::size_t> numNeighborsPerElement;
/// Number of neighbor atom symmetry function derivatives per element.
std::vector<std::size_t> numSymmetryFunctionDerivatives;
#ifndef NOSFCACHE
#ifndef NNP_NO_SF_CACHE
/// Cache size for each element.
std::vector<std::size_t> cacheSizePerElement;
#endif
/// Symmetry function values
std::vector<double> G;
/// Derivative of atomic energy with respect to symmetry functions.
std::vector<double> dEdG;
#ifndef IMPROVED_SFD_MEMORY
#ifdef NNP_FULL_SFD_MEMORY
/// Derivative of symmetry functions with respect to one specific atom
/// coordinate.
std::vector<double> dGdxia;
Expand All @@ -142,7 +142,7 @@ struct Atom
/** Atom constructor, initialize to zero.
*/
Atom();
#ifndef IMPROVED_SFD_MEMORY
#ifdef NNP_FULL_SFD_MEMORY
/** Collect derivative of symmetry functions with repect to one atom's
* coordinate.
*
Expand Down
2 changes: 1 addition & 1 deletion src/libnnp/CompactFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ CompactFunction::CompactFunction() : center (0.0 ),
core.setType(CoreFunction::Type::POLY2);
}

#ifndef NOASYMPOLY
#ifndef NNP_NO_ASYM_POLY
bool CompactFunction::getAsymmetric() const
{
return core.getAsymmetric();
Expand Down
2 changes: 1 addition & 1 deletion src/libnnp/CompactFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class CompactFunction
* @return Type used.
*/
CoreFunction::Type getCoreFunctionType() const;
#ifndef NOASYMPOLY
#ifndef NNP_NO_ASYM_POLY
/** Set asymmetric property in core function.
*
* @param[in] asymmetric Whether asymmetry should be activated.
Expand Down
2 changes: 1 addition & 1 deletion src/libnnp/CoreFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ using namespace nnp;
double const CoreFunction::E = exp(1.0);

CoreFunction::CoreFunction() : type (Type::POLY2 ),
#ifndef NOASYMPOLY
#ifndef NNP_NO_ASYM_POLY
asymmetric(false ),
#endif
fPtr (&CoreFunction:: fPOLY2),
Expand Down
Loading

0 comments on commit 8650d24

Please sign in to comment.