forked from vatican1/GPGPUTasks2023
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
194 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#pragma once | ||
|
||
#include <CL/cl.h> | ||
#include <string> | ||
#include "Util.h" | ||
|
||
namespace cl_objects { | ||
class Device { | ||
cl_device_id id; | ||
public: | ||
Device() : id(nullptr) {} | ||
|
||
explicit Device(cl_device_id id) : id(id) {} | ||
|
||
Device(const Device &other) : id(other.id) {} | ||
|
||
const cl_device_id &getId() const { | ||
return id; | ||
} | ||
|
||
std::string type() const { | ||
auto type = getInfo<cl_device_type>(clGetDeviceInfo, id, CL_DEVICE_TYPE); | ||
if (type & CL_DEVICE_TYPE_GPU) | ||
return "GPU"; | ||
else if (type * CL_DEVICE_TYPE_CPU) | ||
return "CPU"; | ||
} | ||
|
||
std::string name() const { | ||
auto nameVec = getInfoVec<unsigned char, size_t>(clGetDeviceInfo, id, CL_DEVICE_NAME); | ||
return {nameVec.begin(), nameVec.end() - 1}; | ||
} | ||
|
||
cl_ulong globalSize() const { | ||
return getInfo<cl_ulong>(clGetDeviceInfo, id, CL_DEVICE_GLOBAL_MEM_SIZE); | ||
} | ||
|
||
cl_ulong localSize() const { | ||
return getInfo<cl_ulong>(clGetDeviceInfo, id, CL_DEVICE_LOCAL_MEM_SIZE); | ||
} | ||
|
||
explicit operator bool() const { | ||
return id == nullptr; | ||
} | ||
|
||
}; | ||
|
||
template<typename CL_OBJECT_TYPE, typename RELEASE_FUNC> | ||
class WrapperRAII { | ||
CL_OBJECT_TYPE cl_object = nullptr; | ||
RELEASE_FUNC* releaseFunc; | ||
public: | ||
template <typename CREATE_FUNC, typename... Args> | ||
WrapperRAII(CREATE_FUNC createFunc, RELEASE_FUNC releaseFunc, Args... args): releaseFunc(releaseFunc) { | ||
cl_int errcode_ret = 0; | ||
cl_object = createFunc(args..., &errcode_ret); | ||
OCL_SAFE_CALL(errcode_ret); | ||
} | ||
|
||
const CL_OBJECT_TYPE& getObject() const { | ||
return cl_object; | ||
} | ||
|
||
~WrapperRAII() { | ||
if (cl_object) | ||
releaseFunc(cl_object); | ||
} | ||
}; | ||
|
||
class KernelsInProgram { | ||
std::vector<cl_kernel> kernels; | ||
public: | ||
KernelsInProgram(cl_program program) { | ||
kernels = getInfoVec<cl_kernel, cl_uint>(clCreateKernelsInProgram, program); | ||
} | ||
std::vector<cl_kernel> getKernels() const { | ||
return kernels; | ||
} | ||
~KernelsInProgram() { | ||
for(cl_kernel kernel: kernels) { | ||
clReleaseKernel(kernel); | ||
} | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#pragma once | ||
|
||
#include <vector> | ||
|
||
template<typename T> | ||
std::string to_string(T value) { | ||
std::ostringstream ss; | ||
ss << value; | ||
return ss.str(); | ||
} | ||
|
||
void reportError(cl_int err, const std::string &filename, int line) { | ||
if (CL_SUCCESS == err) | ||
return; | ||
|
||
// Таблица с кодами ошибок: | ||
// libs/clew/CL/cl.h:103 | ||
// P.S. Быстрый переход к файлу в CLion: Ctrl+Shift+N -> cl.h (или даже с номером строки: cl.h:103) -> Enter | ||
std::string message = "OpenCL error code " + to_string(err) + " encountered at " + filename + ":" + to_string(line); | ||
throw std::runtime_error(message); | ||
} | ||
|
||
#define OCL_SAFE_CALL(expr) reportError(expr, __FILE__, __LINE__) | ||
|
||
|
||
|
||
template <typename R, typename SIZE_INFO_TYPE, typename CL_GET_INFO_F, typename... Args> | ||
std::vector<R> getInfoVec(CL_GET_INFO_F getInfoF, Args... args) { | ||
SIZE_INFO_TYPE infoSize = 0; | ||
OCL_SAFE_CALL(getInfoF(args..., 0, nullptr, &infoSize)); | ||
|
||
std::vector<R> infoVec(infoSize); | ||
OCL_SAFE_CALL(getInfoF(args..., infoSize, infoVec.data(), nullptr)); | ||
return infoVec; | ||
} | ||
|
||
template <typename R, typename CL_GET_INFO_F, typename... Args> | ||
R getInfo(CL_GET_INFO_F getInfoF, Args... args) { | ||
std::size_t infoSize = 0; | ||
OCL_SAFE_CALL(getInfoF(args..., 0, nullptr, &infoSize)); | ||
|
||
R info = 0; | ||
OCL_SAFE_CALL(getInfoF(args..., infoSize, &info, nullptr)); | ||
return info; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters