csr_opencl.c 3.4 KB

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