-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
329 lines (231 loc) · 8.53 KB
/
main.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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#include <stdlib.h>
// write()
#include <unistd.h>
// printf()
#include <stdio.h>
// file operations: open() etc
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
// strcp()
#include <string.h>
// mmap()
#include <sys/mman.h>
// OpenCL
#define CL_TARGET_OPENCL_VERSION 220
#include <CL/cl.h>
// gettimeofday()
#include <sys/time.h>
int save_to_file(double x0, double y0, double incr, int width, int height, int depth, char *buffer);
cl_int create_opencl_context(int pl, int dev, cl_context *context, cl_device_id *device);
cl_int create_opencl_program(cl_context context, const char *filename, cl_program *program);
// This is the device and platform that we are going to use.
#define PLATFORM 0
#define DEVICE 0
// The OpenCl program
#define PROGRAM "mandelbrot.cl"
#define PROCEDURE "render"
// We query the node for existing platforms.
#define MAX_PLATFORMS 16
#define MAX_DEVICES 16
int main(int argc, const char * argv[]) {
cl_context context;
cl_device_id device;
cl_int err, err0, err1, err2, err3, err4 = 0;
cl_command_queue cmd_queue;
cl_kernel kernel;
cl_program program;
cl_mem global_buffer;
int width = 1980;
int height = 1080;
int depth = 1024;
double x0 = -2.0;
double y0 = 1.0;
double incr = 0.002;
struct timeval t0,t1,t2,t3,t4;
// The arguments to the program, specifies the image.
if(argc < 7) {
printf("usage: mandelbrot <width> <height> <depth> <x-upper> <y-left> <k-incr>\n");
printf("will use deafult values\n");
} else {
width = atoi(argv[1]);
height = atoi(argv[2]);
depth = atoi(argv[3]);
x0 = atof(argv[4]);
y0 = atof(argv[5]);
incr = atof(argv[6]);
}
// We're building a rgb image and need three bytes per pixel.
size_t image_size = sizeof(char) * 3 * width * height;
// We're now ready to set up the OpenCL device for the computation.
//printf("Creating context on platform %d, deviec %d.\n", PLATFORM, DEVICE);
gettimeofday(&t0, NULL);
// This will create a context attached to a device.
err = create_opencl_context(PLATFORM, DEVICE, &context, &device);
if(err != CL_SUCCESS) {
printf("Failed to create context.\n");
return -1;
}
//printf("Creating queue for context.\n");
// Create a queue attached to the context and device.
cmd_queue = clCreateCommandQueueWithProperties(context, device, 0, &err);
if(err != CL_SUCCESS) {
printf("Failed to create queue.\n");
return -1;
}
//printf("Creating local buffer in context.\n");
// This is the image buffer on the device.
global_buffer = clCreateBuffer(context, CL_MEM_WRITE_ONLY, image_size, NULL, &err);
if(err != CL_SUCCESS) {
printf("Failed to create buffer.\n");
return -1;
}
//printf("Loading OpenCL source program\n");
// Load the OpenCL source program.
err = create_opencl_program(context, PROGRAM, &program);
if(err != CL_SUCCESS) {
printf("Failed to load program.\n");
return -1;
}
//printf("Creating OpenCL kernel\n");
kernel = clCreateKernel(program, PROCEDURE, &err);
if(err != CL_SUCCESS) {
printf("Failed to create kernel.\n");
return -1;
}
//printf("Setting kernel arguments\n");
// Now set up the arguments to our kernel: a pointer to the global buffer and information about the image.
err0 = clSetKernelArg(kernel, 0, sizeof(cl_mem), &global_buffer);
err1 = clSetKernelArg(kernel, 1, sizeof(int), &depth);
err2 = clSetKernelArg(kernel, 2, sizeof(double), &x0);
err3 = clSetKernelArg(kernel, 3, sizeof(double), &y0);
err4 = clSetKernelArg(kernel, 4, sizeof(double), &incr);
if(err0 != CL_SUCCESS || err1 != CL_SUCCESS || err2 != CL_SUCCESS || err3 != CL_SUCCESS || err4 != CL_SUCCESS ) {
printf("Failed to set arguments\n");
return -1;
}
//printf("Enqueue of kernels\n");
gettimeofday(&t1, NULL);
size_t device_work_size[2] = {width, height};
// This is where the OpenCL computations starts.
err = clEnqueueNDRangeKernel(cmd_queue, kernel, 2, NULL, device_work_size, NULL, 0, NULL, NULL);
if(err != CL_SUCCESS) {
printf("Failed run program.\n");
return -1;
}
// The host buffer to where we will copy the file
char *host_buffer = malloc(image_size);
// We now enqueue a read operation that will copy the content of the
// global_buffer (on the device) to our host_buffer (in main ḿemory).
// printf("Enqueue read global buffer to heap buffer\n");
err = clEnqueueReadBuffer(cmd_queue, global_buffer, CL_TRUE, 0, image_size, host_buffer, 0, NULL, NULL);
if(err != CL_SUCCESS) {
printf("Failed to read buffer.\n");
return -1;
}
// printf("Waiting until done.\n");
clFinish(cmd_queue);
gettimeofday(&t2, NULL);
// printf("Saving image to file .\n");
save_to_file(x0, y0, incr, width, height, depth, host_buffer);
// Now, eelease OpenCL objects.
clReleaseMemObject(global_buffer);
clReleaseCommandQueue(cmd_queue);
clReleaseContext(context);
gettimeofday(&t3, NULL);
printf("Platform setup in %ld ms\n", (t1.tv_sec - t0.tv_sec)*1000 + (t1.tv_usec - t0.tv_usec)/1000);
printf("Image rendered in %ld ms\n", (t2.tv_sec - t1.tv_sec)*1000 + (t2.tv_usec - t1.tv_usec)/1000);
printf("Saving file in %ld ms\n", (t3.tv_sec - t2.tv_sec)*1000 + (t3.tv_usec - t2.tv_usec)/1000);
printf("Total time %ld ms\n", (t3.tv_sec - t0.tv_sec)*1000 + (t3.tv_usec - t0.tv_usec)/1000);
return 0;
}
cl_int create_opencl_context(int pl, int dev, cl_context *context, cl_device_id *device){
cl_platform_id platform_id[MAX_PLATFORMS];
cl_device_id device_id[MAX_DEVICES];
cl_uint num_platforms = 0;
cl_uint num_devices = 0;
cl_int err = 0;
err = clGetPlatformIDs(MAX_PLATFORMS, &platform_id, &num_platforms);
if(err != CL_SUCCESS) {
printf("Failed to get platform id.\n");
return err;
}
if((num_platforms -1) < pl) {
printf("Platform %d not available.\n", pl);
return CL_INVALID_PLATFORM;
}
err = clGetDeviceIDs(platform_id[pl], CL_DEVICE_TYPE_DEFAULT, MAX_DEVICES, &device_id, &num_devices);
if(err != CL_SUCCESS) {
printf("Failed to get device id.\n");
return err;
}
if((num_devices -1) < dev) {
printf("Device %d on platform %d not available.\n", dev, pl);
return CL_INVALID_DEVICE;
}
*device = device_id[dev];
/*
{
// For the fun of it, let's see what we found
cl_char vendor_name[1024] = {0};
cl_char device_name[1024] = {0};
if(CL_SUCCESS == clGetDeviceInfo(*device, CL_DEVICE_VENDOR, sizeof(vendor_name), vendor_name, NULL)) {
printf("\t\tdevice vendor: %s\n", vendor_name);
}
if(CL_SUCCESS == clGetDeviceInfo(*device, CL_DEVICE_NAME, sizeof(device_name), device_name, NULL)) {
printf("\t\tdevice name: %s\n", device_name);
}
}
*/
/* Create a context with the selected device */
*context = clCreateContext(NULL, 1, device, NULL, NULL, &err);
if(err != CL_SUCCESS) {
printf("Failed to create context.\n");
return err;
}
return CL_SUCCESS;
}
cl_int create_opencl_program(cl_context context, const char *filename, cl_program *program) {
int err;
struct stat statbuf;
FILE *fh;
char *source;
fh = fopen(filename, "r");
if (fh == 0) {
printf("program not found %s\n", filename);
return CL_INVALID_PROGRAM;
}
stat(filename, &statbuf);
source = (char *) malloc(statbuf.st_size + 1);
fread(source, statbuf.st_size, 1, fh);
source[statbuf.st_size] = '\0';
// printf("Creating OpenCL program.\n");
*program = clCreateProgramWithSource(context, 1, (const char**)&source, NULL, &err);
if(err != CL_SUCCESS) {
printf("Failed to load program.\n");
return err;
}
// printf("Building OpenCL program.\n");
err = clBuildProgram(*program, NULL, 0, NULL, NULL, NULL);
if(err != CL_SUCCESS) {
printf("Failed to build program.\n");
return err;
}
// The source is no longer needed
free(source);
return CL_SUCCESS;
}
int save_to_file(double x0, double y0, double incr, int width, int height, int depth, char *buffer) {
// In the end everytghing will be in this file.
const char *filepath = "image.ppm";
// Open the output file for reading and writing.
int fd = open(filepath, O_RDWR | O_CREAT | O_TRUNC, (mode_t)0600);
if (fd== -1) {
printf("Failed to open file %s\n", filepath);
return -1;
}
dprintf(fd, "P6\n# Mandelbrot image: x0 = %f y0 = %f k = %f width = %d height = %d depth = %d\n%d %d\n255\n",
x0, y0, incr, width, height, depth, width, height);
write(fd, buffer, width*height*3);
close(fd);
}