summaryrefslogtreecommitdiffstats
path: root/src/opencl/qclplatform.cpp
blob: 46645760ac0da214f2441c1f2b50e03c4d4f948e (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
/****************************************************************************
**
** 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 "qclplatform.h"
#include "qclext_p.h"
#include <QtCore/qvarlengtharray.h>
#include <QtCore/qdebug.h>

QT_BEGIN_NAMESPACE

/*!
    \class QCLPlatform
    \brief The QCLPlatform class represents an OpenCL platform definition.
    \since 4.7
    \ingroup opencl

    An OpenCL platform consists of the host CPU plus one or more
    devices, and manages memory resources and executable kernels.

    The platforms() function can be used to obtain the list of
    OpenCL platforms that are accessible to the host.  For each
    platform, QCLDevice::devices() can be used to enumerate the
    devices that are managed by the platform.

    QCLPlatform functions can be used to query information about
    the platform:

    \list
    \o profile() - describes the level of OpenCL support that
       is available; either \c{FULL_PROFILE} or \c{EMBEDDED_PROFILE}.
       The isFullProfile() and isEmbeddedProfile() convenience
       functions can be used to check for specific profile strings.
    \o version() - version of OpenCL supported by the platform;
       usually something like \c{OpenCL 1.0}.
    \o name() - name of the platform.
    \o vendor() - name of the vendor that created the platform.
    \o extensionSuffix() - the vendor extension suffix if the \c{cl_khr_icd}
       extension is supported; an empty string otherwise.
    \o extensions() - list of OpenCL extensions that are supported
       by the platform.  The hasExtension() function can be used
       to check for a specific extension.
    \endlist

    The \l{Querying OpenCL Device Capabilities}{clinfo} utility
    program can be used to dump all of the platforms that are
    supported by the system's OpenCL implementation.

    \sa QCLDevice
*/

/*!
    \fn QCLPlatform::QCLPlatform()

    Constructs a default OpenCL platform identifier.
*/

/*!
    \fn QCLPlatform::QCLPlatform(cl_platform_id id)

    Constructs an OpenCL platform identifier that corresponds to the
    native OpenCL value \a id.
*/

/*!
    \fn bool QCLPlatform::isNull() const

    Returns true if this OpenCL platform identifier is null.
*/

static QString qt_cl_platform_string(cl_platform_id id, cl_platform_info name)
{
    size_t size;
    if (!id || clGetPlatformInfo(id, name, 0, 0, &size) != CL_SUCCESS)
        return QString();
    QVarLengthArray<char> buf(size);
    clGetPlatformInfo(id, name, size, buf.data(), &size);
    return QString::fromLatin1(buf.data());
}

static bool qt_cl_is_platform
    (cl_platform_id id, cl_platform_info name, const char *str)
{
    size_t len = qstrlen(str);
    size_t size;
    if (!id || clGetPlatformInfo(id, name, 0, 0, &size) != CL_SUCCESS)
        return false;
    if (size <= len)
        return false;
    QVarLengthArray<char> buf(size);
    clGetPlatformInfo(id, name, size, buf.data(), &size);
    if (qstrncmp(buf.constData(), str, len) != 0)
        return false;
    return buf[len] == '\0';
}

/*!
    Returns true if profile() is \c FULL_PROFILE; false otherwise.

    \sa isEmbeddedProfile()
*/
bool QCLPlatform::isFullProfile() const
{
    return qt_cl_is_platform(m_id, CL_PLATFORM_PROFILE, "FULL_PROFILE");
}

/*!
    Returns true if profile() is \c EMBEDDED_PROFILE; false otherwise.

    \sa isFullProfile()
*/
bool QCLPlatform::isEmbeddedProfile() const
{
    return qt_cl_is_platform(m_id, CL_PLATFORM_PROFILE, "EMBEDDED_PROFILE");
}

/*!
    Returns the profile that is implemented by this OpenCL platform,
    usually \c FULL_PROFILE or \c EMBEDDED_PROFILE.

    \sa isFullProfile(), isEmbeddedProfile()
*/
QString QCLPlatform::profile() const
{
    return qt_cl_platform_string(m_id, CL_PLATFORM_PROFILE);
}

/*!
    Returns the OpenCL version that is implemented by this OpenCL platform,
    usually something like \c{OpenCL 1.0}.
*/
QString QCLPlatform::version() const
{
    return qt_cl_platform_string(m_id, CL_PLATFORM_VERSION);
}

/*!
    Returns the name of this OpenCL platform.
*/
QString QCLPlatform::name() const
{
    return qt_cl_platform_string(m_id, CL_PLATFORM_NAME);
}

/*!
    Returns the name of the vendor of this OpenCL platform.
*/
QString QCLPlatform::vendor() const
{
    return qt_cl_platform_string(m_id, CL_PLATFORM_VENDOR);
}

/*!
    Returns the vendor extension suffix for this platform if the
    \c{cl_khr_icd} extension is supported; an empty string otherwise.
*/
QString QCLPlatform::extensionSuffix() const
{
    return qt_cl_platform_string(m_id, CL_PLATFORM_ICD_SUFFIX_KHR);
}

/*!
    Returns a list of the extensions supported by this OpenCL platform.

    \sa hasExtension()
*/
QStringList QCLPlatform::extensions() const
{
    if (!m_id)
        return QStringList();
    return qt_cl_platform_string(m_id, CL_PLATFORM_EXTENSIONS).simplified().split(QChar(' '));
}

// Defined in qcldevice.cpp.
bool qt_cl_has_extension(const char *list, size_t listLen, const char *name);

/*!
    Returns true if this platform has an extension called \a name;
    false otherwise.

    This function is more efficient than checking for \a name
    in the return value from extensions(), if the caller is only
    interested in a single extension.  Use extensions() to check
    for several extensions at once.

    \sa extensions()
*/
bool QCLPlatform::hasExtension(const char *name) const
{
    size_t size;
    if (!m_id || clGetPlatformInfo(m_id, CL_PLATFORM_EXTENSIONS,
                                   0, 0, &size) != CL_SUCCESS)
        return false;
    QVarLengthArray<char> buf(size);
    clGetPlatformInfo(m_id, CL_PLATFORM_EXTENSIONS, size,
                      buf.data(), &size);
    return qt_cl_has_extension(buf.constData(), size, name);
}

/*!
    \fn cl_platform_id QCLPlatform::platformId() const

    Returns the native OpenCL platform identifier for this object.
*/

/*!
    Returns a list of all OpenCL platforms that are supported by this host.
*/
QList<QCLPlatform> QCLPlatform::platforms()
{
    cl_uint size;
    if (clGetPlatformIDs(0, 0, &size) != CL_SUCCESS)
        return QList<QCLPlatform>();
    QVarLengthArray<cl_platform_id> buf(size);
    clGetPlatformIDs(size, buf.data(), &size);
    QList<QCLPlatform> platforms;
    for (int index = 0; index < buf.size(); ++index)
        platforms.append(QCLPlatform(buf[index]));
    return platforms;
}

/*!
    \fn bool QCLPlatform::operator==(const QCLPlatform &other) const

    Returns true if this OpenCL platform identifier is the same
    as \a other; false otherwise.

    \sa operator!=()
*/

/*!
    \fn bool QCLPlatform::operator!=(const QCLPlatform &other) const

    Returns true if this OpenCL platform identifier is not the
    same as \a other; false otherwise.

    \sa operator==()
*/

#ifndef QT_NO_DEBUG_STREAM

QDebug operator<<(QDebug dbg, const QCLPlatform &platform)
{
    dbg.nospace() << "QCLPlatform(" << platform.name() << ')';
    return dbg.space();
}

#endif

QT_END_NAMESPACE