mandelbrot.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /*
  2. * StarPU
  3. * Copyright (C) Université Bordeaux 1, CNRS 2008-2010 (see AUTHORS file)
  4. *
  5. * This program 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. * This program 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 <starpu.h>
  17. #ifdef STARPU_USE_OPENCL
  18. #include <starpu_opencl.h>
  19. #endif
  20. #include <sys/time.h>
  21. #ifdef STARPU_HAVE_X11
  22. #include <X11/Xlib.h>
  23. #include <X11/Xutil.h>
  24. int use_x11 = 1;
  25. #endif
  26. /* NB: The X11 code is inspired from the http://locklessinc.com/articles/mandelbrot/ article */
  27. static int nblocks = 16;
  28. static int height = 1280;
  29. static int width = 1600;
  30. static int maxIt = 20000;
  31. static double leftX = -0.745;
  32. static double rightX = -0.74375;
  33. static double topY = .15;
  34. static double bottomY = .14875;
  35. /*
  36. * X11 window management
  37. */
  38. #ifdef STARPU_HAVE_X11
  39. /* X11 data */
  40. static Display *dpy;
  41. static Window win;
  42. static XImage *bitmap;
  43. static GC gc;
  44. static KeySym Left=-1, Right, Down, Up, Alt ;
  45. static void exit_x11(void)
  46. {
  47. XDestroyImage(bitmap);
  48. XDestroyWindow(dpy, win);
  49. XCloseDisplay(dpy);
  50. }
  51. static void init_x11(int width, int height, unsigned *buffer)
  52. {
  53. /* Attempt to open the display */
  54. dpy = XOpenDisplay(NULL);
  55. /* Failure */
  56. if (!dpy)
  57. exit(0);
  58. unsigned long white = WhitePixel(dpy,DefaultScreen(dpy));
  59. unsigned long black = BlackPixel(dpy,DefaultScreen(dpy));
  60. win = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0,
  61. width, height, 0, black, white);
  62. /* We want to be notified when the window appears */
  63. XSelectInput(dpy, win, StructureNotifyMask);
  64. /* Make it appear */
  65. XMapWindow(dpy, win);
  66. XTextProperty tp;
  67. char name[128] = "Mandelbrot";
  68. char *n = name;
  69. Status st = XStringListToTextProperty(&n, 1, &tp);
  70. if (st)
  71. XSetWMName(dpy, win, &tp);
  72. /* Wait for the MapNotify event */
  73. XFlush(dpy);
  74. int depth = DefaultDepth(dpy, DefaultScreen(dpy));
  75. Visual *visual = DefaultVisual(dpy, DefaultScreen(dpy));
  76. /* Make bitmap */
  77. bitmap = XCreateImage(dpy, visual, depth,
  78. ZPixmap, 0, (char *)buffer,
  79. width, height, 32, 0);
  80. /* Init GC */
  81. gc = XCreateGC(dpy, win, 0, NULL);
  82. XSetForeground(dpy, gc, black);
  83. XSelectInput(dpy, win, ExposureMask | KeyPressMask | StructureNotifyMask);
  84. Atom wmDeleteMessage;
  85. wmDeleteMessage = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  86. XSetWMProtocols(dpy, win, &wmDeleteMessage, 1);
  87. Left = XStringToKeysym ("Left");
  88. Right = XStringToKeysym ("Right");
  89. Up = XStringToKeysym ("Up");
  90. Down = XStringToKeysym ("Down");
  91. Alt = XStringToKeysym ("Alt");
  92. }
  93. static int handle_events(void)
  94. {
  95. XEvent event;
  96. XNextEvent(dpy, &event);
  97. KeySym key;
  98. char text[255];
  99. if (event.type == KeyPress)
  100. {
  101. XLookupString(&event.xkey,text,255,&key,0);
  102. if (key == Left)
  103. {
  104. double widthX = rightX - leftX;
  105. leftX -= 0.25*widthX;
  106. rightX -= 0.25*widthX;
  107. }
  108. else if (key == Right)
  109. {
  110. double widthX = rightX - leftX;
  111. leftX += 0.25*widthX;
  112. rightX += 0.25*widthX;
  113. }
  114. else if (key == Up)
  115. {
  116. double heightY = topY - bottomY;
  117. topY += 0.25*heightY;
  118. bottomY += 0.25*heightY;
  119. }
  120. else if (key == Down)
  121. {
  122. double heightY = topY - bottomY;
  123. topY -= 0.25*heightY;
  124. bottomY -= 0.25*heightY;
  125. }
  126. else {
  127. double widthX = rightX - leftX;
  128. double heightY = topY - bottomY;
  129. if (text[0] == '-')
  130. {
  131. /* Zoom out */
  132. leftX -= 0.125*widthX;
  133. rightX += 0.125*widthX;
  134. topY += 0.125*heightY;
  135. bottomY -= 0.125*heightY;
  136. }
  137. else if (text[0] == '+')
  138. {
  139. /* Zoom in */
  140. leftX += 0.125*widthX;
  141. rightX -= 0.125*widthX;
  142. topY -= 0.125*heightY;
  143. bottomY += 0.125*heightY;
  144. }
  145. }
  146. if (text[0]=='q') {
  147. return -1;
  148. }
  149. }
  150. if (event.type==ButtonPress) {
  151. /* tell where the mouse Button was Pressed */
  152. printf("You pressed a button at (%i,%i)\n",
  153. event.xbutton.x,event.xbutton.y);
  154. }
  155. return 0;
  156. }
  157. #endif
  158. /*
  159. * OpenCL kernel
  160. */
  161. #ifdef STARPU_USE_OPENCL
  162. char *mandelbrot_opencl_src = "\
  163. #pragma OPENCL EXTENSION cl_khr_fp64 : enable\n\
  164. #define MIN(a,b) (((a)<(b))? (a) : (b)) \n\
  165. __kernel void mandelbrot_kernel(__global unsigned* a, \n\
  166. double leftX, double topY, \n\
  167. double stepX, double stepY, \n\
  168. int maxIt, int iby, int block_size, int width) \n\
  169. { \n\
  170. size_t id_x = get_global_id(0); \n\
  171. size_t id_y = get_global_id(1); \n\
  172. if ((id_x < width) && (id_y < block_size)) \n\
  173. { \n\
  174. double xc = leftX + id_x * stepX; \n\
  175. double yc = topY - (id_y + iby*block_size) * stepY; \n\
  176. int it; \n\
  177. double x,y; \n\
  178. x = y = (double)0.0; \n\
  179. for (it=0;it<maxIt;it++) \n\
  180. { \n\
  181. double x2 = x*x; \n\
  182. double y2 = y*y; \n\
  183. if (x2+y2 > 4.0) break; \n\
  184. double twoxy = (double)2.0*x*y; \n\
  185. x = x2 - y2 + xc; \n\
  186. y = twoxy + yc; \n\
  187. } \n\
  188. unsigned int v = MIN((1024*((float)(it)/(2000))), 256); \n\
  189. a[id_x + width * id_y] = (v<<16|(255-v)<<8); \n\
  190. } \n\
  191. }";
  192. static struct starpu_opencl_program opencl_programs;
  193. static void compute_block_opencl(void *descr[], void *cl_arg)
  194. {
  195. int iby, block_size;
  196. double stepX, stepY;
  197. starpu_unpack_cl_args(cl_arg, &iby, &block_size, &stepX, &stepY);
  198. cl_mem data = (cl_mem)STARPU_VECTOR_GET_PTR(descr[0]);
  199. cl_kernel kernel;
  200. cl_command_queue queue;
  201. int id = starpu_worker_get_id();
  202. int devid = starpu_worker_get_devid(id);
  203. starpu_opencl_load_kernel(&kernel, &queue, &opencl_programs, "mandelbrot_kernel", devid);
  204. clSetKernelArg(kernel, 0, sizeof(cl_mem), &data);
  205. clSetKernelArg(kernel, 1, sizeof(double), &leftX);
  206. clSetKernelArg(kernel, 2, sizeof(double), &topY);
  207. clSetKernelArg(kernel, 3, sizeof(double), &stepX);
  208. clSetKernelArg(kernel, 4, sizeof(double), &stepY);
  209. clSetKernelArg(kernel, 5, sizeof(int), &maxIt);
  210. clSetKernelArg(kernel, 6, sizeof(int), &iby);
  211. clSetKernelArg(kernel, 7, sizeof(int), &block_size);
  212. clSetKernelArg(kernel, 8, sizeof(int), &width);
  213. unsigned dim = 16;
  214. size_t local[2] = {dim, 1};
  215. size_t global[2] = {width, block_size};
  216. clEnqueueNDRangeKernel(queue, kernel, 2, NULL, global, local, 0, NULL, NULL);
  217. clFinish(queue);
  218. starpu_opencl_release_kernel(kernel);
  219. }
  220. #endif
  221. /*
  222. * CPU kernel
  223. */
  224. static void compute_block(void *descr[], void *cl_arg)
  225. {
  226. int ix, iy;
  227. int iby, block_size;
  228. double stepX, stepY;
  229. starpu_unpack_cl_args(cl_arg, &iby, &block_size, &stepX, &stepY);
  230. unsigned *data = (unsigned *)STARPU_VECTOR_GET_PTR(descr[0]);
  231. int local_iy;
  232. for (local_iy = 0; local_iy < block_size; local_iy++)
  233. {
  234. iy = iby*block_size + local_iy;
  235. for (ix = 0; ix < width; ix++)
  236. {
  237. double cx = leftX + ix * stepX;
  238. double cy = topY - iy * stepY;
  239. // Z = X+I*Y
  240. double x = 0;
  241. double y = 0;
  242. int it;
  243. for (it = 0; it < maxIt; it++)
  244. {
  245. double x2 = x*x;
  246. double y2 = y*y;
  247. // Stop iterations when |Z| > 2
  248. if (x2 + y2 > 4.0)
  249. break;
  250. double twoxy = 2.0*x*y;
  251. // Z = Z^2 + C
  252. x = x2 - y2 + cx;
  253. y = twoxy + cy;
  254. }
  255. unsigned int v = STARPU_MIN((1024*((float)(it)/(2000))), 256);
  256. data[ix + local_iy*width] = (v<<16|(255-v)<<8);
  257. }
  258. }
  259. }
  260. static starpu_codelet mandelbrot_cl = {
  261. .where = STARPU_CPU|STARPU_OPENCL,
  262. .type = STARPU_SEQ,
  263. .cpu_func = compute_block,
  264. #ifdef STARPU_USE_OPENCL
  265. .opencl_func = compute_block_opencl,
  266. #endif
  267. .nbuffers = 1
  268. };
  269. static void parse_args(int argc, char **argv)
  270. {
  271. int i;
  272. for (i = 1; i < argc; i++) {
  273. if (strcmp(argv[i], "-h") == 0) {
  274. fprintf(stderr, "Usage: %s [-h] [ -width 800] [-height 600] [-nblocks 16] [-no-x11] [-pos leftx:rightx:bottomy:topy]\n", argv[0]);
  275. exit(-1);
  276. }
  277. if (strcmp(argv[i], "-width") == 0) {
  278. char *argptr;
  279. width = strtol(argv[++i], &argptr, 10);
  280. }
  281. if (strcmp(argv[i], "-height") == 0) {
  282. char *argptr;
  283. height = strtol(argv[++i], &argptr, 10);
  284. }
  285. if (strcmp(argv[i], "-nblocks") == 0) {
  286. char *argptr;
  287. nblocks = strtol(argv[++i], &argptr, 10);
  288. }
  289. if (strcmp(argv[i], "-pos") == 0) {
  290. int ret = sscanf(argv[++i], "%lf:%lf:%lf:%lf", &leftX, &rightX, &bottomY, &topY);
  291. assert(ret == 4);
  292. }
  293. if (strcmp(argv[i], "-no-x11") == 0) {
  294. #ifdef STARPU_HAVE_X11
  295. use_x11 = 0;
  296. #endif
  297. }
  298. }
  299. }
  300. int main(int argc, char **argv)
  301. {
  302. parse_args(argc, argv);
  303. starpu_init(NULL);
  304. unsigned *buffer;
  305. starpu_data_malloc_pinned_if_possible((void **)&buffer, height*width*sizeof(unsigned));
  306. #ifdef STARPU_HAVE_X11
  307. if (use_x11)
  308. init_x11(width, height, buffer);
  309. #endif
  310. int block_size = height/nblocks;
  311. STARPU_ASSERT((height % nblocks) == 0);
  312. #ifdef STARPU_USE_OPENCL
  313. starpu_opencl_load_opencl_from_string(mandelbrot_opencl_src, &opencl_programs);
  314. #endif
  315. starpu_data_handle block_handles[nblocks];
  316. int iby;
  317. for (iby = 0; iby < nblocks; iby++)
  318. {
  319. unsigned *data = &buffer[iby*block_size*width];
  320. starpu_vector_data_register(&block_handles[iby], 0,
  321. (uintptr_t)data, block_size*width, sizeof(unsigned));
  322. }
  323. while (1)
  324. {
  325. double stepX = (rightX - leftX)/width;
  326. double stepY = (topY - bottomY)/height;
  327. struct timeval start, end;
  328. gettimeofday(&start, NULL);
  329. for (iby = 0; iby < nblocks; iby++)
  330. {
  331. starpu_insert_task(&mandelbrot_cl,
  332. STARPU_VALUE, &iby, sizeof(iby),
  333. STARPU_VALUE, &block_size, sizeof(block_size),
  334. STARPU_VALUE, &stepX, sizeof(stepX),
  335. STARPU_VALUE, &stepY, sizeof(stepY),
  336. STARPU_W, block_handles[iby],
  337. 0);
  338. }
  339. for (iby = 0; iby < nblocks; iby++)
  340. {
  341. starpu_data_acquire(block_handles[iby], STARPU_R);
  342. #ifdef STARPU_HAVE_X11
  343. if (use_x11)
  344. {
  345. XPutImage(dpy, win, gc, bitmap,
  346. 0, iby*block_size,
  347. 0, iby*block_size,
  348. width, block_size);
  349. }
  350. #endif
  351. starpu_data_release(block_handles[iby]);
  352. }
  353. gettimeofday(&end, NULL);
  354. double timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  355. fprintf(stderr, "Time to generate frame : %f ms\n", timing/1000.0);
  356. #ifdef STARPU_HAVE_X11
  357. if (use_x11 && handle_events())
  358. break;
  359. #endif
  360. }
  361. #ifdef STARPU_HAVE_X11
  362. if (use_x11)
  363. exit_x11();
  364. #endif
  365. for (iby = 0; iby < nblocks; iby++)
  366. starpu_data_unregister(block_handles[iby]);
  367. starpu_data_free_pinned_if_possible(buffer);
  368. starpu_shutdown();
  369. return 0;
  370. }