cl_setkernelarg.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011 Inria
  4. * Copyright (C) 2012,2017 CNRS
  5. * Copyright (C) 2010-2011,2013, 2018 Université de Bordeaux
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include "socl.h"
  19. CL_API_SUFFIX__VERSION_1_0
  20. CL_API_ENTRY cl_int CL_API_CALL
  21. soclSetKernelArg(cl_kernel kernel,
  22. cl_uint arg_index,
  23. size_t arg_size,
  24. const void * arg_value)
  25. {
  26. if (kernel == NULL)
  27. return CL_INVALID_KERNEL;
  28. if (arg_index == (cl_uint)-1)
  29. {
  30. kernel->split_func = arg_value;
  31. return CL_SUCCESS;
  32. }
  33. else if (arg_index == (cl_uint)-2)
  34. {
  35. kernel->split_space = *(cl_uint*)arg_value;
  36. if (kernel->split_perfs != NULL)
  37. {
  38. free(kernel->split_perfs);
  39. }
  40. kernel->split_perfs = calloc(kernel->split_space, sizeof(cl_ulong));
  41. return CL_SUCCESS;
  42. }
  43. else if (arg_index == (cl_uint)-3)
  44. {
  45. kernel->split_data = (void *)arg_value;
  46. return CL_SUCCESS;
  47. }
  48. if (arg_index >= kernel->num_args)
  49. return CL_INVALID_ARG_INDEX;
  50. //FIXME: we don't return CL_INVALID_ARG_VALUE if "arg_value is NULL for an argument that is not declared with __local qualifier or vice-versa"
  51. //FIXME: we don't return CL_INVALID_MEM_OBJECT
  52. //FIXME: we don't return CL_INVALID_ARG_SIZE
  53. /* Free previous argument (set to NULL) */
  54. switch (kernel->arg_type[arg_index])
  55. {
  56. case Null:
  57. break;
  58. case Buffer:
  59. kernel->arg_type[arg_index] = Null;
  60. free(kernel->arg_value[arg_index]);
  61. kernel->arg_value[arg_index] = NULL;
  62. break;
  63. case Immediate:
  64. free(kernel->arg_value[arg_index]);
  65. kernel->arg_type[arg_index] = Null;
  66. kernel->arg_value[arg_index] = NULL;
  67. break;
  68. }
  69. kernel->arg_type[arg_index] = Null;
  70. kernel->arg_size[arg_index] = arg_size;
  71. DEBUG_MSG("[Kernel %d] Set argument %d: argsize %ld argvalue %p\n", kernel->id, arg_index, (long)arg_size, arg_value);
  72. /* Argument is not Null */
  73. if (arg_value != NULL)
  74. {
  75. cl_mem buf = NULL;
  76. /* Check if argument is a memory object */
  77. if ((arg_size == sizeof(cl_mem)) && ((buf = mem_object_fetch(arg_value)) != NULL))
  78. {
  79. DEBUG_MSG("Found buffer %d \n", buf->id);
  80. kernel->arg_type[arg_index] = Buffer;
  81. kernel->arg_value[arg_index] = malloc(sizeof(void*));
  82. *(cl_mem*)kernel->arg_value[arg_index] = buf; //We do not use gc_entity_store here because kernels do not hold reference on buffers (see OpenCL spec)
  83. }
  84. else
  85. {
  86. /* Argument must be an immediate buffer */
  87. DEBUG_MSG("Immediate data\n");
  88. kernel->arg_type[arg_index] = Immediate;
  89. kernel->arg_value[arg_index] = malloc(arg_size);
  90. memcpy(kernel->arg_value[arg_index], arg_value, arg_size);
  91. }
  92. }
  93. return CL_SUCCESS;
  94. }