matrix_opencl.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011-2021 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. #include "../test_interfaces.h"
  18. #define KERNEL_LOCATION "tests/datawizard/interfaces/matrix/matrix_opencl_kernel.cl"
  19. extern struct test_config matrix_config;
  20. static struct starpu_opencl_program matrix_program;
  21. void test_matrix_opencl_func(void *buffers[], void *args)
  22. {
  23. STARPU_SKIP_IF_VALGRIND;
  24. int id, devid, factor, ret;
  25. unsigned int n;
  26. cl_int err;
  27. cl_kernel kernel;
  28. cl_command_queue queue;
  29. cl_event event;
  30. cl_context context;
  31. cl_mem val, fail;
  32. ret = starpu_opencl_load_opencl_from_file(KERNEL_LOCATION,
  33. &matrix_program,
  34. NULL);
  35. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_opencl_from_file");
  36. factor = *(int *)args;
  37. n = STARPU_MATRIX_GET_NX(buffers[0]);
  38. n*= STARPU_MATRIX_GET_NY(buffers[0]);
  39. val = (cl_mem)STARPU_MATRIX_GET_DEV_HANDLE(buffers[0]);
  40. id = starpu_worker_get_id_check();
  41. devid = starpu_worker_get_devid(id);
  42. starpu_opencl_get_context(devid, &context);
  43. err = starpu_opencl_load_kernel(&kernel,
  44. &queue,
  45. &matrix_program,
  46. "matrix_opencl",
  47. devid);
  48. if (err != CL_SUCCESS)
  49. STARPU_OPENCL_REPORT_ERROR(err);
  50. fail = clCreateBuffer(context, CL_MEM_COPY_HOST_PTR,
  51. sizeof(int), &matrix_config.copy_failed, &err);
  52. if (err != CL_SUCCESS)
  53. STARPU_OPENCL_REPORT_ERROR(err);
  54. /* Setting args */
  55. int nargs;
  56. nargs = starpu_opencl_set_kernel_args(&err, &kernel,
  57. sizeof(val), &val,
  58. sizeof(n), &n,
  59. sizeof(fail), &fail,
  60. sizeof(factor), &factor,
  61. 0);
  62. if (nargs != 4)
  63. STARPU_OPENCL_REPORT_ERROR(err);
  64. {
  65. size_t global=n;
  66. size_t local;
  67. size_t s;
  68. cl_device_id device;
  69. starpu_opencl_get_device(devid, &device);
  70. err = clGetKernelWorkGroupInfo (kernel,
  71. device,
  72. CL_KERNEL_WORK_GROUP_SIZE,
  73. sizeof(local),
  74. &local,
  75. &s);
  76. if (err != CL_SUCCESS)
  77. STARPU_OPENCL_REPORT_ERROR(err);
  78. if (local > global)
  79. local = global;
  80. else
  81. global = (global + local-1) / local * local;
  82. err = clEnqueueNDRangeKernel(queue,
  83. kernel,
  84. 1,
  85. NULL,
  86. &global,
  87. &local,
  88. 0,
  89. NULL,
  90. &event);
  91. if (err != CL_SUCCESS)
  92. STARPU_OPENCL_REPORT_ERROR(err);
  93. }
  94. err = clEnqueueReadBuffer(queue,
  95. fail,
  96. CL_TRUE,
  97. 0,
  98. sizeof(int),
  99. &matrix_config.copy_failed,
  100. 0,
  101. NULL,
  102. NULL);
  103. if (err != CL_SUCCESS)
  104. STARPU_OPENCL_REPORT_ERROR(err);
  105. clFinish(queue);
  106. starpu_opencl_collect_stats(event);
  107. clReleaseEvent(event);
  108. starpu_opencl_release_kernel(kernel);
  109. ret = starpu_opencl_unload_opencl(&matrix_program);
  110. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_unload_opencl");
  111. }