gl_interop.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2012-2021 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  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. #include <starpu.h>
  26. #include <unistd.h>
  27. #if (defined(STARPU_USE_CUDA) && defined(STARPU_OPENGL_RENDER))
  28. #include <GL/freeglut.h>
  29. void dummy(void *buffers[], void *cl_arg)
  30. {
  31. float *v = (float *) STARPU_VECTOR_GET_PTR(buffers[0]);
  32. printf("Codelet running\n");
  33. cudaMemsetAsync(v, 0, STARPU_VECTOR_GET_NX(buffers[0]) * sizeof(float), starpu_cuda_get_local_stream());
  34. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  35. printf("Codelet done\n");
  36. }
  37. struct starpu_codelet cl =
  38. {
  39. .cuda_funcs = { dummy },
  40. .nbuffers = 1,
  41. .modes = { STARPU_W },
  42. };
  43. void foo(void)
  44. {
  45. }
  46. void display(float i)
  47. {
  48. glClear(GL_COLOR_BUFFER_BIT);
  49. glColor3f(1, 1, 1);
  50. glBegin(GL_LINES);
  51. glVertex2f(-i, -i);
  52. glVertex2f(i, i);
  53. glEnd();
  54. glFinish();
  55. glutPostRedisplay();
  56. glutMainLoopEvent();
  57. }
  58. void callback_func(void *foo)
  59. {
  60. printf("Callback running, rendering\n");
  61. float i = 1.;
  62. while (i > 0)
  63. {
  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. }
  72. #endif
  73. int main(int argc, char **argv)
  74. {
  75. #if !(defined(STARPU_USE_CUDA) && defined(STARPU_OPENGL_RENDER))
  76. return 77;
  77. #else
  78. struct starpu_conf conf;
  79. int cuda_device = 0;
  80. int cuda_devices[] = { cuda_device };
  81. struct starpu_driver drivers[] =
  82. {
  83. { .type = STARPU_CUDA_WORKER, .id.cuda_id = cuda_device }
  84. };
  85. int ret;
  86. struct starpu_task *task;
  87. starpu_data_handle_t handle;
  88. glutInit(&argc, argv);
  89. glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
  90. glutInitWindowPosition(0, 0);
  91. glutInitWindowSize(300,200);
  92. glutCreateWindow("StarPU OpenGL interoperability test");
  93. glClearColor (0.5, 0.5, 0.5, 0.0);
  94. /* Enable OpenGL interoperability */
  95. starpu_conf_init(&conf);
  96. starpu_conf_noworker(&conf);
  97. conf.ncuda = 1;
  98. conf.cuda_opengl_interoperability = cuda_devices;
  99. conf.n_cuda_opengl_interoperability = sizeof(cuda_devices) / sizeof(*cuda_devices);
  100. conf.not_launched_drivers = drivers;
  101. conf.n_not_launched_drivers = sizeof(drivers) / sizeof(*drivers);
  102. ret = starpu_init(&conf);
  103. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  104. starpu_vector_data_register(&handle, -1, 0, 10, sizeof(float));
  105. /* Submit just one dumb task */
  106. task = starpu_task_create();
  107. task->cl = &cl;
  108. task->handles[0] = handle;
  109. task->callback_func = callback_func;
  110. task->callback_arg = NULL;
  111. ret = starpu_task_submit(task);
  112. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  113. /* And run the driver inside main, which will run the task */
  114. printf("running the driver\n");
  115. starpu_driver_run(&drivers[0]);
  116. printf("finished running the driver\n");
  117. starpu_shutdown();
  118. return 0;
  119. #endif
  120. }