HEaaN
Crypto Lab's great homomorphic encryption library
Device.hpp
Go to the documentation of this file.
1 // //
3 // Copyright (C) 2021-2023 Crypto Lab Inc. //
4 // //
5 // - This file is part of HEaaN homomorphic encryption library. //
6 // - HEaaN cannot be copied and/or distributed without the express permission //
7 // of Crypto Lab Inc. //
8 // //
10 
11 #pragma once
12 
13 #include <initializer_list>
14 #include <string>
15 #include <vector>
16 
17 #include "HEaaN/Exception.hpp"
18 #include "HEaaN/HEaaNExport.hpp"
19 #include "HEaaN/Integers.hpp"
21 #include <set>
22 #include <tuple>
23 
24 namespace HEaaN {
25 
26 enum class DeviceType {
27  CPU,
28  GPU,
29  UNDEFINED,
30  CPU_PINNED,
31 };
32 
34 public:
35  // If a type is given only, we infer the id.
36  constexpr Device(const DeviceType type)
37  : Device{type,
38  type == DeviceType::GPU ? CudaTools::cudaGetDevice() : 0} {}
39 
40  constexpr explicit Device(const DeviceType type, int device_id)
41  : type_{type}, device_id_{device_id} {}
42  constexpr DeviceType type() const { return type_; }
43  constexpr int id() const { return device_id_; }
44  constexpr bool operator==(const Device &other) const {
45  return type() == other.type() && device_id_ == other.device_id_;
46  }
47  constexpr bool operator!=(const Device &other) const {
48  return !(*this == other);
49  }
50 
51  constexpr bool operator<(const Device &other) const {
52  return std::tie(device_id_, type_) <
53  std::tie(other.device_id_, other.type_);
54  }
55 
56 private:
58  // cuda device id. Zero when type_ is CPU.
60 };
61 
63  return Device{DeviceType::CPU};
64 } // TODO(wk): remove?
65 
66 using DeviceSet = std::set<Device>;
67 
68 // A set of CUDA device IDs.
69 using CudaDeviceIds = std::set<int>;
70 
72 // Set the current CUDA device id. The default id is set to 0.
73 HEAAN_API void setCurrentCudaDevice(int device_id);
74 
76 public:
78  : select_{device.type() == DeviceType::GPU},
80  if (select_)
81  setCurrentCudaDevice(device.id());
82  }
83 
85  if (select_)
87  }
88 
92  operator=(const ScopedCudaDeviceSelector &) = delete;
94 
95 private:
96  bool select_;
98 };
99 
100 } // namespace HEaaN
HEaaN::ScopedCudaDeviceSelector::operator=
ScopedCudaDeviceSelector & operator=(const ScopedCudaDeviceSelector &)=delete
HEaaN::DeviceType::GPU
@ GPU
HEaaN::DeviceSet
std::set< Device > DeviceSet
Definition: Device.hpp:66
HEaaN::CudaDeviceIds
std::set< int > CudaDeviceIds
Definition: Device.hpp:69
HEaaN::getDefaultDevice
constexpr HEAAN_API Device getDefaultDevice()
Definition: Device.hpp:62
HEaaN::Device::device_id_
int device_id_
Definition: Device.hpp:59
HEaaN::DeviceType::CPU_PINNED
@ CPU_PINNED
HEaaN::DeviceType::CPU
@ CPU
HEaaN::ScopedCudaDeviceSelector::~ScopedCudaDeviceSelector
~ScopedCudaDeviceSelector()
Definition: Device.hpp:84
HEaaN::Device::type
constexpr DeviceType type() const
Definition: Device.hpp:42
HEaaN::Device::operator<
constexpr bool operator<(const Device &other) const
Definition: Device.hpp:51
HEaaN::Device::operator==
constexpr bool operator==(const Device &other) const
Definition: Device.hpp:44
HEaaN::Device
Definition: Device.hpp:33
HEaaN::Device::Device
constexpr Device(const DeviceType type, int device_id)
Definition: Device.hpp:40
HEaaN::CudaTools::cudaGetDevice
HEAAN_API int cudaGetDevice()
Integers.hpp
HEaaN::ScopedCudaDeviceSelector::ScopedCudaDeviceSelector
ScopedCudaDeviceSelector(Device device)
Definition: Device.hpp:77
HEaaN::Device::type_
DeviceType type_
Definition: Device.hpp:57
HEaaN::ScopedCudaDeviceSelector::before_
Device before_
Definition: Device.hpp:97
HEaaN::DeviceType::UNDEFINED
@ UNDEFINED
HEaaN::Device::id
constexpr int id() const
Definition: Device.hpp:43
HEaaN::ScopedCudaDeviceSelector::select_
bool select_
Definition: Device.hpp:96
HEaaN::DeviceType
DeviceType
Definition: Device.hpp:26
Exception.hpp
HEaaN::Device::Device
constexpr Device(const DeviceType type)
Definition: Device.hpp:36
HEaaNExport.hpp
HEaaN::setCurrentCudaDevice
HEAAN_API void setCurrentCudaDevice(int device_id)
HEaaN::ScopedCudaDeviceSelector
Definition: Device.hpp:75
CudaTools.hpp
HEAAN_API
#define HEAAN_API
Definition: HEaaNExport.hpp:26
HEaaN
Definition: Bootstrapper.hpp:16
HEaaN::Device::operator!=
constexpr bool operator!=(const Device &other) const
Definition: Device.hpp:47
HEaaN::getCurrentCudaDevice
HEAAN_API Device getCurrentCudaDevice()