summaryrefslogtreecommitdiffstats
path: root/src/opencl/qclkernel.cpp
blob: 58a0db845b0fd4557d35a9a4a2fdb95f6398a719 (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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtOpenCL module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qclkernel.h"
#include "qclprogram.h"
#include "qclbuffer.h"
#include "qclcontext.h"
#include "qclext_p.h"
#include <QtCore/qvarlengtharray.h>
#include <QtCore/qpoint.h>
#include <QtGui/qvector2d.h>
#include <QtGui/qvector3d.h>
#include <QtGui/qvector4d.h>
#include <QtGui/qmatrix4x4.h>
#include <QtGui/qcolor.h>

QT_BEGIN_NAMESPACE

/*!
    \class QCLKernel
    \brief The QCLKernel class represents an executable entry point function in an OpenCL program.
    \since 4.7
    \ingroup opencl

    \section1 Executing kernels

    QCLKernel corresponds to an instance of an OpenCL kernel, decorated
    with a specific globalWorkSize() and localWorkSize().  It is possible
    to use the same OpenCL kernel with different work sizes by altering
    the size for each execution request:

    \code
    QCLKernel kernel = program.createKernel("foo");

    kernel.setGlobalWorkSize(100, 100);
    kernel.setArg(0, a1);
    kernel.setArg(1, b1);
    kernel.run();

    kernel.setGlobalWorkSize(200, 200);
    kernel.setArg(0, a2);
    kernel.setArg(1, b2);
    kernel.run();
    \endcode

    Alternatively, operator()() can be used to avoid the setArg() calls:

    \code
    QCLKernel kernel = program.createKernel("foo");

    kernel.setGlobalWorkSize(100, 100);
    kernel(a1, b1);

    kernel.setGlobalWorkSize(200, 200);
    kernel(a2, b2);
    \endcode

    Up to 10 arguments can be provided to operator()().  Use explicit
    setArg() and run() calls with kernels that have more than
    10 arguments.

    The following types are handled specially via setArg() and operator()():
    \c cl_int, \c cl_uint, \c cl_long, \c cl_ulong, \c float,
    QVector2D, QVector3D, QVector4D, QPointF, QPoint, QMatrix4x4,
    QCLBuffer, QCLImage2D, QCLImage3D, QCLVector, and QCLSampler.
    Other argument types must be set explicitly by calling the
    setArg() override that takes a buffer and size.

    \section1 Asynchronous execution

    Note that both run() and operator()() return immediately;
    they will not block until execution is finished.  Both functions
    return a QCLEvent object that can be used to wait for the
    request to finish:

    \code
    kernel.setGlobalWorkSize(100, 100);
    QCLEvent event = kernel(a1, b1);
    event.waitForFinished();
    \endcode

    Usually it isn't necessary for an explicit QCLEvent wait
    because the next OpenCL request will implicitly block until
    the kernel finishes execution:

    \code
    QCLBuffer buffer = ...;

    kernel.setGlobalWorkSize(100, 100);
    kernel(buffer);

    buffer.read(...);
    \endcode

    With the default in-order command execution policy, OpenCL will ensure
    that the QCLBuffer::read() request will not begin execution until the
    kernel execution finishes.

    \section1 Kernels and QtConcurrent

    QtConcurrent::run() can also be used to execute a kernel:

    \code
    kernel.setGlobalWorkSize(100, 100);
    QFuture<void> future = QtConcurrent::run(kernel, a1, b1);
    future.waitForFinished();
    \endcode

    This will create a background thread on the main CPU to enqueue
    the kernel for execution and to wait for the kernel to complete.

    Only 5 arguments can be passed to a kernel using
    QtConcurrent::run(), which is the same as for regular functions
    and QtConcurrent.  Use explicit setArg() calls and runInThread()
    for kernels with more than 5 arguments.

    Because kernels do not have return values, QtConcurrent::run()
    on a QCLKernel will always return a QFuture<void>.

    The main advantage of QFuture<void> compared to QCLEvent is
    that it can be used with QFutureWatcher to receive signal
    notification of when a kernel completes execution:

    \code
    QFutureWatcher<void> *watcher = new QFutureWatcher<void>(this);
    watcher->setFuture(QtConcurrent::run(kernel, a1, b1));
    connect(watcher, SIGNAL(finished()), this, SLOT(eventFinished()));
    \endcode

    QCLEvent objects can be implicitly converted into a QFuture<void>,
    so any function that returns a QCLEvent can be used with QFutureWatcher.

    \sa QCLProgram, {OpenCL and QtConcurrent}
*/

class QCLKernelPrivate
{
public:
    QCLKernelPrivate(QCLContext *ctx, cl_kernel kid)
        : context(ctx)
        , id(kid)
        , globalWorkSize(1)
        , localWorkSize(0)
    {}
    QCLKernelPrivate(const QCLKernelPrivate *other)
        : context(other->context)
        , id(other->id)
        , globalWorkSize(other->globalWorkSize)
        , localWorkSize(other->localWorkSize)
    {
        if (id)
            clRetainKernel(id);
    }
    ~QCLKernelPrivate()
    {
        if (id)
            clReleaseKernel(id);
    }

    void copy(const QCLKernelPrivate *other)
    {
        context = other->context;
        globalWorkSize = other->globalWorkSize;
        localWorkSize = other->localWorkSize;
        if (id != other->id) {
            if (id)
                clReleaseKernel(id);
            id = other->id;
            if (id)
                clRetainKernel(id);
        }
    }

    QCLContext *context;
    cl_kernel id;
    QCLWorkSize globalWorkSize;
    QCLWorkSize localWorkSize;
};

/*!
    Constructs a null OpenCL kernel object.
*/
QCLKernel::QCLKernel()
    : d_ptr(new QCLKernelPrivate(0, 0)), m_kernelId(0)
{
}

/*!
    Constructs an OpenCL kernel object from the native identifier \a id,
    and associates it with \a context.  This class will take over
    ownership of \a id and release it in the destructor.
*/
QCLKernel::QCLKernel(QCLContext *context, cl_kernel id)
    : d_ptr(new QCLKernelPrivate(context, id)), m_kernelId(id)
{
}

/*!
    Constructs a copy of \a other.
*/
QCLKernel::QCLKernel(const QCLKernel &other)
    : d_ptr(new QCLKernelPrivate(other.d_ptr.data()))
    , m_kernelId(other.m_kernelId)
{
}

/*!
    Releases this OpenCL kernel object.  If this is the last
    reference to the kernel, it will be destroyed.
*/
QCLKernel::~QCLKernel()
{
}

/*!
    Assigns \a other to this object.
*/
QCLKernel &QCLKernel::operator=(const QCLKernel &other)
{
    d_ptr->copy(other.d_ptr.data());
    m_kernelId = other.m_kernelId;
    return *this;
}

/*!
    Returns true if this OpenCL kernel object is null; false otherwise.
*/
bool QCLKernel::isNull() const
{
    Q_D(const QCLKernel);
    return d->id == 0;
}

/*!
    Returns true if this OpenCL kernel object has the same
    identifier as \a other; false otherwise.

    Note: this operator checks for equality solely on kernelId().
    The two objects may have different global and local work sizes.

    \sa operator!=(), kernelId()
*/
bool QCLKernel::operator==(const QCLKernel &other) const
{
    return d_ptr->id == other.d_ptr->id;
}

/*!
    Returns true if this OpenCL kernel object does not have the same
    identifier as \a other; false otherwise.

    \sa operator==(), kernelId()
*/
bool QCLKernel::operator!=(const QCLKernel &other) const
{
    return d_ptr->id != other.d_ptr->id;
}

/*!
    Returns the native OpenCL identifier for this kernel.
*/
cl_kernel QCLKernel::kernelId() const
{
    Q_D(const QCLKernel);
    return d->id;
}

/*!
    Returns the OpenCL context that this kernel was created within.
*/
QCLContext *QCLKernel::context() const
{
    Q_D(const QCLKernel);
    return d->context;
}

/*!
    Returns the OpenCL program that this kernel is associated with.
*/
QCLProgram QCLKernel::program() const
{
    Q_D(const QCLKernel);
    if (!d->id)
        return QCLProgram();
    cl_program prog = 0;
    if (clGetKernelInfo(d->id, CL_KERNEL_PROGRAM,
                        sizeof(prog), &prog, 0) != CL_SUCCESS)
        return QCLProgram();
    return QCLProgram(d->context, prog);
}

/*!
    Returns the name of this OpenCL kernel's entry point function.
*/
QString QCLKernel::name() const
{
    Q_D(const QCLKernel);
    size_t size = 0;
    if (clGetKernelInfo(d->id, CL_KERNEL_FUNCTION_NAME,
                        0, 0, &size) != CL_SUCCESS || !size)
        return QString();
    QVarLengthArray<char> buf(size);
    if (clGetKernelInfo(d->id, CL_KERNEL_FUNCTION_NAME,
                        size, buf.data(), 0) != CL_SUCCESS)
        return QString();
    return QString::fromLatin1(buf.constData(), size);
}

/*!
    Returns the number of arguments that are expected by
    this OpenCL kernel.

    \sa setArg()
*/
int QCLKernel::argCount() const
{
    Q_D(const QCLKernel);
    cl_uint count = 0;
    if (clGetKernelInfo(d->id, CL_KERNEL_NUM_ARGS, sizeof(count), &count, 0)
            != CL_SUCCESS)
        return 0;
    return int(count);
}

/*!
    Returns the work group size that was declared in the kernel's
    source code using a \c{reqd_work_group_size} qualifier.
    Returns (0, 0, 0) if the size is not declared.

    The default device for context() is used to retrieve the
    work group size.
*/
QCLWorkSize QCLKernel::declaredWorkGroupSize() const
{
    Q_D(const QCLKernel);
    size_t sizes[3];
    if (clGetKernelWorkGroupInfo
            (d->id, d->context->defaultDevice().deviceId(),
             CL_KERNEL_COMPILE_WORK_GROUP_SIZE,
             sizeof(sizes), sizes, 0) != CL_SUCCESS)
        return QCLWorkSize(0, 0, 0);
    else
        return QCLWorkSize(sizes[0], sizes[1], sizes[2]);
}

/*!
    \overload

    Returns the work group size that was declared in the kernel's
    source code using a \c{reqd_work_group_size} qualifier.
    Returns (0, 0, 0) if the size is not declared.

    The specified \a device is used to retrieve the work group size.
*/
QCLWorkSize QCLKernel::declaredWorkGroupSize(const QCLDevice &device) const
{
    Q_D(const QCLKernel);
    size_t sizes[3];
    if (clGetKernelWorkGroupInfo
            (d->id, device.deviceId(),
             CL_KERNEL_COMPILE_WORK_GROUP_SIZE,
             sizeof(sizes), sizes, 0) != CL_SUCCESS)
        return QCLWorkSize(0, 0, 0);
    else
        return QCLWorkSize(sizes[0], sizes[1], sizes[2]);
}

/*!
    Returns the global work size for this instance of the kernel.
    The default value is 1.

    \sa setGlobalWorkSize(), localWorkSize()
*/
QCLWorkSize QCLKernel::globalWorkSize() const
{
    Q_D(const QCLKernel);
    return d->globalWorkSize;
}

/*!
    Sets the global work size for this instance of the kernel to \a size.

    \sa globalWorkSize(), setLocalWorkSize()
*/
void QCLKernel::setGlobalWorkSize(const QCLWorkSize &size)
{
    Q_D(QCLKernel);
    d->globalWorkSize = size;
}

/*!
    \fn void QCLKernel::setGlobalWorkSize(size_t width, size_t height)
    \overload

    Sets the global work size for this instance of the kernel to
    \a width x \a height.
*/

/*!
    \fn void QCLKernel::setGlobalWorkSize(size_t width, size_t height, size_t depth)
    \overload

    Sets the global work size for this instance of the kernel to
    \a width x \a height x \a depth.
*/

/*!
    \fn void QCLKernel::setLocalWorkSize(size_t width, size_t height)
    \overload

    Sets the local work size for this instance of the kernel to
    \a width x \a height.
*/

/*!
    \fn void QCLKernel::setLocalWorkSize(size_t width, size_t height, size_t depth)
    \overload

    Sets the local work size for this instance of the kernel to
    \a width x \a height x \a depth.
*/

/*!
    Returns the local work size for this instance of the kernel.
    The default value is 0, which indicates that the local
    work size is not used.

    \sa setLocalWorkSize(), globalWorkSize()
*/
QCLWorkSize QCLKernel::localWorkSize() const
{
    Q_D(const QCLKernel);
    return d->localWorkSize;
}

/*!
    Sets the local work size for this instance of the kernel to \a size.

    \sa localWorkSize(), setGlobalWorkSize()
*/
void QCLKernel::setLocalWorkSize(const QCLWorkSize &size)
{
    Q_D(QCLKernel);
    d->localWorkSize = size;
}

/*!
    Returns the recommended best local work size for 2D image processing
    on this kernel.  Default value is 8x8 unless the maximum work size
    is not large enough to accomodate 8x8 items.

    \sa bestLocalWorkSizeImage3D()
*/
QCLWorkSize QCLKernel::bestLocalWorkSizeImage2D() const
{
    QList<QCLDevice> devices = program().devices();
    size_t maxItems = devices.isEmpty() ? 1 : devices.at(0). maximumWorkItemsPerGroup();
    size_t size = 8;
    while (size > 1 && (size * size) > maxItems)
        size /= 2;
    return QCLWorkSize(size, size);
}

/*!
    Returns the recommended best local work size for 3D image processing
    on this kernel.  Default value is 8x8x8 unless the maximum work size
    is not large enough to accomodate 8x8x8 items.

    \sa bestLocalWorkSizeImage2D()
*/
QCLWorkSize QCLKernel::bestLocalWorkSizeImage3D() const
{
    QList<QCLDevice> devices = program().devices();
    size_t maxItems = devices.isEmpty() ? 1 : devices.at(0). maximumWorkItemsPerGroup();
    size_t size = 8;
    while (size > 1 && (size * size * size) > maxItems)
        size /= 2;
    return QCLWorkSize(size, size, size);
}

/*!
    Returns the preferred work group size multiple, which is a
    performance hint for the local work group size on OpenCL 1.1
    systems.  Returns zero if the system is OpenCL 1.0, or a
    preferred work group size multiple is not available.
*/
size_t QCLKernel::preferredWorkSizeMultiple() const
{
    Q_D(const QCLKernel);
    size_t size;
    if (clGetKernelWorkGroupInfo
            (d->id, d->context->defaultDevice().deviceId(),
             CL_KERNEL_PREFERRED_WORK_GROUP_SIZE_MULTIPLE,
             sizeof(size), &size, 0) != CL_SUCCESS)
        return 0;
    else
        return size;
}

/*!
    \fn void QCLKernel::setArg(int index, cl_int value)

    Sets argument \a index for this kernel to \a value.

    The argument is assumed to have been declared with the
    type \c int.
*/

/*!
    \fn void QCLKernel::setArg(int index, cl_uint value)

    Sets argument \a index for this kernel to \a value.

    The argument is assumed to have been declared with the
    type \c uint.
*/

/*!
    \fn void QCLKernel::setArg(int index, cl_long value)

    Sets argument \a index for this kernel to \a value.

    The argument is assumed to have been declared with the
    type \c long.
*/

/*!
    \fn void QCLKernel::setArg(int index, cl_ulong value)

    Sets argument \a index for this kernel to \a value.

    The argument is assumed to have been declared with the
    type \c ulong.
*/

/*!
    \fn void QCLKernel::setArg(int index, float value)

    Sets argument \a index for this kernel to \a value.

    The argument is assumed to have been declared with the
    type \c float.
*/

/*!
    \fn void QCLKernel::setArg(int index, const QVector2D &value)

    Sets argument \a index for this kernel to \a value.

    The argument is assumed to have been declared with the
    type \c float2.
*/

/*!
    \fn void QCLKernel::setArg(int index, const QVector3D &value)

    Sets argument \a index for this kernel to \a value.

    The argument is assumed to have been declared with the
    type \c float4 (OpenCL does not have a \c float3 type).
    The value will be passed to the kernel as (x, y, z, 1).
*/

/*!
    \fn void QCLKernel::setArg(int index, const QVector4D &value)

    Sets argument \a index for this kernel to \a value.

    The argument is assumed to have been declared with the
    type \c float4.
*/

/*!
    Sets argument \a index for this kernel to \a value.

    The argument is assumed to have been declared with the
    type \c float4 as is passed the red, green, blue, and
    alpha components of \a value as floating-point values
    between 0 and 1.
*/
void QCLKernel::setArg(int index, const QColor &value)
{
    float values[4] =
        {value.redF(), value.greenF(), value.blueF(), value.alphaF()};
    clSetKernelArg(m_kernelId, index, sizeof(values), values);
}

/*!
    Sets argument \a index for this kernel to \a value.

    The argument is assumed to have been declared with the
    type \c float4 as is passed the red, green, blue, and
    alpha components of \a value as floating-point values
    between 0 and 1.
*/
void QCLKernel::setArg(int index, Qt::GlobalColor value)
{
    QColor color(value);
    float values[4] =
        {color.redF(), color.greenF(), color.blueF(), color.alphaF()};
    clSetKernelArg(m_kernelId, index, sizeof(values), values);
}

/*!
    \fn void QCLKernel::setArg(int index, const QPoint &value)

    Sets argument \a index for this kernel to \a value.

    The argument is assumed to have been declared with the
    type \c int2.
*/

/*!
    \fn void QCLKernel::setArg(int index, const QPointF &value)

    Sets argument \a index for this kernel to \a value.

    The argument is assumed to have been declared with the
    type \c float2.
*/

/*!
    Sets argument \a index for this kernel to \a value.

    The argument is assumed to have been declared with the
    type \c float16.
*/
void QCLKernel::setArg(int index, const QMatrix4x4 &value)
{
    if (sizeof(qreal) == sizeof(float)) {
        clSetKernelArg(m_kernelId, index, sizeof(float) * 16, value.constData());
    } else {
        float values[16];
        for (int posn = 0; posn < 16; ++posn)
            values[posn] = float(value.constData()[posn]);
        clSetKernelArg(m_kernelId, index, sizeof(values), values);
    }
}

/*!
    \fn void QCLKernel::setArg(int index, const QCLMemoryObject &value)

    Sets argument \a index for this kernel to \a value.

    The argument is assumed to have been declared with the
    type \c image2d_t, \c image3d_t, or be a pointer to a buffer,
    according to the type of memory object represented by \a value.
*/

/*!
    \fn void QCLKernel::setArg(int index, const QCLVector<T> &value)

    Sets argument \a index for this kernel to \a value.

    The argument is assumed to have been declared as a pointer
    to a buffer.
*/

/*!
    \fn void QCLKernel::setArg(int index, const QCLSampler &value)

    Sets argument \a index for this kernel to \a value.

    The argument is assumed to have been declared with the
    type \c sampler_t.
*/

/*!
    \fn void QCLKernel::setArg(int index, const void *data, size_t size)

    Sets argument \a index to the \a size bytes at \a data.
*/

/*!
    Requests that this kernel instance be run on globalWorkSize() items,
    optionally subdivided into work groups of localWorkSize() items.

    Returns an event object that can be used to wait for the kernel
    to finish execution.  The request is executed on the active
    command queue for context().

    \sa operator()(), runInThread()
*/
QCLEvent QCLKernel::run()
{
    Q_D(const QCLKernel);
    cl_event event;
    cl_int error = clEnqueueNDRangeKernel
        (d->context->activeQueue(), m_kernelId, d->globalWorkSize.dimensions(),
         0, d->globalWorkSize.sizes(),
         (d->localWorkSize.width() ? d->localWorkSize.sizes() : 0),
         0, 0, &event);
    d->context->reportError("QCLKernel::run:", error);
    if (error != CL_SUCCESS)
        return QCLEvent();
    else
        return QCLEvent(event);
}

/*!
    \overload

    Requests that this kernel instance be run on globalWorkSize() items,
    optionally subdivided into work groups of localWorkSize() items.

    If \a after is not an empty list, it indicates the events that must
    be signaled as finished before this kernel instance can begin executing.

    Returns an event object that can be used to wait for the kernel
    to finish execution.  The request is executed on the active
    command queue for context().
*/
QCLEvent QCLKernel::run(const QCLEventList &after)
{
    Q_D(const QCLKernel);
    cl_event event;
    cl_int error = clEnqueueNDRangeKernel
        (d->context->activeQueue(), m_kernelId, d->globalWorkSize.dimensions(),
         0, d->globalWorkSize.sizes(),
         (d->localWorkSize.width() ? d->localWorkSize.sizes() : 0),
         after.size(), after.eventData(), &event);
    d->context->reportError("QCLKernel::run:", error);
    if (error != CL_SUCCESS)
        return QCLEvent();
    else
        return QCLEvent(event);
}

#ifndef QT_NO_CONCURRENT

static void qt_run_kernel
    (cl_kernel kernel, cl_command_queue queue,
     const QCLWorkSize &globalWorkSize, const QCLWorkSize &localWorkSize)
{
    cl_event event;
    cl_int error = clEnqueueNDRangeKernel
        (queue, kernel, globalWorkSize.dimensions(), 0, globalWorkSize.sizes(),
         (localWorkSize.width() ? localWorkSize.sizes() : 0),
         0, 0, &event);
    if (error == CL_SUCCESS) {
        clWaitForEvents(1, &event);
        clReleaseEvent(event);
    } else {
        qWarning() << "QCLKernel::runInThread:" << QCLContext::errorName(error);
    }
    clReleaseKernel(kernel);
    clReleaseCommandQueue(queue);
}

/*!
    Requests that this kernel instance be run on globalWorkSize() items,
    optionally subdivided into work groups of localWorkSize() items.
    The kernel will be enqueued and executed in a background thread.

    Returns a QFuture object that can be used to wait for the kernel
    to finish execution.  The request is executed on the active
    command queue for context().

    Usually runInThread() is called implicitly via QtConcurrent::run():

    \code
    kernel.setGlobalWorkSize(100, 100);
    QFuture<void> future = QtConcurrent::run(kernel, a1, b1);
    future.waitForFinished();
    \endcode

    The kernel object must not be reused until the background
    thread finishes execution of the kernel.  Thus, the following
    code will have unexpected effects:

    \code
    QFuture<void> future1 = QtConcurrent::run(kernel, a1, b1);
    QFuture<void> future2 = QtConcurrent::run(kernel, a2, b2);
    future1.waitForFinished();
    future2.waitForFinished();
    \endcode

    The recommended method to run the same kernel multiple times in a
    background thread is as follows:

    \code
    void runKernelTwice(QCLKernel &kernel)
    {
        kernel(a1, b1);
        kernel(a2, b2).waitForFinished();
    }

    QFuture<void> future = QtConcurrent::run(runKernelTwice, kernel);
    \endcode

    \sa run()
*/
QFuture<void> QCLKernel::runInThread()
{
    Q_D(const QCLKernel);
    cl_kernel kernel = m_kernelId;
    cl_command_queue queue = d->context->activeQueue();
    if (!kernel || !queue)
        return QFuture<void>();
    clRetainKernel(kernel);
    clRetainCommandQueue(queue);
    return QtConcurrent::run
        (qt_run_kernel, kernel, queue, d->globalWorkSize, d->localWorkSize);
}

#endif

/*!
    \fn QCLEvent QCLKernel::operator()()

    Runs this kernel instance with zero arguments.
    Returns an event object that can be used to wait for the
    kernel to finish execution.
*/

/*!
    \fn QCLEvent QCLKernel::operator()(const T1 &arg1)

    Runs this kernel instance with the argument \a arg1.
    Returns an event object that can be used to wait for the
    kernel to finish execution.
*/

/*!
    \fn QCLEvent QCLKernel::operator()(const T1 &arg1, const T2 &arg2)

    Runs this kernel instance with the arguments \a arg1 and \a arg2.
    Returns an event object that can be used to wait for the
    kernel to finish execution.
*/

/*!
    \fn QCLEvent QCLKernel::operator()(const T1 &arg1, const T2 &arg2, const T3 &arg3)

    Runs this kernel instance with the arguments \a arg1, \a arg2,
    and \a arg3.  Returns an event object that can be used to wait for the
    kernel to finish execution.
*/

/*!
    \fn QCLEvent QCLKernel::operator()(const T1 &arg1, const T2 &arg2, const T3 &arg3, const T4 &arg4)

    Runs this kernel instance with the arguments \a arg1, \a arg2,
    \a arg3, and \a arg4.  Returns an event object that can be used to
    wait for the kernel to finish execution.
*/

/*!
    \fn QCLEvent QCLKernel::operator()(const T1 &arg1, const T2 &arg2, const T3 &arg3, const T4 &arg4, const T5 &arg5)

    Runs this kernel instance with the arguments \a arg1, \a arg2,
    \a arg3, \a arg4, and \a arg5.  Returns an event object that can be
    used to wait for the kernel to finish execution.
*/

/*!
    \fn QCLEvent QCLKernel::operator()(const T1 &arg1, const T2 &arg2, const T3 &arg3, const T4 &arg4, const T5 &arg5, const T6 &arg6)

    Runs this kernel instance with the arguments \a arg1, \a arg2,
    \a arg3, \a arg4, \a arg5, and \a arg6.  Returns an event object that
    can be used to wait for the kernel to finish execution.
*/

/*!
    \fn QCLEvent QCLKernel::operator()(const T1 &arg1, const T2 &arg2, const T3 &arg3, const T4 &arg4, const T5 &arg5, const T6 &arg6, const T7 &arg7)

    Runs this kernel instance with the arguments \a arg1, \a arg2,
    \a arg3, \a arg4, \a arg5, \a arg6, and \a arg7.  Returns an event
    object that can be used to wait for the kernel to finish execution.
*/

/*!
    \fn QCLEvent QCLKernel::operator()(const T1 &arg1, const T2 &arg2, const T3 &arg3, const T4 &arg4, const T5 &arg5, const T6 &arg6, const T7 &arg7, const T8 &arg8)

    Runs this kernel instance with the arguments \a arg1, \a arg2,
    \a arg3, \a arg4, \a arg5, \a arg6, \a arg7, and \a arg8.  Returns
    an event object that can be used to wait for the kernel to finish
    execution.
*/

/*!
    \fn QCLEvent QCLKernel::operator()(const T1 &arg1, const T2 &arg2, const T3 &arg3, const T4 &arg4, const T5 &arg5, const T6 &arg6, const T7 &arg7, const T8 &arg8, const T9 &arg9)

    Runs this kernel instance with the arguments \a arg1, \a arg2,
    \a arg3, \a arg4, \a arg5, \a arg6, \a arg7, \a arg8, and \a arg9.
    Returns an event object that can be used to wait for the kernel
    to finish execution.
*/

/*!
    \fn QCLEvent QCLKernel::operator()(const T1 &arg1, const T2 &arg2, const T3 &arg3, const T4 &arg4, const T5 &arg5, const T6 &arg6, const T7 &arg7, const T8 &arg8, const T9 &arg9, const T10 &arg10)

    Runs this kernel instance with the arguments \a arg1, \a arg2,
    \a arg3, \a arg4, \a arg5, \a arg6, \a arg7, \a arg8, \a arg9,
    and \a arg10.  Returns an event object that can be used to wait
    for the kernel to finish execution.
*/

QT_END_NAMESPACE