-
Notifications
You must be signed in to change notification settings - Fork 0
/
Platform.c
90 lines (78 loc) · 2.56 KB
/
Platform.c
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <CL/cl.h>
#include "Platform.h"
#include "Device.h"
#include "Util.h"
static int CL_PLATFORM_INFO[5] = {
CL_PLATFORM_PROFILE,
CL_PLATFORM_VERSION,
CL_PLATFORM_NAME,
CL_PLATFORM_VENDOR,
CL_PLATFORM_EXTENSIONS,
};
enum {
kPlatformInfoProfile,
kPlatformInfoVersion,
kPlatformInfoName,
kPlatformInfoVendor,
kPlatformInfoExtenstions,
kPlatformInfoSize,
} PlatformInfo;
struct _Platform
{
cl_platform_id platform_id;
char *platform_info[kPlatformInfoSize];
Device **devices;
int num_devices;
};
Platform *new_Platform(cl_platform_id platform_id)
{
Platform *self = (Platform*)malloc(sizeof(Platform));
self->platform_id = platform_id;
for(int i = 0; i < kPlatformInfoSize; ++i)
{
cl_int err;
char buf[256];
size_t str_size;
err = clGetPlatformInfo(platform_id,
CL_PLATFORM_INFO[i],
sizeof(buf),
(void*)buf,
&str_size);
if (PERR(err == CL_INVALID_PLATFORM, "Invalid Platform"))
goto bail;
if (PERR(err == CL_INVALID_VALUE, "Invalid Value"))
goto bail;
self->platform_info[i] = malloc(str_size);
memcpy(self->platform_info[i], buf, str_size);
}
cl_int err;
cl_uint num_devices;
cl_device_id *devices;
devices = (cl_device_id*)malloc(sizeof(cl_device_id) * MAX_DEVICES);
if((PERR(err = clGetDeviceIDs(platform_id,
CL_DEVICE_TYPE_GPU,
MAX_DEVICES,
devices,
&num_devices), "Unable to get device IDs")))
goto bail;
self->num_devices = num_devices;
self->devices = (Device**)malloc(sizeof(Device*) * num_devices);
for(unsigned int i = 0; i < num_devices; ++i)
self->devices[i] = new_Device(devices[i]);
free(devices);
return self;
bail:
delete_Platform(self);
return NULL;
}
void delete_Platform(Platform *self)
{
for(int i = 0; i < kPlatformInfoSize; ++i)
SFREE(self->platform_info[i]);
for(int i = 0; i < self->num_devices; ++i)
delete_Device(self->devices[i]);
SFREE(self);
}