SG++-Doxygen-Documentation
Loading...
Searching...
No Matches
benchmark_gridInteraction.cpp

This example can be found under base/examples/benchmark_gridInteraction.cpp.

// Copyright (C) 2008-today The SG++ project
// This file is part of the SG++ project. For conditions of distribution and
// use, please see the copyright notice provided with SG++ or at
// sgpp.sparsegrids.org
// #include <bits/stdc++.h>
#include <chrono>
#include <set>
#include <vector>
void genAllInteractions(std::set<std::set<size_t>> &inter, size_t d) {
std::set<size_t> tmp;
inter.emplace(tmp);
size_t count = static_cast<size_t>(pow(2., static_cast<double>(d)));
for (size_t i = 1; i < count; ++i) {
tmp.clear();
for (size_t j = 0; j < d; ++j) {
if ((i & (1 << j)) > 0) {
tmp.emplace(j);
}
}
inter.emplace(tmp);
}
}
int main() {
std::vector<size_t> dims = {1, 2, 3};
size_t lvl_start = 3;
size_t lvl_end = 5;
for (auto const &d : dims) {
std::set<std::set<size_t>> interactions;
genAllInteractions(interactions, d);
/*std::cout << std::endl << "~~~" << std::endl << std::endl;
for (auto it1 = interactions.begin(); it1 != interactions.end(); ++it1) {
std::cout << "[ ";
for (auto it2 = it1->begin(); it2 != it1->end(); ++it2) {
std::cout << *it2 << " ";
}
std::cout << "]" << std::endl;
}
std::cout << std::endl;*/
for (size_t lvl = lvl_start; lvl <= lvl_end; ++lvl) {
// get info on grid
std::unique_ptr<Grid> grid(Grid::createModLinearGrid(d));
grid->getGenerator().regularInter(lvl, interactions, 0.);
GridStorage &gS = grid->getStorage();
size_t N = gS.getSize();
// Create alpha vector
DataVector alpha(N);
// set all alphas to zero
for (int i = 0; i < static_cast<int>(N); ++i) {
alpha[i] = static_cast<double>(0.);
}
// generate random points
const size_t numberDataPoints = lvl * d * 10;
double **points = new double *[numberDataPoints];
for (size_t i = 0; i < numberDataPoints; i++) {
points[i] = new double[d];
for (size_t j = 0; j < d; j++) {
points[i][j] = rand() / static_cast<double>(RAND_MAX);
}
}
DataVector result(numberDataPoints);
for (unsigned int i = 0; i < (numberDataPoints); ++i) {
result[i] = static_cast<double>(0);
}
DataMatrix dataset(numberDataPoints, d);
for (unsigned int i = 0; i < (numberDataPoints); ++i) {
DataVector temp(d);
for (int j = 0; j < static_cast<int>(d); ++j) {
temp[j] = points[i][j];
}
dataset.setRow(i, temp);
}
// Make the runs; We use 10000 runs of each, to avoid measurement bias
std::cout << "dim: " << d << ", lvl: " << lvl
<< ", no.of grid points: " << N
<< ", no. of data points: " << numberDataPoints << std::endl;
auto start = std::chrono::high_resolution_clock::now();
for (size_t k = 0; k < 10000; ++k) {
// Run and measure time for interaction-based eval
interactions)
->mult(alpha, result);
}
auto finish = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed = finish - start;
std::cout << "\tInteract 10000 runs\t: " << elapsed.count() << std::endl;
// Create new grid
grid = std::unique_ptr<Grid>(Grid::createModLinearGrid(d));
grid->getGenerator().regular(lvl, 0.);
start = std::chrono::high_resolution_clock::now();
for (size_t k = 0; k < 10000; ++k) {
// Run and measure time for regular eval
->mult(alpha, result);
}
finish = std::chrono::high_resolution_clock::now();
elapsed = finish - start;
std::cout << "\tStandard 10000 runs\t: " << elapsed.count() << std::endl;
// Release memory
for (size_t i = 0; i < numberDataPoints; i++) {
delete[] points[i];
}
delete[] points;
}
}
return 0;
}
A class to store two-dimensional data.
Definition DataMatrix.hpp:28
A class to store one-dimensional data.
Definition DataVector.hpp:25
abstract base class for all types grids used in sgpp the class gives pure virtual function definition...
Definition Grid.hpp:191
Generic hash table based storage of grid points.
Definition HashGridStorage.hpp:42
Interface for multiplication with Matrices and .
Definition OperationMultipleEval.hpp:27
virtual void mult(DataVector &alpha, DataVector &result)=0
Multiplication of with vector .
int main()
Definition densityMultiplication.cpp:22
sgpp::datadriven::Dataset dataset
Definition multHPX.cpp:42
std::unique_ptr< sgpp::base::Grid > grid(nullptr)
sgpp::base::DataVector alpha
Definition multHPX.cpp:40
int N
Definition parabola.py:15
i
Definition statsfileInfo.py:101
int j
Definition statsfile2gnuplot.py:87
HashGridStorage GridStorage
Main typedef for GridStorage.
Definition GridStorage.hpp:28
base::OperationMultipleEval * createOperationMultipleEvalInter(base::Grid &grid, base::DataMatrix &dataset, std::set< std::set< size_t > > interactions)
Similar to createOperationMultipleEval, but makes use of interaction terms during evaluation.
Definition BaseOpFactory.cpp:627
base::OperationMultipleEval * createOperationMultipleEval(base::Grid &grid, base::DataMatrix &dataset)
Factory method, returning an OperationMultipleEval for the grid at hand.
Definition BaseOpFactory.cpp:595