mandelbrot.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2011, 2014 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012, 2013 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. #include <math.h>
  19. #include <limits.h>
  20. #ifdef STARPU_HAVE_X11
  21. #include <X11/Xlib.h>
  22. #include <X11/Xutil.h>
  23. int use_x11 = 1;
  24. #endif
  25. int demo = 0;
  26. static double demozoom = 0.05;
  27. /* NB: The X11 code is inspired from the http://locklessinc.com/articles/mandelbrot/ article */
  28. static int nblocks = 20;
  29. static int height = 400;
  30. static int width = 640;
  31. static int maxIt = 20000; /* max number of iteration in the Mandelbrot function */
  32. static int niter = -1; /* number of loops in case we don't use X11, -1 means infinite */
  33. static int use_spmd = 0;
  34. static double leftX = -0.745;
  35. static double rightX = -0.74375;
  36. static double topY = .15;
  37. static double bottomY = .14875;
  38. /*
  39. * X11 window management
  40. */
  41. #ifdef STARPU_HAVE_X11
  42. /* X11 data */
  43. static Display *dpy;
  44. static Window win;
  45. static XImage *bitmap;
  46. static GC gc;
  47. static KeySym Left=-1, Right, Down, Up, Alt ;
  48. static void exit_x11(void)
  49. {
  50. XDestroyImage(bitmap);
  51. XDestroyWindow(dpy, win);
  52. XCloseDisplay(dpy);
  53. }
  54. static void init_x11(int width, int height, unsigned *buffer)
  55. {
  56. /* Attempt to open the display */
  57. dpy = XOpenDisplay(NULL);
  58. /* Failure */
  59. if (!dpy)
  60. exit(0);
  61. unsigned long white = WhitePixel(dpy,DefaultScreen(dpy));
  62. unsigned long black = BlackPixel(dpy,DefaultScreen(dpy));
  63. win = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0,
  64. width, height, 0, black, white);
  65. /* We want to be notified when the window appears */
  66. XSelectInput(dpy, win, StructureNotifyMask);
  67. /* Make it appear */
  68. XMapWindow(dpy, win);
  69. XTextProperty tp;
  70. char name[128] = "Mandelbrot - StarPU";
  71. char *n = name;
  72. Status st = XStringListToTextProperty(&n, 1, &tp);
  73. if (st)
  74. XSetWMName(dpy, win, &tp);
  75. /* Wait for the MapNotify event */
  76. XFlush(dpy);
  77. int depth = DefaultDepth(dpy, DefaultScreen(dpy));
  78. Visual *visual = DefaultVisual(dpy, DefaultScreen(dpy));
  79. /* Make bitmap */
  80. bitmap = XCreateImage(dpy, visual, depth,
  81. ZPixmap, 0, (char *)buffer,
  82. width, height, 32, 0);
  83. /* Init GC */
  84. gc = XCreateGC(dpy, win, 0, NULL);
  85. XSetForeground(dpy, gc, black);
  86. XSelectInput(dpy, win, ExposureMask | KeyPressMask | StructureNotifyMask);
  87. Atom wmDeleteMessage;
  88. wmDeleteMessage = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  89. XSetWMProtocols(dpy, win, &wmDeleteMessage, 1);
  90. Left = XStringToKeysym ("Left");
  91. Right = XStringToKeysym ("Right");
  92. Up = XStringToKeysym ("Up");
  93. Down = XStringToKeysym ("Down");
  94. Alt = XStringToKeysym ("Alt");
  95. }
  96. static int handle_events(void)
  97. {
  98. XEvent event;
  99. XNextEvent(dpy, &event);
  100. KeySym key;
  101. char text[255];
  102. if (event.type == KeyPress)
  103. {
  104. XLookupString(&event.xkey,text,255,&key,0);
  105. if (key == Left)
  106. {
  107. double widthX = rightX - leftX;
  108. leftX -= 0.25*widthX;
  109. rightX -= 0.25*widthX;
  110. }
  111. else if (key == Right)
  112. {
  113. double widthX = rightX - leftX;
  114. leftX += 0.25*widthX;
  115. rightX += 0.25*widthX;
  116. }
  117. else if (key == Up)
  118. {
  119. double heightY = topY - bottomY;
  120. topY += 0.25*heightY;
  121. bottomY += 0.25*heightY;
  122. }
  123. else if (key == Down)
  124. {
  125. double heightY = topY - bottomY;
  126. topY -= 0.25*heightY;
  127. bottomY -= 0.25*heightY;
  128. }
  129. else
  130. {
  131. double widthX = rightX - leftX;
  132. double heightY = topY - bottomY;
  133. if (text[0] == '-')
  134. {
  135. /* Zoom out */
  136. leftX -= 0.125*widthX;
  137. rightX += 0.125*widthX;
  138. topY += 0.125*heightY;
  139. bottomY -= 0.125*heightY;
  140. }
  141. else if (text[0] == '+')
  142. {
  143. /* Zoom in */
  144. leftX += 0.125*widthX;
  145. rightX -= 0.125*widthX;
  146. topY -= 0.125*heightY;
  147. bottomY += 0.125*heightY;
  148. }
  149. }
  150. if (text[0]=='q')
  151. {
  152. return -1;
  153. }
  154. }
  155. if (event.type==ButtonPress)
  156. {
  157. /* tell where the mouse Button was Pressed */
  158. printf("You pressed a button at (%i,%i)\n",
  159. event.xbutton.x,event.xbutton.y);
  160. }
  161. return 0;
  162. }
  163. #endif
  164. /*
  165. * OpenCL kernel
  166. */
  167. #ifdef STARPU_USE_OPENCL
  168. char *mandelbrot_opencl_src = "\
  169. #pragma OPENCL EXTENSION cl_khr_fp64 : enable\n\
  170. #define MIN(a,b) (((a)<(b))? (a) : (b)) \n\
  171. __kernel void mandelbrot_kernel(__global unsigned* a, \n\
  172. double leftX, double topY, \n\
  173. double stepX, double stepY, \n\
  174. int maxIt, int iby, int block_size, int width) \n\
  175. { \n\
  176. size_t id_x = get_global_id(0); \n\
  177. size_t id_y = get_global_id(1); \n\
  178. if ((id_x < width) && (id_y < block_size)) \n\
  179. { \n\
  180. double xc = leftX + id_x * stepX; \n\
  181. double yc = topY - (id_y + iby*block_size) * stepY; \n\
  182. int it; \n\
  183. double x,y; \n\
  184. x = y = (double)0.0; \n\
  185. for (it=0;it<maxIt;it++) \n\
  186. { \n\
  187. double x2 = x*x; \n\
  188. double y2 = y*y; \n\
  189. if (x2+y2 > 4.0) break; \n\
  190. double twoxy = (double)2.0*x*y; \n\
  191. x = x2 - y2 + xc; \n\
  192. y = twoxy + yc; \n\
  193. } \n\
  194. unsigned int v = MIN((1024*((float)(it)/(2000))), 256); \n\
  195. a[id_x + width * id_y] = (v<<16|(255-v)<<8); \n\
  196. } \n\
  197. }";
  198. static struct starpu_opencl_program opencl_programs;
  199. static void compute_block_opencl(void *descr[], void *cl_arg)
  200. {
  201. int iby, block_size;
  202. double stepX, stepY;
  203. int *pcnt; /* unused for CUDA tasks */
  204. starpu_codelet_unpack_args(cl_arg, &iby, &block_size, &stepX, &stepY, &pcnt);
  205. cl_mem data = (cl_mem)STARPU_VECTOR_GET_DEV_HANDLE(descr[0]);
  206. cl_kernel kernel;
  207. cl_command_queue queue;
  208. cl_event event;
  209. int id = starpu_worker_get_id();
  210. int devid = starpu_worker_get_devid(id);
  211. starpu_opencl_load_kernel(&kernel, &queue, &opencl_programs, "mandelbrot_kernel", devid);
  212. clSetKernelArg(kernel, 0, sizeof(data), &data);
  213. clSetKernelArg(kernel, 1, sizeof(leftX), &leftX);
  214. clSetKernelArg(kernel, 2, sizeof(topY), &topY);
  215. clSetKernelArg(kernel, 3, sizeof(stepX), &stepX);
  216. clSetKernelArg(kernel, 4, sizeof(stepY), &stepY);
  217. clSetKernelArg(kernel, 5, sizeof(maxIt), &maxIt);
  218. clSetKernelArg(kernel, 6, sizeof(iby), &iby);
  219. clSetKernelArg(kernel, 7, sizeof(block_size), &block_size);
  220. clSetKernelArg(kernel, 8, sizeof(width), &width);
  221. unsigned dim = 16;
  222. size_t local[2] = {dim, 1};
  223. size_t global[2] = {width, block_size};
  224. clEnqueueNDRangeKernel(queue, kernel, 2, NULL, global, local, 0, NULL, NULL);
  225. starpu_opencl_release_kernel(kernel);
  226. }
  227. #endif
  228. /*
  229. * CPU kernel
  230. */
  231. static void compute_block(void *descr[], void *cl_arg)
  232. {
  233. int ix, iy;
  234. int iby, block_size;
  235. double stepX, stepY;
  236. int *pcnt; /* unused for sequential tasks */
  237. starpu_codelet_unpack_args(cl_arg, &iby, &block_size, &stepX, &stepY, &pcnt);
  238. unsigned *data = (unsigned *)STARPU_VECTOR_GET_PTR(descr[0]);
  239. int local_iy;
  240. for (local_iy = 0; local_iy < block_size; local_iy++)
  241. {
  242. iy = iby*block_size + local_iy;
  243. for (ix = 0; ix < width; ix++)
  244. {
  245. double cx = leftX + ix * stepX;
  246. double cy = topY - iy * stepY;
  247. /* Z = X+I*Y */
  248. double x = 0;
  249. double y = 0;
  250. int it;
  251. for (it = 0; it < maxIt; it++)
  252. {
  253. double x2 = x*x;
  254. double y2 = y*y;
  255. /* Stop iterations when |Z| > 2 */
  256. if (x2 + y2 > 4.0)
  257. break;
  258. double twoxy = 2.0*x*y;
  259. /* Z = Z^2 + C */
  260. x = x2 - y2 + cx;
  261. y = twoxy + cy;
  262. }
  263. unsigned int v = STARPU_MIN((1024*((float)(it)/(2000))), 256);
  264. data[ix + local_iy*width] = (v<<16|(255-v)<<8);
  265. }
  266. }
  267. }
  268. static void compute_block_spmd(void *descr[], void *cl_arg)
  269. {
  270. int iby, block_size;
  271. double stepX, stepY;
  272. int *pcnt;
  273. starpu_codelet_unpack_args(cl_arg, &iby, &block_size, &stepX, &stepY, &pcnt);
  274. unsigned *data = (unsigned *)STARPU_VECTOR_GET_PTR(descr[0]);
  275. int ix, iy; /* global coordinates */
  276. int local_iy; /* current line */
  277. while (1)
  278. {
  279. local_iy = STARPU_ATOMIC_ADD((unsigned int *)pcnt, 1) - 1;
  280. if (local_iy >= block_size)
  281. break;
  282. iy = iby*block_size + local_iy;
  283. for (ix = 0; ix < width; ix++)
  284. {
  285. double cx = leftX + ix * stepX;
  286. double cy = topY - iy * stepY;
  287. /* Z = X+I*Y */
  288. double x = 0;
  289. double y = 0;
  290. int it;
  291. for (it = 0; it < maxIt; it++)
  292. {
  293. double x2 = x*x;
  294. double y2 = y*y;
  295. /* Stop iterations when |Z| > 2 */
  296. if (x2 + y2 > 4.0)
  297. break;
  298. double twoxy = 2.0*x*y;
  299. /* Z = Z^2 + C */
  300. x = x2 - y2 + cx;
  301. y = twoxy + cy;
  302. }
  303. unsigned int v = STARPU_MIN((1024*((float)(it)/(2000))), 256);
  304. data[ix + local_iy*width] = (v<<16|(255-v)<<8);
  305. }
  306. }
  307. }
  308. static struct starpu_codelet spmd_mandelbrot_cl =
  309. {
  310. .type = STARPU_SPMD,
  311. .max_parallelism = INT_MAX,
  312. .cpu_funcs = {compute_block_spmd, NULL},
  313. #ifdef STARPU_USE_OPENCL
  314. .opencl_funcs = {compute_block_opencl, NULL},
  315. .opencl_flags = {STARPU_OPENCL_ASYNC},
  316. #endif
  317. .nbuffers = 1
  318. };
  319. static struct starpu_codelet mandelbrot_cl =
  320. {
  321. .type = STARPU_SEQ,
  322. .cpu_funcs = {compute_block, NULL},
  323. #ifdef STARPU_USE_OPENCL
  324. .opencl_funcs = {compute_block_opencl, NULL},
  325. .opencl_flags = {STARPU_OPENCL_ASYNC},
  326. #endif
  327. .nbuffers = 1
  328. };
  329. static void parse_args(int argc, char **argv)
  330. {
  331. int i;
  332. for (i = 1; i < argc; i++)
  333. {
  334. if (strcmp(argv[i], "-h") == 0)
  335. {
  336. fprintf(stderr, "Usage: %s [-h] [ -width 800] [-height 600] [-nblocks 16] [-no-x11] [-pos leftx:rightx:bottomy:topy] [-niter 1000] [-spmd] [-demo] [-demozoom 0.2]\n", argv[0]);
  337. exit(-1);
  338. }
  339. if (strcmp(argv[i], "-width") == 0)
  340. {
  341. char *argptr;
  342. width = strtol(argv[++i], &argptr, 10);
  343. }
  344. if (strcmp(argv[i], "-height") == 0)
  345. {
  346. char *argptr;
  347. height = strtol(argv[++i], &argptr, 10);
  348. }
  349. if (strcmp(argv[i], "-nblocks") == 0)
  350. {
  351. char *argptr;
  352. nblocks = strtol(argv[++i], &argptr, 10);
  353. }
  354. if (strcmp(argv[i], "-niter") == 0)
  355. {
  356. char *argptr;
  357. niter = strtol(argv[++i], &argptr, 10);
  358. }
  359. if (strcmp(argv[i], "-pos") == 0)
  360. {
  361. int ret = sscanf(argv[++i], "%lf:%lf:%lf:%lf", &leftX, &rightX, &bottomY, &topY);
  362. assert(ret == 4);
  363. }
  364. if (strcmp(argv[i], "-demo") == 0)
  365. {
  366. demo = 1;
  367. leftX = -50.22749575062760;
  368. rightX = 48.73874621262927;
  369. topY = -49.35016705749115;
  370. bottomY = 49.64891691946615;
  371. }
  372. if (strcmp(argv[i], "-demozoom") == 0)
  373. {
  374. char *argptr;
  375. demozoom = strtof(argv[++i], &argptr);
  376. }
  377. if (strcmp(argv[i], "-no-x11") == 0)
  378. {
  379. #ifdef STARPU_HAVE_X11
  380. use_x11 = 0;
  381. #endif
  382. }
  383. if (strcmp(argv[i], "-spmd") == 0)
  384. {
  385. use_spmd = 1;
  386. }
  387. }
  388. }
  389. int main(int argc, char **argv)
  390. {
  391. int ret;
  392. parse_args(argc, argv);
  393. /* We don't use CUDA in that example */
  394. struct starpu_conf conf;
  395. starpu_conf_init(&conf);
  396. conf.ncuda = 0;
  397. if (use_spmd)
  398. conf.sched_policy_name = "peager";
  399. ret = starpu_init(&conf);
  400. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  401. unsigned *buffer;
  402. starpu_malloc((void **)&buffer, height*width*sizeof(unsigned));
  403. #ifdef STARPU_HAVE_X11
  404. if (use_x11)
  405. init_x11(width, height, buffer);
  406. #endif
  407. int block_size = height/nblocks;
  408. STARPU_ASSERT((height % nblocks) == 0);
  409. #ifdef STARPU_USE_OPENCL
  410. starpu_opencl_load_opencl_from_string(mandelbrot_opencl_src, &opencl_programs, NULL);
  411. #endif
  412. starpu_data_handle_t block_handles[nblocks];
  413. int iby;
  414. for (iby = 0; iby < nblocks; iby++)
  415. {
  416. unsigned *data = &buffer[iby*block_size*width];
  417. starpu_vector_data_register(&block_handles[iby], STARPU_MAIN_RAM,
  418. (uintptr_t)data, block_size*width, sizeof(unsigned));
  419. }
  420. unsigned iter = 0;
  421. double start, end;
  422. if (demo)
  423. start = starpu_timing_now();
  424. while (niter-- != 0)
  425. {
  426. int ret;
  427. double stepX = (rightX - leftX)/width;
  428. double stepY = (topY - bottomY)/height;
  429. /* In case we have a SPMD task, each worker will grab tasks in
  430. * a greedy and select which piece of image to compute by
  431. * incrementing a counter shared by all the workers within the
  432. * parallel task. */
  433. int per_block_cnt[nblocks];
  434. for (iby = 0; iby < nblocks; iby++)
  435. {
  436. per_block_cnt[iby] = 0;
  437. int *pcnt = &per_block_cnt[iby];
  438. ret = starpu_task_insert(use_spmd?&spmd_mandelbrot_cl:&mandelbrot_cl,
  439. STARPU_VALUE, &iby, sizeof(iby),
  440. STARPU_VALUE, &block_size, sizeof(block_size),
  441. STARPU_VALUE, &stepX, sizeof(stepX),
  442. STARPU_VALUE, &stepY, sizeof(stepY),
  443. STARPU_W, block_handles[iby],
  444. STARPU_VALUE, &pcnt, sizeof(int *),
  445. STARPU_TAG_ONLY, (starpu_tag_t) (niter*nblocks + iby),
  446. 0);
  447. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  448. }
  449. for (iby = 0; iby < nblocks; iby++)
  450. {
  451. #ifdef STARPU_HAVE_X11
  452. if (use_x11)
  453. {
  454. starpu_data_acquire(block_handles[iby], STARPU_R);
  455. XPutImage(dpy, win, gc, bitmap,
  456. 0, iby*block_size,
  457. 0, iby*block_size,
  458. width, block_size);
  459. starpu_data_release(block_handles[iby]);
  460. }
  461. #endif
  462. }
  463. if (demo)
  464. {
  465. /* Zoom in */
  466. double zoom_factor = demozoom;
  467. double widthX = rightX - leftX;
  468. double heightY = topY - bottomY;
  469. iter++;
  470. /* If the window is too small, we reset the demo and display some statistics */
  471. if ((fabs(widthX) < 1e-12) || (fabs(heightY) < 1e-12))
  472. {
  473. leftX = -50.22749575062760;
  474. rightX = 48.73874621262927;
  475. topY = -49.35016705749115;
  476. bottomY = 49.64891691946615;
  477. end = starpu_timing_now();
  478. double timing = end - start;
  479. fprintf(stderr, "Time to generate %u frames : %f s\n", iter, timing/1000000.0);
  480. fprintf(stderr, "Average FPS: %f\n", ((double)iter*1e+6)/timing);
  481. /* Reset counters */
  482. iter = 0;
  483. start = starpu_timing_now();
  484. }
  485. else
  486. {
  487. leftX += (zoom_factor/2)*widthX;
  488. rightX -= (zoom_factor/2)*widthX;
  489. topY -= (zoom_factor/2)*heightY;
  490. bottomY += (zoom_factor/2)*heightY;
  491. }
  492. }
  493. #ifdef STARPU_HAVE_X11
  494. else if (use_x11 && handle_events())
  495. break;
  496. #endif
  497. }
  498. #ifdef STARPU_HAVE_X11
  499. if (use_x11)
  500. exit_x11();
  501. #endif
  502. for (iby = 0; iby < nblocks; iby++)
  503. starpu_data_unregister(block_handles[iby]);
  504. /* starpu_data_free_pinned_if_possible(buffer); */
  505. starpu_shutdown();
  506. return 0;
  507. }