2 #include <dlprim/opencl_include.hpp> 3 #include <dlprim/definitions.hpp> 19 typedef std::chrono::high_resolution_clock clock_type;
20 typedef std::chrono::time_point<clock_type> time_point_type;
23 Section(
char const *n) : name(n) {}
24 Section(
char const *n,time_point_type t) : name(n), start(t) {}
25 char const *name=
"unknown";
26 time_point_type start;
33 char const *name =
nullptr;
38 void enter(
char const *name)
40 sections_.push_back(
Section(name,std::chrono::high_resolution_clock::now()));
42 sections_.back().parent = sids_.top();
43 sids_.push(sections_.size() - 1);
47 int sid = sids_.top();
48 auto now = clock_type::now();
49 auto diff = now - sections_[sid].start;
50 sections_[sid].time_sec = std::chrono::duration_cast<std::chrono::duration<double> >(diff).count();
62 std::shared_ptr<Data> add_event(
char const *name,
int index=-1,cl::Event *ev =
nullptr)
64 std::shared_ptr<Data> e(
new Data());
68 e->section = sids_.top();
76 std::vector<Section> §ions() {
79 std::vector<std::shared_ptr<Data> > &events() {
84 std::vector<Section> sections_;
85 std::stack<int> sids_;
86 std::vector<std::shared_ptr<Data> > events_;
125 event_(nullptr), events_(nullptr) {}
131 queue_(new cl::CommandQueue(q)),event_(nullptr),events_(nullptr)
138 queue_(new cl::CommandQueue(q)),event_(event),events_(nullptr)
146 queue_(new cl::CommandQueue(q)),event_(nullptr),events_(events)
152 ExecutionContext(cl::CommandQueue
const &q,std::vector<cl::Event> *events,cl::Event *event) :
153 queue_(new cl::CommandQueue(q)),event_(event),events_(events)
157 bool is_cpu_context()
const 165 bool timing_enabled()
const 188 ctx.timing_ = timing_;
198 timing_->enter(name);
221 DLPRIM_CHECK(queue_);
230 cl::Event *
event(
char const *name =
"unknown",
int id = -1)
const 232 if(timing_ && !timing_->cpu_only) {
233 return &timing_->add_event(name,
id,event_)->event;
250 if(queue_ ==
nullptr)
260 if(queue_ ==
nullptr)
269 if(queue_ ==
nullptr)
280 return first_context();
282 return last_context();
283 return middle_context();
287 std::shared_ptr<TimingData> timing_;
288 std::shared_ptr<cl::CommandQueue> queue_;
290 std::vector<cl::Event> *events_;
316 Context(std::string
const &dev_id);
325 Context(cl::Context
const &c,cl::Platform
const &p,cl::Device
const &d);
342 std::string name()
const;
369 bool check_device_extension(std::string
const &name);
372 std::string
const &device_extensions();
380 int estimated_core_count();
395 cl::CommandQueue
make_queue(cl_command_queue_properties props=0)
398 if(!is_cpu_context())
399 q=std::move(cl::CommandQueue(context_,device_,props));
413 void select_opencl_device(
int p,
int d);
414 cl::Platform platform_;
416 cl::Context context_;
418 std::map<std::string,bool> ext_cache_;
427 void operator=(
ExecGuard const &) =
delete;
cl::Device & device()
Get OpenCL device object.
Definition: context.hpp:363
void enter(char const *name) const
Profiling scope enter called by ExecGuard::ExecGuard()
Definition: context.hpp:195
ExecutionContext(cl::CommandQueue const &q)
Create context from cl::CommandQueue, note no events will be waited/signaled.
Definition: context.hpp:130
Definition: context.hpp:424
Definition: context.hpp:22
std::vector< cl::Event > * events() const
Get events to wait for.
Definition: context.hpp:240
ExecutionContext last_context() const
Create context that signals for completion event if needed - use only if you know that more kernels r...
Definition: context.hpp:267
ExecutionContext(cl::CommandQueue const &q, cl::Event *event)
Create a context with a request to signal completion event.
Definition: context.hpp:137
ExecutionContext make_execution_context(cl_command_queue_properties props=0)
Generate ExecutionContext (queue + events)
Definition: context.hpp:404
ContextType
Device used with the context, CPU or OpenCL device.
Definition: context.hpp:305
Definition: context.hpp:31
bool is_opencl_context() const
Returns true if the context was created as OpenCL context.
Definition: context.hpp:353
ExecutionContext(cl::CommandQueue const &q, std::vector< cl::Event > *events)
Create a context with a request to wait for events.
Definition: context.hpp:145
cl::CommandQueue make_queue(cl_command_queue_properties props=0)
Creates a new Command queue for the context with optional properties.
Definition: context.hpp:395
ExecutionContext middle_context() const
Create context does not wait or signals use only if you know that more kernels run before and after...
Definition: context.hpp:258
ExecutionContext()
default constructor - can be used for CPU context
Definition: context.hpp:124
This is main object that represent the pair of OpenCL platform and device all other objects use it...
Definition: context.hpp:302
Class used for benchmarking of the model.
Definition: context.hpp:16
cl::Event * event(char const *name="unknown", int id=-1) const
Get event to signal. Note: name is used for profiling. Such that profiling is enabled profiling conte...
Definition: context.hpp:230
ExecutionContext first_context() const
Create context that waits for event if needed - use only if you know that more kernels are followed...
Definition: context.hpp:248
void leave() const
Profiling scope leave, called by ExecGuard::~ExecGuard()
Definition: context.hpp:203
Mane namespace.
Definition: context.hpp:9
bool is_cpu_context() const
Returns true if the context was created as CPU context.
Definition: context.hpp:348
cl::Context & context()
Get OpenCL context object.
Definition: context.hpp:390
ExecutionContext generate_series_context(size_t id, size_t total) const
Create contexts for multiple enqueues.
Definition: context.hpp:185
cl::CommandQueue & queue() const
Get the command queue. Never call it in non-OpenCL context.
Definition: context.hpp:219
ExecutionContext(cl::CommandQueue const &q, std::vector< cl::Event > *events, cl::Event *event)
Create a context with a request to signal completion event and wait for events.
Definition: context.hpp:152
cl::Platform & platform()
Get OpenCL platform object.
Definition: context.hpp:358
void enable_timing(std::shared_ptr< TimingData > p)
Add benchmarking/traceing object data.
Definition: context.hpp:173
This class is used to pass cl::Events that the kernel should wait for and/or signal event completion...
Definition: context.hpp:121