incrementer.c 3.1 KB

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