binary.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2020 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 shows how to load OpenCL programs, either from a .cl file, or from a
  18. * string containing the program itself.
  19. */
  20. #include <starpu.h>
  21. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  22. #ifdef STARPU_USE_OPENCL
  23. extern void opencl_codelet(void *descr[], void *_args);
  24. struct starpu_opencl_program opencl_program;
  25. #endif
  26. struct starpu_codelet cl =
  27. {
  28. #ifdef STARPU_USE_OPENCL
  29. .opencl_funcs = {opencl_codelet},
  30. .opencl_flags = {STARPU_OPENCL_ASYNC},
  31. #endif
  32. .nbuffers = 1,
  33. .modes = {STARPU_RW}
  34. };
  35. int compute(char *file_name, int load_as_file, int with_malloc)
  36. {
  37. float float_array[4] STARPU_ATTRIBUTE_ALIGNED(16) = { 0.0f, 0.0f, 0.0f, 0.0f};
  38. starpu_data_handle_t float_array_handle;
  39. unsigned i;
  40. int ret = 0;
  41. #ifdef STARPU_QUICK_CHECK
  42. unsigned niter = 50;
  43. #else
  44. unsigned niter = 500;
  45. #endif
  46. starpu_vector_data_register(&float_array_handle, STARPU_MAIN_RAM, (uintptr_t)&float_array, 4, sizeof(float));
  47. #ifdef STARPU_USE_OPENCL
  48. if (load_as_file)
  49. {
  50. ret = starpu_opencl_compile_opencl_from_file(file_name, NULL);
  51. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_compile_opencl_from_file");
  52. ret = starpu_opencl_load_binary_opencl(file_name, &opencl_program);
  53. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_binary_opencl");
  54. }
  55. else if (with_malloc)
  56. {
  57. char *located_file_name;
  58. char *located_dir_name;
  59. char *opencl_program_source;
  60. starpu_opencl_load_program_source_malloc(file_name, &located_file_name, &located_dir_name, &opencl_program_source);
  61. ret = starpu_opencl_compile_opencl_from_string(opencl_program_source, "incrementer", NULL);
  62. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_compile_opencl_from_file");
  63. ret = starpu_opencl_load_binary_opencl("incrementer", &opencl_program);
  64. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_binary_opencl");
  65. free(located_file_name);
  66. free(located_dir_name);
  67. free(opencl_program_source);
  68. }
  69. else
  70. {
  71. char located_file_name[1024];
  72. char located_dir_name[1024];
  73. char opencl_program_source[16384];
  74. starpu_opencl_load_program_source(file_name, located_file_name, located_dir_name, opencl_program_source);
  75. ret = starpu_opencl_compile_opencl_from_string(opencl_program_source, "incrementer", NULL);
  76. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_compile_opencl_from_file");
  77. ret = starpu_opencl_load_binary_opencl("incrementer", &opencl_program);
  78. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_binary_opencl");
  79. }
  80. #endif
  81. for (i = 0; i < niter; i++)
  82. {
  83. ret = starpu_task_insert(&cl, STARPU_RW, float_array_handle, STARPU_TAG_ONLY, (starpu_tag_t) i, 0);
  84. if (STARPU_UNLIKELY(ret == -ENODEV))
  85. {
  86. FPRINTF(stderr, "No worker may execute this task\n");
  87. exit(0);
  88. }
  89. }
  90. starpu_task_wait_for_all();
  91. /* update the array in RAM */
  92. starpu_data_unregister(float_array_handle);
  93. #ifdef STARPU_USE_OPENCL
  94. starpu_opencl_unload_opencl(&opencl_program);
  95. #endif
  96. FPRINTF(stderr, "array -> %f, %f, %f, %f\n", float_array[0], float_array[1], float_array[2], float_array[3]);
  97. if (float_array[0] != niter || float_array[0] != float_array[1] + float_array[2] + float_array[3])
  98. {
  99. FPRINTF(stderr, "Incorrect result\n");
  100. ret = 1;
  101. }
  102. return ret;
  103. }
  104. int main(void)
  105. {
  106. int ret = 0;
  107. struct starpu_conf conf;
  108. starpu_conf_init(&conf);
  109. conf.ncuda = 0;
  110. ret = starpu_init(&conf);
  111. if (STARPU_UNLIKELY(ret == -ENODEV))
  112. {
  113. FPRINTF(stderr, "This application requires an OpenCL worker.\n");
  114. return 77;
  115. }
  116. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  117. if (starpu_opencl_worker_get_count() == 0)
  118. {
  119. FPRINTF(stderr, "This application requires an OpenCL worker.\n");
  120. starpu_shutdown();
  121. return 77;
  122. }
  123. ret = compute("examples/incrementer/incrementer_kernels_opencl_kernel.cl", 1, -1);
  124. if (ret == 0)
  125. ret = compute("examples/incrementer/incrementer_kernels_opencl_kernel.cl", 0, 0);
  126. else
  127. FPRINTF(stderr, "Error when calling compute %d\n", ret);
  128. if (ret == 0)
  129. ret = compute("examples/incrementer/incrementer_kernels_opencl_kernel.cl", 0, 1);
  130. else
  131. FPRINTF(stderr, "Error when calling compute %d\n", ret);
  132. starpu_shutdown();
  133. return ret;
  134. }