binary.c 4.8 KB

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