File indexing completed on 2025-08-03 08:19:07
0001
0002
0003
0004 #ifndef __OPENCL_BACKEND_H__
0005 #define __OPENCL_BACKEND_H__
0006
0007 #define __CL_ENABLE_EXCEPTIONS
0008
0009
0010
0011
0012
0013 #include "cl.hpp"
0014 #include "Config.h"
0015
0016 #include <map>
0017 #include <ctime>
0018 #include <algorithm>
0019 #include <random>
0020 #include <string>
0021 #include <sstream>
0022 #include <iostream>
0023 #include <iomanip>
0024 #include <fstream>
0025 #include <type_traits>
0026
0027 namespace clvisc {
0028
0029 #ifdef USE_SINGLE_PRECISION
0030
0031 typedef cl_float cl_real;
0032 typedef cl_float2 cl_real2;
0033 typedef cl_float4 cl_real4;
0034 typedef cl_float3 cl_real3;
0035 typedef cl_float8 cl_real8;
0036 #else
0037 typedef cl_double cl_real;
0038 typedef cl_double2 cl_real2;
0039 typedef cl_double4 cl_real4;
0040 typedef cl_double3 cl_real3;
0041 typedef cl_double8 cl_real8;
0042 #endif
0043
0044
0045
0046 class CompileOption {
0047 public:
0048 std::stringstream opt;
0049
0050 CompileOption();
0051
0052
0053 CompileOption(bool use_single_precision, bool optimize);
0054
0055
0056
0057 std::string str();
0058
0059 void Define(std::string name);
0060 void SetDoubleConst(std::string name, double value);
0061 void SetFloatConst(std::string name, float value);
0062 void SetIntConst(std::string name, int value);
0063 void KernelIncludePath(std::string abs_path);
0064 };
0065
0066 class OpenclBackend {
0067 public:
0068 OpenclBackend(std::string device_type, int device_id);
0069
0070
0071
0072 cl::Context context_;
0073 cl::CommandQueue queue_;
0074
0075 std::map<std::string, cl::Program> programs;
0076 std::map<std::string, cl::Kernel> kernel_funcs;
0077 std::map<std::string, cl::Buffer> buffers;
0078
0079
0080
0081
0082 cl::Program BuildProgram(std::string fname, const std::string & option);
0083
0084 cl::Kernel CreateKernel(const cl::Program & prg, std::string func_name) {
0085 return cl::Kernel(prg, func_name.c_str());
0086 }
0087
0088
0089 inline cl::Context Context() {return context_;};
0090
0091
0092 inline cl::CommandQueue Queue() {return queue_;};
0093
0094
0095 void DeviceInfo();
0096
0097
0098 cl_int DeviceType();
0099
0100
0101 float ExcutionTime(cl::Event & event);
0102
0103
0104
0105 cl::Buffer CreateBuffer(size_t bytes_of_buffer);
0106
0107
0108 cl::Image2D CreateImage2DByCopyVector(std::vector<cl_float4> & source_vector,
0109 size_t width, size_t height, bool read_only);
0110
0111
0112
0113
0114 template <typename ValueType>
0115 cl::Buffer CreateBufferByCopyVector(std::vector<ValueType> & source_vector,
0116 bool read_only);
0117
0118 void enqueue_run(const cl::Kernel & kernel_,
0119 const cl::NDRange & global_size,
0120 const cl::NDRange & local_size);
0121
0122 template <typename ValueType>
0123 void enqueue_copy(const std::vector<ValueType> & src_vector, cl::Buffer & dst_buffer);
0124
0125 template <typename ValueType>
0126 void enqueue_copy(const cl::Buffer & src_buffer, std::vector<ValueType> & dst_vector);
0127
0128
0129 void enqueue_copy(const cl::Buffer & src_buffer, cl::Buffer & dst_buffer, size_t size);
0130 private:
0131 cl_int device_type_;
0132 std::vector<cl::Device> devices_;
0133 cl_int device_id_;
0134 cl::Device device_;
0135
0136
0137
0138 cl::Context CreateContext_(const cl_int & device_type);
0139
0140
0141 };
0142
0143 }
0144
0145 #endif