incrementer.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. static unsigned niter = 50000;
  19. #ifdef STARPU_USE_CUDA
  20. extern void cuda_codelet(void *descr[], __attribute__ ((unused)) void *_args);
  21. #endif
  22. extern void cuda_codelet_host(float *tab);
  23. void cpu_codelet(void *descr[], __attribute__ ((unused)) void *_args)
  24. {
  25. float *val = (float *)STARPU_GET_VECTOR_PTR(descr[0]);
  26. val[0] += 1.0f; val[1] += 1.0f;
  27. }
  28. int main(int argc, char **argv)
  29. {
  30. starpu_init(NULL);
  31. if (argc == 2)
  32. niter = atoi(argv[1]);
  33. float float_array[3] __attribute__ ((aligned (16))) = { 0.0f, 0.0f, 0.0f};
  34. starpu_data_handle float_array_handle;
  35. starpu_register_vector_data(&float_array_handle, 0 /* home node */,
  36. (uintptr_t)&float_array, 3, sizeof(float));
  37. starpu_codelet cl =
  38. {
  39. /* CUBLAS stands for CUDA kernels controlled from the host */
  40. .where = STARPU_CPU|STARPU_CUDA,
  41. .cpu_func = cpu_codelet,
  42. #ifdef STARPU_USE_CUDA
  43. .cuda_func = cuda_codelet,
  44. #endif
  45. .nbuffers = 1
  46. };
  47. struct timeval start;
  48. struct timeval end;
  49. gettimeofday(&start, NULL);
  50. unsigned i;
  51. for (i = 0; i < niter; i++)
  52. {
  53. struct starpu_task *task = starpu_task_create();
  54. task->cl = &cl;
  55. task->callback_func = NULL;
  56. task->buffers[0].handle = float_array_handle;
  57. task->buffers[0].mode = STARPU_RW;
  58. int ret = starpu_submit_task(task);
  59. if (STARPU_UNLIKELY(ret == -ENODEV))
  60. {
  61. fprintf(stderr, "No worker may execute this task\n");
  62. exit(0);
  63. }
  64. }
  65. starpu_wait_all_tasks();
  66. /* update the array in RAM */
  67. starpu_sync_data_with_mem(float_array_handle, STARPU_R);
  68. gettimeofday(&end, NULL);
  69. fprintf(stderr, "array -> %f, %f, %f\n", float_array[0],
  70. float_array[1], float_array[2]);
  71. if (float_array[0] != float_array[1] + float_array[2])
  72. return 1;
  73. starpu_release_data_from_mem(float_array_handle);
  74. double timing = (double)((end.tv_sec - start.tv_sec)*1000000 +
  75. (end.tv_usec - start.tv_usec));
  76. fprintf(stderr, "%d elems took %lf ms\n", niter, timing/1000);
  77. starpu_shutdown();
  78. return 0;
  79. }