block_opencl.c 3.3 KB

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