binary.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010-2011 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012 Centre National de la Recherche Scientifique
  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. #include <starpu.h>
  18. #include <pthread.h>
  19. #include <sys/time.h>
  20. #define FPRINTF(ofile, fmt, args ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ##args); }} while(0)
  21. #ifdef STARPU_USE_OPENCL
  22. #include <starpu_opencl.h>
  23. extern void opencl_codelet(void *descr[], __attribute__ ((unused)) 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, NULL},
  30. #endif
  31. .nbuffers = 1,
  32. .modes = {STARPU_RW}
  33. };
  34. int compute(char *file_name, int load_as_file)
  35. {
  36. float float_array[4] __attribute__ ((aligned (16))) = { 0.0f, 0.0f, 0.0f, 0.0f};
  37. starpu_data_handle_t float_array_handle;
  38. unsigned i;
  39. int ret = 0;
  40. unsigned niter = 500;
  41. starpu_vector_data_register(&float_array_handle, 0, (uintptr_t)&float_array, 4, sizeof(float));
  42. #ifdef STARPU_USE_OPENCL
  43. if (load_as_file)
  44. {
  45. ret = starpu_opencl_compile_opencl_from_file(file_name, NULL);
  46. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_compile_opencl_from_file");
  47. ret = starpu_opencl_load_binary_opencl(file_name, &opencl_program);
  48. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_binary_opencl");
  49. }
  50. else
  51. {
  52. char located_file_name[1024];
  53. char located_dir_name[1024];
  54. char opencl_program_source[16384];
  55. starpu_opencl_load_program_source(file_name, located_file_name, located_dir_name, opencl_program_source);
  56. ret = starpu_opencl_compile_opencl_from_string(opencl_program_source, "incrementer", NULL);
  57. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_compile_opencl_from_file");
  58. ret = starpu_opencl_load_binary_opencl("incrementer", &opencl_program);
  59. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_binary_opencl");
  60. }
  61. #endif
  62. for (i = 0; i < niter; i++)
  63. {
  64. ret = starpu_insert_task(&cl, STARPU_RW, float_array_handle, 0);
  65. if (STARPU_UNLIKELY(ret == -ENODEV))
  66. {
  67. FPRINTF(stderr, "No worker may execute this task\n");
  68. exit(0);
  69. }
  70. }
  71. starpu_task_wait_for_all();
  72. /* update the array in RAM */
  73. starpu_data_unregister(float_array_handle);
  74. FPRINTF(stderr, "array -> %f, %f, %f, %f\n", float_array[0], float_array[1], float_array[2], float_array[3]);
  75. if (float_array[0] != niter || float_array[0] != float_array[1] + float_array[2] + float_array[3])
  76. {
  77. FPRINTF(stderr, "Incorrect result\n");
  78. ret = 1;
  79. }
  80. return ret;
  81. }
  82. int main(int argc, char **argv)
  83. {
  84. int ret = 0;
  85. struct starpu_conf conf;
  86. starpu_conf_init(&conf);
  87. conf.ncpus = 0;
  88. conf.ncuda = 0;
  89. ret = starpu_init(&conf);
  90. if (STARPU_UNLIKELY(ret == -ENODEV))
  91. {
  92. FPRINTF(stderr, "This application requires an OpenCL worker.\n");
  93. starpu_shutdown();
  94. return 77;
  95. }
  96. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  97. ret = compute("examples/incrementer/incrementer_kernels_opencl_kernel.cl", 1);
  98. if (ret == 0)
  99. ret = compute("examples/incrementer/incrementer_kernels_opencl_kernel.cl", 0);
  100. starpu_shutdown();
  101. return ret;
  102. }