批改娘 10089. Print Platform Information (OpenCL)

contents

  1. 1. Problem
  2. 2. Sample Input
  3. 3. Sample Output
  4. 4. 備註
  5. 5. 解法

Problem

使用 OpenCL 印出裝置訊息。請參考課程講義。

Sample Input

no input

Sample Output

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
1 platform found
Platform Name NVIDIA CUDA
Platform Vendor NVIDIA Corporation
Platform Version OpenCL 1.2 CUDA 7.5.23
Platform Profile FULL_PROFILE
3 Devices
0 CPU Devices
3 GPU Devices
Device name GeForce GTX 980 Ti
Global memory size 6442254336
Local memory size 49152
# of compute units 22
max # of work items in a work group 1024
Device name GeForce GTX 970
Global memory size 4294770688
Local memory size 49152
# of compute units 13
max # of work items in a work group 1024
<other ...>

備註

可參考上方的題解頁面

解法

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
#include <stdio.h>
#include <assert.h>
#include <inttypes.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <CL/cl.h>
#define MAXDEV 8
#define MAXPLAT 8
#define MAXN 2048
#define MAXSTRBUF 1024
// -- start working with OpenCL
#define CheckFailAndExit(status) \
if (status != CL_SUCCESS) {\
fprintf(stderr, "Error: Line %u in file %s\n\n", __LINE__, __FILE__); \
}
#define clPrint(fmt, ...) fprintf(stdout, fmt, ##__VA_ARGS__)
int infoGPU() {
size_t logLen;
char logBuf[MAXSTRBUF];
cl_int clStat;
cl_uint clPlatN, clDevN;
cl_platform_id clPlatIDs[MAXPLAT], clPlatID;
cl_device_id clDevIDs[MAXDEV], clDevID;
clGetPlatformIDs(MAXPLAT, clPlatIDs, &clPlatN);
clPrint("%d platform found\n", clPlatN);
for (int i = 0; i < clPlatN; i++) {
clPlatID = clPlatIDs[i];
const cl_platform_info clPlatInfoQuery[] = {
CL_PLATFORM_NAME, CL_PLATFORM_VENDOR,
CL_PLATFORM_VERSION, CL_PLATFORM_PROFILE
};
const cl_platform_info clPlatDevInfoQuery[] = {
CL_DEVICE_TYPE_ALL, CL_DEVICE_TYPE_CPU,
CL_DEVICE_TYPE_GPU
};
const cl_platform_info clDevInfoQuery[] = {
CL_DEVICE_GLOBAL_MEM_SIZE, CL_DEVICE_LOCAL_MEM_SIZE,
CL_DEVICE_MAX_COMPUTE_UNITS, CL_DEVICE_MAX_WORK_GROUP_SIZE
};
const char const queryPlatFmt[][32] = {
"Platform Name %s\n", "Platform Vendor %s\n",
"Platform Version %s\n", "Platform Profile %s\n"
};
const char const queryPlatDevFmt[][32] = {
"%u Devices\n", "%u CPU Devices\n",
"%u GPU Devices\n"
};
const char const queryDevFmt[][64] = {
"Global memory size %lld\n", "Local memory size %lld\n",
"# of compute units %lld\n", "max # of work items in a work group %lld\n"
};
const int platQcnt = sizeof(clPlatInfoQuery) / sizeof(cl_platform_info);
const int platDevQcnt = sizeof(clPlatDevInfoQuery) / sizeof(cl_platform_info);
const int devQcnt = sizeof(clDevInfoQuery) / sizeof(cl_platform_info);
for (int j = 0; j < platQcnt; j++) {
clStat = clGetPlatformInfo(clPlatID, clPlatInfoQuery[j], MAXSTRBUF, logBuf, &logLen);
CheckFailAndExit(clStat);
clPrint(queryPlatFmt[j], logBuf);
}
for (int j = 0; j < platDevQcnt; j++) {
clGetDeviceIDs(clPlatID, clPlatDevInfoQuery[j], MAXDEV, clDevIDs, &clDevN);
clPrint(queryPlatDevFmt[j], clDevN);
if (clPlatDevInfoQuery[j] == CL_DEVICE_TYPE_ALL)
continue;
for (int k = 0; k < clDevN; k++) {
clDevID = clDevIDs[k];
clStat = clGetDeviceInfo(clDevID, CL_DEVICE_NAME, MAXSTRBUF, logBuf, &logLen);
CheckFailAndExit(clStat);
clPrint("Device name %s\n", logBuf);
for (int p = 0; p < devQcnt; p++) {
cl_ulong clVal;
clStat = clGetDeviceInfo(clDevID, clDevInfoQuery[p], sizeof(cl_ulong), &clVal, NULL);
CheckFailAndExit(clStat);
clPrint(queryDevFmt[p], clVal);
}
}
}
}
}
int main() {
infoGPU();
return 0;
}