Device Class
2.1 Basic Information
Class:
HEaaN::Device
Purpose: Unified management of CPU and GPU devices
2.2 API Reference
2.2.1 Constructors
Device(DeviceType type)
Device(DeviceType type, int device_id)
Function: Creates a device object
Usage Example:
//Create CPU device (uses CPU device)
Device cpu_device(DeviceType::CPU);
// Create GPU device (uses currently active GPU)
Device gpu_device(DeviceType::GPU);
//Create specific GPU device
Device sepcific_gpu(DeviceType::GPU, 1); // Use GPU 1
device_id
is only allowed to use 0
.
2.2.2 Device Information Retrieval
DeviceType type() const
int id() const
Function: Retrieves device type and ID
Usage Example:
Device device(DeviceType::GPU);
if(device.type() == DeviceType::GPU) {
int gpuId = device.id();
}
2.2.3 Device Comparison
bool operator==(const Device &other) const
bool operator!=(const Device &other) const
bool operator<(const Device &other) const
Function: Device object comparison
Usage Example:
Device device1(DeviceType::GPU, 0);
Device device2(DeviceType::GPU, 1);
if(device1 != device2) {
// Different devices
}
2.3 Utility Functions
2.3.1 Default Device
Device getDefaultDevice()
Function: Returns the default CPU device.
Return Value: Device object of CPU type
2.3.2 CUDA Device Management
Device getCurrentCudaDevice()
void setCurrentCudaDevice(int device_id)
Functions: Retrieves and sets the current CUDA device
Usage Example:
HEaaN::setCurrentCudaDevice(0);
Device currentDevice = HEaaN::getCurrentDevice();
2.4 ScopedCudaDeviceSelector
This class is used when allocating memory for different devices in a multi-GPU environment.
Within a specific scope, it stores the previous device, sets a new device, and then reverts back to the previous device when exiting that scope.
Last updated
Was this helpful?