mandelbrot.c 13 KB

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