mandelbrot.c 15 KB

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