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 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. int *pcnt; // unused for CUDA tasks
  203. starpu_unpack_cl_args(cl_arg, &iby, &block_size, &stepX, &stepY, &pcnt);
  204. cl_mem data = (cl_mem)STARPU_VECTOR_GET_PTR(descr[0]);
  205. cl_kernel kernel;
  206. cl_command_queue queue;
  207. cl_event event;
  208. int id = starpu_worker_get_id();
  209. int devid = starpu_worker_get_devid(id);
  210. starpu_opencl_load_kernel(&kernel, &queue, &opencl_programs, "mandelbrot_kernel", devid);
  211. clSetKernelArg(kernel, 0, sizeof(cl_mem), &data);
  212. clSetKernelArg(kernel, 1, sizeof(double), &leftX);
  213. clSetKernelArg(kernel, 2, sizeof(double), &topY);
  214. clSetKernelArg(kernel, 3, sizeof(double), &stepX);
  215. clSetKernelArg(kernel, 4, sizeof(double), &stepY);
  216. clSetKernelArg(kernel, 5, sizeof(int), &maxIt);
  217. clSetKernelArg(kernel, 6, sizeof(int), &iby);
  218. clSetKernelArg(kernel, 7, sizeof(int), &block_size);
  219. clSetKernelArg(kernel, 8, sizeof(int), &width);
  220. unsigned dim = 16;
  221. size_t local[2] = {dim, 1};
  222. size_t global[2] = {width, block_size};
  223. clEnqueueNDRangeKernel(queue, kernel, 2, NULL, global, local, 0, NULL, &event);
  224. clFinish(queue);
  225. starpu_opencl_collect_stats(event);
  226. clReleaseEvent(event);
  227. starpu_opencl_release_kernel(kernel);
  228. }
  229. #endif
  230. /*
  231. * CPU kernel
  232. */
  233. static void compute_block(void *descr[], void *cl_arg)
  234. {
  235. int ix, iy;
  236. int iby, block_size;
  237. double stepX, stepY;
  238. int *pcnt; // unused for sequential tasks
  239. starpu_unpack_cl_args(cl_arg, &iby, &block_size, &stepX, &stepY, &pcnt);
  240. unsigned *data = (unsigned *)STARPU_VECTOR_GET_PTR(descr[0]);
  241. int local_iy;
  242. for (local_iy = 0; local_iy < block_size; local_iy++)
  243. {
  244. iy = iby*block_size + local_iy;
  245. for (ix = 0; ix < width; ix++)
  246. {
  247. double cx = leftX + ix * stepX;
  248. double cy = topY - iy * stepY;
  249. // Z = X+I*Y
  250. double x = 0;
  251. double y = 0;
  252. int it;
  253. for (it = 0; it < maxIt; it++)
  254. {
  255. double x2 = x*x;
  256. double y2 = y*y;
  257. // Stop iterations when |Z| > 2
  258. if (x2 + y2 > 4.0)
  259. break;
  260. double twoxy = 2.0*x*y;
  261. // Z = Z^2 + C
  262. x = x2 - y2 + cx;
  263. y = twoxy + cy;
  264. }
  265. unsigned int v = STARPU_MIN((1024*((float)(it)/(2000))), 256);
  266. data[ix + local_iy*width] = (v<<16|(255-v)<<8);
  267. }
  268. }
  269. }
  270. static void compute_block_spmd(void *descr[], void *cl_arg)
  271. {
  272. int iby, block_size;
  273. double stepX, stepY;
  274. int *pcnt;
  275. starpu_unpack_cl_args(cl_arg, &iby, &block_size, &stepX, &stepY, &pcnt);
  276. unsigned *data = (unsigned *)STARPU_VECTOR_GET_PTR(descr[0]);
  277. int ix, iy; // global coordinates
  278. int local_iy; // current line
  279. while (1)
  280. {
  281. local_iy = STARPU_ATOMIC_ADD(pcnt, 1) - 1;
  282. if (local_iy >= block_size)
  283. break;
  284. iy = iby*block_size + local_iy;
  285. for (ix = 0; ix < width; ix++)
  286. {
  287. double cx = leftX + ix * stepX;
  288. double cy = topY - iy * stepY;
  289. // Z = X+I*Y
  290. double x = 0;
  291. double y = 0;
  292. int it;
  293. for (it = 0; it < maxIt; it++)
  294. {
  295. double x2 = x*x;
  296. double y2 = y*y;
  297. // Stop iterations when |Z| > 2
  298. if (x2 + y2 > 4.0)
  299. break;
  300. double twoxy = 2.0*x*y;
  301. // Z = Z^2 + C
  302. x = x2 - y2 + cx;
  303. y = twoxy + cy;
  304. }
  305. unsigned int v = STARPU_MIN((1024*((float)(it)/(2000))), 256);
  306. data[ix + local_iy*width] = (v<<16|(255-v)<<8);
  307. }
  308. }
  309. }
  310. static starpu_codelet spmd_mandelbrot_cl = {
  311. .where = STARPU_CPU|STARPU_OPENCL,
  312. .type = STARPU_SPMD,
  313. .max_parallelism = INT_MAX,
  314. .cpu_func = compute_block_spmd,
  315. #ifdef STARPU_USE_OPENCL
  316. .opencl_func = compute_block_opencl,
  317. #endif
  318. .nbuffers = 1
  319. };
  320. static starpu_codelet mandelbrot_cl = {
  321. .where = STARPU_CPU|STARPU_OPENCL,
  322. .type = STARPU_SEQ,
  323. .cpu_func = compute_block,
  324. #ifdef STARPU_USE_OPENCL
  325. .opencl_func = compute_block_opencl,
  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. if (strcmp(argv[i], "-h") == 0) {
  334. fprintf(stderr, "Usage: %s [-h] [ -width 800] [-height 600] [-nblocks 16] [-no-x11] [-pos leftx:rightx:bottomy:topy] [-niter 1000] [-spmd]\n", argv[0]);
  335. exit(-1);
  336. }
  337. if (strcmp(argv[i], "-width") == 0) {
  338. char *argptr;
  339. width = strtol(argv[++i], &argptr, 10);
  340. }
  341. if (strcmp(argv[i], "-height") == 0) {
  342. char *argptr;
  343. height = strtol(argv[++i], &argptr, 10);
  344. }
  345. if (strcmp(argv[i], "-nblocks") == 0) {
  346. char *argptr;
  347. nblocks = strtol(argv[++i], &argptr, 10);
  348. }
  349. if (strcmp(argv[i], "-niter") == 0) {
  350. char *argptr;
  351. niter = strtol(argv[++i], &argptr, 10);
  352. }
  353. if (strcmp(argv[i], "-pos") == 0) {
  354. int ret = sscanf(argv[++i], "%lf:%lf:%lf:%lf", &leftX, &rightX, &bottomY, &topY);
  355. assert(ret == 4);
  356. }
  357. if (strcmp(argv[i], "-demo") == 0) {
  358. demo = 1;
  359. leftX = -50.22749575062760;
  360. rightX = 48.73874621262927;
  361. topY = -49.35016705749115;
  362. bottomY = 49.64891691946615;
  363. }
  364. if (strcmp(argv[i], "-no-x11") == 0) {
  365. #ifdef STARPU_HAVE_X11
  366. use_x11 = 0;
  367. #endif
  368. }
  369. if (strcmp(argv[i], "-spmd") == 0) {
  370. use_spmd = 1;
  371. }
  372. }
  373. }
  374. int main(int argc, char **argv)
  375. {
  376. parse_args(argc, argv);
  377. /* We don't use CUDA in that example */
  378. struct starpu_conf conf;
  379. starpu_conf_init(&conf);
  380. conf.ncuda = 0;
  381. if (use_spmd)
  382. conf.sched_policy_name = "pgreedy";
  383. starpu_init(&conf);
  384. unsigned *buffer;
  385. starpu_data_malloc_pinned_if_possible((void **)&buffer, height*width*sizeof(unsigned));
  386. #ifdef STARPU_HAVE_X11
  387. if (use_x11)
  388. init_x11(width, height, buffer);
  389. #endif
  390. int block_size = height/nblocks;
  391. STARPU_ASSERT((height % nblocks) == 0);
  392. #ifdef STARPU_USE_OPENCL
  393. starpu_opencl_load_opencl_from_string(mandelbrot_opencl_src, &opencl_programs);
  394. #endif
  395. starpu_data_handle block_handles[nblocks];
  396. int iby;
  397. for (iby = 0; iby < nblocks; iby++)
  398. {
  399. unsigned *data = &buffer[iby*block_size*width];
  400. starpu_vector_data_register(&block_handles[iby], 0,
  401. (uintptr_t)data, block_size*width, sizeof(unsigned));
  402. }
  403. unsigned iter = 0;
  404. struct timeval start, end;
  405. if (demo)
  406. gettimeofday(&start, NULL);
  407. while (niter-- != 0)
  408. {
  409. double stepX = (rightX - leftX)/width;
  410. double stepY = (topY - bottomY)/height;
  411. /* In case we have a SPMD task, each worker will grab tasks in
  412. * a greedy and select which piece of image to compute by
  413. * incrementing a counter shared by all the workers within the
  414. * parallel task. */
  415. int per_block_cnt[nblocks];
  416. for (iby = 0; iby < nblocks; iby++)
  417. {
  418. per_block_cnt[iby] = 0;
  419. int *pcnt = &per_block_cnt[iby];
  420. starpu_insert_task(use_spmd?&spmd_mandelbrot_cl:&mandelbrot_cl,
  421. STARPU_VALUE, &iby, sizeof(iby),
  422. STARPU_VALUE, &block_size, sizeof(block_size),
  423. STARPU_VALUE, &stepX, sizeof(stepX),
  424. STARPU_VALUE, &stepY, sizeof(stepY),
  425. STARPU_W, block_handles[iby],
  426. STARPU_VALUE, &pcnt, sizeof(int *),
  427. 0);
  428. }
  429. for (iby = 0; iby < nblocks; iby++)
  430. {
  431. starpu_data_acquire(block_handles[iby], STARPU_R);
  432. #ifdef STARPU_HAVE_X11
  433. if (use_x11)
  434. {
  435. XPutImage(dpy, win, gc, bitmap,
  436. 0, iby*block_size,
  437. 0, iby*block_size,
  438. width, block_size);
  439. }
  440. #endif
  441. starpu_data_release(block_handles[iby]);
  442. }
  443. if (demo)
  444. {
  445. /* Zoom in */
  446. double zoom_factor = 0.05;
  447. double widthX = rightX - leftX;
  448. double heightY = topY - bottomY;
  449. iter++;
  450. /* If the window is too small, we reset the demo and display some statistics */
  451. if ((fabs(widthX) < 1e-12) || (fabs(heightY) < 1e-12))
  452. {
  453. leftX = -50.22749575062760;
  454. rightX = 48.73874621262927;
  455. topY = -49.35016705749115;
  456. bottomY = 49.64891691946615;
  457. gettimeofday(&end, NULL);
  458. double timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  459. fprintf(stderr, "Time to generate %d frames : %f s\n", iter, timing/1000000.0);
  460. fprintf(stderr, "Average FPS: %f\n", ((double)iter*1e+6)/timing);
  461. /* Reset counters */
  462. iter = 0;
  463. gettimeofday(&start, NULL);
  464. }
  465. else {
  466. leftX += (zoom_factor/2)*widthX;
  467. rightX -= (zoom_factor/2)*widthX;
  468. topY -= (zoom_factor/2)*heightY;
  469. bottomY += (zoom_factor/2)*heightY;
  470. }
  471. }
  472. #ifdef STARPU_HAVE_X11
  473. else if (use_x11 && handle_events())
  474. break;
  475. #endif
  476. }
  477. #ifdef STARPU_HAVE_X11
  478. if (use_x11)
  479. exit_x11();
  480. #endif
  481. for (iby = 0; iby < nblocks; iby++)
  482. starpu_data_unregister(block_handles[iby]);
  483. // starpu_data_free_pinned_if_possible(buffer);
  484. starpu_shutdown();
  485. return 0;
  486. }