/
JSTOpenCLContext.m
163 lines (122 loc) · 4.59 KB
/
JSTOpenCLContext.m
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#import "JSTOpenCLContext.h"
#import "JSTOpenCLProgram.h"
@interface JSTOpenCLContext ()
- (BOOL)setupContext;
@end
@implementation JSTOpenCLContext
@synthesize computeContext;
@synthesize computeCommands;
@synthesize computeDeviceId;
@synthesize computeDeviceType;
- (id)init {
self = [super init];
if (!self) {
return nil;
}
if ([self setupContext] == NO) {
[self release];
return nil;
}
return self;
}
- (void)dealloc {
clReleaseCommandQueue(computeCommands);
[super dealloc];
}
- (BOOL)setupContext {
int err = 0;
computeDeviceType = CL_DEVICE_TYPE_CPU;
err = clGetDeviceIDs(NULL, computeDeviceType, 1, &computeDeviceId, NULL);
if (err != CL_SUCCESS) {
NSLog( @"Error: Failed to locate compute device!" );
return NO;
}
// Create a context containing the compute device(s)
computeContext = clCreateContext(0, 1, &computeDeviceId, clLogMessagesToStdoutAPPLE, NULL, &err);
if (!computeContext) {
NSLog( @"Error: Failed to create a compute context!" );
return NO;
}
unsigned int device_count;
cl_device_id device_ids[16];
size_t returned_size;
err = clGetContextInfo(computeContext, CL_CONTEXT_DEVICES, sizeof(device_ids), device_ids, &returned_size);
if(err) {
NSLog( @"Error: Failed to retrieve compute devices for context!" );
return NO;
}
device_count = (unsigned int)(returned_size / sizeof(cl_device_id));
int i = 0;
//int device_found = 0;
cl_device_type device_type;
for(i = 0; i < device_count; i++) {
clGetDeviceInfo(device_ids[i], CL_DEVICE_TYPE, sizeof(cl_device_type), &device_type, NULL);
if(device_type == computeDeviceType) {
computeDeviceId = device_ids[i];
//device_found = 1;
break;
}
}
// Create a command queue
//
computeCommands = clCreateCommandQueue(computeContext, computeDeviceId, 0, &err);
if (!computeCommands) {
NSLog( @"Error: Failed to create a command queue!" );
return NO;
}
// Report the device vendor and device name
//
cl_char vendor_name[1024] = {0};
cl_char device_name[1024] = {0};
err = clGetDeviceInfo(computeDeviceId, CL_DEVICE_VENDOR, sizeof(vendor_name), vendor_name, &returned_size);
err|= clGetDeviceInfo(computeDeviceId, CL_DEVICE_NAME, sizeof(device_name), device_name, &returned_size);
if (err != CL_SUCCESS) {
NSLog( @"Error: Failed to retrieve device info!\n" );
return NO;
}
//debug( @"Connecting to %s %s...\n", vendor_name, device_name );
return YES;
}
- (JSTOpenCLProgram*)programWithSource:(NSString*) sourceCode; {
JSTOpenCLProgram *program = [[JSTOpenCLProgram alloc] initWithContext:self];
program.sourceCode = sourceCode;
return [program autorelease];
}
- (JSTOpenCLBuffer*)bufferWithSize:(size_t) size attributes:(cl_bitfield)attribs {
JSTOpenCLBuffer *buffer = [[JSTOpenCLBuffer alloc] initWithContext:self memory:NULL size:size attributes:attribs];
return [buffer autorelease];
}
- (JSTOpenCLBuffer*)bufferWithMemory:(void*)memory size:(size_t)size attributes:(cl_bitfield)attribs {
JSTOpenCLBuffer *buffer = [[JSTOpenCLBuffer alloc] initWithContext: self memory:memory size:size attributes:attribs];
return [buffer autorelease];
}
- (void)finish { // waits until all processing has finished
clFinish( computeCommands );
}
- (NSUInteger)maximumWorkItemDimensions {
cl_uint maximumWorkItemDimensions;
clGetDeviceInfo(computeDeviceId, CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS, sizeof(cl_uint), &maximumWorkItemDimensions, NULL);
return maximumWorkItemDimensions;
}
- (NSUInteger)maximum2DImageWidth {
size_t width;
clGetDeviceInfo(computeDeviceId, CL_DEVICE_IMAGE2D_MAX_WIDTH, sizeof(size_t), &width, NULL);
return width;
}
- (NSUInteger)maximum2DImageHeight {
size_t height;
clGetDeviceInfo(computeDeviceId, CL_DEVICE_IMAGE2D_MAX_HEIGHT, sizeof(size_t), &height, NULL);
return height;
}
- (NSArray*)maximumWorkItemSizes {
NSUInteger numberOfDimensions = [self maximumWorkItemDimensions];
size_t *sizes = malloc(numberOfDimensions * sizeof(cl_uint));
clGetDeviceInfo(computeDeviceId, CL_DEVICE_MAX_WORK_GROUP_SIZE, numberOfDimensions * sizeof( size_t ), sizes, NULL);
NSMutableArray *sizeArray = [NSMutableArray array];
for ( NSInteger i = 0; i < numberOfDimensions; i++ ) {
[sizeArray addObject: [NSNumber numberWithInteger: sizes[i]]];
}
free(sizes);
return sizeArray;
}
@end