vector_scal_opencl.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2012, 2013 Centre National de la Recherche Scientifique
  4. * Copyright (C) 2010 Institut National de Recherche en Informatique et Automatique
  5. * Copyright (C) 2011 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. //! [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,
  36. &programs,
  37. "vector_mult_opencl", /* Name of the codelet */
  38. devid);
  39. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  40. err = clSetKernelArg(kernel, 0, sizeof(n), &n);
  41. err |= clSetKernelArg(kernel, 1, sizeof(val), &val);
  42. err |= clSetKernelArg(kernel, 2, sizeof(*factor), factor);
  43. if (err) STARPU_OPENCL_REPORT_ERROR(err);
  44. }
  45. { /* OpenCL specific code */
  46. size_t global=n;
  47. size_t local;
  48. size_t s;
  49. cl_device_id device;
  50. starpu_opencl_get_device(devid, &device);
  51. err = clGetKernelWorkGroupInfo (kernel, device, CL_KERNEL_WORK_GROUP_SIZE,
  52. sizeof(local), &local, &s);
  53. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  54. if (local > global) local=global;
  55. err = clEnqueueNDRangeKernel(queue, kernel, 1, NULL, &global, &local, 0,
  56. NULL, &event);
  57. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  58. }
  59. { /* OpenCL specific code */
  60. clFinish(queue);
  61. starpu_opencl_collect_stats(event);
  62. clReleaseEvent(event);
  63. starpu_opencl_release_kernel(kernel);
  64. }
  65. }
  66. //! [To be included. You should update doxygen if you see this text.]