Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / clDNN / src / gpu / command_queues_builder.cpp
1 /*
2 // Copyright (c) 2018 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16
17 ///////////////////////////////////////////////////////////////////////////////////////////////////
18 #include "command_queues_builder.h"
19 #include "error_handler.h"
20
21 namespace cldnn { namespace gpu{
22
23     command_queues_builder::command_queues_builder(const cl::Context& context, const cl::Device& device, const cl_platform_id& platform_id)
24         : _context(context)
25         , _device(device)
26         , _platform_id(platform_id)
27         , _priority_mode(cldnn_priority_disabled)
28         , _throttle_mode(cldnn_throttle_disabled)
29     {}
30
31     cl_command_queue_properties command_queues_builder::get_properties()
32     {
33         cl_command_queue_properties ret = ((_profiling ? CL_QUEUE_PROFILING_ENABLE : 0) | (_out_of_order ? CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE : 0));
34         return ret;
35     }
36
37     void command_queues_builder::build()
38     {
39         auto properties = get_properties();
40
41         if (_priority_mode == cldnn_priority_disabled &&
42             _throttle_mode == cldnn_throttle_disabled)
43         {
44             _queue = cl::CommandQueue(_context, _device, properties);
45             return;
46         }
47
48         unsigned cl_queue_priority_value = CL_QUEUE_PRIORITY_MED_KHR;
49
50         switch (_priority_mode)
51         {
52         case cldnn_priority_high:
53             cl_queue_priority_value = CL_QUEUE_PRIORITY_HIGH_KHR;
54             break;
55         case cldnn_priority_low:
56             cl_queue_priority_value = CL_QUEUE_PRIORITY_LOW_KHR;
57             break;
58         default:
59             break;
60         }
61
62         unsigned cl_queue_throttle_value = CL_QUEUE_THROTTLE_MED_KHR;
63
64         switch (_throttle_mode)
65         {
66         case cldnn_throttle_high:
67             cl_queue_throttle_value = CL_QUEUE_THROTTLE_HIGH_KHR;
68             break;
69         case cldnn_throttle_low:
70             cl_queue_throttle_value = CL_QUEUE_THROTTLE_LOW_KHR;
71             break;
72         default:
73             break;
74         }
75
76         cl_int error_code = CL_SUCCESS;
77
78         if (_priority_mode != cldnn_priority_disabled &&
79             _throttle_mode != cldnn_throttle_disabled)
80         {
81             cl_queue_properties properties_low[] = {
82                 CL_QUEUE_PRIORITY_KHR, cl_queue_priority_value,
83                 CL_QUEUE_THROTTLE_KHR, cl_queue_throttle_value,
84                 CL_QUEUE_PROPERTIES, properties,
85                 0 };
86
87             _queue = clCreateCommandQueueWithProperties(
88                 _context.get(),
89                 _device.get(),
90                 properties_low,
91                 &error_code);
92         }
93         else if (_priority_mode != cldnn_priority_disabled)
94         {
95             cl_queue_properties properties_low[] = {
96                 CL_QUEUE_PRIORITY_KHR, cl_queue_priority_value,
97                 CL_QUEUE_PROPERTIES, properties,
98                 0 };
99
100             _queue = clCreateCommandQueueWithProperties(
101                 _context.get(),
102                 _device.get(),
103                 properties_low,
104                 &error_code);
105         }
106         else if (_throttle_mode != cldnn_throttle_disabled)
107         {
108             cl_queue_properties properties_low[] = {
109                 CL_QUEUE_THROTTLE_KHR, cl_queue_throttle_value,
110                 CL_QUEUE_PROPERTIES, properties,
111                 0 };
112
113             _queue = clCreateCommandQueueWithProperties(
114                 _context.get(),
115                 _device.get(),
116                 properties_low,
117                 &error_code);
118         }
119
120         if (error_code != CL_SUCCESS) {
121             CLDNN_ERROR_MESSAGE("Command queues builders", "clCreateCommandQueueWithPropertiesINTEL error " + std::to_string(error_code));
122         }
123     }
124
125     void command_queues_builder::set_priority_mode(cldnn_priority_mode_type priority, bool extension_support)
126     {
127         if (priority != cldnn_priority_disabled && !extension_support)
128         {
129             CLDNN_ERROR_MESSAGE(
130                 "Command queues builders - priority_mode",
131                 "The param priority_mode is set in engine_configuration,\
132                 but cl_khr_priority_hints or cl_khr_create_command_queue\
133                 is not supported by current OpenCL implementation.");
134         }
135         _priority_mode = priority;
136     }
137
138     void command_queues_builder::set_throttle_mode(cldnn_throttle_mode_type throttle, bool extension_support)
139     {
140         if (throttle != cldnn_throttle_disabled && !extension_support)
141         {
142             CLDNN_ERROR_MESSAGE(
143                 "Command queues builders - throttle_mode",
144                 "The param throttle_mode is set in engine_configuration,\
145                 but cl_khr_throttle_hints is not supported by current OpenCL implementation.");
146         }
147         _throttle_mode = throttle;
148     }
149 }
150 }
151