binary.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010-2011, 2013-2015 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013, 2014, 2016 CNRS
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. /*
  18. * This shows how to load OpenCL programs, either from a .cl file, or from a
  19. * string containing the program itself.
  20. */
  21. #include <starpu.h>
  22. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  23. #ifdef STARPU_USE_OPENCL
  24. extern void opencl_codelet(void *descr[], STARPU_ATTRIBUTE_UNUSED void *_args);
  25. struct starpu_opencl_program opencl_program;
  26. #endif
  27. struct starpu_codelet cl =
  28. {
  29. #ifdef STARPU_USE_OPENCL
  30. .opencl_funcs = {opencl_codelet},
  31. .opencl_flags = {STARPU_OPENCL_ASYNC},
  32. #endif
  33. .nbuffers = 1,
  34. .modes = {STARPU_RW}
  35. };
  36. int compute(char *file_name, int load_as_file, int with_malloc)
  37. {
  38. float float_array[4] STARPU_ATTRIBUTE_ALIGNED(16) = { 0.0f, 0.0f, 0.0f, 0.0f};
  39. starpu_data_handle_t float_array_handle;
  40. unsigned i;
  41. int ret = 0;
  42. #ifdef STARPU_QUICK_CHECK
  43. unsigned niter = 50;
  44. #else
  45. unsigned niter = 500;
  46. #endif
  47. starpu_vector_data_register(&float_array_handle, STARPU_MAIN_RAM, (uintptr_t)&float_array, 4, sizeof(float));
  48. #ifdef STARPU_USE_OPENCL
  49. if (load_as_file)
  50. {
  51. ret = starpu_opencl_compile_opencl_from_file(file_name, NULL);
  52. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_compile_opencl_from_file");
  53. ret = starpu_opencl_load_binary_opencl(file_name, &opencl_program);
  54. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_binary_opencl");
  55. }
  56. else if (with_malloc)
  57. {
  58. char *located_file_name;
  59. char *located_dir_name;
  60. char *opencl_program_source;
  61. starpu_opencl_load_program_source_malloc(file_name, &located_file_name, &located_dir_name, &opencl_program_source);
  62. ret = starpu_opencl_compile_opencl_from_string(opencl_program_source, "incrementer", NULL);
  63. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_compile_opencl_from_file");
  64. ret = starpu_opencl_load_binary_opencl("incrementer", &opencl_program);
  65. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_binary_opencl");
  66. free(located_file_name);
  67. free(located_dir_name);
  68. free(opencl_program_source);
  69. }
  70. else
  71. {
  72. char located_file_name[1024];
  73. char located_dir_name[1024];
  74. char opencl_program_source[16384];
  75. starpu_opencl_load_program_source(file_name, located_file_name, located_dir_name, opencl_program_source);
  76. ret = starpu_opencl_compile_opencl_from_string(opencl_program_source, "incrementer", NULL);
  77. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_compile_opencl_from_file");
  78. ret = starpu_opencl_load_binary_opencl("incrementer", &opencl_program);
  79. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_binary_opencl");
  80. }
  81. #endif
  82. for (i = 0; i < niter; i++)
  83. {
  84. ret = starpu_task_insert(&cl, STARPU_RW, float_array_handle, STARPU_TAG_ONLY, (starpu_tag_t) i, 0);
  85. if (STARPU_UNLIKELY(ret == -ENODEV))
  86. {
  87. FPRINTF(stderr, "No worker may execute this task\n");
  88. exit(0);
  89. }
  90. }
  91. starpu_task_wait_for_all();
  92. /* update the array in RAM */
  93. starpu_data_unregister(float_array_handle);
  94. #ifdef STARPU_USE_OPENCL
  95. starpu_opencl_unload_opencl(&opencl_program);
  96. #endif
  97. FPRINTF(stderr, "array -> %f, %f, %f, %f\n", float_array[0], float_array[1], float_array[2], float_array[3]);
  98. if (float_array[0] != niter || float_array[0] != float_array[1] + float_array[2] + float_array[3])
  99. {
  100. FPRINTF(stderr, "Incorrect result\n");
  101. ret = 1;
  102. }
  103. return ret;
  104. }
  105. int main(int argc, char **argv)
  106. {
  107. int ret = 0;
  108. struct starpu_conf conf;
  109. starpu_conf_init(&conf);
  110. conf.ncpus = 0;
  111. conf.ncuda = 0;
  112. conf.nmic = 0;
  113. conf.nscc = 0;
  114. conf.nmpi_ms = 0;
  115. ret = starpu_init(&conf);
  116. if (STARPU_UNLIKELY(ret == -ENODEV))
  117. {
  118. FPRINTF(stderr, "This application requires an OpenCL worker.\n");
  119. return 77;
  120. }
  121. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  122. ret = compute("examples/incrementer/incrementer_kernels_opencl_kernel.cl", 1, -1);
  123. if (ret == 0)
  124. ret = compute("examples/incrementer/incrementer_kernels_opencl_kernel.cl", 0, 0);
  125. else
  126. FPRINTF(stderr, "Error when calling compute %d\n", ret);
  127. if (ret == 0)
  128. ret = compute("examples/incrementer/incrementer_kernels_opencl_kernel.cl", 0, 1);
  129. else
  130. FPRINTF(stderr, "Error when calling compute %d\n", ret);
  131. starpu_shutdown();
  132. return ret;
  133. }