00001
00002
00007 #ifndef Blink_Library_Blink_h
00008 #define Blink_Library_Blink_h
00009
00010 #include "BlinkBuild.h"
00011 #include <string>
00012 #include <vector>
00013
00014 #include "BlinkTypes.h"
00015 #include "BlinkConstants.h"
00016 #include "BlinkVersion.h"
00017
00018 namespace Blink {
00019
00020 typedef BlinkComputeDeviceID ComputeDeviceID;
00021 typedef BlinkCodegenFlags CodegenFlags;
00022 typedef BlinkDataType DataType;
00023 typedef BlinkRef Ref;
00024 typedef BlinkErrorCode ErrorCode;
00025 typedef BlinkErrorRef ErrorRef;
00026 typedef BlinkMemoryType MemoryType;
00027
00028 typedef BlinkKernelType KernelType;
00029 typedef BlinkKernelGranularity KernelGranularity;
00030 typedef BlinkImageAccessType ImageAccessType;
00031 typedef BlinkImagePatternType ImagePatternType;
00032 typedef BlinkImageEdgeType ImageEdgeType;
00033
00037 struct DeviceMemory : BlinkDeviceMemory {
00038 DeviceMemory() {}
00039 DeviceMemory(const DeviceMemory& memory);
00040 DeviceMemory(MemoryType type, void* memoryBasePtr, unsigned long long memoryBaseSize, unsigned long long offset);
00041 };
00042
00043 class RefHolder {
00044 public:
00045 RefHolder();
00046
00047 RefHolder(Ref ref, bool retain);
00048 RefHolder(const RefHolder& holder);
00049
00050 RefHolder& operator=(const RefHolder& holder);
00051 void take(Ref ref);
00052
00053 RefHolder& operator=(Ref ref);
00054
00055 Ref ref() const;
00056
00057 ~RefHolder();
00058
00059 protected:
00060 Ref _ref;
00061 };
00062
00063
00065 class ComputeDevice : protected RefHolder {
00066 public:
00067
00068 ComputeDevice(const ComputeDevice& dev);
00069
00071 static ComputeDevice CurrentCPUDevice();
00072
00074 static ComputeDevice CurrentGPUDevice();
00075
00077 static ComputeDevice DeviceWithID(ComputeDeviceID id);
00078
00080 bool available() const;
00081
00083 std::string name() const;
00084
00085 private:
00086 ComputeDevice(Ref deviceRef);
00087
00088 friend class ComputeDeviceBinder;
00089 friend class Image;
00090 friend class Kernel;
00091 };
00092
00097 class ComputeDeviceBinder : protected RefHolder {
00098 public:
00099 ComputeDeviceBinder(ComputeDevice& device);
00100 };
00101
00105 class ProgramSource : protected RefHolder {
00106 public:
00107 ProgramSource(const char* source);
00108 ProgramSource(const std::string& source);
00109 ProgramSource(const ProgramSource& foo);
00110 private:
00111 void setSource(const char* source);
00112 friend class IKernel;
00113 friend class Kernel;
00114 };
00115
00121 struct Rect : BlinkRect {
00122
00123 Rect();
00124
00125 Rect(const Rect& r);
00126
00128 Rect(int x1, int y1, int x2, int y2);
00129
00130 int width() const;
00131 int height() const;
00132 };
00133
00137 struct KernelIterationController : BlinkKernelIterationController {
00138
00139 KernelIterationController(const KernelIterationController& iterationController);
00141 KernelIterationController(const Rect& bounds);
00142 };
00143
00148 struct BufferDesc : BlinkBufferDesc {
00149
00150 BufferDesc();
00151
00153 BufferDesc(unsigned int pixelStepBytes,
00154 unsigned int rowStepBytes,
00155 unsigned int componentStepBytes);
00156 };
00157
00161 struct PixelInfo : BlinkPixelInfo {
00162
00163 PixelInfo();
00165 PixelInfo(int nComponents, DataType dataType);
00166 };
00167
00171 struct ImageInfo : BlinkImageInfo {
00172
00173 ImageInfo();
00174
00175 ImageInfo(const ImageInfo& info);
00176
00178 ImageInfo(const Rect& bounds, const PixelInfo& pixelInfo);
00179
00180 Rect bounds() const;
00181 };
00182
00183 struct ImageAccessDesc : BlinkImageAccessDesc {
00184 ImageAccessDesc(void) {}
00185 ImageAccessDesc(BlinkImagePatternType type);
00186 ImageAccessDesc(BlinkAxisType axis, int min, int max);
00187 ImageAccessDesc(Rect Range);
00188 };
00189
00191 class Image : protected RefHolder {
00192 public:
00193
00195 Image();
00196
00198 Image(const Image& src);
00199
00200
00201 Image(const ImageInfo& info, ComputeDevice& device);
00202
00203
00204 Image(const DeviceMemory& memory, const ImageInfo &info, const BufferDesc& desc, ComputeDevice &device);
00205
00206 Image& operator =(const Image&src);
00207
00212 Image distributeTo(const ComputeDevice& dev);
00213
00217 Image makeLike(const ComputeDevice& dev);
00218
00220 void copyFrom(const Image& src);
00221
00223 ImageInfo info() const;
00224
00226 void clear();
00227
00229 bool isEmpty() const;
00230
00232 void copyToBuffer(void* buffer, const BufferDesc& bufferDesc) const;
00233
00235 void copyFromBuffer(const void* buffer, const BufferDesc& bufferDesc) const;
00236
00237 Image(Ref ref);
00238
00239 private:
00240
00241 friend class Kernel;
00242
00243 };
00244
00248 struct KernelInfo : BlinkKernelInfo {
00249
00250 KernelInfo();
00251
00252 KernelInfo(const KernelInfo& info);
00253
00254 KernelInfo(const std::string& name, KernelType type,
00255 KernelGranularity granularity);
00256
00257 std::string getName() const;
00258 KernelType getType() const;
00259 KernelGranularity getGranularity() const;
00260 };
00261
00265 struct KernelImageInfo : BlinkKernelImageInfo {
00266
00267 KernelImageInfo();
00268
00269 KernelImageInfo(const KernelImageInfo& info);
00270
00271 KernelImageInfo(const std::string& name, ImageAccessType accessType,
00272 ImagePatternType patternType, ImageEdgeType edgeType);
00273
00274 std::string getName() const;
00275 ImageAccessType getAccessType() const;
00276 ImagePatternType getPatternType() const;
00277 ImageEdgeType getEdgeType() const;
00278 };
00279
00283 struct KernelParameterInfo : BlinkKernelParameterInfo {
00284
00285 KernelParameterInfo();
00286
00287 KernelParameterInfo(const KernelParameterInfo& info);
00288
00289 KernelParameterInfo(const std::string& name, DataType type, unsigned nComponents,
00290 unsigned nElements);
00291
00292 unsigned getNumParameters();
00293
00294 std::string getName() const;
00295 DataType getDataType() const;
00296 unsigned getNComponents() const;
00297 unsigned getNElements() const;
00298 };
00299
00301 class IKernel : protected RefHolder {
00302
00303 public:
00305 IKernel(const ProgramSource& programSource);
00306
00307 unsigned getNumImages() const;
00308 unsigned getNumParameters() const;
00309
00310 KernelInfo getKernelInfo() const;
00311
00312 std::vector<KernelImageInfo> getImagesInfo() const;
00313
00314 std::vector<KernelParameterInfo> getParametersInfo() const;
00315
00316 protected:
00317 IKernel();
00318 };
00319
00321 class Kernel : public IKernel {
00322
00323 public:
00328 Kernel(const ProgramSource& programSource, ComputeDevice& dev, const std::vector<Image>& images, CodegenFlags flags = kBlinkCodegenDefault);
00329
00330 void setParamValue(const std::string& name, bool value);
00331 void setParamValue(const std::string& name, float value);
00332 void setParamValue(const std::string& name, int value);
00333
00335 void setParamValue(const std::string& name, const char* value, unsigned int count);
00336 void setParamValue(const std::string& name, const float* value, unsigned int count);
00337 void setParamValue(const std::string& name, const int* value, unsigned int count);
00338
00339 void getParamValue(const std::string& name, char* value, unsigned int count);
00340 void getParamValue(const std::string& name, float* value, unsigned int count);
00341 void getParamValue(const std::string& name, int* value, unsigned int count);
00342
00343 std::vector<Blink::ImageAccessDesc> getInputRanges(void);
00344
00345
00346 void iterate();
00347
00348
00349 void iterate(const KernelIterationController& controller);
00350 };
00351
00352
00356 class Exception : protected RefHolder
00357 {
00358 public:
00360 std::string userMessage() const;
00361
00362 protected:
00363 Exception(Ref ref);
00364 friend void TranslateBlinkError(ErrorCode errorCode, ErrorRef errorRef);
00365 };
00366
00370 class OutOfMemoryException : public Exception
00371 {
00372 protected:
00373 OutOfMemoryException(Ref ref);
00374 friend void TranslateBlinkError(ErrorCode errorCode, ErrorRef errorRef);
00375 };
00376
00377
00379 class ComputeException : public Exception
00380 {
00381 protected:
00382 ComputeException(Ref ref);
00383 friend void TranslateBlinkError(ErrorCode errorCode, ErrorRef errorRef);
00384 };
00385
00387 class ImageSpecException : public Exception
00388 {
00389 protected:
00390 ImageSpecException(Ref ref);
00391 friend void TranslateBlinkError(ErrorCode errorCode, ErrorRef errorRef);
00392 };
00393
00394
00396 class CompilationException : public Exception
00397 {
00398 protected:
00399 CompilationException(Ref ref);
00400 friend void TranslateBlinkError(ErrorCode errorCode, ErrorRef errorRef);
00401 };
00402
00406 class ParseException : public CompilationException
00407 {
00408 public:
00412 int lineNumber() const;
00413
00415 std::string parseError() const;
00416 protected:
00417 ParseException(Ref ref);
00418 friend void TranslateBlinkError(ErrorCode errorCode, ErrorRef errorRef);
00419 };
00420
00422 class DeviceException : public Exception
00423 {
00424 protected:
00425 DeviceException(Ref ref);
00426 friend void TranslateBlinkError(ErrorCode errorCode, ErrorRef errorRef);
00427 };
00428
00432 class ParameterException: public Exception
00433 {
00434 protected:
00435 ParameterException(Ref ref);
00436 friend void TranslateBlinkError(ErrorCode errorCode, ErrorRef errorRef);
00437 };
00438
00440 class IncompatibleImagesException : public Exception
00441 {
00442 protected:
00443 IncompatibleImagesException(Ref ref);
00444 friend void TranslateBlinkError(ErrorCode errorCode, ErrorRef errorRef);
00445 };
00446
00447 class InvalidAccessDescException : public Exception
00448 {
00449 protected:
00450 InvalidAccessDescException(Ref ref);
00451 friend void TranslateBlinkError(ErrorCode errorCode, ErrorRef errorRef);
00452 };
00453
00454 }
00455 #include "BlinkImpl.h"
00456 #endif