incrementer.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * StarPU
  3. * Copyright (C) INRIA 2008-2009 (see AUTHORS file)
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <starpu.h>
  17. #include <pthread.h>
  18. #include <sys/time.h>
  19. static unsigned niter = 50000;
  20. #ifdef STARPU_USE_CUDA
  21. extern void cuda_codelet(void *descr[], __attribute__ ((unused)) void *_args);
  22. #endif
  23. #ifdef STARPU_USE_OPENCL
  24. #include <starpu_opencl.h>
  25. extern void opencl_codelet(void *descr[], __attribute__ ((unused)) void *_args);
  26. struct starpu_opencl_program opencl_code;
  27. #endif
  28. extern void cuda_codelet_host(float *tab);
  29. void cpu_codelet(void *descr[], __attribute__ ((unused)) void *_args)
  30. {
  31. float *val = (float *)STARPU_GET_VECTOR_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. if (argc == 2)
  38. niter = atoi(argv[1]);
  39. float float_array[4] __attribute__ ((aligned (16))) = { 0.0f, 0.0f, 0.0f, 0.0f};
  40. starpu_data_handle float_array_handle;
  41. starpu_vector_data_register(&float_array_handle, 0 /* home node */,
  42. (uintptr_t)&float_array, 4, sizeof(float));
  43. #ifdef STARPU_USE_OPENCL
  44. starpu_opencl_load_opencl_from_file("examples/incrementer/incrementer_kernels_opencl_codelet.cl", &opencl_code);
  45. #endif
  46. starpu_codelet cl =
  47. {
  48. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL,
  49. .cpu_func = cpu_codelet,
  50. #ifdef STARPU_USE_CUDA
  51. .cuda_func = cuda_codelet,
  52. #endif
  53. #ifdef STARPU_USE_OPENCL
  54. .opencl_func = opencl_codelet,
  55. #endif
  56. .nbuffers = 1
  57. };
  58. struct timeval start;
  59. struct timeval end;
  60. gettimeofday(&start, NULL);
  61. unsigned i;
  62. for (i = 0; i < niter; i++)
  63. {
  64. struct starpu_task *task = starpu_task_create();
  65. task->cl = &cl;
  66. task->callback_func = NULL;
  67. task->buffers[0].handle = float_array_handle;
  68. task->buffers[0].mode = STARPU_RW;
  69. int ret = starpu_task_submit(task);
  70. if (STARPU_UNLIKELY(ret == -ENODEV))
  71. {
  72. fprintf(stderr, "No worker may execute this task\n");
  73. exit(0);
  74. }
  75. }
  76. starpu_task_wait_for_all();
  77. /* update the array in RAM */
  78. starpu_data_sync_with_mem(float_array_handle, STARPU_R);
  79. gettimeofday(&end, NULL);
  80. fprintf(stderr, "array -> %f, %f, %f, %f\n", float_array[0],
  81. float_array[1], float_array[2], float_array[3]);
  82. if (float_array[0] != float_array[1] + float_array[2] + float_array[3]) {
  83. fprintf(stderr, "Incorrect result\n");
  84. return 1;
  85. }
  86. starpu_data_release_from_mem(float_array_handle);
  87. double timing = (double)((end.tv_sec - start.tv_sec)*1000000 +
  88. (end.tv_usec - start.tv_usec));
  89. fprintf(stderr, "%d elems took %lf ms\n", niter, timing/1000);
  90. starpu_shutdown();
  91. return 0;
  92. }