1 //
2 // Copyright 2012 Francisco Jerez
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a
5 // copy of this software and associated documentation files (the "Software"),
6 // to deal in the Software without restriction, including without limitation
7 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 // and/or sell copies of the Software, and to permit persons to whom the
9 // Software is furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 // OTHER DEALINGS IN THE SOFTWARE.
21 //
22
23 #include "core/queue.hpp"
24 #include "core/event.hpp"
25 #include "pipe/p_screen.h"
26 #include "pipe/p_context.h"
27 #include "pipe/p_state.h"
28 #include "util/u_debug.h"
29
30 using namespace clover;
31
32 namespace {
33 void
debug_notify_callback(void * data,unsigned * id,enum util_debug_type type,const char * fmt,va_list args)34 debug_notify_callback(void *data,
35 unsigned *id,
36 enum util_debug_type type,
37 const char *fmt,
38 va_list args) {
39 const command_queue *queue = (const command_queue *)data;
40 char buffer[1024];
41 vsnprintf(buffer, sizeof(buffer), fmt, args);
42 queue->context().notify(buffer);
43 }
44 }
45
command_queue(clover::context & ctx,clover::device & dev,cl_command_queue_properties props)46 command_queue::command_queue(clover::context &ctx, clover::device &dev,
47 cl_command_queue_properties props) :
48 context(ctx), device(dev), _props(props) {
49 pipe = dev.pipe->context_create(dev.pipe, NULL, PIPE_CONTEXT_COMPUTE_ONLY);
50 if (!pipe)
51 throw error(CL_INVALID_DEVICE);
52
53 if (ctx.notify) {
54 struct util_debug_callback cb;
55 memset(&cb, 0, sizeof(cb));
56 cb.debug_message = &debug_notify_callback;
57 cb.data = this;
58 if (pipe->set_debug_callback)
59 pipe->set_debug_callback(pipe, &cb);
60 }
61 }
command_queue(clover::context & ctx,clover::device & dev,std::vector<cl_queue_properties> properties)62 command_queue::command_queue(clover::context &ctx, clover::device &dev,
63 std::vector<cl_queue_properties> properties) :
64 context(ctx), device(dev), _properties(properties), _props(0) {
65
66 for(std::vector<cl_queue_properties>::size_type i = 0; i != properties.size(); i += 2) {
67 if (properties[i] == 0)
68 break;
69 if (properties[i] == CL_QUEUE_PROPERTIES)
70 _props |= properties[i + 1];
71 else if (properties[i] != CL_QUEUE_SIZE)
72 throw error(CL_INVALID_VALUE);
73 }
74
75 pipe = dev.pipe->context_create(dev.pipe, NULL, PIPE_CONTEXT_COMPUTE_ONLY);
76 if (!pipe)
77 throw error(CL_INVALID_DEVICE);
78
79 if (ctx.notify) {
80 struct util_debug_callback cb;
81 memset(&cb, 0, sizeof(cb));
82 cb.debug_message = &debug_notify_callback;
83 cb.data = this;
84 if (pipe->set_debug_callback)
85 pipe->set_debug_callback(pipe, &cb);
86 }
87 }
88
~command_queue()89 command_queue::~command_queue() {
90 pipe->destroy(pipe);
91 }
92
93 void
flush()94 command_queue::flush() {
95 std::lock_guard<std::mutex> lock(queued_events_mutex);
96 flush_unlocked();
97 }
98
99 void
flush_unlocked()100 command_queue::flush_unlocked() {
101 pipe_screen *screen = device().pipe;
102 pipe_fence_handle *fence = NULL;
103
104 if (!queued_events.empty()) {
105 pipe->flush(pipe, &fence, 0);
106
107 while (!queued_events.empty() &&
108 queued_events.front()().signalled()) {
109 queued_events.front()().fence(fence);
110 queued_events.pop_front();
111 }
112
113 screen->fence_reference(screen, &fence, NULL);
114 }
115 }
116
117 void
svm_migrate(const std::vector<void const * > & svm_pointers,const std::vector<size_t> & sizes,cl_mem_migration_flags flags)118 command_queue::svm_migrate(const std::vector<void const*> &svm_pointers,
119 const std::vector<size_t> &sizes,
120 cl_mem_migration_flags flags) {
121 if (!pipe->svm_migrate)
122 return;
123
124 bool to_device = !(flags & CL_MIGRATE_MEM_OBJECT_HOST);
125 bool mem_undefined = flags & CL_MIGRATE_MEM_OBJECT_CONTENT_UNDEFINED;
126 pipe->svm_migrate(pipe, svm_pointers.size(), svm_pointers.data(),
127 sizes.data(), to_device, mem_undefined);
128 }
129
130 cl_command_queue_properties
props() const131 command_queue::props() const {
132 return _props;
133 }
134
135 std::vector<cl_queue_properties>
properties() const136 command_queue::properties() const {
137 return _properties;
138 }
139
140 bool
profiling_enabled() const141 command_queue::profiling_enabled() const {
142 return _props & CL_QUEUE_PROFILING_ENABLE;
143 }
144
145 void
sequence(hard_event & ev)146 command_queue::sequence(hard_event &ev) {
147 std::lock_guard<std::mutex> lock(queued_events_mutex);
148 if (!queued_events.empty())
149 queued_events.back()().chain(ev);
150
151 queued_events.push_back(ev);
152
153 // Arbitrary threshold.
154 // The CTS tends to run a lot of subtests without flushing with the image
155 // tests, so flush regularly to prevent stack overflows.
156 if (queued_events.size() > 1000)
157 flush_unlocked();
158 }
159