/*
PIP - Platform Independent Primitives
OpenCL wrappers
Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see .
*/
#ifndef PIOPENCL_H
#define PIOPENCL_H
#include "pivariant.h"
class PIOpenCL {
public:
struct KernelArg;
struct Device;
struct Platform;
class Context;
class Program;
class Kernel;
typedef PIVector DeviceList;
enum AddressQualifier {
AddressGlobal,
AddressLocal,
AddressConstant,
AddressPrivate,
};
enum AccessQualifier {
AccessReadOnly,
AccessWriteOnly,
AccessReadWrite,
AccessNone,
};
enum TypeQualifier {
TypeConst,
TypeRestrict,
TypeVolatile,
TypeNone,
};
enum ArgType {
Char = 1,
UChar,
Short,
UShort,
Int,
UInt,
Long,
ULong,
Float,
Double,
};
struct KernelArg {
KernelArg();
AddressQualifier address_qualifier;
AccessQualifier access_qualifier;
TypeQualifier type_qualifier;
PIString arg_name;
PIString type_name;
PIString base_type_name;
bool is_pointer;
ArgType arg_type;
int dims;
private:
friend class Kernel;
void init(void * _k, uint index);
};
struct Device {
Device() {id = platform_id = 0; max_compute_units = max_clock_frequency = 0; max_memory_size = 0;}
bool isValid() const {return id != 0;}
PIString displayText() const {return name.trimmed() + " (" + device_version.trimmed() + ")";}
void * id;
void * platform_id;
PIString name;
PIString vendor;
PIString device_version;
PIString driver_version;
int max_compute_units;
int max_clock_frequency;
ullong max_memory_size;
};
struct Platform {
Platform() {id = 0;}
bool isValid() const {return id != 0;}
PIString displayText() const {return name.trimmed() + " (" + version.trimmed() + ", " + profile.trimmed() + ")";}
void * id;
PIString name;
PIString vendor;
PIString profile;
PIString version;
PIStringList extensions;
PIVector devices;
};
class Context {
friend class Program;
public:
~Context();
static Context * create(const DeviceList & dl);
static Context * create(const Device & d) {return create(DeviceList() << d);}
Program * createProgram(const PIString & source, PIString * error = 0);
private:
Context();
void zero();
void deletePrograms();
PIVector programs_;
PRIVATE_DECLARATION
};
class Program {
friend class Context;
friend class Kernel;
public:
~Program();
const PIString & sourceCode() const {return source_;}
const Kernel * kernel(int index = 0) const {return kernels_[index];}
const PIVector & kernels() const {return kernels_;}
private:
Program();
void zero();
bool initKernels(PIVector kerns);
Context * context_;
PIString source_;
PIVector kernels_;
PRIVATE_DECLARATION
};
class Kernel {
friend class Program;
public:
const PIString & name() const {return name_;}
const PIVector & args() const {return args_;}
template bool setArgValue(int index, const T & value) {return setArgValueV(index, PIVariant::fromValue(value));}
template bool setArgValue(const PIString & arg, const T & value) {return setArgValue(argIndex(arg), value);}
private:
Kernel();
~Kernel();
void zero();
bool init();
bool setArgValueV(int index, const PIVariant & value);
int argIndex(const PIString & an) const;
KernelArg argByName(const PIString & an) const;
Context * context_;
Program * program_;
PIString name_;
PIVector args_;
PRIVATE_DECLARATION
};
static void init();
static const PIVector & platforms();
static const PIVector devices();
static Device deviceByID(void * id);
static PIString prepareProgram(const PIString & prog);
private:
static PIString prog_header;
PIOpenCL() {;}
class Initializer {
public:
Initializer();
static Initializer * instance();
void init();
PIVector platforms_;
bool inited_;
};
};
PICout operator <<(PICout s, const PIOpenCL::KernelArg & v);
#endif // PIOPENCL_H