n2p2 - A neural network potential package
nnp-cutoff.cpp
Go to the documentation of this file.
1// n2p2 - A neural network potential package
2// Copyright (C) 2018 Andreas Singraber (University of Vienna)
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <https://www.gnu.org/licenses/>.
16
17#include "CutoffFunction.h"
18#include "Log.h"
19#include "Stopwatch.h"
20#include "utility.h"
21#include <iostream> // std::cout
22#include <fstream> // std::ofstream
23
24using namespace std;
25using namespace nnp;
26
29ofstream logFile;
33size_t p = 100000000;
34double d = 0.99999 / p;
35double tbf = 0.0;
36double tbdf = 0.0;
37double tbfdf = 0.0;
38double tlf = 0.0;
39double tldf = 0.0;
40double tlfdf = 0.0;
41
42void runTest(bool write)
43{
44 swf.reset();
45 swf.start();
46 for (size_t i = 0; i < p; ++i)
47 {
48 __asm__ __volatile__("");
49 }
50 tlf = swf.stop();
51 if (write) nnplog << strpr(" %10.2E %6.1f ", tlf, tlf / tlf);
52
53 swdf.reset();
54 swdf.start();
55 for (size_t i = 0; i < p; ++i)
56 {
57 __asm__ __volatile__("");
58 }
59 tldf = swdf.stop();
60 if (write) nnplog << strpr(" %10.2E %6.1f ", tldf, tldf / tldf);
61
62 swfdf.reset();
63 swfdf.start();
64 for (size_t i = 0; i < p; ++i)
65 {
66 __asm__ __volatile__("");
67 }
68 tlfdf = swfdf.stop();
69 if (write) nnplog << strpr(" %10.2E %6.1f %10.2E %9.1f\n",
70 tlfdf,
71 tlfdf / tlfdf,
72 tlfdf - (tlf + tldf),
73 100.0 * tlfdf / (tlf + tldf));
74}
75
77{
78 fc.setCutoffType(cutoffType);
79
80 swf.reset();
81 swf.start();
82 for (size_t i = 0; i < p; ++i)
83 {
84 fc.f(i * d);
85 }
86 double tf = swf.stop();
87 if (cutoffType == CutoffFunction::CT_HARD) tbf = tf;
88 nnplog << strpr(" %10.2E %6.1f %6.1f", tf, tf / tlf, tf / tbf);
89
90 swdf.reset();
91 swdf.start();
92 for (size_t i = 0; i < p; ++i)
93 {
94 fc.df(i * d);
95 }
96 double tdf = swdf.stop();
97 if (cutoffType == CutoffFunction::CT_HARD) tbdf = tdf;
98 nnplog << strpr(" %10.2E %6.1f %6.1f", tdf, tdf / tldf, tdf / tbdf);
99
100 swfdf.reset();
101 swfdf.start();
102 for (size_t i = 0; i < p; ++i)
103 {
104 double f;
105 double df;
106 fc.fdf(i * d, f, df);
107 }
108 double tfdf = swfdf.stop();
109 if (cutoffType == CutoffFunction::CT_HARD) tbfdf = tfdf;
110
111 nnplog << strpr(" %10.2E %6.1f %6.1f %10.2E %9.1f\n",
112 tfdf,
113 tfdf / tlfdf,
114 tfdf / tbfdf,
115 tfdf - (tf + tdf),
116 100.0 * tfdf / (tf + tdf));
117}
118
119int main()
120{
121 logFile.open("nnp-cutoff.log");
123
124 nnplog << "-------------------------------------------------------------------------------------------------------------\n";
125 nnplog << "Speed test tool for cutoff functions:\n";
126 nnplog << "-------------------------------------------------------------------------------------------------------------\n";
127 nnplog << "Column f : Time for calling f (no derivatives ).\n";
128 nnplog << "Column df : Time for calling df (only derivatives).\n";
129 nnplog << "Column fdf : Time for calling fdf (f and df at once).\n";
130 nnplog << "Column compL : Time compared to LOOP ONLY.\n";
131 nnplog << "Column compH : Time compared to CT_HARD.\n";
132 nnplog << "Column diff : Time difference between calling f + df (separately) and fdf.\n";
133 nnplog << "Column ratio : Ratio time(fdf) / (time(f) + time(df)) in %.\n";
134 nnplog << "-------------------------------------------------------------------------------------------------------------\n";
135 nnplog << "CutoffType : f [s] compL compH df [s] compL compH fdf[s] compL compH diff [s] ratio [%]\n";
136 nnplog << "-------------------------------------------------------------------------------------------------------------\n";
137
138 // Initialize...
139 runTest(false);
140
141 nnplog << "LOOP ONLY : ";
142 runTest(true);
143
144 fc.setCutoffRadius(1.0);
145
146 nnplog << "CT_HARD : ";
147 runTest(CutoffFunction::CT_HARD);
148
149 nnplog << "CT_COS : ";
151 runTest(CutoffFunction::CT_COS);
152
153 nnplog << "CT_TANHU : ";
154 runTest(CutoffFunction::CT_TANHU);
155
156 nnplog << "CT_TANH : ";
157 runTest(CutoffFunction::CT_TANH);
158
159 nnplog << "CT_EXP : ";
161 runTest(CutoffFunction::CT_EXP);
162
163 nnplog << "CT_POLY1 : ";
165 runTest(CutoffFunction::CT_POLY1);
166
167 nnplog << "CT_POLY2 : ";
169 runTest(CutoffFunction::CT_POLY2);
170
171 nnplog << "CT_POLY3 : ";
173 runTest(CutoffFunction::CT_POLY3);
174
175 nnplog << "CT_POLY4 : ";
177 runTest(CutoffFunction::CT_POLY4);
178
179 logFile.close();
180
181 return 0;
182}
CutoffType
List of available cutoff function types.
double f(double r) const
Cutoff function .
void fdf(double r, double &fc, double &dfc) const
Calculate cutoff function and derivative .
void setCutoffParameter(double const alpha)
Set parameter for polynomial cutoff function (CT_POLY).
void setCutoffType(CutoffType const cutoffType)
Set cutoff type.
double df(double r) const
Derivative of cutoff function .
void setCutoffRadius(double const cutoffRadius)
Set cutoff radius.
Logging class for library output.
Definition: Log.h:34
void registerStreamPointer(std::ofstream *const &streamPointer)
Register new C++ ofstream pointer.
Definition: Log.cpp:91
Implements a simple stopwatch on different platforms.
Definition: Stopwatch.h:39
void reset()
Reset stopwatch (total and loop time zero, clock not running).
Definition: Stopwatch.cpp:76
double stop()
Stop and return total time.
Definition: Stopwatch.cpp:62
void start(bool newLoop=true)
Start the stopwatch.
Definition: Stopwatch.cpp:40
Definition: Atom.h:28
string strpr(const char *format,...)
String version of printf function.
Definition: utility.cpp:90
Stopwatch swf
Definition: nnp-cutoff.cpp:30
double tbf
Definition: nnp-cutoff.cpp:35
Log nnplog
Definition: nnp-cutoff.cpp:28
double tlfdf
Definition: nnp-cutoff.cpp:40
double tbfdf
Definition: nnp-cutoff.cpp:37
double tbdf
Definition: nnp-cutoff.cpp:36
void runTest(bool write)
Definition: nnp-cutoff.cpp:42
double tlf
Definition: nnp-cutoff.cpp:38
size_t p
Definition: nnp-cutoff.cpp:33
double d
Definition: nnp-cutoff.cpp:34
double tldf
Definition: nnp-cutoff.cpp:39
CutoffFunction fc
Definition: nnp-cutoff.cpp:27
Stopwatch swdf
Definition: nnp-cutoff.cpp:31
int main()
Definition: nnp-cutoff.cpp:119
Stopwatch swfdf
Definition: nnp-cutoff.cpp:32
ofstream logFile
Definition: nnp-cutoff.cpp:29