Created
September 6, 2018 18:45
-
-
Save seantalts/e8abe1c002a65dc6f6c5f029759ba35e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define OPENCL_PLATFORM_ID 0 | |
#define OPENCL_DEVICE_ID 0 | |
#define CL_USE_DEPRECATED_OPENCL_1_1_APIS | |
//#define USE_TIMING_OUTPUTS | |
//#include <CL/cl.hpp> | |
#include <Eigen/Dense> | |
#include <stan/math.hpp> | |
#include <vector> | |
#include <iostream> | |
#include <chrono> | |
using Eigen::Matrix; | |
using Eigen::Dynamic; | |
using namespace std; | |
using namespace stan::math; | |
//max ~ 40k * 1k | |
const int CASES = 20000; | |
const int ATTRS = 5000; | |
const int REPEATS = 10; | |
template<typename T> | |
void perf_test(T func, int cases = CASES, int attrs = ATTRS, int repeats = REPEATS) { | |
volatile Matrix<double, Dynamic, 1> y_ = Matrix<double, Dynamic, 1>::Random(cases); | |
volatile double alpha_ = -2; | |
volatile Matrix<double, Dynamic, 1> beta_ = Matrix<double, Dynamic, 1>::Random(attrs); | |
volatile Matrix<double, Dynamic, Dynamic> x_ = Matrix<double, Dynamic, Dynamic>::Random(cases, attrs); | |
volatile double sigma_ = 2; | |
Matrix<double, Dynamic, 1> y = *const_cast<Matrix<double, Dynamic, 1>*>(&y_); | |
Matrix<double, Dynamic, Dynamic> x = *const_cast<Matrix<double, Dynamic, Dynamic>*>(&x_); | |
auto start = std::chrono::steady_clock::now(); | |
for (int i = 0; i < repeats; i++) { | |
var alpha = /*reinterpret_cast<var>*/(alpha_); | |
Matrix<var, Dynamic, 1> beta = *const_cast<Matrix<double, Dynamic, 1>*>(&beta_); | |
var sigma = /*reinterpret_cast<var>*/(sigma_); | |
vector<var> grad_args(beta.data(), beta.data() + beta.size()); | |
grad_args.push_back(alpha); | |
grad_args.push_back(sigma); | |
var target = func(y, x, alpha, beta, sigma); | |
volatile double no_opt = target.val(); | |
vector<double> grad; | |
target.grad(grad_args, grad); | |
for (double val : grad) { | |
no_opt = val; | |
} | |
recover_memory(); | |
} | |
auto end = std::chrono::steady_clock::now(); | |
cout << "Total time of execution: " | |
<< std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() | |
<< "ms" << endl; | |
recover_memory(); | |
ChainableStack::instance().memalloc_.free_all(); | |
} | |
int main() { | |
using Eigen::Matrix; | |
using Eigen::Dynamic; | |
/* | |
cout << "naive implementation:" << endl; | |
perf_test( | |
normal_id_glm_lpdf_basic<false, Matrix<double, Dynamic, 1>, Matrix<double, Dynamic, Dynamic>, var, Matrix<var, Dynamic, 1>, var>); | |
cout << ":" << endl; | |
perf_test( | |
normal_id_glm_lpdf<false, Matrix<double, Dynamic, 1>, Matrix<double, Dynamic, Dynamic>, var, Matrix<var, Dynamic, 1>, var>); | |
cout << "typed implementation with operands_and_partials:" << endl; | |
perf_test(normal_id_glm_lpdf_opt_typed); | |
/*cout << "opt:" << endl; | |
perf_test( | |
normal_id_glm_lpdf_opt<false, Matrix<double, Dynamic, 1>, Matrix<double, Dynamic, Dynamic>, var, Matrix<var, Dynamic, 1>, var>); | |
*/ | |
cout << "library implementation:" << endl; | |
perf_test( | |
normal_id_glm_lpdf<false, Matrix<double, Dynamic, 1>, Matrix<double, Dynamic, Dynamic>, var, Matrix<var, Dynamic, 1>, var>); | |
// cout << "opt_typed:" << endl; | |
// perf_test(normal_id_glm_lpdf_opt_typed); | |
/*cout << "opt:" << endl; | |
perf_test( | |
normal_id_glm_lpdf_opt<false, Matrix<double, Dynamic, 1>, Matrix<double, Dynamic, Dynamic>, var, Matrix<var, Dynamic, 1>, var>); | |
*/ | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment