tensor_opencl.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011,2012 Inria
  4. * Copyright (C) 2012,2015-2017 CNRS
  5. * Copyright (C) 2011, 2019 Université de Bordeaux
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include <starpu.h>
  19. #include "../test_interfaces.h"
  20. #define KERNEL_LOCATION "tests/datawizard/interfaces/tensor/tensor_opencl_kernel.cl"
  21. extern struct test_config tensor_config;
  22. static struct starpu_opencl_program opencl_program;
  23. void
  24. test_tensor_opencl_func(void *buffers[], void *args)
  25. {
  26. STARPU_SKIP_IF_VALGRIND;
  27. int id, devid, ret;
  28. int factor = *(int *) args;
  29. cl_int err;
  30. cl_kernel kernel;
  31. cl_command_queue queue;
  32. cl_event event;
  33. ret = starpu_opencl_load_opencl_from_file(KERNEL_LOCATION, &opencl_program, NULL);
  34. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_opencl_from_file");
  35. int nx = STARPU_TENSOR_GET_NX(buffers[0]);
  36. int ny = STARPU_TENSOR_GET_NY(buffers[0]);
  37. int nz = STARPU_TENSOR_GET_NZ(buffers[0]);
  38. int nt = STARPU_TENSOR_GET_NT(buffers[0]);
  39. unsigned ldy = STARPU_TENSOR_GET_LDY(buffers[0]);
  40. unsigned ldz = STARPU_TENSOR_GET_LDZ(buffers[0]);
  41. unsigned ldt = STARPU_TENSOR_GET_LDT(buffers[0]);
  42. cl_mem tensor = (cl_mem) STARPU_TENSOR_GET_DEV_HANDLE(buffers[0]);
  43. cl_context context;
  44. id = starpu_worker_get_id_check();
  45. devid = starpu_worker_get_devid(id);
  46. starpu_opencl_get_context(devid, &context);
  47. cl_mem fail = clCreateBuffer(context, CL_MEM_COPY_HOST_PTR,
  48. sizeof(int), &tensor_config.copy_failed, &err);
  49. if (err != CL_SUCCESS)
  50. STARPU_OPENCL_REPORT_ERROR(err);
  51. err = starpu_opencl_load_kernel(&kernel,
  52. &queue,
  53. &opencl_program,
  54. "tensor_opencl",
  55. devid);
  56. if (err != CL_SUCCESS)
  57. STARPU_OPENCL_REPORT_ERROR(err);
  58. int nargs;
  59. nargs = starpu_opencl_set_kernel_args(&err, &kernel,
  60. sizeof(tensor), &tensor,
  61. sizeof(nx), &nx,
  62. sizeof(ny), &ny,
  63. sizeof(nz), &nz,
  64. sizeof(nt), &nt,
  65. sizeof(ldy), &ldy,
  66. sizeof(ldz), &ldz,
  67. sizeof(ldt), &ldt,
  68. sizeof(factor), &factor,
  69. sizeof(fail), &fail,
  70. 0);
  71. if (nargs != 10)
  72. {
  73. fprintf(stderr, "Failed to set argument #%d\n", nargs);
  74. STARPU_OPENCL_REPORT_ERROR(err);
  75. }
  76. {
  77. size_t global = nx * ny * nz * nt;
  78. err = clEnqueueNDRangeKernel(queue,
  79. kernel,
  80. 1,
  81. NULL,
  82. &global,
  83. NULL,
  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. &tensor_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. ret = starpu_opencl_unload_opencl(&opencl_program);
  106. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_unload_opencl");
  107. }