variable_opencl.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  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 "../test_interfaces.h"
  18. #define KERNEL_LOCATION "tests/datawizard/interfaces/variable/variable_opencl_kernel.cl"
  19. extern struct test_config variable_config;
  20. static struct starpu_opencl_program opencl_program;
  21. void test_variable_opencl_func(void *buffers[], void *args)
  22. {
  23. STARPU_SKIP_IF_VALGRIND;
  24. int id, devid, ret;
  25. int factor = *(int *) args;
  26. cl_int err;
  27. cl_kernel kernel;
  28. cl_command_queue queue;
  29. cl_event event;
  30. ret = starpu_opencl_load_opencl_from_file(KERNEL_LOCATION, &opencl_program, NULL);
  31. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_opencl_from_file");
  32. cl_mem val = (cl_mem)STARPU_VARIABLE_GET_PTR(buffers[0]);
  33. cl_context context;
  34. id = starpu_worker_get_id_check();
  35. devid = starpu_worker_get_devid(id);
  36. starpu_opencl_get_context(devid, &context);
  37. cl_mem fail = clCreateBuffer(context, CL_MEM_COPY_HOST_PTR,
  38. sizeof(int), &variable_config.copy_failed, &err);
  39. if (err != CL_SUCCESS)
  40. STARPU_OPENCL_REPORT_ERROR(err);
  41. err = starpu_opencl_load_kernel(&kernel,
  42. &queue,
  43. &opencl_program,
  44. "variable_opencl",
  45. devid);
  46. if (err != CL_SUCCESS)
  47. STARPU_OPENCL_REPORT_ERROR(err);
  48. err = clSetKernelArg(kernel, 0, sizeof(val), &val);
  49. if (err != CL_SUCCESS)
  50. STARPU_OPENCL_REPORT_ERROR(err);
  51. err = clSetKernelArg(kernel, 1, sizeof(fail), &fail);
  52. if (err)
  53. STARPU_OPENCL_REPORT_ERROR(err);
  54. err = clSetKernelArg(kernel, 2, sizeof(factor), &factor);
  55. if (err)
  56. STARPU_OPENCL_REPORT_ERROR(err);
  57. {
  58. size_t global = 1;
  59. size_t local = 1;
  60. size_t s;
  61. cl_device_id device;
  62. starpu_opencl_get_device(devid, &device);
  63. err = clEnqueueNDRangeKernel(queue,
  64. kernel,
  65. 1,
  66. NULL,
  67. &global,
  68. &local,
  69. 0,
  70. NULL,
  71. &event);
  72. if (err != CL_SUCCESS)
  73. STARPU_OPENCL_REPORT_ERROR(err);
  74. }
  75. err = clEnqueueReadBuffer(queue,
  76. fail,
  77. CL_TRUE,
  78. 0,
  79. sizeof(int),
  80. &variable_config.copy_failed,
  81. 0,
  82. NULL,
  83. NULL);
  84. if (err != CL_SUCCESS)
  85. STARPU_OPENCL_REPORT_ERROR(err);
  86. clFinish(queue);
  87. starpu_opencl_collect_stats(event);
  88. clReleaseEvent(event);
  89. starpu_opencl_release_kernel(kernel);
  90. ret = starpu_opencl_unload_opencl(&opencl_program);
  91. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_unload_opencl");
  92. return;
  93. }