incrementer.c 3.2 KB

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