KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gpu_opencl.h
Go to the documentation of this file.
1 /* $Id: gpu_opencl.h 17393 2014-08-23 20:19:14Z predoehl $ */
2 /* {{{=========================================================================== *
3  |
4  | Copyright (c) 1994-2012 by Kobus Barnard (author)
5  |
6  | Personal and educational use of this code is granted, provided that this
7  | header is kept intact, and that the authorship is not misrepresented, that
8  | its use is acknowledged in publications, and relevant papers are cited.
9  |
10  | For other use contact the author (kobus AT cs DOT arizona DOT edu).
11  |
12  | Please note that the code in this file has not necessarily been adequately
13  | tested. Naturally, there is no guarantee of performance, support, or fitness
14  | for any particular task. Nonetheless, I am interested in hearing about
15  | problems that you encounter.
16  |
17  | Author: Kyle Simek
18  * =========================================================================== }}}*/
19 
20 // vim: tabstop=4 shiftwidth=4 foldmethod=marker
21 
22 #ifndef KJB_CPP_GPU_OPENCL_H
23 #define KJB_CPP_GPU_OPENCL_H
24 
25 #include <l_cpp/l_exception.h>
26 
27 #ifdef KJB_HAVE_OPENCL
28  #ifdef MAC_OSX
29  #include <OpenCL/opencl.h>
30  #else
31  #include <CL/cl.h>
32  #endif
33 
34 // "On error throw exception" -- in this case, "error" is ANY nonzero value.
35 #define CL_ETX(a) \
36 { \
37  cl_int __err = a; \
38  if(__err) \
39  { \
40  throw ::kjb::gpu::Opencl_error(__err, __FILE__, __LINE__); \
41  } \
42 }
43 
44 #define CL_ETX_2(a, msg) \
45 { \
46  cl_int __err = a; \
47  if(__err) \
48  { \
49  throw ::kjb::gpu::Opencl_error(__err, msg, __FILE__, __LINE__); \
50  } \
51 }
52 
53 // "On error throw exception" -- in this case, "error" is ANY nonzero value.
54 #define CL_EPETE(a) \
55 { \
56  cl_int __err = a; \
57  if(__err) \
58  { \
59  std::cerr << "OpenCL error: " << kjb::gpu::get_opencl_error_string(__err) << std::endl; \
60  abort(); \
61  } \
62 }
63 
64 namespace kjb
65 {
66 namespace gpu
67 {
68 
69 
70 const char* get_opencl_error_string(cl_int err);
71 
72 class Opencl_error : public kjb::Runtime_error
73 {
74 public:
75  Opencl_error(cl_int code, const char* file, int line) :
76  Runtime_error(get_opencl_error_string(code), file, line),
77  code_(code)
78  {}
79 
80  Opencl_error(cl_int code, const std::string& message, const char* file, int line) :
81  Runtime_error(message, file, line),
82  code_(code)
83  {}
84 
85  Opencl_error(const std::string& message, const char* file, int line) :
86  Runtime_error(message, file, line),
87  code_(0)
88  {}
89 
90  cl_int get_error_code() const
91  {
92  return code_;
93  }
94 
95 private:
96  cl_int code_;
97 };
98 
99 }
100 }
101 #endif /* KJB_HAVE_OPENCL */
102 #endif
Support for error handling exception classes in libKJB.
Object thrown when computation fails somehow during execution.
Definition: l_exception.h:321