block_opencl.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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
  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 <starpu_opencl.h>
  18. #include "../test_interfaces.h"
  19. #define KERNEL_LOCATION "tests/datawizard/interfaces/block/block_opencl_kernel.cl"
  20. extern struct test_config block_config;
  21. static struct starpu_opencl_program opencl_program;
  22. void
  23. test_block_opencl_func(void *buffers[], void *args)
  24. {
  25. int id, devid;
  26. int factor = *(int *) args;
  27. cl_int err;
  28. cl_kernel kernel;
  29. cl_command_queue queue;
  30. cl_event event;
  31. starpu_opencl_load_opencl_from_file(KERNEL_LOCATION, &opencl_program, NULL);
  32. int nx = STARPU_BLOCK_GET_NX(buffers[0]);
  33. int ny = STARPU_BLOCK_GET_NY(buffers[0]);
  34. int nz = STARPU_BLOCK_GET_NZ(buffers[0]);
  35. unsigned ldy = STARPU_BLOCK_GET_LDY(buffers[0]);
  36. unsigned ldz = STARPU_BLOCK_GET_LDZ(buffers[0]);
  37. cl_mem block = (cl_mem) STARPU_BLOCK_GET_DEV_HANDLE(buffers[0]);
  38. cl_context context;
  39. id = starpu_worker_get_id();
  40. devid = starpu_worker_get_devid(id);
  41. starpu_opencl_get_context(devid, &context);
  42. cl_mem fail = clCreateBuffer(context, CL_MEM_COPY_HOST_PTR,
  43. sizeof(int), &block_config.copy_failed, &err);
  44. if (err != CL_SUCCESS)
  45. STARPU_OPENCL_REPORT_ERROR(err);
  46. err = starpu_opencl_load_kernel(&kernel,
  47. &queue,
  48. &opencl_program,
  49. "block_opencl",
  50. devid);
  51. if (err != CL_SUCCESS)
  52. STARPU_OPENCL_REPORT_ERROR(err);
  53. int nargs;
  54. nargs = starpu_opencl_set_kernel_args(&err, &kernel,
  55. sizeof(block), &block,
  56. sizeof(nx), &nx,
  57. sizeof(ny), &ny,
  58. sizeof(nz), &nz,
  59. sizeof(ldy), &ldy,
  60. sizeof(ldz), &ldz,
  61. sizeof(factor), &factor,
  62. sizeof(fail), &fail,
  63. 0);
  64. if (nargs != 8)
  65. {
  66. fprintf(stderr, "Failed to set argument #%d\n", nargs);
  67. STARPU_OPENCL_REPORT_ERROR(err);
  68. }
  69. {
  70. size_t global = nx * ny * nz;
  71. err = clEnqueueNDRangeKernel(queue,
  72. kernel,
  73. 1,
  74. NULL,
  75. &global,
  76. NULL,
  77. 0,
  78. NULL,
  79. &event);
  80. if (err != CL_SUCCESS)
  81. STARPU_OPENCL_REPORT_ERROR(err);
  82. }
  83. err = clEnqueueReadBuffer(queue,
  84. fail,
  85. CL_TRUE,
  86. 0,
  87. sizeof(int),
  88. &block_config.copy_failed,
  89. 0,
  90. NULL,
  91. NULL);
  92. if (err != CL_SUCCESS)
  93. STARPU_OPENCL_REPORT_ERROR(err);
  94. clFinish(queue);
  95. starpu_opencl_collect_stats(event);
  96. clReleaseEvent(event);
  97. starpu_opencl_release_kernel(kernel);
  98. starpu_opencl_unload_opencl(&opencl_program);
  99. }