mandelbrot.c 14 KB

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