votca 2024.2-dev
Loading...
Searching...
No Matches
gpu_benchmark.cc
Go to the documentation of this file.
1/*
2 * Copyright 2009-2021 The VOTCA Development Team (http://www.votca.org)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18// Standard includes
19#include <chrono>
20#include <execution>
21#include <iomanip>
22#include <numeric>
23#include <string>
24
27#include "votca/xtp/eigen.h"
28#include "votca/xtp/logger.h"
30#include "votca/xtp/orbitals.h"
31#include "votca/xtp/rpa.h"
33
34// Local private VOTCA includes
35#include "gpu_benchmark.h"
36
37namespace votca {
38namespace xtp {
39
41 outputfile_ = options.get("outputfile").as<std::string>();
42 repetitions_ = options.get("repetitions").as<Index>();
43}
44
45std::pair<double, double> CalcStatistics(const std::vector<double>& v) {
46 double mean = std::reduce(v.begin(), v.end()) / ((double)v.size());
47 double sq_sum = std::inner_product(v.begin(), v.end(), v.begin(), 0.0);
48 double stdev = std::sqrt(sq_sum / ((double)v.size()) - mean * mean);
49 return {mean, stdev};
50}
51
52template <class T>
53tools::Property RunPart(T&& payload, const std::string& name,
54 Index repetitions) {
55 std::vector<double> individual_timings;
56 individual_timings.reserve(repetitions);
57 Index count = 0;
58 std::cout << name << std::endl;
59 for (Index i = 0; i < repetitions; i++) {
60 std::chrono::time_point<std::chrono::steady_clock> start =
61 std::chrono::steady_clock::now();
62 count += payload();
63 std::chrono::time_point<std::chrono::steady_clock> end =
64 std::chrono::steady_clock::now();
65 individual_timings.push_back(
66 std::chrono::duration_cast<std::chrono::duration<double> >(end - start)
67 .count());
68 }
69 // this is more to prevent optimisation of the whole loop if the payload is
70 // small
71 if (count != repetitions) {
72 std::cout << "sth went wrong with the count" << std::endl;
73 }
74 auto [mean1, std1] = CalcStatistics(individual_timings);
75 std::cout << "avg:" << mean1 << " std:" << std1 << std::endl;
76 tools::Property output(name, "", "");
77 output.add("avg", std::to_string(mean1));
78 output.add("std", std::to_string(std1));
79 tools::Property& runs = output.add("runs", "");
80 for (double time : individual_timings) {
81 runs.add("timing", std::to_string(time));
82 }
83 return output;
84}
85
87
88 Orbitals orb;
89 orb.ReadFromCpt(job_name_ + ".orb");
90 std::cout << "\n\nCreating benchmark for " << job_name_ << ".orb"
91 << std::endl;
92 AOBasis basis = orb.getDftBasis();
93 AOBasis auxbasis = orb.getAuxBasis();
94
95 std::cout << "Repetitions:" << repetitions_ << std::endl;
96 std::cout << "Number of CPUs:" << OPENMP::getMaxThreads()
97 << " \nNumber gpus:" << OpenMP_CUDA::UsingGPUs() << std::endl;
98 std::cout << "Using MKL for Eigen overload:" << XTP_HAS_MKL_OVERLOAD()
99 << std::endl;
100
101 TCMatrix_gwbse Mmn;
102 Index max_3c = std::max(orb.getBSEcmax(), orb.getGWAmax());
103 Mmn.Initialize(auxbasis.AOBasisSize(), orb.getRPAmin(), max_3c,
104 orb.getRPAmin(), orb.getRPAmax());
105 std::cout << "BasisSet:" << basis.Name() << " size:" << basis.AOBasisSize()
106 << std::endl;
107 std::cout << "AuxBasisSet:" << auxbasis.Name()
108 << " size:" << auxbasis.AOBasisSize() << std::endl;
109 std::cout << "rpamin:" << orb.getRPAmin() << " rpamax:" << orb.getRPAmax()
110 << std::endl;
111
112 tools::Property output("GPU_Benchmark", "", "");
113 output.add("Repetitions", std::to_string(repetitions_));
114 output.add("CPUs", std::to_string(OPENMP::getMaxThreads()));
115
116 output.add("GPUs", std::to_string(OpenMP_CUDA::UsingGPUs()));
117 output.add("MKL_overload", std::to_string(XTP_HAS_MKL_OVERLOAD()));
118 output.add("Basisset", basis.Name());
119 output.add("Basissetsize", std::to_string(basis.AOBasisSize()));
120 output.add("AuxBasisset", auxbasis.Name());
121 output.add("AuxBasissetsize", std::to_string(auxbasis.AOBasisSize()));
122 output.add("rpamin", std::to_string(orb.getRPAmin()));
123 output.add("rpamax", std::to_string(orb.getRPAmax()));
124 output.add(RunPart(
125 [&]() {
126 Mmn.Fill(auxbasis, basis, orb.MOs().eigenvectors());
127 return 1;
128 },
129 "Filling_ThreeCenter", repetitions_));
130
131 Eigen::MatrixXd op =
132 Eigen::MatrixXd::Random(auxbasis.AOBasisSize(), auxbasis.AOBasisSize());
133 output.add(RunPart(
134 [&]() {
136 return 1;
137 },
138 "Multiplication_of_tensor_with_matrix", repetitions_));
139 Mmn.Rebuild();
140 Logger log;
141 RPA rpa(log, Mmn);
142 rpa.configure(orb.getHomo(), orb.getRPAmin(), orb.getRPAmax());
143 Index rpatotal = orb.getRPAmax() - orb.getRPAmin() + 1;
145 orb.MOs().eigenvalues().segment(orb.getRPAmin(), rpatotal));
146 double frequency = 0.5;
147 Eigen::MatrixXd result =
148 Eigen::MatrixXd::Zero(auxbasis.AOBasisSize(), auxbasis.AOBasisSize());
149 output.add(RunPart(
150 [&]() {
151 result += rpa.calculate_epsilon_i(frequency);
152 result += rpa.calculate_epsilon_r(-frequency);
153 return 1;
154 },
155 "RPA_evaluation", repetitions_));
156 frequency += result.sum();
157 Index hqp_size = orb.getBSEcmax() - orb.getBSEvmin() + 1;
158 Eigen::MatrixXd Hqp_fake = Eigen::MatrixXd::Random(hqp_size, hqp_size);
159 Eigen::VectorXd epsilon_inv_fake = result.diagonal();
161 opt.cmax = orb.getBSEcmax();
162 opt.homo = orb.getHomo();
163 opt.qpmin = orb.getGWAmin();
164 opt.rpamin = orb.getRPAmin();
165 opt.vmin = orb.getBSEvmin();
166 SingletOperator_TDA s_op(epsilon_inv_fake, Mmn, Hqp_fake);
167 s_op.configure(opt);
168 TripletOperator_TDA t_op(epsilon_inv_fake, Mmn, Hqp_fake);
169 t_op.configure(opt);
170 SingletOperator_BTDA_B sbtda_op(epsilon_inv_fake, Mmn, Hqp_fake);
171 sbtda_op.configure(opt);
172 HxOperator hx_op(epsilon_inv_fake, Mmn, Hqp_fake);
173 hx_op.configure(opt);
174 Index spacesize = 100; // A searchspace of 100 is pretty decent
175 Eigen::MatrixXd state = Eigen::MatrixXd::Random(s_op.size(), spacesize);
176 Eigen::MatrixXd result_op = Eigen::MatrixXd::Zero(s_op.size(), spacesize);
177
178 output.add(RunPart(
179 [&]() {
180 result_op += s_op * state;
181 return 1;
182 },
183 "SingletOperator_TDA", repetitions_));
184
185 output.add(RunPart(
186 [&]() {
187 result_op += t_op * state;
188 return 1;
189 },
190 "TripletOperator_TDA", repetitions_));
191
192 output.add(RunPart(
193 [&]() {
194 result_op += sbtda_op * state;
195 return 1;
196 },
197 "SingletOperator_BTDA_B", repetitions_));
198
199 output.add(RunPart(
200 [&]() {
201 result_op += hx_op * state;
202 return 1;
203 },
204 "HxOperator", repetitions_));
205 std::ofstream outputfile;
206 outputfile.open(outputfile_);
207 outputfile << output << std::endl;
208 outputfile.close();
209 frequency += result_op.sum();
210 return true;
211}
212
213} // namespace xtp
214} // namespace votca
const Eigen::VectorXd & eigenvalues() const
Definition eigensystem.h:30
const Eigen::MatrixXd & eigenvectors() const
Definition eigensystem.h:33
class to manage program options with xml serialization functionality
Definition property.h:55
Property & add(const std::string &key, const std::string &value)
add a new property to structure
Definition property.cc:108
Property & get(const std::string &key)
get existing property
Definition property.cc:79
T as() const
return value as type
Definition property.h:283
Container to hold Basisfunctions for all atoms.
Definition aobasis.h:42
Index AOBasisSize() const
Definition aobasis.h:46
const std::string & Name() const
Definition aobasis.h:81
void configure(BSEOperator_Options opt)
void ParseOptions(const tools::Property &user_options)
Logger is used for thread-safe output of messages.
Definition logger.h:164
static Index UsingGPUs()
container for molecular orbitals
Definition orbitals.h:46
Index getHomo() const
Definition orbitals.h:68
Index getBSEcmax() const
Definition orbitals.h:290
const AOBasis & getAuxBasis() const
Definition orbitals.h:218
Index getBSEvmin() const
Definition orbitals.h:284
Index getRPAmax() const
Definition orbitals.h:264
Index getGWAmin() const
Definition orbitals.h:249
const tools::EigenSystem & MOs() const
Definition orbitals.h:122
Index getGWAmax() const
Definition orbitals.h:251
void ReadFromCpt(const std::string &filename)
Definition orbitals.cc:692
Index getRPAmin() const
Definition orbitals.h:262
const AOBasis & getDftBasis() const
Definition orbitals.h:208
std::string job_name_
Definition qmtool.h:50
Eigen::MatrixXd calculate_epsilon_i(double frequency) const
Definition rpa.h:47
void setRPAInputEnergies(const Eigen::VectorXd &rpaenergies)
Definition rpa.h:59
Eigen::MatrixXd calculate_epsilon_r(double frequency) const
Definition rpa.h:51
void configure(Index homo, Index rpamin, Index rpamax)
Definition rpa.h:39
void Fill(const AOBasis &auxbasis, const AOBasis &dftbasis, const Eigen::MatrixXd &dft_orbitals)
void Initialize(Index basissize, Index mmin, Index mmax, Index nmin, Index nmax)
void MultiplyRightWithAuxMatrix(const Eigen::MatrixXd &matrix)
Index getMaxThreads()
Definition eigen.h:128
tools::Property RunPart(T &&payload, const std::string &name, Index repetitions)
bool XTP_HAS_MKL_OVERLOAD()
Definition eigen.h:41
std::pair< double, double > CalcStatistics(const std::vector< double > &v)
base class for all analysis tools
Definition basebead.h:33
Eigen::Index Index
Definition types.h:26