mandelbrot.c 14 KB

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