Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
opencl_backend.h
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file opencl_backend.h
1 
4 #ifndef __OPENCL_BACKEND_H__
5 #define __OPENCL_BACKEND_H__
6 
7 #define __CL_ENABLE_EXCEPTIONS
8 //#if defined(__APPLE__) || defined(__MACOSX)
9 //#include <OpenCL/cl.hpp>
10 //#else
11 //#include <CL/cl.hpp>
12 //#endif
13 #include "cl.hpp"
14 #include "Config.h"
15 
16 #include <map>
17 #include <ctime>
18 #include <algorithm>
19 #include <random>
20 #include <string>
21 #include <sstream>
22 #include <iostream>
23 #include <iomanip>
24 #include <fstream>
25 #include <type_traits>
26 
27 namespace clvisc {
28 
29 #ifdef USE_SINGLE_PRECISION
30 
31  typedef cl_float cl_real;
32  typedef cl_float2 cl_real2;
33  typedef cl_float4 cl_real4;
34  typedef cl_float3 cl_real3;
35  typedef cl_float8 cl_real8;
36 #else
37  typedef cl_double cl_real;
38  typedef cl_double2 cl_real2;
39  typedef cl_double4 cl_real4;
40  typedef cl_double3 cl_real3;
41  typedef cl_double8 cl_real8;
42 #endif
43 
44 
45 /* compile options to build opencl kernel files on device*/
47  public:
48  std::stringstream opt;
49 
50  CompileOption();
53  CompileOption(bool use_single_precision, bool optimize);
54 
55 
57  std::string str();
58 
59  void Define(std::string name);
60  void SetDoubleConst(std::string name, double value);
61  void SetFloatConst(std::string name, float value);
62  void SetIntConst(std::string name, int value);
63  void KernelIncludePath(std::string abs_path);
64 };
65 
67  public:
68  OpenclBackend(std::string device_type, int device_id);
69 
70  // make context_ and queue_ public in case one wants
71  // to use the native opencl APIs.
74 
75  std::map<std::string, cl::Program> programs;
76  std::map<std::string, cl::Kernel> kernel_funcs;
77  std::map<std::string, cl::Buffer> buffers;
78 
83 
84  cl::Kernel CreateKernel(const cl::Program & prg, std::string func_name) {
85  return cl::Kernel(prg, func_name.c_str());
86  }
87 
89  inline cl::Context Context() {return context_;};
90 
92  inline cl::CommandQueue Queue() {return queue_;};
93 
95  void DeviceInfo();
96 
98  cl_int DeviceType();
99 
101  float ExcutionTime(cl::Event & event);
102 
105  cl::Buffer CreateBuffer(size_t bytes_of_buffer);
106 
108  cl::Image2D CreateImage2DByCopyVector(std::vector<cl_float4> & source_vector,
109  size_t width, size_t height, bool read_only);
110 
114  template <typename ValueType>
115  cl::Buffer CreateBufferByCopyVector(std::vector<ValueType> & source_vector,
116  bool read_only);
117 
118  void enqueue_run(const cl::Kernel & kernel_,
119  const cl::NDRange & global_size,
120  const cl::NDRange & local_size);
121 
122  template <typename ValueType>
123  void enqueue_copy(const std::vector<ValueType> & src_vector, cl::Buffer & dst_buffer);
124 
125  template <typename ValueType>
126  void enqueue_copy(const cl::Buffer & src_buffer, std::vector<ValueType> & dst_vector);
127 
128  // copy cl::Buffer to cl::Buffer
129  void enqueue_copy(const cl::Buffer & src_buffer, cl::Buffer & dst_buffer, size_t size);
130  private:
131  cl_int device_type_;
132  std::vector<cl::Device> devices_;
133  cl_int device_id_;
138  cl::Context CreateContext_(const cl_int & device_type);
139 
141 };
142 
143 } // end namespace clvisc
144 
145 #endif