gl_interop.c 3.4 KB

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