-
Notifications
You must be signed in to change notification settings - Fork 3
/
platform.cpp
110 lines (98 loc) · 2.91 KB
/
platform.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include "platform.h"
namespace GPGPU_LIB
{
PlatformManager::PlatformManager()
{
std::vector<cl::Platform> tmp;
cl_int op = cl::Platform::get(&tmp);
if (op != CL_SUCCESS)
{
throw std::invalid_argument(std::string("Platform::get error: ") + getErrorString(op));
}
for (int i = 0; i < tmp.size(); i++)
{
int ver = tmp[i].getInfo<CL_PLATFORM_VERSION>(&op).at(7) - '0';
if (op != CL_SUCCESS)
{
throw std::invalid_argument(std::string("Platformquery version error: ") + getErrorString(op));
}
if (ver >= 2 && CL_HPP_MINIMUM_OPENCL_VERSION >= 200)
{
platforms.push_back(tmp[i]);
}
else if (ver >= 1)
{
platforms.push_back(tmp[i]);
}
}
}
void PlatformManager::printPlatforms()
{
for (auto& p : platforms)
{
std::cout << p.getInfo<CL_PLATFORM_NAME>() << std::endl;
}
}
std::vector<Device> PlatformManager::getDevices(int typeOfDevice, int nOtherDevices)
{
std::vector<Device> devices;
int countId = 0;
for (int i = 0; i < platforms.size(); i++)
{
std::vector<cl::Device> devicesTmp;
cl_int op2 = platforms[i].getDevices(typeOfDevice, &devicesTmp);
if (op2 != CL_SUCCESS)
{
throw std::invalid_argument(std::string("getDevices error: ") + getErrorString(op2));
}
for (int j = 0; j < devicesTmp.size(); j++)
{
bool duplicate = false;
for (int k = 0; k < devices.size(); k++)
{
if (devices[k].device.get() == devicesTmp[j].get())
{
duplicate = true;
break;
}
}
if (!duplicate)
{
cl_bool sharesRAM;
bool isCPU = (CL_DEVICE_TYPE_CPU == typeOfDevice);
cl_int op = devicesTmp[j].getInfo(CL_DEVICE_HOST_UNIFIED_MEMORY, &sharesRAM);
if (op != CL_SUCCESS)
{
throw std::invalid_argument(std::string("getInfo CL_DEVICE_HOST_UNIFIED_MEMORY error: ") + getErrorString(op));
}
// debugging
//sharesRAM = false;
//
// if there are other devices too, leave some threads for their control
if ((nOtherDevices > 0) && (CL_DEVICE_TYPE_CPU == typeOfDevice))
{
// leaving some of threads for other devices' i/o control
cl_device_partition_property p[]{ CL_DEVICE_PARTITION_BY_COUNTS, std::thread::hardware_concurrency() - nOtherDevices, CL_DEVICE_PARTITION_BY_COUNTS_LIST_END, 0 };
std::vector<cl::Device> clDevices;
cl_int err_create;
if ((err_create = devicesTmp[j].createSubDevices(p, &clDevices)) == CL_SUCCESS)
{
devicesTmp[j] = clDevices[0];
}
}
Device dev(devicesTmp[j], countId++, (CL_DEVICE_TYPE_CPU == typeOfDevice) || sharesRAM, isCPU);
devices.push_back(dev);
}
}
}
std::vector<Device> devicesResult;
for (int i = 0; i < devices.size(); i++)
{
if (devices[i].ver >= CL_HPP_MINIMUM_OPENCL_VERSION)
{
devicesResult.push_back(devices[i]);
}
}
return devicesResult;
}
}