aboutsummaryrefslogtreecommitdiff
path: root/test_conformance/extensions/cl_khr_command_buffer/command_buffer_profiling.cpp
blob: c06bbf762e31cbd42120144d04249ef2b398ca4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
//
// Copyright (c) 2022 The Khronos Group Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#include "basic_command_buffer.h"
#include "procs.h"

#include <vector>

namespace {

////////////////////////////////////////////////////////////////////////////////
// Command-buffer profiling test cases:
// -all commands are recorded to a single command-queue
// -profiling a command-buffer with simultaneous use

template <bool simultaneous_request>
struct CommandBufferProfiling : public BasicCommandBufferTest
{
    CommandBufferProfiling(cl_device_id device, cl_context context,
                           cl_command_queue queue)
        : BasicCommandBufferTest(device, context, queue), wait_event(nullptr)
    {
        simultaneous_use_requested = simultaneous_request;
        if (simultaneous_request) buffer_size_multiplier = 2;
    }

    //--------------------------------------------------------------------------
    bool Skip() override
    {
        if (BasicCommandBufferTest::Skip()) return true;

        Version version = get_device_cl_version(device);
        const cl_device_info host_queue_query = version >= Version(2, 0)
            ? CL_DEVICE_QUEUE_ON_HOST_PROPERTIES
            : CL_DEVICE_QUEUE_PROPERTIES;

        cl_command_queue_properties host_queue_props = 0;
        int error =
            clGetDeviceInfo(device, host_queue_query, sizeof(host_queue_props),
                            &host_queue_props, NULL);
        if (error != CL_SUCCESS)
        {
            print_error(
                error, "clGetDeviceInfo for CL_DEVICE_QUEUE_PROPERTIES failed");
            return true;
        }

        if ((host_queue_props & CL_QUEUE_PROFILING_ENABLE) == 0)
        {
            log_info(
                "Queue property CL_QUEUE_PROFILING_ENABLE not supported \n");
            return true;
        }
        return (simultaneous_use_requested && !simultaneous_use_support);
    }

    //--------------------------------------------------------------------------
    cl_int SetUp(int elements) override
    {
        cl_int error = CL_SUCCESS;
        queue = clCreateCommandQueue(context, device, CL_QUEUE_PROFILING_ENABLE,
                                     &error);
        test_error(error, "clCreateCommandQueue failed");

        return BasicCommandBufferTest::SetUp(elements);
    }

    //--------------------------------------------------------------------------
    cl_int Run() override
    {
        cl_int error = CL_SUCCESS;

        // record command buffer
        error = RecordCommandBuffer();
        test_error(error, "RecordCommandBuffer failed");

        if (simultaneous_use_requested)
        {
            // enqueue simultaneous command-buffers with profiling command queue
            error = RunSimultaneous();
            test_error(error, "RunSimultaneous failed");
        }
        else
        {
            // enqueue single command-buffer with profiling command queue
            error = RunSingle();
            test_error(error, "RunSingle failed");
        }

        return CL_SUCCESS;
    }

    //--------------------------------------------------------------------------
    cl_int RecordCommandBuffer()
    {
        cl_int error = CL_SUCCESS;

        error = clCommandNDRangeKernelKHR(
            command_buffer, nullptr, nullptr, kernel, 1, nullptr, &num_elements,
            nullptr, 0, nullptr, nullptr, nullptr);
        test_error(error, "clCommandNDRangeKernelKHR failed");

        error = clFinalizeCommandBufferKHR(command_buffer);
        test_error(error, "clFinalizeCommandBufferKHR failed");
        return CL_SUCCESS;
    }

    //--------------------------------------------------------------------------
#define ADD_PROF_PARAM(prop)                                                   \
    {                                                                          \
        prop, #prop, 0                                                         \
    }
    struct ProfilingParam
    {
        cl_profiling_info param;
        std::string name;
        cl_ulong value;
    };

    //--------------------------------------------------------------------------
    cl_int VerifyResult(const clEventWrapper& event)
    {
        cl_int error = CL_SUCCESS;
        cl_int status;
        error = clGetEventInfo(event, CL_EVENT_COMMAND_EXECUTION_STATUS,
                               sizeof(status), &status, NULL);
        test_error(error, "clGetEventInfo() failed");

        if (status != CL_SUCCESS)
            test_fail("Kernel execution status %d! (%s:%d)\n", status, __FILE__,
                      __LINE__);

        std::vector<ProfilingParam> prof_params = {
            ADD_PROF_PARAM(CL_PROFILING_COMMAND_QUEUED),
            ADD_PROF_PARAM(CL_PROFILING_COMMAND_SUBMIT),
            ADD_PROF_PARAM(CL_PROFILING_COMMAND_START),
            ADD_PROF_PARAM(CL_PROFILING_COMMAND_END),
        };

        // gather profiling timestamps
        for (auto&& p : prof_params)
        {
            error = clGetEventProfilingInfo(event, p.param, sizeof(p.value),
                                            &p.value, NULL);
            test_error(error, "clGetEventProfilingInfo() failed");
        }

        // verify the results by comparing timestamps
        bool all_vals_0 = prof_params.front().value != 0;
        for (size_t i = 1; i < prof_params.size(); i++)
        {
            all_vals_0 = (prof_params[i].value != 0) ? false : all_vals_0;
            if (prof_params[i - 1].value > prof_params[i].value)
            {
                log_error("Profiling %s=0x%x should be smaller than or equal "
                          "to %s=0x%x for "
                          "kernels that use the on-device queue",
                          prof_params[i - 1].name.c_str(),
                          prof_params[i - 1].param, prof_params[i].name.c_str(),
                          prof_params[i].param);
                return TEST_FAIL;
            }
        }

        if (all_vals_0)
        {
            log_error("All values are 0. This is exceedingly unlikely.\n");
            return TEST_FAIL;
        }

        log_info("Profiling info for command-buffer kernel succeeded.\n");
        return TEST_PASS;
    }

