gl_interop.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2012 Inria
  4. * Copyright (C) 2012,2013,2015,2017 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. #include <starpu.h>
  28. #include <unistd.h>
  29. #if (defined(STARPU_USE_CUDA) && defined(STARPU_OPENGL_RENDER))
  30. #include <GL/freeglut.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. {
  41. .cuda_funcs = { dummy },
  42. .nbuffers = 1,
  43. .modes = { STARPU_W },
  44. };
  45. void foo(void)
  46. {
  47. }
  48. void display(float i)
  49. {
  50. glClear(GL_COLOR_BUFFER_BIT);
  51. glColor3f(1, 1, 1);
  52. glBegin(GL_LINES);
  53. glVertex2f(-i, -i);
  54. glVertex2f(i, i);
  55. glEnd();
  56. glFinish();
  57. glutPostRedisplay();
  58. glutMainLoopEvent();
  59. }
  60. void callback_func(void *foo)
  61. {
  62. printf("Callback running, rendering\n");
  63. float i = 1.;
  64. while (i > 0)
  65. {
  66. usleep(100000);
  67. display(i);
  68. i -= 0.1;
  69. }
  70. printf("rendering done\n");
  71. /* Tell it was already the last submitted task */
  72. starpu_drivers_request_termination();
  73. }
  74. #endif
  75. int main(int argc, char **argv)
  76. {
  77. #if !(defined(STARPU_USE_CUDA) && defined(STARPU_OPENGL_RENDER))
  78. return 77;
  79. #else
  80. struct starpu_conf conf;
  81. int cuda_device = 0;
  82. int cuda_devices[] = { cuda_device };
  83. struct starpu_driver drivers[] =
  84. {
  85. { .type = STARPU_CUDA_WORKER, .id.cuda_id = cuda_device }
  86. };
  87. int ret;
  88. struct starpu_task *task;
  89. starpu_data_handle_t handle;
  90. glutInit(&argc, argv);
  91. glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
  92. glutInitWindowPosition(0, 0);
  93. glutInitWindowSize(300,200);
  94. glutCreateWindow("StarPU OpenGL interoperability test");
  95. glClearColor (0.5, 0.5, 0.5, 0.0);
  96. /* Enable OpenGL interoperability */
  97. starpu_conf_init(&conf);
  98. conf.ncuda = 1;
  99. conf.ncpus = 0;
  100. conf.nopencl = 0;
  101. conf.cuda_opengl_interoperability = cuda_devices;
  102. conf.n_cuda_opengl_interoperability = sizeof(cuda_devices) / sizeof(*cuda_devices);
  103. conf.not_launched_drivers = drivers;
  104. conf.n_not_launched_drivers = sizeof(drivers) / sizeof(*drivers);
  105. ret = starpu_init(&conf);
  106. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  107. starpu_vector_data_register(&handle, -1, 0, 10, sizeof(float));
  108. /* Submit just one dumb task */
  109. task = starpu_task_create();
  110. task->cl = &cl;
  111. task->handles[0] = handle;
  112. task->callback_func = callback_func;
  113. task->callback_arg = NULL;
  114. ret = starpu_task_submit(task);
  115. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  116. /* And run the driver inside main, which will run the task */
  117. printf("running the driver\n");
  118. starpu_driver_run(&drivers[0]);
  119. printf("finished running the driver\n");
  120. starpu_shutdown();
  121. return 0;
  122. #endif
  123. }