mandelbrot.c 11 KB

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