source: CLRX/CLRadeonExtender/trunk/samples/CLUtils.h @ 3411

Last change on this file since 3411 was 3411, checked in by matszpk, 2 years ago

CLRadeonExtender: Commenting samples and CLRXWrapper.

File size: 2.7 KB
Line 
1/*
2 *  CLRadeonExtender - Unofficial OpenCL Radeon Extensions Library
3 *  Copyright (C) 2014-2017 Mateusz Szpakowski
4 *
5 *  This library is free software; you can redistribute it and/or
6 *  modify it under the terms of the GNU Lesser General Public
7 *  License as published by the Free Software Foundation; either
8 *  version 2.1 of the License, or (at your option) any later version.
9 *
10 *  This library is distributed in the hope that it will be useful,
11 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 *  Lesser General Public License for more details.
14 *
15 *  You should have received a copy of the GNU Lesser General Public
16 *  License along with this library; if not, write to the Free Software
17 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18 */
19
20#ifndef __CLRXSAMPLES_CLUTILS_H__
21#define __CLRXSAMPLES_CLUTILS_H__
22
23#include <cstdio>
24#include <string>
25#include <vector>
26#include <exception>
27#include <CL/cl.h>
28
29/// error class based on std::exception
30class CLError: public std::exception
31{
32private:
33    cl_int error;
34    std::string description;
35public:
36    /// empty constructor
37    CLError() : error(0)
38    { }
39    explicit CLError(const char* _description) : error(0), description(_description)
40    { }
41    CLError(cl_int _error, const char* _description) : error(_error)
42    {
43        char buf[20];
44        ::snprintf(buf, 20, "%d", _error);
45        description = "Error code: ";
46        description += buf;
47        description += ", Desc: ";
48        description += _description;
49    }
50    virtual ~CLError() noexcept
51    { }
52    const char* what() const noexcept
53    { return (!description.empty()) ? description.c_str() : "No error!"; }
54    int code() const
55    { return error; }
56};
57
58class CLFacade
59{
60protected:
61    cl_device_id device;
62    cl_context context;
63    cl_command_queue queue;
64    cl_program program;
65    // mem object to retain and release while destructing
66    std::vector<cl_mem> memObjects;
67    std::vector<cl_kernel> kernels;
68   
69    cl_uint computeUnits;
70    size_t maxWorkGroupSize;
71   
72    void getKernelInfo(cl_kernel kernel, size_t& workGroupSize,
73               size_t& workGroupSizeMultiple);
74   
75    void callNDRangeKernel(cl_kernel kernel, cl_uint workDim, const size_t* offset,
76               const size_t* workSize, const size_t* localSize);
77public:
78    // return true if application should immediately return
79    static bool parseArgs(const char* progName, const char* usagePart, int argc,
80                  const char** argv, cl_uint& deviceIndex, cxuint& useCL);
81   
82    explicit CLFacade(cl_uint deviceIndex, const char* sourceCode,
83                      const char* kernelNames = nullptr, cxuint useCL = 0);
84    ~CLFacade();
85};
86
87#endif
Note: See TracBrowser for help on using the repository browser.