gl_interop.c 3.3 KB

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