| 1 |
// Copyright (C) 2008-2010 Dag Lindbo, Anders Logg and Ilmar Wilbers |
| 2 |
// |
| 3 |
// This file is part of DOLFIN. |
| 4 |
// |
| 5 |
// DOLFIN is free software: you can redistribute it and/or modify |
| 6 |
// it under the terms of the GNU Lesser General Public License as published by |
| 7 |
// the Free Software Foundation, either version 3 of the License, or |
| 8 |
// (at your option) any later version. |
| 9 |
// |
| 10 |
// DOLFIN is distributed in the hope that it will be useful, |
| 11 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
// GNU Lesser General Public License for more details. |
| 14 |
// |
| 15 |
// You should have received a copy of the GNU Lesser General Public License |
| 16 |
// along with DOLFIN. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
// |
| 18 |
// First added: 2008-07-22 |
| 19 |
// Last changed: 2011-09-21 |
| 20 |
|
| 21 |
#include <string> |
| 22 |
#include <vector> |
| 23 |
#include <iostream> |
| 24 |
#include <dolfin.h> |
| 25 |
#include "forms.h" |
| 26 |
|
| 27 |
using namespace dolfin; |
| 28 |
|
| 29 |
double assemble_form(Form& form) |
| 30 |
{ |
| 31 |
// Assemble once |
| 32 |
const double t0 = time(); |
| 33 |
Matrix A; |
| 34 |
Assembler assembler; |
| 35 |
assembler.assemble(A, form); |
| 36 |
return time() - t0; |
| 37 |
} |
| 38 |
|
| 39 |
double reassemble_form(Form& form) |
| 40 |
{ |
| 41 |
// Assemble once |
| 42 |
Matrix A; |
| 43 |
Assembler assembler; |
| 44 |
assembler.assemble(A, form); |
| 45 |
|
| 46 |
// Reassemble |
| 47 |
const double t0 = time(); |
| 48 |
assembler.assemble(A, form); |
| 49 |
return time() - t0; |
| 50 |
} |
| 51 |
|
| 52 |
int main(int argc, char* argv[]) |
| 53 |
{ |
| 54 |
info("Assembly for various forms and backends"); |
| 55 |
set_log_active(false); |
| 56 |
|
| 57 |
// FIXME: Why? |
| 58 |
parameters["reorder_dofs_serial"] = false; |
| 59 |
|
| 60 |
// Forms |
| 61 |
std::vector<std::string> forms; |
| 62 |
forms.push_back("poisson1"); |
| 63 |
forms.push_back("poisson2"); |
| 64 |
forms.push_back("poisson3"); |
| 65 |
forms.push_back("stokes"); |
| 66 |
forms.push_back("stabilization"); |
| 67 |
forms.push_back("elasticity"); |
| 68 |
forms.push_back("navierstokes"); |
| 69 |
|
| 70 |
// Backends |
| 71 |
std::vector<std::string> backends = {"PETSc", "Tpetra", "Eigen"}; |
| 72 |
{ |
| 73 |
for (auto it = backends.begin(); it != backends.end();) |
| 74 |
if (has_linear_algebra_backend(*it)) |
| 75 |
++it; |
| 76 |
else |
| 77 |
it = backends.erase(it); |
| 78 |
} |
| 79 |
|
| 80 |
// Override forms and backends with command-line arguments |
| 81 |
if (argc == 3) |
| 82 |
{ |
| 83 |
forms.clear(); |
| 84 |
forms.push_back(argv[1]); |
| 85 |
backends.clear(); |
| 86 |
backends.push_back(argv[2]); |
| 87 |
} |
| 88 |
else if (argc != 1) |
| 89 |
{ |
| 90 |
std::cout << "Usage: bench [form] [backend]" << std::endl; |
| 91 |
exit(1); |
| 92 |
} |
| 93 |
|
| 94 |
// Tables for results |
| 95 |
Table t0("Assemble total"); |
| 96 |
Table t1("Init dofmap"); |
| 97 |
Table t2("Build sparsity"); |
| 98 |
Table t3("Init tensor"); |
| 99 |
Table t4("Delete sparsity"); |
| 100 |
Table t5("Assemble cells"); |
| 101 |
Table t6("Overhead"); |
| 102 |
Table t7("Reassemble total"); |
| 103 |
|
| 104 |
// Benchmark assembly |
| 105 |
for (unsigned int i = 0; i < forms.size(); i++) |
| 106 |
{ |
| 107 |
std::cout << "Form: " << forms[i] << std::endl; |
| 108 |
for (unsigned int j = 0; j < backends.size(); j++) |
| 109 |
{ |
| 110 |
parameters["linear_algebra_backend"] = backends[j]; |
| 111 |
parameters["timer_prefix"] = backends[j]; |
| 112 |
std::cout << " Backend: " << backends[j] << std::endl; |
| 113 |
const double tt0 = bench_form(forms[i], assemble_form); |
| 114 |
const auto timing1 = timing(backends[j] + t1.name(), TimingClear::clear); |
| 115 |
const auto timing2 = timing(backends[j] + t2.name(), TimingClear::clear); |
| 116 |
const auto timing3 = timing(backends[j] + t3.name(), TimingClear::clear); |
| 117 |
const auto timing4 = timing(backends[j] + t4.name(), TimingClear::clear); |
| 118 |
const auto timing5 = timing(backends[j] + t5.name(), TimingClear::clear); |
| 119 |
const double tt1 |
| 120 |
= std::get<1>(timing1)/static_cast<double>(std::get<0>(timing1)); |
| 121 |
const double tt2 |
| 122 |
= std::get<1>(timing2)/static_cast<double>(std::get<0>(timing2)); |
| 123 |
const double tt3 |
| 124 |
= std::get<1>(timing3)/static_cast<double>(std::get<0>(timing3)); |
| 125 |
const double tt4 |
| 126 |
= std::get<1>(timing4)/static_cast<double>(std::get<0>(timing4)); |
| 127 |
const double tt5 |
| 128 |
= std::get<1>(timing5)/static_cast<double>(std::get<0>(timing5)); |
| 129 |
t0(forms[i], backends[j]) = tt0; |
| 130 |
t1(forms[i], backends[j]) = tt1; |
| 131 |
t2(forms[i], backends[j]) = tt2; |
| 132 |
t3(forms[i], backends[j]) = tt3; |
| 133 |
t4(forms[i], backends[j]) = tt4; |
| 134 |
t5(forms[i], backends[j]) = tt5; |
| 135 |
t6(forms[i], backends[j]) = tt0 - tt1 - tt2 - tt3 - tt4 - tt5; |
| 136 |
std::cout << " BENCH " << forms[i] << "-" << backends[j] << " " << tt0 |
| 137 |
<< std::endl; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
// Benchmark reassembly |
| 142 |
if (argc == 1) |
| 143 |
{ |
| 144 |
for (unsigned int i = 0; i < forms.size(); i++) |
| 145 |
{ |
| 146 |
std::cout << "Form: " << forms[i] << std::endl; |
| 147 |
for (unsigned int j = 0; j < backends.size(); j++) |
| 148 |
{ |
| 149 |
parameters["linear_algebra_backend"] = backends[j]; |
| 150 |
parameters["timer_prefix"] = backends[j]; |
| 151 |
std::cout << " Backend: " << backends[j] << std::endl; |
| 152 |
t7(forms[i], backends[j]) = bench_form(forms[i], reassemble_form); |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
// Display results |
| 158 |
set_log_active(true); |
| 159 |
std::cout << std::endl; info(t0, true); |
| 160 |
std::cout << std::endl; info(t1, true); |
| 161 |
std::cout << std::endl; info(t2, true); |
| 162 |
std::cout << std::endl; info(t3, true); |
| 163 |
std::cout << std::endl; info(t4, true); |
| 164 |
std::cout << std::endl; info(t5, true); |
| 165 |
std::cout << std::endl; info(t6, true); |
| 166 |
if (argc == 1) |
| 167 |
std::cout << std::endl; info(t7, true); |
| 168 |
|
| 169 |
return 0; |
| 170 |
} |