matrix_opencl.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 <starpu.h>
  16. #include <starpu_opencl.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. int id, devid, factor;
  24. unsigned int n;
  25. cl_int err;
  26. cl_kernel kernel;
  27. cl_command_queue queue;
  28. cl_event event;
  29. cl_context context;
  30. cl_mem val, fail;
  31. starpu_opencl_load_opencl_from_file(KERNEL_LOCATION,
  32. &matrix_program,
  33. NULL);
  34. factor = *(int *)args;
  35. n = STARPU_MATRIX_GET_NX(buffers[0]);
  36. n*= STARPU_MATRIX_GET_NY(buffers[0]);
  37. val = (cl_mem)STARPU_MATRIX_GET_PTR(buffers[0]);
  38. id = starpu_worker_get_id();
  39. devid = starpu_worker_get_devid(id);
  40. starpu_opencl_get_context(devid, &context);
  41. err = starpu_opencl_load_kernel(&kernel,
  42. &queue,
  43. &matrix_program,
  44. "matrix_opencl",
  45. devid);
  46. if (err != CL_SUCCESS)
  47. STARPU_OPENCL_REPORT_ERROR(err);
  48. fail = clCreateBuffer(context, CL_MEM_COPY_HOST_PTR,
  49. sizeof(int), &matrix_config.copy_failed, &err);
  50. if (err != CL_SUCCESS)
  51. STARPU_OPENCL_REPORT_ERROR(err);
  52. /* Setting args */
  53. int nargs;
  54. nargs = starpu_opencl_set_kernel_args(&err, &kernel,
  55. sizeof(val), &val,
  56. sizeof(n), &n,
  57. sizeof(fail), &fail,
  58. sizeof(factor), &factor,
  59. 0);
  60. if (nargs != 4)
  61. STARPU_OPENCL_REPORT_ERROR(err);
  62. {
  63. size_t global=n;
  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. err = clEnqueueNDRangeKernel(queue,
  79. kernel,
  80. 1,
  81. NULL,
  82. &global,
  83. &local,
  84. 0,
  85. NULL,
  86. &event);
  87. if (err != CL_SUCCESS)
  88. STARPU_OPENCL_REPORT_ERROR(err);
  89. }
  90. err = clEnqueueReadBuffer(queue,
  91. fail,
  92. CL_TRUE,
  93. 0,
  94. sizeof(int),
  95. &matrix_config.copy_failed,
  96. 0,
  97. NULL,
  98. NULL);
  99. if (err != CL_SUCCESS)
  100. STARPU_OPENCL_REPORT_ERROR(err);
  101. clFinish(queue);
  102. starpu_opencl_collect_stats(event);
  103. clReleaseEvent(event);
  104. starpu_opencl_release_kernel(kernel);
  105. starpu_opencl_unload_opencl(&matrix_program);
  106. }