-
Notifications
You must be signed in to change notification settings - Fork 203
/
Copy pathGpuIds.cpp
70 lines (65 loc) · 2.01 KB
/
GpuIds.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "GpuIds.hpp"
#include <stdlib.h>
#include <string.h>
#include <cuda_runtime_api.h>
GpuIds::~GpuIds() {
free(m_piDeviceIds); m_piDeviceIds = nullptr;
m_iCount = 0;
}
GpuIds::GpuIds() : m_piDeviceIds (nullptr), m_iCount(0) {
}
void GpuIds::SetIds(int iCount, int* piDeviceIds) {
if (iCount > 0 && piDeviceIds != 0) {
if (m_piDeviceIds) {
free(m_piDeviceIds); m_piDeviceIds = nullptr;
m_iCount = 0;
}
m_piDeviceIds = (int*)malloc(iCount * sizeof(int));
if (m_piDeviceIds) {
for (int iI = 0; iI < iCount; ++iI) {
m_piDeviceIds[iI] = piDeviceIds[iI];
}
m_iCount = iCount;
}
}
}
int GpuIds::GetLength() const {
return m_iCount;
}
int& GpuIds::operator[](int iIndex){
return m_piDeviceIds[iIndex];
}
int GpuIds::operator[](int iIndex) const {
return m_piDeviceIds[iIndex];
}
void GpuIds::SetAllGpus(int iTotalDeviceCount) {
// Set all GPUs for compatibility
// Makeup valid GpuIds.
int* aiIds = nullptr;
if (iTotalDeviceCount == 0) {
(int*)malloc(iTotalDeviceCount*sizeof(int));
for (int iI = 0; iI < iTotalDeviceCount; ++iI) {
aiIds[iI] = iI;
}
}
SetIds(iTotalDeviceCount, aiIds);
free(aiIds); aiIds = 0;
}
bool GpuIds::AreEqualDevices() const {
int deviceCount = this->GetLength();
const int devicenamelength = 256; // The length 256 is fixed by spec of cudaDeviceProp::name
char devicename[devicenamelength];
cudaDeviceProp deviceProp;
for (int dev = 0; dev < deviceCount; dev++) {
// cudaSetDevice(m_piDeviceIds[dev]);
cudaGetDeviceProperties(&deviceProp, m_piDeviceIds[dev]);
if (dev>0) {
if (strcmp(devicename, deviceProp.name) != 0) {
return false;
}
}
memset(devicename, 0, devicenamelength);
strcpy(devicename, deviceProp.name);
}
return true;
}