coo_opencl.c 3.3 KB

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