vector_scal_c.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. *
  5. * StarPU 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. * StarPU 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. /*
  17. * This example demonstrates how to use StarPU to scale an array by a factor.
  18. * It shows how to manipulate data with StarPU's data management library.
  19. * 1- how to declare a piece of data to StarPU (starpu_vector_data_register)
  20. * 2- how to describe which data are accessed by a task (task->handles[0])
  21. * 3- how a kernel can manipulate the data (buffers[0].vector.ptr)
  22. *
  23. * This is a variant of vector_scal.c which shows it can be integrated with fortran.
  24. */
  25. #include <starpu.h>
  26. #include <stdio.h>
  27. extern void scal_cpu_func(void *buffers[], void *_args);
  28. extern void scal_cuda_func(void *buffers[], void *_args);
  29. static struct starpu_perfmodel vector_scal_model =
  30. {
  31. .type = STARPU_HISTORY_BASED,
  32. .symbol = "vector_scal_model"
  33. };
  34. static struct starpu_codelet cl =
  35. {
  36. .modes = { STARPU_RW },
  37. /* CPU implementation of the codelet */
  38. .cpu_funcs = {scal_cpu_func},
  39. .cpu_funcs_name = {"scal_cpu_func"},
  40. #ifdef STARPU_USE_CUDA
  41. /* CUDA implementation of the codelet */
  42. .cuda_funcs = {scal_cuda_func},
  43. .cuda_flags = {STARPU_CUDA_ASYNC},
  44. #endif
  45. .nbuffers = 1,
  46. .model = &vector_scal_model
  47. };
  48. int compute_(int *F_NX, float *vector)
  49. {
  50. int NX = *F_NX;
  51. int ret;
  52. /* Initialize StarPU with default configuration */
  53. ret = starpu_init(NULL);
  54. if (ret == -ENODEV) return 77;
  55. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  56. /* Tell StaPU to associate the "vector" vector with the "vector_handle"
  57. * identifier. When a task needs to access a piece of data, it should
  58. * refer to the handle that is associated to it.
  59. * In the case of the "vector" data interface:
  60. * - the first argument of the registration method is a pointer to the
  61. * handle that should describe the data
  62. * - the second argument is the memory node where the data (ie. "vector")
  63. * resides initially: STARPU_MAIN_RAM stands for an address in main memory, as
  64. * opposed to an adress on a GPU for instance.
  65. * - the third argument is the adress of the vector in RAM
  66. * - the fourth argument is the number of elements in the vector
  67. * - the fifth argument is the size of each element.
  68. */
  69. starpu_data_handle_t vector_handle;
  70. starpu_vector_data_register(&vector_handle, STARPU_MAIN_RAM, (uintptr_t)vector, NX, sizeof(vector[0]));
  71. float factor = 3.14;
  72. /* create a synchronous task: any call to starpu_task_submit will block
  73. * until it is terminated */
  74. struct starpu_task *task = starpu_task_create();
  75. task->synchronous = 1;
  76. task->cl = &cl;
  77. /* the codelet manipulates one buffer in RW mode */
  78. task->handles[0] = vector_handle;
  79. /* an argument is passed to the codelet, beware that this is a
  80. * READ-ONLY buffer and that the codelet may be given a pointer to a
  81. * COPY of the argument */
  82. task->cl_arg = &factor;
  83. task->cl_arg_size = sizeof(factor);
  84. /* execute the task on any eligible computational ressource */
  85. ret = starpu_task_submit(task);
  86. if (ret != -ENODEV) STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  87. /* StarPU does not need to manipulate the array anymore so we can stop
  88. * monitoring it */
  89. starpu_data_unregister(vector_handle);
  90. /* terminate StarPU, no task can be submitted after */
  91. starpu_shutdown();
  92. return ret;
  93. }