    //--------------------------------------------------------------------------
    cl_int RunSingle()
    {
        cl_int error = CL_SUCCESS;
        std::vector<cl_int> output_data(num_elements);

        error = clEnqueueFillBuffer(queue, in_mem, &pattern, sizeof(cl_int), 0,
                                    data_size(), 0, nullptr, nullptr);
        test_error(error, "clEnqueueFillBuffer failed");

        clEventWrapper query_event;
        error = clEnqueueCommandBufferKHR(0, nullptr, command_buffer, 0,
                                          nullptr, &query_event);
        test_error(error, "clEnqueueCommandBufferKHR failed");

        error = clEnqueueReadBuffer(queue, out_mem, CL_TRUE, 0, data_size(),
                                    output_data.data(), 0, nullptr, nullptr);
        test_error(error, "clEnqueueReadBuffer failed");

        error = VerifyResult(query_event);
        test_error(error, "VerifyResult failed");

        return CL_SUCCESS;
    }

    //--------------------------------------------------------------------------
    struct SimulPassData
    {
        cl_int offset;
        std::vector<cl_int> output_buffer;
        clEventWrapper query_event;
    };

    //--------------------------------------------------------------------------
    cl_int EnqueueSimultaneousPass(SimulPassData& pd)
    {
        cl_int error = clEnqueueFillBuffer(
            queue, out_mem, &pattern, sizeof(cl_int),
            pd.offset * sizeof(cl_int), data_size(), 0, nullptr, nullptr);
        test_error(error, "clEnqueueFillBuffer failed");

        error = clEnqueueFillBuffer(queue, off_mem, &pd.offset, sizeof(cl_int),
                                    0, sizeof(cl_int), 0, nullptr, nullptr);
        test_error(error, "clEnqueueFillBuffer failed");

        if (!wait_event)
        {
            wait_event = clCreateUserEvent(context, &error);
            test_error(error, "clCreateUserEvent failed");
        }

        error = clEnqueueCommandBufferKHR(0, nullptr, command_buffer, 1,
                                          &wait_event, &pd.query_event);
        test_error(error, "clEnqueueCommandBufferKHR failed");

        error = clEnqueueReadBuffer(
            queue, out_mem, CL_FALSE, pd.offset * sizeof(cl_int), data_size(),
            pd.output_buffer.data(), 0, nullptr, nullptr);
        test_error(error, "clEnqueueReadBuffer failed");

        return CL_SUCCESS;
    }

    //--------------------------------------------------------------------------
    cl_int RunSimultaneous()
    {
        cl_int error = CL_SUCCESS;
        cl_int offset = static_cast<cl_int>(num_elements);

        std::vector<SimulPassData> simul_passes = {
            { 0, std::vector<cl_int>(num_elements) },
            { offset, std::vector<cl_int>(num_elements) }
        };

        for (auto&& pass : simul_passes)
        {
            error = EnqueueSimultaneousPass(pass);
            test_error(error, "EnqueueSimultaneousPass failed");
        }

        error = clSetUserEventStatus(wait_event, CL_COMPLETE);
        test_error(error, "clSetUserEventStatus failed");

        error = clFinish(queue);
        test_error(error, "clFinish failed");

        for (auto&& pass : simul_passes)
        {
            error = VerifyResult(pass.query_event);
            test_error(error, "VerifyResult failed");
        }

        return CL_SUCCESS;
    }

    //--------------------------------------------------------------------------
    clEventWrapper wait_event;

    const cl_int pattern = 0xA;
};

} // anonymous namespace

int test_basic_profiling(cl_device_id device, cl_context context,
                         cl_command_queue queue, int num_elements)
{
    return MakeAndRunTest<CommandBufferProfiling<false>>(device, context, queue,
                                                         num_elements);
}

int test_simultaneous_profiling(cl_device_id device, cl_context context,
                                cl_command_queue queue, int num_elements)
{
    return MakeAndRunTest<CommandBufferProfiling<true>>(device, context, queue,
                                                        num_elements);
}