vector_scal_opencl.c 2.6 KB

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