Skip to content

Commit

Permalink
Merge pull request #399 from clEsperanto/device-management
Browse files Browse the repository at this point in the history
Device management
  • Loading branch information
StRigaud authored Nov 19, 2024
2 parents 7161f12 + d712c88 commit 5c88807
Show file tree
Hide file tree
Showing 7 changed files with 444 additions and 146 deletions.
11 changes: 8 additions & 3 deletions clic/include/backend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -537,21 +537,26 @@ class CUDABackend : public Backend
*/
class OpenCLBackend : public Backend
{
private:
std::vector<Device::Pointer> device_list_;

public:
OpenCLBackend() = default;
OpenCLBackend();
~OpenCLBackend() override;

OpenCLBackend(const OpenCLBackend &) = default;

OpenCLBackend(OpenCLBackend &&) = default;

~OpenCLBackend() override = default;

auto
operator=(const OpenCLBackend &) -> OpenCLBackend & = default;

auto
operator=(OpenCLBackend &&) -> OpenCLBackend & = default;

auto
initialiseRessources() -> void;

[[nodiscard]] auto
getDevices(const std::string & type) const -> std::vector<Device::Pointer> override;

Expand Down
122 changes: 116 additions & 6 deletions clic/include/device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,19 @@ class Device
[[nodiscard]] virtual auto
getType() const -> Device::Type = 0;

/**
* @brief Get device type as string
* @return std::string
*/
[[nodiscard]] virtual auto
getDeviceType() const -> std::string = 0;

/**
* @brief Get device index in context
*/
[[nodiscard]] virtual auto
getDeviceIndex() const -> size_t = 0;

/**
* @brief Get device platform
* @return std::string
Expand All @@ -112,6 +125,12 @@ class Device
[[nodiscard]] virtual auto
supportImage() const -> bool = 0;

/**
* @brief Get shared context devices
*/
[[nodiscard]] virtual auto
getNbDevicesFromContext() const -> size_t = 0;

/**
* @brief operator << for Device::Type
*/
Expand Down Expand Up @@ -150,6 +169,47 @@ class Device
class OpenCLDevice : public Device
{
public:
struct Context
{
cl_context ptr = nullptr;
size_t nb_device = 0;

Context(const cl_context & ptr);
~Context();
auto
get() const -> const cl_context &;
};

struct CommandQueue
{
cl_command_queue ptr = nullptr;

CommandQueue(const cl_command_queue & ptr);
~CommandQueue();
auto
get() const -> const cl_command_queue &;
};

struct Ressources
{
cl_device_id device_ptr = nullptr;
cl_platform_id platform_ptr = nullptr;
cl_device_type device_type = 0;
std::string device_name = "";
std::string platform_name = "";
std::string platform_vendor = "";
bool image_support = false;
size_t device_index = 0;

Ressources(const cl_platform_id & platform, const cl_device_id & device, size_t index);
~Ressources();
auto
get_device() const -> const cl_device_id &;
auto
get_platform() const -> const cl_platform_id &;
};


/**
* @brief Construct a new OpenCLDevice object
* @param platform
Expand All @@ -158,6 +218,19 @@ class OpenCLDevice : public Device
*/
OpenCLDevice(const cl_platform_id & platform, const cl_device_id & device);

/**
* @brief Construct a new Device object
* @param ressources
* @param context
* @param command_queue
* @param device_index
* @param nb_device
* @return OpenCLDevice
*/
OpenCLDevice(const std::shared_ptr<Ressources> & ressources,
const std::shared_ptr<Context> & context,
const std::shared_ptr<CommandQueue> & command_queue);

/**
* @brief Destroy the OpenCLDevice object
*/
Expand Down Expand Up @@ -201,6 +274,19 @@ class OpenCLDevice : public Device
[[nodiscard]] auto
getType() const -> Device::Type override;

/**
* @brief Get device type as string
* @return std::string
*/
[[nodiscard]] auto
getDeviceType() const -> std::string override;

/**
* @brief Get device index in context
*/
[[nodiscard]] auto
getDeviceIndex() const -> size_t override;

/**
* @brief Check if device is initialized
* @return bool
Expand Down Expand Up @@ -258,6 +344,12 @@ class OpenCLDevice : public Device
[[nodiscard]] auto
getInfoExtended() const -> std::string override;

/**
* @brief Get shared context devices
*/
[[nodiscard]] auto
getNbDevicesFromContext() const -> size_t override;

/**
* @brief check if device is compatible with cl_image
* @return bool
Expand All @@ -266,12 +358,11 @@ class OpenCLDevice : public Device
supportImage() const -> bool override;

private:
cl_device_id clDevice;
cl_platform_id clPlatform;
cl_context clContext;
cl_command_queue clCommandQueue;
bool initialized = false;
bool waitFinish = false;
std::shared_ptr<Ressources> clRessources = nullptr;
std::shared_ptr<Context> clContext = nullptr;
std::shared_ptr<CommandQueue> clCommandQueue = nullptr;
bool initialized = false;
bool waitFinish = false;
};
#endif // USE_OPENCL

Expand Down Expand Up @@ -332,6 +423,13 @@ class CUDADevice : public Device
[[nodiscard]] auto
getType() const -> Device::Type override;

/**
* @brief Get device type as string
* @return std::string
*/
[[nodiscard]] auto
getDeviceType() const -> std::string override;

/**
* @brief Check if device is initialized
* @return bool
Expand Down Expand Up @@ -403,6 +501,18 @@ class CUDADevice : public Device
[[nodiscard]] auto
supportImage() const -> bool override;

/**
* @brief Get shared context devices
*/
[[nodiscard]] auto
getNbDevicesFromContext() const -> size_t override;

/**
* @brief Get device index in context
*/
[[nodiscard]] auto
getDeviceIndex() const -> size_t override;

private:
int cudaDeviceIndex;
CUdevice cudaDevice;
Expand Down
19 changes: 4 additions & 15 deletions clic/src/array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,13 @@ Array::Array(const size_t width,

Array::~Array()
{
try
if (initialized())
{
if (initialized() && data_.use_count() == 1)
if (data_.use_count() == 1 && get() != nullptr)
{
auto * ptr = get();
if (ptr != nullptr)
{
backend_.freeMemory(device(), mtype(), ptr);
}
backend_.freeMemory(device(), mtype(), get());
}
}
catch (const std::exception & e)
{
std::cerr << "Error cle::~Array: " << e.what() << std::endl;
}
catch (...)
{
std::cerr << "Unknown error in Array::~Array" << std::endl;
data_.reset();
}
}

Expand Down
7 changes: 7 additions & 0 deletions clic/src/cudadevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,13 @@ CUDADevice::getName(bool lowercase) const -> std::string
return std::string(device_name);
}


[[nodiscard]] auto
getDeviceType() const -> std::string
{
return "gpu";
}

auto
CUDADevice::getArch() const -> std::string
{
Expand Down
Loading

0 comments on commit 5c88807

Please sign in to comment.