Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / clDNN / tests / test_cases / command_queue_test.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 <api/CPP/topology.hpp>
19 #include <api/CPP/network.hpp>
20 #include <api/CPP/engine.hpp>
21 #include <api/CPP/input_layout.hpp>
22 #include "test_utils/test_utils.h"
23 #include "api/CPP/arg_max_min.hpp"
24
25 using namespace cldnn;
26 using namespace tests;
27 using namespace std;
28
29 #ifdef _WIN32
30 #include <windows.h>
31 #include <WinBase.h>
32 static int                              g_run_once = 1;
33 static int                              g_qpc_availible;
34 static LARGE_INTEGER                    g_qpc_frec;
35
36 // Function for future use to measure performance
37 unsigned long GetMilliseconds(void)
38 {
39     unsigned long ms;
40     LARGE_INTEGER qpc_ticks;
41
42     if (g_run_once) {
43         g_qpc_availible = QueryPerformanceFrequency(&g_qpc_frec);
44         // QPC returns nonzero value if HW supports high resolution perf counter
45         EXPECT_NE(g_qpc_availible, 0); 
46         g_run_once = 0;
47     }
48
49     if (g_qpc_availible) {
50         QueryPerformanceCounter(&qpc_ticks);
51         ms = (unsigned long)(1000.0 * ((double)(qpc_ticks.QuadPart)) / ((double)(g_qpc_frec.QuadPart)));
52     }
53     // back up if High-Resolution Timer is not available
54     else ms = GetTickCount();
55
56     return ms;
57 }
58 #endif
59
60
61 // Run some topology too see if command queue does work correctly
62 // Coppied from arg_max_gpu.base test.
63 void exexute_network(cldnn::engine engine)
64 {
65     //  Input  : 2x3x2x2
66     static const int32_t x_size = 2, y_size = 2, feature_num = 3, batch_num = 2;
67
68     auto input = memory::allocate(engine, { data_types::f32, format::bfyx,{ batch_num, feature_num, x_size , y_size } });
69     topology topology;
70     topology.add(input_layout("input", input.get_layout()));
71     topology.add(arg_max_min("arg_max", "input", arg_max_min::max));
72
73     vector<float> input_vec = {
74         //y0x0 y0x1 y1x0 y1x1
75         /*b0f0*/0.1f, -0.1f, 0.9f,  1.5f,
76         /*b0f1*/0.2f, 0.2f,  -10.f, 5.2f,
77         /*b0f2*/0.2f, 0.2f,  -10.f, 5.2f,
78
79         /*b1f0*/3.f,  0.5f,  7.f,   10.f,
80         /*b1f1*/4.f,  0.5f,  8.f,   8.2f,
81         /*b1f2*/0.2f, 0.2f,  -10.f, 5.2f
82     };
83     set_values(input, input_vec);
84
85     network network(engine, topology);
86
87     network.set_input_data("input", input);
88
89     auto outputs = network.execute();
90
91     EXPECT_EQ(outputs.size(), size_t(1));
92     EXPECT_EQ(outputs.begin()->first, "arg_max");
93
94     auto output = outputs.at("arg_max").get_memory();
95     auto output_ptr = output.pointer<float>();
96     float out_buffer[batch_num];
97     for (uint32_t i = 0; i < batch_num; i++)
98     {
99         out_buffer[i] = get_value<float>(output_ptr, i);
100     }
101     int size = x_size * y_size * feature_num;
102     int index;
103     float value;
104     for (int i = 0; i < batch_num; i++) {
105         EXPECT_GE(out_buffer[i], 0);
106         EXPECT_LT(out_buffer[i], size);
107         index = (int)out_buffer[i];
108         value = input_vec[i*size + (int)index];
109         for (int j = 0; j < size; j++)
110         {
111             EXPECT_LE(input_vec[i*size + j], value);
112         }
113     }
114 }
115
116 TEST(command_queue_test, test_priority_hints) {
117     engine_configuration configuration =
118         engine_configuration(
119             false,          // profiling
120             false,          // decorate_kernel_names
121             false,          // dump_custom_program
122             "",             // options
123             "",             // single_kernel
124             true,           // primitives_parallelisation
125             "",             // engine_log
126             "",             // sources_dumps_dir
127             priority_mode_types::low,
128             throttle_mode_types::disabled);
129     cldnn::engine engine(configuration);
130     exexute_network(engine);
131 }
132
133 TEST(command_queue_test, test_throttle_hints) {
134     engine_configuration configuration =
135         engine_configuration(
136             false,          // profiling
137             false,          // decorate_kernel_names
138             false,          // dump_custom_program
139             "",             // options
140             "",             // single_kernel
141             true,           // primitives_parallelisation
142             "",             // engine_log
143             "",             // sources_dumps_dir
144             priority_mode_types::disabled,
145             throttle_mode_types::high);
146     cldnn::engine engine(configuration);
147     exexute_network(engine);
148 }
149
150 TEST(command_queue_test, test_priority_and_throttle_hints) {
151     engine_configuration configuration =
152         engine_configuration(
153             false,          // profiling
154             false,          // decorate_kernel_names
155             false,          // dump_custom_program
156             "",             // options
157             "",             // single_kernel
158             true,           // primitives_parallelisation
159             "",             // engine_log
160             "",             // sources_dumps_dir
161             priority_mode_types::high,
162             throttle_mode_types::low);
163     cldnn::engine engine(configuration);
164     exexute_network(engine);
165 }