matrix_opencl.c 3.3 KB

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