gl_interop_idle.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2012 Université de Bordeaux 1
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. /*
  17. * This example demonstrates how to use StarPU combined with OpenGL rendering,
  18. * which needs:
  19. *
  20. * - initializing GLUT first,
  21. * - enabling it at initialization,
  22. * - running the corresponding CUDA worker in the GLUT thread (here, the main
  23. * thread).
  24. *
  25. * The difference with gl_interop.c is that this version runs StarPU Tasks in
  26. * the glut idle handler.
  27. */
  28. #include <starpu.h>
  29. #include <unistd.h>
  30. #include <GL/glut.h>
  31. void dummy(void *buffers[], void *cl_arg)
  32. {
  33. float *v = (float *) STARPU_VECTOR_GET_PTR(buffers[0]);
  34. printf("Codelet running\n");
  35. cudaMemsetAsync(v, 0, STARPU_VECTOR_GET_NX(buffers[0]) * sizeof(float), starpu_cuda_get_local_stream());
  36. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  37. printf("Codelet done\n");
  38. }
  39. struct starpu_codelet cl = {
  40. .cuda_funcs = { dummy, NULL },
  41. .nbuffers = 1,
  42. .modes = { STARPU_W },
  43. };
  44. void foo(void) {
  45. }
  46. void display(float i) {
  47. glClear(GL_COLOR_BUFFER_BIT);
  48. glColor3f(1, 1, 1);
  49. glBegin(GL_LINES);
  50. glVertex2f(-i, -i);
  51. glVertex2f(i, i);
  52. glEnd();
  53. glFinish();
  54. glutPostRedisplay();
  55. }
  56. static int cuda_devices[] = { 0 };
  57. static struct starpu_driver drivers[] = {
  58. { .type = STARPU_CUDA_WORKER }
  59. };
  60. void callback_func(void *foo) {
  61. printf("Callback running, rendering\n");
  62. float i = 1.;
  63. while (i > 0) {
  64. usleep(100000);
  65. display(i);
  66. i -= 0.1;
  67. }
  68. printf("rendering done\n");
  69. /* Tell it was already the last submitted task */
  70. starpu_drivers_request_termination();
  71. /* And terminate StarPU */
  72. starpu_driver_deinit(&drivers[0]);
  73. starpu_shutdown();
  74. exit(0);
  75. }
  76. static void idle(void)
  77. {
  78. starpu_driver_run_once(&drivers[0]);
  79. }
  80. int main(int argc, char **argv)
  81. {
  82. #if !(defined(STARPU_USE_CUDA) && defined(STARPU_OPENGL_RENDER))
  83. return 77;
  84. #else
  85. struct starpu_conf conf;
  86. int ret;
  87. struct starpu_task *task;
  88. starpu_data_handle_t handle;
  89. int cuda_device = 0;
  90. cuda_devices[0] = cuda_device;
  91. drivers[0].id.cuda_id = cuda_device;
  92. glutInit(&argc, argv);
  93. glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
  94. glutInitWindowPosition(0, 0);
  95. glutInitWindowSize(300,200);
  96. glutCreateWindow("StarPU OpenGL interoperability test");
  97. glClearColor (0.5, 0.5, 0.5, 0.0);
  98. /* Enable OpenGL interoperability */
  99. starpu_conf_init(&conf);
  100. conf.ncuda = 1;
  101. conf.ncpus = 0;
  102. conf.nopencl = 0;
  103. conf.cuda_opengl_interoperability = cuda_devices;
  104. conf.n_cuda_opengl_interoperability = sizeof(cuda_devices) / sizeof(*cuda_devices);
  105. conf.not_launched_drivers = drivers;
  106. conf.n_not_launched_drivers = sizeof(drivers) / sizeof(*drivers);
  107. ret = starpu_init(&conf);
  108. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  109. starpu_vector_data_register(&handle, -1, 0, 10, sizeof(float));
  110. /* Submit just one dumb task */
  111. task = starpu_task_create();
  112. task->cl = &cl;
  113. task->handles[0] = handle;
  114. task->callback_func = callback_func;
  115. task->callback_arg = NULL;
  116. ret = starpu_task_submit(task);
  117. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  118. /* And run the driver inside main, which will run the task */
  119. printf("running the driver\n");
  120. /* Initialize it */
  121. starpu_driver_init(&drivers[0]);
  122. /* Register driver loop content as idle handler */
  123. glutIdleFunc(idle);
  124. /* Now run the glut loop */
  125. glutMainLoop();
  126. /* And deinitialize driver */
  127. starpu_driver_deinit(&drivers[0]);
  128. printf("finished running the driver\n");
  129. starpu_shutdown();
  130. return 0;
  131. #endif
  132. }