scratch_opencl.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2012-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. #include <starpu.h>
  17. /*
  18. * Queue an OpenCL kernel for the scratch test.
  19. */
  20. extern struct starpu_opencl_program opencl_program;
  21. void opencl_f(void *buffers[], void *args)
  22. {
  23. (void) args;
  24. int id, devid;
  25. cl_int err;
  26. cl_kernel kernel;
  27. cl_command_queue queue;
  28. unsigned n = STARPU_VECTOR_GET_NX(buffers[0]);
  29. unsigned elemsize = STARPU_VECTOR_GET_ELEMSIZE(buffers[0]);
  30. cl_mem val = (cl_mem) STARPU_VECTOR_GET_DEV_HANDLE(buffers[0]);
  31. cl_mem tmp = (cl_mem) STARPU_VECTOR_GET_DEV_HANDLE(buffers[1]);
  32. id = starpu_worker_get_id_check();
  33. devid = starpu_worker_get_devid(id);
  34. err = starpu_opencl_load_kernel(&kernel, &queue, &opencl_program, "increment_vector_opencl", devid);
  35. if (err != CL_SUCCESS)
  36. STARPU_OPENCL_REPORT_ERROR(err);
  37. err = clEnqueueCopyBuffer(queue,
  38. val,
  39. tmp,
  40. 0, /* offset in val */
  41. 0, /* offset in tmp */
  42. n * elemsize,
  43. 0, /* num_events_in_wait_list */
  44. NULL, /* event_wait_list */
  45. NULL); /* event */
  46. if (err != CL_SUCCESS)
  47. STARPU_OPENCL_REPORT_ERROR(err);
  48. err = clSetKernelArg(kernel, 0, sizeof(val), &val);
  49. err|= clSetKernelArg(kernel, 1, sizeof(tmp), &tmp);
  50. err|= clSetKernelArg(kernel, 2, sizeof(n), &n);
  51. if (err)
  52. STARPU_OPENCL_REPORT_ERROR(err);
  53. {
  54. size_t global=n;
  55. size_t local;
  56. size_t s;
  57. cl_device_id device;
  58. starpu_opencl_get_device(devid, &device);
  59. err = clGetKernelWorkGroupInfo (kernel, device, CL_KERNEL_WORK_GROUP_SIZE, sizeof(local), &local, &s);
  60. if (err != CL_SUCCESS)
  61. STARPU_OPENCL_REPORT_ERROR(err);
  62. if (local > global)
  63. local=global;
  64. else
  65. global = (global + local-1) / local * local;
  66. err = clEnqueueNDRangeKernel(queue, kernel, 1, NULL, &global, &local, 0, NULL, NULL);
  67. if (err != CL_SUCCESS)
  68. STARPU_OPENCL_REPORT_ERROR(err);
  69. }
  70. starpu_opencl_release_kernel(kernel);
  71. }