multiformat_conversion_codelets_opencl.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011-2021 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. #include <starpu.h>
  17. extern struct starpu_opencl_program opencl_conversion_program;
  18. void cpu_to_opencl_opencl_func(void *buffers[], void *args)
  19. {
  20. (void) args;
  21. int id, devid;
  22. cl_int err;
  23. cl_kernel kernel;
  24. cl_command_queue queue;
  25. unsigned n = STARPU_MULTIFORMAT_GET_NX(buffers[0]);
  26. cl_mem src = (cl_mem) STARPU_MULTIFORMAT_GET_CPU_PTR(buffers[0]);
  27. cl_mem dst = (cl_mem) STARPU_MULTIFORMAT_GET_OPENCL_PTR(buffers[0]);
  28. id = starpu_worker_get_id_check();
  29. devid = starpu_worker_get_devid(id);
  30. err = starpu_opencl_load_kernel(&kernel,
  31. &queue,
  32. &opencl_conversion_program,
  33. "cpu_to_opencl_opencl",
  34. devid);
  35. if (err != CL_SUCCESS)
  36. STARPU_OPENCL_REPORT_ERROR(err);
  37. err = clSetKernelArg(kernel, 0, sizeof(src), &src);
  38. if (err != CL_SUCCESS)
  39. STARPU_OPENCL_REPORT_ERROR(err);
  40. err = clSetKernelArg(kernel, 1, sizeof(dst), &dst);
  41. if (err != CL_SUCCESS)
  42. STARPU_OPENCL_REPORT_ERROR(err);
  43. err = clSetKernelArg(kernel, 2, sizeof(n), &n);
  44. if (err != CL_SUCCESS)
  45. STARPU_OPENCL_REPORT_ERROR(err);
  46. {
  47. size_t global=n;
  48. size_t local;
  49. size_t s;
  50. cl_device_id device;
  51. starpu_opencl_get_device(devid, &device);
  52. err = clGetKernelWorkGroupInfo (kernel,
  53. device,
  54. CL_KERNEL_WORK_GROUP_SIZE,
  55. sizeof(local),
  56. &local,
  57. &s);
  58. if (err != CL_SUCCESS)
  59. STARPU_OPENCL_REPORT_ERROR(err);
  60. if (local > global)
  61. local = global;
  62. else
  63. global = (global + local-1) / local * local;
  64. err = clEnqueueNDRangeKernel(queue,
  65. kernel,
  66. 1,
  67. NULL,
  68. &global,
  69. &local,
  70. 0,
  71. NULL,
  72. NULL);
  73. if (err != CL_SUCCESS)
  74. STARPU_OPENCL_REPORT_ERROR(err);
  75. }
  76. starpu_opencl_release_kernel(kernel);
  77. }