2011-01-24 11:56:11 +01:00
|
|
|
#ifndef OPENCV_GPU_SAMPLE_PERFORMANCE_H_
|
|
|
|
#define OPENCV_GPU_SAMPLE_PERFORMANCE_H_
|
|
|
|
|
2011-01-24 11:33:01 +01:00
|
|
|
#include <iostream>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <vector>
|
2012-02-08 12:02:32 +01:00
|
|
|
#include <numeric>
|
2011-01-24 11:33:01 +01:00
|
|
|
#include <string>
|
2011-05-21 20:32:34 +02:00
|
|
|
#include "opencv2/core/core.hpp"
|
2012-01-30 08:20:09 +01:00
|
|
|
#include "opencv2/gpu/gpu.hpp"
|
2011-01-24 11:33:01 +01:00
|
|
|
|
2011-01-25 16:11:01 +01:00
|
|
|
#define TAB " "
|
|
|
|
|
2011-01-25 15:43:54 +01:00
|
|
|
class Runnable
|
2011-01-24 11:33:01 +01:00
|
|
|
{
|
|
|
|
public:
|
2011-04-01 09:31:06 +02:00
|
|
|
explicit Runnable(const std::string& name): name_(name) {}
|
|
|
|
virtual ~Runnable() {}
|
|
|
|
|
|
|
|
const std::string& name() const { return name_; }
|
|
|
|
|
2011-01-24 11:33:01 +01:00
|
|
|
virtual void run() = 0;
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string name_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class TestSystem
|
|
|
|
{
|
|
|
|
public:
|
2011-01-28 08:45:01 +01:00
|
|
|
static TestSystem& instance()
|
2011-01-24 11:33:01 +01:00
|
|
|
{
|
|
|
|
static TestSystem me;
|
2011-01-28 08:45:01 +01:00
|
|
|
return me;
|
2011-01-24 11:33:01 +01:00
|
|
|
}
|
|
|
|
|
2011-04-01 09:31:06 +02:00
|
|
|
void setWorkingDir(const std::string& val) { working_dir_ = val; }
|
2011-02-17 16:25:50 +01:00
|
|
|
const std::string& workingDir() const { return working_dir_; }
|
2011-01-25 16:11:01 +01:00
|
|
|
|
2011-04-01 09:31:06 +02:00
|
|
|
void setTestFilter(const std::string& val) { test_filter_ = val; }
|
2011-02-17 16:25:50 +01:00
|
|
|
const std::string& testFilter() const { return test_filter_; }
|
2011-01-24 11:33:01 +01:00
|
|
|
|
2012-02-16 14:08:22 +01:00
|
|
|
void setNumIters(int num_iters) { num_iters_ = num_iters; }
|
2012-02-08 12:02:32 +01:00
|
|
|
|
2011-02-17 16:25:50 +01:00
|
|
|
void addInit(Runnable* init) { inits_.push_back(init); }
|
|
|
|
void addTest(Runnable* test) { tests_.push_back(test); }
|
2011-01-24 11:33:01 +01:00
|
|
|
void run();
|
|
|
|
|
2011-02-17 16:25:50 +01:00
|
|
|
// It's public because OpenCV callback uses it
|
|
|
|
void printError(const std::string& msg);
|
|
|
|
|
|
|
|
std::stringstream& startNewSubtest()
|
2011-01-26 08:49:56 +01:00
|
|
|
{
|
2011-02-17 16:25:50 +01:00
|
|
|
finishCurrentSubtest();
|
|
|
|
return cur_subtest_description_;
|
2011-01-26 08:49:56 +01:00
|
|
|
}
|
|
|
|
|
2012-02-16 14:08:22 +01:00
|
|
|
bool stop() const { return cur_iter_idx_ >= num_iters_; }
|
2012-02-08 12:02:32 +01:00
|
|
|
|
2011-01-24 11:33:01 +01:00
|
|
|
void cpuOn() { cpu_started_ = cv::getTickCount(); }
|
|
|
|
void cpuOff()
|
|
|
|
{
|
|
|
|
int64 delta = cv::getTickCount() - cpu_started_;
|
2012-02-08 12:02:32 +01:00
|
|
|
cpu_times_.push_back(delta);
|
2012-02-16 14:08:22 +01:00
|
|
|
++cur_iter_idx_;
|
2012-02-08 12:02:32 +01:00
|
|
|
}
|
|
|
|
void cpuComplete()
|
|
|
|
{
|
2012-02-16 14:08:22 +01:00
|
|
|
cpu_elapsed_ += meanTime(cpu_times_);
|
2011-02-17 16:25:50 +01:00
|
|
|
cur_subtest_is_empty_ = false;
|
2012-02-16 14:08:22 +01:00
|
|
|
cur_iter_idx_ = 0;
|
2012-02-08 12:02:32 +01:00
|
|
|
}
|
2011-01-24 11:33:01 +01:00
|
|
|
|
|
|
|
void gpuOn() { gpu_started_ = cv::getTickCount(); }
|
|
|
|
void gpuOff()
|
|
|
|
{
|
|
|
|
int64 delta = cv::getTickCount() - gpu_started_;
|
2012-02-08 12:02:32 +01:00
|
|
|
gpu_times_.push_back(delta);
|
2012-02-16 14:08:22 +01:00
|
|
|
++cur_iter_idx_;
|
2012-02-08 12:02:32 +01:00
|
|
|
}
|
|
|
|
void gpuComplete()
|
|
|
|
{
|
2012-02-16 14:08:22 +01:00
|
|
|
gpu_elapsed_ += meanTime(gpu_times_);
|
2011-02-17 16:25:50 +01:00
|
|
|
cur_subtest_is_empty_ = false;
|
2012-02-16 14:08:22 +01:00
|
|
|
cur_iter_idx_ = 0;
|
2011-01-24 11:33:01 +01:00
|
|
|
}
|
|
|
|
|
2011-10-03 16:05:52 +02:00
|
|
|
bool isListMode() const { return is_list_mode_; }
|
|
|
|
void setListMode(bool value) { is_list_mode_ = value; }
|
|
|
|
|
2011-01-24 11:33:01 +01:00
|
|
|
private:
|
2012-02-16 14:08:22 +01:00
|
|
|
TestSystem():
|
|
|
|
cur_subtest_is_empty_(true), cpu_elapsed_(0),
|
|
|
|
gpu_elapsed_(0), speedup_total_(0.0),
|
|
|
|
num_subtests_called_(0), is_list_mode_(false),
|
|
|
|
num_iters_(10), cur_iter_idx_(0)
|
2012-02-08 12:02:32 +01:00
|
|
|
{
|
2012-02-16 14:08:22 +01:00
|
|
|
cpu_times_.reserve(num_iters_);
|
|
|
|
gpu_times_.reserve(num_iters_);
|
|
|
|
}
|
2011-01-24 11:33:01 +01:00
|
|
|
|
2011-02-17 16:25:50 +01:00
|
|
|
void finishCurrentSubtest();
|
|
|
|
void resetCurrentSubtest()
|
2011-01-25 16:11:01 +01:00
|
|
|
{
|
|
|
|
cpu_elapsed_ = 0;
|
|
|
|
gpu_elapsed_ = 0;
|
2011-02-17 16:25:50 +01:00
|
|
|
cur_subtest_description_.str("");
|
|
|
|
cur_subtest_is_empty_ = true;
|
2012-02-16 14:08:22 +01:00
|
|
|
cur_iter_idx_ = 0;
|
2012-02-08 12:02:32 +01:00
|
|
|
cpu_times_.clear();
|
|
|
|
gpu_times_.clear();
|
2011-01-25 16:11:01 +01:00
|
|
|
}
|
|
|
|
|
2012-02-16 14:08:22 +01:00
|
|
|
double meanTime(const std::vector<int64> &samples);
|
|
|
|
|
2011-01-26 09:12:06 +01:00
|
|
|
void printHeading();
|
|
|
|
void printSummary();
|
2011-02-17 16:25:50 +01:00
|
|
|
void printMetrics(double cpu_time, double gpu_time, double speedup);
|
2011-01-26 09:12:06 +01:00
|
|
|
|
2011-01-26 12:37:54 +01:00
|
|
|
std::string working_dir_;
|
2011-02-17 16:25:50 +01:00
|
|
|
std::string test_filter_;
|
2011-01-26 12:37:54 +01:00
|
|
|
|
2011-01-25 15:43:54 +01:00
|
|
|
std::vector<Runnable*> inits_;
|
|
|
|
std::vector<Runnable*> tests_;
|
2011-01-24 11:33:01 +01:00
|
|
|
|
2011-02-17 16:25:50 +01:00
|
|
|
std::stringstream cur_subtest_description_;
|
|
|
|
bool cur_subtest_is_empty_;
|
2011-01-24 11:33:01 +01:00
|
|
|
|
2011-01-25 09:32:45 +01:00
|
|
|
int64 cpu_started_, cpu_elapsed_;
|
|
|
|
int64 gpu_started_, gpu_elapsed_;
|
|
|
|
|
|
|
|
double speedup_total_;
|
|
|
|
int num_subtests_called_;
|
2011-10-03 16:05:52 +02:00
|
|
|
|
|
|
|
bool is_list_mode_;
|
2012-02-08 12:02:32 +01:00
|
|
|
|
2012-02-16 14:08:22 +01:00
|
|
|
int num_iters_;
|
|
|
|
int cur_iter_idx_;
|
2012-02-08 12:02:32 +01:00
|
|
|
std::vector<int64> cpu_times_;
|
|
|
|
std::vector<int64> gpu_times_;
|
2011-01-24 11:33:01 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-03-01 10:17:22 +01:00
|
|
|
#define GLOBAL_INIT(name) \
|
2011-01-28 08:45:01 +01:00
|
|
|
struct name##_init: Runnable { \
|
2011-01-25 15:43:54 +01:00
|
|
|
name##_init(): Runnable(#name) { \
|
2011-01-28 08:45:01 +01:00
|
|
|
TestSystem::instance().addInit(this); \
|
2011-01-25 15:43:54 +01:00
|
|
|
} \
|
|
|
|
void run(); \
|
|
|
|
} name##_init_instance; \
|
|
|
|
void name##_init::run()
|
|
|
|
|
|
|
|
|
2011-01-26 08:49:56 +01:00
|
|
|
#define TEST(name) \
|
2011-01-28 08:45:01 +01:00
|
|
|
struct name##_test: Runnable { \
|
2011-01-26 08:49:56 +01:00
|
|
|
name##_test(): Runnable(#name) { \
|
2011-01-28 08:45:01 +01:00
|
|
|
TestSystem::instance().addTest(this); \
|
2011-01-26 08:49:56 +01:00
|
|
|
} \
|
|
|
|
void run(); \
|
|
|
|
} name##_test_instance; \
|
|
|
|
void name##_test::run()
|
|
|
|
|
2011-02-17 16:25:50 +01:00
|
|
|
#define SUBTEST TestSystem::instance().startNewSubtest()
|
2012-02-08 12:02:32 +01:00
|
|
|
|
2012-02-16 14:08:22 +01:00
|
|
|
#define CPU_ON \
|
|
|
|
while (!TestSystem::instance().stop()) { \
|
|
|
|
TestSystem::instance().cpuOn()
|
|
|
|
#define CPU_OFF \
|
|
|
|
TestSystem::instance().cpuOff(); \
|
|
|
|
} TestSystem::instance().cpuComplete()
|
|
|
|
|
|
|
|
#define GPU_ON \
|
|
|
|
while (!TestSystem::instance().stop()) { \
|
|
|
|
TestSystem::instance().gpuOn()
|
|
|
|
#define GPU_OFF \
|
|
|
|
TestSystem::instance().gpuOff(); \
|
|
|
|
} TestSystem::instance().gpuComplete()
|
|
|
|
|
|
|
|
// Generates a matrix
|
2011-01-25 15:43:54 +01:00
|
|
|
void gen(cv::Mat& mat, int rows, int cols, int type, cv::Scalar low,
|
|
|
|
cv::Scalar high);
|
|
|
|
|
2011-02-17 16:25:50 +01:00
|
|
|
// Returns abs path taking into account test system working dir
|
2011-01-26 12:37:54 +01:00
|
|
|
std::string abspath(const std::string& relpath);
|
|
|
|
|
2011-02-10 15:33:02 +01:00
|
|
|
#endif // OPENCV_GPU_SAMPLE_PERFORMANCE_H_
|