vector_scal_opencl.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010,2012,2013,2015,2017,2018 CNRS
  4. * Copyright (C) 2011,2014 Université de Bordeaux
  5. * Copyright (C) 2010 Inria
  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. //! [To be included. You should update doxygen if you see this text.]
  19. #include <starpu.h>
  20. extern struct starpu_opencl_program programs;
  21. void scal_opencl_func(void *buffers[], void *_args)
  22. {
  23. float *factor = _args;
  24. int id, devid, err; /* OpenCL specific code */
  25. cl_kernel kernel; /* OpenCL specific code */
  26. cl_command_queue queue; /* OpenCL specific code */
  27. cl_event event; /* OpenCL specific code */
  28. /* length of the vector */
  29. unsigned n = STARPU_VECTOR_GET_NX(buffers[0]);
  30. /* OpenCL copy of the vector pointer */
  31. cl_mem val = (cl_mem)STARPU_VECTOR_GET_DEV_HANDLE(buffers[0]);
  32. { /* OpenCL specific code */
  33. id = starpu_worker_get_id();
  34. devid = starpu_worker_get_devid(id);
  35. err = starpu_opencl_load_kernel(&kernel, &queue, &programs,
  36. "vector_mult_opencl", /* Name of the codelet */
  37. devid);
  38. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  39. err = clSetKernelArg(kernel, 0, sizeof(n), &n);
  40. err |= clSetKernelArg(kernel, 1, sizeof(val), &val);
  41. err |= clSetKernelArg(kernel, 2, sizeof(*factor), factor);
  42. if (err) STARPU_OPENCL_REPORT_ERROR(err);
  43. }
  44. { /* OpenCL specific code */
  45. size_t global=n;
  46. size_t local;
  47. size_t s;
  48. cl_device_id device;
  49. starpu_opencl_get_device(devid, &device);
  50. err = clGetKernelWorkGroupInfo (kernel, device, CL_KERNEL_WORK_GROUP_SIZE, sizeof(local), &local, &s);
  51. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  52. if (local > global) local=global;
  53. err = clEnqueueNDRangeKernel(queue, kernel, 1, NULL, &global, &local, 0, NULL, &event);
  54. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  55. }
  56. { /* OpenCL specific code */
  57. clFinish(queue);
  58. starpu_opencl_collect_stats(event);
  59. clReleaseEvent(event);
  60. starpu_opencl_release_kernel(kernel);
  61. }
  62. }
  63. //! [To be included. You should update doxygen if you see this text.]