starpu_insert_task_utils.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011 Centre National de la Recherche Scientifique
  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. #include <util/starpu_insert_task_utils.h>
  17. #include <common/config.h>
  18. #include <common/utils.h>
  19. size_t starpu_insert_task_get_arg_size(va_list varg_list)
  20. {
  21. int arg_type;
  22. size_t arg_buffer_size;
  23. arg_buffer_size = 0;
  24. arg_buffer_size += sizeof(char);
  25. while ((arg_type = va_arg(varg_list, int)) != 0) {
  26. if (arg_type==STARPU_R || arg_type==STARPU_W || arg_type==STARPU_RW || arg_type == STARPU_SCRATCH) {
  27. va_arg(varg_list, starpu_data_handle);
  28. }
  29. else if (arg_type==STARPU_VALUE) {
  30. va_arg(varg_list, void *);
  31. size_t cst_size = va_arg(varg_list, size_t);
  32. arg_buffer_size += sizeof(size_t);
  33. arg_buffer_size += cst_size;
  34. }
  35. else if (arg_type==STARPU_CALLBACK) {
  36. va_arg(varg_list, void (*)(void *));
  37. }
  38. else if (arg_type==STARPU_CALLBACK_ARG) {
  39. va_arg(varg_list, void *);
  40. }
  41. else if (arg_type==STARPU_PRIORITY) {
  42. va_arg(varg_list, int);
  43. }
  44. }
  45. va_end(varg_list);
  46. return arg_buffer_size;
  47. }
  48. int starpu_insert_task_create_and_submit(size_t arg_buffer_size, starpu_codelet *cl, struct starpu_task **task, va_list varg_list) {
  49. int arg_type;
  50. unsigned current_buffer = 0;
  51. unsigned char nargs = 0;
  52. char *arg_buffer = malloc(arg_buffer_size);
  53. unsigned current_arg_offset = 0;
  54. /* We will begin the buffer with the number of args (which is stored as a char) */
  55. current_arg_offset += sizeof(char);
  56. while((arg_type = va_arg(varg_list, int)) != 0)
  57. {
  58. if (arg_type==STARPU_R || arg_type==STARPU_W || arg_type==STARPU_RW || arg_type == STARPU_SCRATCH)
  59. {
  60. /* We have an access mode : we expect to find a handle */
  61. starpu_data_handle handle = va_arg(varg_list, starpu_data_handle);
  62. starpu_access_mode mode = arg_type;
  63. (*task)->buffers[current_buffer].handle = handle;
  64. (*task)->buffers[current_buffer].mode = mode;
  65. current_buffer++;
  66. }
  67. else if (arg_type==STARPU_VALUE)
  68. {
  69. /* We have a constant value: this should be followed by a pointer to the cst value and the size of the constant */
  70. void *ptr = va_arg(varg_list, void *);
  71. size_t cst_size = va_arg(varg_list, size_t);
  72. *(size_t *)(&arg_buffer[current_arg_offset]) = cst_size;
  73. current_arg_offset += sizeof(size_t);
  74. memcpy(&arg_buffer[current_arg_offset], ptr, cst_size);
  75. current_arg_offset += cst_size;
  76. nargs++;
  77. STARPU_ASSERT(current_arg_offset <= arg_buffer_size);
  78. }
  79. else if (arg_type==STARPU_CALLBACK)
  80. {
  81. void (*callback_func)(void *);
  82. callback_func = va_arg(varg_list, void (*)(void *));
  83. (*task)->callback_func = callback_func;
  84. }
  85. else if (arg_type==STARPU_CALLBACK_ARG) {
  86. void *callback_arg;
  87. callback_arg = va_arg(varg_list, void *);
  88. (*task)->callback_arg = callback_arg;
  89. }
  90. else if (arg_type==STARPU_PRIORITY)
  91. {
  92. /* Followed by a priority level */
  93. int prio = va_arg(varg_list, int);
  94. (*task)->priority = prio;
  95. }
  96. }
  97. va_end(varg_list);
  98. arg_buffer[0] = nargs;
  99. STARPU_ASSERT(current_buffer == cl->nbuffers);
  100. (*task)->cl = cl;
  101. (*task)->cl_arg = arg_buffer;
  102. int ret = starpu_task_submit(*task);
  103. if (STARPU_UNLIKELY(ret == -ENODEV))
  104. fprintf(stderr, "No one can execute task %p wih cl %p (symbol %s)\n", *task, (*task)->cl, ((*task)->cl->model && (*task)->cl->model->symbol)?(*task)->cl->model->symbol:"none");
  105. STARPU_ASSERT(!ret);
  106. return ret;
  107. }