gl_interop_idle.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2012 Inria
  4. * Copyright (C) 2012-2013,2015 CNRS
  5. * Copyright (C) 2012-2014 Université de Bordeaux
  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 example demonstrates how to use StarPU combined with OpenGL rendering,
  20. * which needs:
  21. *
  22. * - initializing GLUT first,
  23. * - enabling it at initialization,
  24. * - running the corresponding CUDA worker in the GLUT thread (here, the main
  25. * thread).
  26. *
  27. * The difference with gl_interop.c is that this version runs StarPU Tasks in
  28. * the glut idle handler.
  29. */
  30. #include <starpu.h>
  31. #include <unistd.h>
  32. #if (defined(STARPU_USE_CUDA) && defined(STARPU_OPENGL_RENDER))
  33. #include <GL/freeglut.h>
  34. void dummy(void *buffers[], void *cl_arg)
  35. {
  36. float *v = (float *) STARPU_VECTOR_GET_PTR(buffers[0]);
  37. printf("Codelet running\n");
  38. cudaMemsetAsync(v, 0, STARPU_VECTOR_GET_NX(buffers[0]) * sizeof(float), starpu_cuda_get_local_stream());
  39. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  40. printf("Codelet done\n");
  41. }
  42. struct starpu_codelet cl =
  43. {
  44. .cuda_funcs = { dummy },
  45. .nbuffers = 1,
  46. .modes = { STARPU_W },
  47. };
  48. void foo(void)
  49. {
  50. }
  51. void display(float i)
  52. {
  53. glClear(GL_COLOR_BUFFER_BIT);
  54. glColor3f(1, 1, 1);
  55. glBegin(GL_LINES);
  56. glVertex2f(-i, -i);
  57. glVertex2f(i, i);
  58. glEnd();
  59. glFinish();
  60. glutPostRedisplay();
  61. }
  62. static int cuda_devices[] = { 0 };
  63. static struct starpu_driver drivers[] =
  64. {
  65. { .type = STARPU_CUDA_WORKER }
  66. };
  67. void callback_func(void *foo)
  68. {
  69. printf("Callback running, rendering\n");
  70. float i = 1.;
  71. while (i > 0)
  72. {
  73. usleep(100000);
  74. display(i);
  75. i -= 0.1;
  76. }
  77. printf("rendering done\n");
  78. /* Tell it was already the last submitted task */
  79. starpu_drivers_request_termination();
  80. /* And terminate StarPU */
  81. starpu_driver_deinit(&drivers[0]);
  82. starpu_shutdown();
  83. exit(0);
  84. }
  85. static void idle(void)
  86. {
  87. starpu_driver_run_once(&drivers[0]);
  88. }
  89. #endif
  90. int main(int argc, char **argv)
  91. {
  92. #if !(defined(STARPU_USE_CUDA) && defined(STARPU_OPENGL_RENDER))
  93. return 77;
  94. #else
  95. struct starpu_conf conf;
  96. int ret;
  97. struct starpu_task *task;
  98. starpu_data_handle_t handle;
  99. int cuda_device = 0;
  100. cuda_devices[0] = cuda_device;
  101. drivers[0].id.cuda_id = cuda_device;
  102. glutInit(&argc, argv);
  103. glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
  104. glutInitWindowPosition(0, 0);
  105. glutInitWindowSize(300,200);
  106. glutCreateWindow("StarPU OpenGL interoperability test");
  107. glClearColor (0.5, 0.5, 0.5, 0.0);
  108. /* Enable OpenGL interoperability */
  109. starpu_conf_init(&conf);
  110. conf.ncuda = 1;
  111. conf.ncpus = 0;
  112. conf.nopencl = 0;
  113. conf.cuda_opengl_interoperability = cuda_devices;
  114. conf.n_cuda_opengl_interoperability = sizeof(cuda_devices) / sizeof(*cuda_devices);
  115. conf.not_launched_drivers = drivers;
  116. conf.n_not_launched_drivers = sizeof(drivers) / sizeof(*drivers);
  117. ret = starpu_init(&conf);
  118. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  119. starpu_vector_data_register(&handle, -1, 0, 10, sizeof(float));
  120. /* Submit just one dumb task */
  121. task = starpu_task_create();
  122. task->cl = &cl;
  123. task->handles[0] = handle;
  124. task->callback_func = callback_func;
  125. task->callback_arg = NULL;
  126. ret = starpu_task_submit(task);
  127. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  128. /* And run the driver inside main, which will run the task */
  129. printf("running the driver\n");
  130. /* Initialize it */
  131. starpu_driver_init(&drivers[0]);
  132. /* Register driver loop content as idle handler */
  133. glutIdleFunc(idle);
  134. /* Now run the glut loop */
  135. glutMainLoop();
  136. /* And deinitialize driver */
  137. starpu_driver_deinit(&drivers[0]);
  138. printf("finished running the driver\n");
  139. starpu_shutdown();
  140. return 0;
  141. #endif
  142. }