Add ComputeManager; add SmoHooks for getting ClDevices, buffers

We added a new centralized OpenCL Compute manager. This can later
be extended to support CUDA, SyCL, etc. SMO can be configured at
build time to choose which API it will use for compute.

Moreover, the ComputeMgr allows us to register buffers which are
available to all cl_contexts.
This commit is contained in:
2025-11-19 22:33:30 -04:00
parent a910909ad5
commit 27b43c6686
9 changed files with 665 additions and 45 deletions
+120
View File
@@ -0,0 +1,120 @@
#ifndef _USER_COMPUTE_H
#define _USER_COMPUTE_H
#include <memory>
#include <vector>
#define CL_TARGET_OPENCL_VERSION 120
#include <CL/cl.h>
namespace smo {
namespace compute {
/**
* @brief OpenCL compute device information
*
* Manages a single OpenCL device, creating and owning its context and command
* queue.
*/
class ComputeDevice
{
public:
/**
* @brief Construct a ComputeDevice from platform and device IDs
*
* Creates the OpenCL context and command queue for the device.
* Throws std::runtime_error if context or queue creation fails.
*
* @param platformId OpenCL platform ID
* @param deviceId OpenCL device ID
*/
ComputeDevice(cl_platform_id platformId, cl_device_id deviceId);
~ComputeDevice()
{
if (commandQueue)
{
clReleaseCommandQueue(commandQueue);
commandQueue = nullptr;
}
if (context)
{
clReleaseContext(context);
context = nullptr;
}
}
// Non-copyable
ComputeDevice(const ComputeDevice&) = delete;
ComputeDevice& operator=(const ComputeDevice&) = delete;
cl_platform_id platform;
cl_device_id device;
cl_context context;
cl_command_queue commandQueue;
};
/**
* @brief Association between an OpenCL buffer and a compute device
*/
struct ClBufferDeviceAssociation
{
ClBufferDeviceAssociation(
cl_mem buf, const std::shared_ptr<ComputeDevice>& dev)
: buffer(buf), device(dev)
{}
cl_mem buffer;
std::shared_ptr<ComputeDevice> device;
};
/**
* @brief OpenCL buffer created on all compute devices
*
* Manages a USE_HOST_PTR buffer created on all available compute devices.
* The constructor creates buffers for all devices, and the destructor releases
* them.
*/
class ClBuffer
{
public:
/**
* @brief Construct a ClBuffer and create buffers on all devices
*
* Creates a USE_HOST_PTR buffer on each device's context.
* Throws std::runtime_error if buffer creation fails for any device.
*
* @param hostPtr Host pointer to use
* @param size Size of buffer in bytes
* @param flags Additional OpenCL memory flags
* @param devices Vector of compute devices to create buffers on
*/
ClBuffer(
void* hostPtr, size_t size, cl_mem_flags flags,
const std::vector<std::shared_ptr<ComputeDevice>>& devices);
~ClBuffer()
{
for (auto& assoc : associations)
{
if (assoc.buffer)
{
clReleaseMemObject(assoc.buffer);
assoc.buffer = nullptr;
}
}
}
// Non-copyable
ClBuffer(const ClBuffer&) = delete;
ClBuffer& operator=(const ClBuffer&) = delete;
void* hostPtr;
size_t size;
cl_mem_flags flags;
std::vector<ClBufferDeviceAssociation> associations;
};
} // namespace compute
} // namespace smo
#endif // _USER_COMPUTE_H
+40
View File
@@ -6,9 +6,12 @@
#include <string>
#include <functional>
#include <memory>
#include <vector>
#include <preprocessor.h>
#include <user/deviceAttachmentSpec.h>
#include <callback.h>
#define CL_TARGET_OPENCL_VERSION 120
#include <CL/cl.h>
class OptionParser;
@@ -16,6 +19,11 @@ namespace smo {
class ComponentThread;
namespace compute {
class ClBuffer;
class ComputeDevice;
} // namespace compute
namespace stim_buff {
/**
@@ -92,6 +100,38 @@ struct SmoCallbacks
* equivalent to calling OptionParser::getOptions().
*/
OptionParser& (*OptionParser_getOptions)(void);
/**
* @brief Create a USE_HOST_PTR buffer on all OpenCL contexts
* @param hostPtr Host pointer to the memory
* @param size Size of the buffer in bytes
* @param flags Additional OpenCL memory flags
* @return Shared pointer to ClBuffer managing buffers on all devices
*/
std::shared_ptr<smo::compute::ClBuffer>
(*ComputeManager_createUseHostPtrBuffer)(
void* hostPtr, size_t size, cl_mem_flags flags);
/**
* @brief Release USE_HOST_PTR buffers from all contexts
* @param buffer Shared pointer to ClBuffer to release
*/
void (*ComputeManager_releaseUseHostPtrBuffer)(
std::shared_ptr<smo::compute::ClBuffer> buffer);
/**
* @brief Get a compute device
* @return Shared pointer to ComputeDevice, or nullptr if no devices available
*/
std::shared_ptr<smo::compute::ComputeDevice>
(*ComputeManager_getDevice)(void);
/**
* @brief Release a compute device
* @param device Shared pointer to ComputeDevice to release
*/
void (*ComputeManager_releaseDevice)(
std::shared_ptr<smo::compute::ComputeDevice> device);
};
struct Sal_Mgmt_LibOps