mandelbrot.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010,2011 University of Bordeaux
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <unistd.h>
  20. /* Uncomment this to activate X11 display */
  21. #define USE_X11
  22. #define SHORT_LOG 1
  23. #define ROUND_ROBIN
  24. #ifdef USE_X11
  25. #include <X11/Xlib.h>
  26. #include <X11/Xutil.h>
  27. int use_x11 = 1;
  28. #else
  29. int use_x11 = 0;
  30. #endif
  31. int demo = 0;
  32. int frames = -1;
  33. #include <pthread.h>
  34. #include <assert.h>
  35. #include <sys/time.h>
  36. #include <CL/cl.h>
  37. #define error(...) do { fprintf(stderr, "Error: " __VA_ARGS__); exit(EXIT_FAILURE); } while(0)
  38. #define check(err, str) do { if(err != CL_SUCCESS) { fprintf(stderr, "OpenCL Error (%d): %s\n",err, str); exit(EXIT_FAILURE); }} while(0)
  39. #ifdef UNUSED
  40. #elif defined(__GNUC__)
  41. # define UNUSED(x) UNUSED_ ## x __attribute__((unused))
  42. #else
  43. # define UNUSED(x) x
  44. #endif
  45. const char * kernel_src = "\
  46. #pragma OPENCL EXTENSION cl_khr_fp64 : enable\n\
  47. #define TYPE double \n\
  48. #define MIN(a,b) (((a)<(b))? (a) : (b))\n\
  49. __kernel void mandelbrot_kernel(__global uint * a,\n\
  50. TYPE leftX, TYPE topY,\n\
  51. TYPE stepX, TYPE stepY,\n\
  52. uint maxIt, uint iby, uint block_size)\n\
  53. {\n\
  54. TYPE xc = leftX + get_global_id(0) * stepX;\n\
  55. TYPE yc = iby*block_size*stepY + topY + get_global_id(1) * stepY;\n\
  56. int it;\n\
  57. TYPE x,y;\n\
  58. x = y = (TYPE)0.0;\n\
  59. for (it=0;it<maxIt;it++)\n\
  60. {\n\
  61. TYPE x2 = x*x;\n\
  62. TYPE y2 = y*y;\n\
  63. if (x2+y2 > (TYPE)4) break; \n\
  64. TYPE twoxy = (TYPE)2*x*y;\n\
  65. x = x2 - y2 + xc;\n\
  66. y = twoxy + yc;\n\
  67. }\n\
  68. uint v = MIN((1024*((float)(it)/(2000))), 256);\n\
  69. a[get_global_id(0) + get_global_id(1)*get_global_size(0)] = (v<<16|(255-v)<<8); \n\
  70. }";
  71. static cl_uint nblocks = 8;
  72. static cl_uint height = 768;
  73. static cl_uint width = 1024;
  74. static cl_uint maxIt = 20000;
  75. static cl_uint group_size = 64;
  76. static double leftX = -0.745;
  77. static double rightX = -0.74375;
  78. static double topY = .15;
  79. static double bottomY = .14875;
  80. #ifdef USE_X11
  81. /* X11 data */
  82. static Display *dpy;
  83. static Window win;
  84. static XImage *bitmap;
  85. static GC gc;
  86. static KeySym Left=-1, Right, Down, Up, Alt ;
  87. static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  88. static void exit_x11(void)
  89. {
  90. XDestroyImage(bitmap);
  91. XDestroyWindow(dpy, win);
  92. XCloseDisplay(dpy);
  93. }
  94. static void init_x11(int width, int height, cl_uint *buffer)
  95. {
  96. /* Attempt to open the display */
  97. dpy = XOpenDisplay(NULL);
  98. /* Failure */
  99. if (!dpy)
  100. exit(0);
  101. unsigned long white = WhitePixel(dpy,DefaultScreen(dpy));
  102. unsigned long black = BlackPixel(dpy,DefaultScreen(dpy));
  103. win = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0,
  104. width, height, 0, black, white);
  105. /* We want to be notified when the window appears */
  106. XSelectInput(dpy, win, StructureNotifyMask);
  107. /* Make it appear */
  108. XMapWindow(dpy, win);
  109. XTextProperty tp;
  110. char name[128] = "Mandelbrot";
  111. char *n = name;
  112. Status st = XStringListToTextProperty(&n, 1, &tp);
  113. if (st)
  114. XSetWMName(dpy, win, &tp);
  115. /* Wait for the MapNotify event */
  116. XFlush(dpy);
  117. int depth = DefaultDepth(dpy, DefaultScreen(dpy));
  118. Visual *visual = DefaultVisual(dpy, DefaultScreen(dpy));
  119. /* Make bitmap */
  120. bitmap = XCreateImage(dpy, visual, depth,
  121. ZPixmap, 0, (char *)buffer,
  122. width, height, 32, 0);
  123. /* Init GC */
  124. gc = XCreateGC(dpy, win, 0, NULL);
  125. XSetForeground(dpy, gc, black);
  126. XSelectInput(dpy, win, ExposureMask | KeyPressMask | StructureNotifyMask);
  127. Atom wmDeleteMessage;
  128. wmDeleteMessage = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  129. XSetWMProtocols(dpy, win, &wmDeleteMessage, 1);
  130. Left = XStringToKeysym ("Left");
  131. Right = XStringToKeysym ("Right");
  132. Up = XStringToKeysym ("Up");
  133. Down = XStringToKeysym ("Down");
  134. Alt = XStringToKeysym ("Alt");
  135. }
  136. static int handle_events(void)
  137. {
  138. XEvent event;
  139. XNextEvent(dpy, &event);
  140. KeySym key;
  141. char text[255];
  142. double coef = 0.05;
  143. if (event.type == KeyPress)
  144. {
  145. XLookupString(&event.xkey,text,255,&key,0);
  146. if (key == Left)
  147. {
  148. double widthX = rightX - leftX;
  149. leftX -= coef*widthX;
  150. rightX -= coef*widthX;
  151. }
  152. else if (key == Right)
  153. {
  154. double widthX = rightX - leftX;
  155. leftX += coef*widthX;
  156. rightX += coef*widthX;
  157. }
  158. else if (key == Down)
  159. {
  160. double heightY = topY - bottomY;
  161. topY += coef*heightY;
  162. bottomY += coef*heightY;
  163. }
  164. else if (key == Up)
  165. {
  166. double heightY = topY - bottomY;
  167. topY -= coef*heightY;
  168. bottomY -= coef*heightY;
  169. }
  170. else {
  171. double widthX = rightX - leftX;
  172. double heightY = topY - bottomY;
  173. if (text[0] == '-')
  174. {
  175. /* Zoom out */
  176. leftX -= (coef/2)*widthX;
  177. rightX += (coef/2)*widthX;
  178. topY += (coef/2)*heightY;
  179. bottomY -= (coef/2)*heightY;
  180. }
  181. else if (text[0] == '+')
  182. {
  183. /* Zoom in */
  184. leftX += (coef/2)*widthX;
  185. rightX -= (coef/2)*widthX;
  186. topY -= (coef/2)*heightY;
  187. bottomY += (coef/2)*heightY;
  188. }
  189. }
  190. if (text[0]=='q') {
  191. return -1;
  192. }
  193. }
  194. if (event.type==ButtonPress) {
  195. /* tell where the mouse Button was Pressed */
  196. printf("You pressed a button at (%i,%i)\n",
  197. event.xbutton.x,event.xbutton.y);
  198. }
  199. return 0;
  200. }
  201. #endif //USE_X11
  202. static void parse_args(int argc, char **argv)
  203. {
  204. int i;
  205. for (i = 1; i < argc; i++) {
  206. if (strcmp(argv[i], "-h") == 0) {
  207. fprintf(stderr, "Usage: %s [-h] [ -width 1024] [-height 768] [-nblocks 16] [-group_size 64] [-no-x11] [-demo] [-frames N] [-pos leftx:rightx:bottomy:topy]\n", argv[0]);
  208. exit(-1);
  209. }
  210. if (strcmp(argv[i], "-width") == 0) {
  211. char *argptr;
  212. width = strtol(argv[++i], &argptr, 10);
  213. }
  214. if (strcmp(argv[i], "-frames") == 0) {
  215. char *argptr;
  216. frames = strtol(argv[++i], &argptr, 10);
  217. }
  218. if (strcmp(argv[i], "-height") == 0) {
  219. char *argptr;
  220. height = strtol(argv[++i], &argptr, 10);
  221. }
  222. if (strcmp(argv[i], "-group_size") == 0) {
  223. char *argptr;
  224. group_size = strtol(argv[++i], &argptr, 10);
  225. }
  226. if (strcmp(argv[i], "-nblocks") == 0) {
  227. char *argptr;
  228. nblocks = strtol(argv[++i], &argptr, 10);
  229. }
  230. if (strcmp(argv[i], "-pos") == 0) {
  231. int ret = sscanf(argv[++i], "%lf:%lf:%lf:%lf", &leftX, &rightX, &bottomY, &topY);
  232. assert(ret == 4);
  233. }
  234. if (strcmp(argv[i], "-demo") == 0) {
  235. demo = 1;
  236. leftX = -50.22749575062760;
  237. rightX = 48.73874621262927;
  238. topY = -49.35016705749115;
  239. bottomY = 49.64891691946615;
  240. }
  241. if (strcmp(argv[i], "-no-x11") == 0) {
  242. #ifdef USE_X11
  243. use_x11 = 0;
  244. #endif
  245. }
  246. }
  247. }
  248. int main(int argc, char **argv) {
  249. #define MAX_DEVICES 20
  250. cl_platform_id platforms[15];
  251. cl_uint num_platforms;
  252. cl_device_id devices[15];
  253. cl_uint num_devices;
  254. cl_context context;
  255. cl_program program;
  256. cl_kernel kernel;
  257. cl_command_queue cq[MAX_DEVICES];
  258. cl_int err;
  259. cl_uint i;
  260. parse_args(argc, argv);
  261. cl_uint block_size = height/nblocks;
  262. assert((height % nblocks) == 0);
  263. assert((width % group_size) == 0);
  264. err = clGetPlatformIDs(0, NULL, &num_platforms);
  265. if (num_platforms == 0) {
  266. printf("No OpenCL platform found. If you use SOCL, this could mean StarPU wasn't configured for OpenCL. Try disabling CUDA support in StarPU (export STARPU_NCUDA=0).\n");
  267. exit(0);
  268. }
  269. err = clGetPlatformIDs(sizeof(platforms)/sizeof(cl_platform_id), platforms, NULL);
  270. check(err, "clGetPlatformIDs");
  271. unsigned int platform_idx;
  272. for (platform_idx=0; platform_idx<num_platforms; platform_idx++) {
  273. err = clGetDeviceIDs(platforms[platform_idx], CL_DEVICE_TYPE_GPU, sizeof(devices)/sizeof(cl_device_id), devices, &num_devices);
  274. check(err, "clGetDeviceIDs");
  275. if (num_devices != 0)
  276. break;
  277. }
  278. if (num_devices == 0)
  279. error("No OpenCL device found\n");
  280. cl_context_properties properties[] = {CL_CONTEXT_PLATFORM, (cl_context_properties)platforms[platform_idx], 0};
  281. context = clCreateContext(properties, num_devices, devices, NULL, NULL, &err);
  282. check(err, "clCreateContext");
  283. program = clCreateProgramWithSource(context, 1, &kernel_src, NULL, &err);
  284. check(err, "clCreateProgram");
  285. err = clBuildProgram(program, 0, NULL, NULL, NULL, NULL);
  286. check(err, "clBuildProgram");
  287. kernel = clCreateKernel(program, "mandelbrot_kernel", &err);
  288. check(err, "clCreateKernel");
  289. for (i=0; i<num_devices; i++)
  290. cq[i] = clCreateCommandQueue(context, devices[i], CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE, &err);
  291. check(err, "clCreateCommandQueue");
  292. cl_uint *buffer;
  293. buffer = malloc(height*width*sizeof(cl_uint));
  294. #ifdef USE_X11
  295. if (use_x11)
  296. init_x11(width, height, buffer);
  297. #endif // USE_X11
  298. cl_mem block_handles[nblocks];
  299. cl_uint iby;
  300. for (iby = 0; iby < nblocks; iby++) {
  301. cl_uint *data = &buffer[iby*block_size*width];
  302. block_handles[iby] = clCreateBuffer(context, CL_MEM_WRITE_ONLY | CL_MEM_USE_HOST_PTR, block_size*width*sizeof(cl_uint), data, &err);
  303. }
  304. int stop = 0;
  305. int frame = 0;
  306. while (!stop) {
  307. struct timeval start, end;
  308. gettimeofday(&start, NULL);
  309. if (frames != -1) {
  310. frame++;
  311. stop = (frame == frames);
  312. }
  313. double stepX = (rightX - leftX)/width;
  314. double stepY = (topY - bottomY)/height;
  315. cl_event ker_events[nblocks];
  316. void * ptrs[nblocks];
  317. for (iby = 0; iby < nblocks; iby++) {
  318. err = clSetKernelArg(kernel, 0, sizeof(cl_mem), &block_handles[iby]);
  319. check(err, "clSetKernelArg out");
  320. err = clSetKernelArg(kernel, 1, sizeof(cl_double), &leftX);
  321. check(err, "clSetKernelArg leftX");
  322. err = clSetKernelArg(kernel, 2, sizeof(cl_double), &topY);
  323. check(err, "clSetKernelArg topY");
  324. err = clSetKernelArg(kernel, 3, sizeof(cl_double), &stepX);
  325. check(err, "clSetKernelArg leftX");
  326. err = clSetKernelArg(kernel, 4, sizeof(cl_double), &stepY);
  327. check(err, "clSetKernelArg topY");
  328. err = clSetKernelArg(kernel, 5, sizeof(cl_uint), &maxIt);
  329. check(err, "clSetKernelArg maxIt");
  330. err = clSetKernelArg(kernel, 6, sizeof(cl_uint), &iby);
  331. check(err, "clSetKernelArg iby");
  332. err = clSetKernelArg(kernel, 7, sizeof(cl_uint), &block_size);
  333. check(err, "clSetKernelArg block_size");
  334. size_t local[3] = {group_size, 1, 1};
  335. size_t global[3] = {width, block_size, 1};
  336. #ifdef ROUND_ROBIN
  337. int dev = iby % num_devices;
  338. #else
  339. int dev = 0;
  340. #endif
  341. err = clEnqueueNDRangeKernel(cq[dev], kernel, 3, NULL, global, local, 0, NULL, &ker_events[iby]);
  342. check(err, "clEnqueueNDRangeKernel");
  343. }
  344. for (iby = 0; iby < nblocks; iby++) {
  345. #ifdef ROUND_ROBIN
  346. int dev = iby % num_devices;
  347. #else
  348. int dev = 0;
  349. #endif
  350. ptrs[iby] = clEnqueueMapBuffer(cq[dev], block_handles[iby], CL_FALSE,CL_MAP_READ, 0, block_size*width*sizeof(cl_uint), 1, &ker_events[iby], NULL, NULL);
  351. }
  352. #ifdef ROUND_ROBIN
  353. for (i = 0; i < num_devices; i++)
  354. clFinish(cq[i]);
  355. #else
  356. clFinish(cq[0]);
  357. #endif
  358. gettimeofday(&end, NULL);
  359. double timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  360. #ifdef SHORT_LOG
  361. fprintf(stderr, "%f\n", timing/1000.0);
  362. #else
  363. fprintf(stderr, "Time to generate frame : %f ms\n", timing/1000.0);
  364. fprintf(stderr, "%14.14f:%14.14f:%14.14f:%14.14f\n", leftX, rightX, bottomY, topY);
  365. #endif
  366. #ifdef USE_X11
  367. if (use_x11) {
  368. for (iby = 0; iby < nblocks; iby++) {
  369. pthread_mutex_lock(&mutex);
  370. XPutImage(dpy, win, gc, bitmap,
  371. 0, iby*block_size,
  372. 0, iby*block_size,
  373. width, block_size);
  374. pthread_mutex_unlock(&mutex);
  375. }
  376. }
  377. #endif
  378. for (iby = 0; iby < nblocks; iby++) {
  379. #ifdef ROUND_ROBIN
  380. int dev = iby % num_devices;
  381. #else
  382. int dev = 0;
  383. #endif
  384. clEnqueueUnmapMemObject(cq[dev], block_handles[iby], ptrs[iby], 0, NULL, NULL);
  385. clReleaseEvent(ker_events[iby]);
  386. }
  387. if (demo) {
  388. /* Zoom in */
  389. double zoom_factor = 0.05;
  390. double widthX = rightX - leftX;
  391. double heightY = topY - bottomY;
  392. leftX += (zoom_factor/2)*widthX;
  393. rightX -= (zoom_factor/2)*widthX;
  394. topY -= (zoom_factor/2)*heightY;
  395. bottomY += (zoom_factor/2)*heightY;
  396. }
  397. else {
  398. #ifdef USE_X11
  399. if (use_x11) {
  400. handle_events();
  401. }
  402. #else
  403. stop = 1;
  404. #endif
  405. }
  406. }
  407. #ifdef USE_X11
  408. if (use_x11)
  409. exit_x11();
  410. #endif
  411. for (iby = 0; iby < nblocks; iby++) {
  412. clReleaseMemObject(block_handles[iby]);
  413. }
  414. for (i=0; i<num_devices; i++)
  415. clReleaseCommandQueue(cq[i]);
  416. clReleaseKernel(kernel);
  417. clReleaseProgram(program);
  418. clReleaseContext(context);
  419. return 0;
  420. }