incrementer.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010-2011, 2013 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012, 2013 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 <sys/time.h>
  19. static unsigned niter = 50000;
  20. #define FPRINTF(ofile, fmt, args ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ##args); }} while(0)
  21. #ifdef STARPU_USE_CUDA
  22. extern void cuda_codelet(void *descr[], __attribute__ ((unused)) void *_args);
  23. #endif
  24. #ifdef STARPU_USE_OPENCL
  25. extern void opencl_codelet(void *descr[], __attribute__ ((unused)) void *_args);
  26. struct starpu_opencl_program opencl_program;
  27. #endif
  28. void cpu_codelet(void *descr[], __attribute__ ((unused)) void *_args)
  29. {
  30. float *val = (float *)STARPU_VECTOR_GET_PTR(descr[0]);
  31. val[0] += 1.0f; val[1] += 1.0f;
  32. }
  33. int main(int argc, char **argv)
  34. {
  35. int ret = 0;
  36. ret = starpu_init(NULL);
  37. if (ret == -ENODEV)
  38. return 77;
  39. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  40. #ifdef STARPU_QUICK_CHECK
  41. niter /= 100;
  42. #endif
  43. if (argc == 2)
  44. niter = atoi(argv[1]);
  45. float float_array[4] __attribute__ ((aligned (16))) = { 0.0f, 0.0f, 0.0f, 0.0f};
  46. starpu_data_handle_t float_array_handle;
  47. starpu_vector_data_register(&float_array_handle, 0 /* home node */,
  48. (uintptr_t)&float_array, 4, sizeof(float));
  49. #ifdef STARPU_USE_OPENCL
  50. ret = starpu_opencl_load_opencl_from_file("examples/incrementer/incrementer_kernels_opencl_kernel.cl", &opencl_program, NULL);
  51. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_opencl_from_file");
  52. #endif
  53. struct starpu_codelet cl =
  54. {
  55. .cpu_funcs = {cpu_codelet, NULL},
  56. #ifdef STARPU_USE_CUDA
  57. .cuda_funcs = {cuda_codelet, NULL},
  58. #endif
  59. #ifdef STARPU_USE_OPENCL
  60. .opencl_funcs = {opencl_codelet, NULL},
  61. #endif
  62. .nbuffers = 1,
  63. .modes = {STARPU_RW},
  64. .name = "increment"
  65. };
  66. struct timeval start;
  67. struct timeval end;
  68. gettimeofday(&start, NULL);
  69. unsigned i;
  70. for (i = 0; i < niter; i++)
  71. {
  72. struct starpu_task *task = starpu_task_create();
  73. task->cl = &cl;
  74. task->callback_func = NULL;
  75. task->handles[0] = float_array_handle;
  76. ret = starpu_task_submit(task);
  77. if (STARPU_UNLIKELY(ret == -ENODEV))
  78. {
  79. FPRINTF(stderr, "No worker may execute this task\n");
  80. exit(0);
  81. }
  82. }
  83. starpu_task_wait_for_all();
  84. /* update the array in RAM */
  85. starpu_data_unregister(float_array_handle);
  86. gettimeofday(&end, NULL);
  87. FPRINTF(stderr, "array -> %f, %f, %f, %f\n", float_array[0],
  88. float_array[1], float_array[2], float_array[3]);
  89. if (float_array[0] != niter || float_array[0] != float_array[1] + float_array[2] + float_array[3])
  90. {
  91. FPRINTF(stderr, "Incorrect result\n");
  92. ret = 1;
  93. }
  94. double timing = (double)((end.tv_sec - start.tv_sec)*1000000 +
  95. (end.tv_usec - start.tv_usec));
  96. FPRINTF(stderr, "%u elems took %f ms\n", niter, timing/1000);
  97. starpu_shutdown();
  98. return ret;
  99. }