stencil_opencl.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * StarPU
  3. * Copyright (C) INRIA 2008-2010 (see AUTHORS file)
  4. *
  5. * This program 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. * This program 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. #include <starpu_opencl.h>
  18. #include <starpu_util.h>
  19. #include "stencil.h"
  20. void opencl_codelet(void *descr[], void *_args)
  21. {
  22. float *data = (float *)STARPU_GET_VECTOR_PTR(descr[0]);
  23. float *results = (float *)STARPU_GET_VECTOR_PTR(descr[1]);
  24. float *C0 = (float *)STARPU_GET_VECTOR_PTR(descr[2]);
  25. float *C1 = (float *)STARPU_GET_VECTOR_PTR(descr[3]);
  26. cl_kernel kernel;
  27. cl_command_queue queue;
  28. int id, devid, err;
  29. id = starpu_get_worker_id();
  30. devid = starpu_get_worker_devid(id);
  31. err = starpu_opencl_load_kernel(&kernel, &queue, "examples/stencil/stencil_opencl_codelet.cl", "stencil", devid);
  32. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  33. err = 0;
  34. err = clSetKernelArg(kernel, 0, sizeof(cl_mem), &data);
  35. err |= clSetKernelArg(kernel, 1, sizeof(cl_mem), &results);
  36. err |= clSetKernelArg(kernel, 2, sizeof(cl_mem), &C0);
  37. err |= clSetKernelArg(kernel, 3, sizeof(cl_mem), &C1);
  38. if (err) STARPU_OPENCL_REPORT_ERROR(err);
  39. {
  40. size_t global[3];
  41. size_t local[3];
  42. // Execute the kernel over the entire range of our 3d input data set
  43. local[0] = XBLOCK / X_PER_THREAD; // threads along the X axis
  44. local[1] = YBLOCK / Y_PER_THREAD; // threads along the Y axis
  45. local[2] = ZBLOCK / Z_PER_THREAD; // threads along the Z axis
  46. global[0] = DIM / X_PER_THREAD; // virtual size of global X axis
  47. global[1] = DIM / Y_PER_THREAD; // virtual size of global Y axis
  48. global[2] = DIM / Z_PER_THREAD; // virtual size of global Z axis
  49. err = clEnqueueNDRangeKernel(queue, kernel, 3, NULL, global, local, 0, NULL, NULL);
  50. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  51. }
  52. clFinish(queue);
  53. starpu_opencl_release(kernel);
  54. }