variable_opencl.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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/variable/variable_opencl_kernel.cl"
  20. extern struct test_config variable_config;
  21. static struct starpu_opencl_program opencl_program;
  22. void test_variable_opencl_func(void *buffers[], void *args)
  23. {
  24. int id, devid;
  25. int factor = *(int *) args;
  26. cl_int err;
  27. cl_kernel kernel;
  28. cl_command_queue queue;
  29. cl_event event;
  30. starpu_opencl_load_opencl_from_file(KERNEL_LOCATION, &opencl_program, NULL);
  31. cl_mem val = (cl_mem)STARPU_VARIABLE_GET_PTR(buffers[0]);
  32. cl_context context;
  33. id = starpu_worker_get_id();
  34. devid = starpu_worker_get_devid(id);
  35. starpu_opencl_get_context(devid, &context);
  36. cl_mem fail = clCreateBuffer(context, CL_MEM_COPY_HOST_PTR,
  37. sizeof(int), &variable_config.copy_failed, &err);
  38. if (err != CL_SUCCESS)
  39. STARPU_OPENCL_REPORT_ERROR(err);
  40. err = starpu_opencl_load_kernel(&kernel,
  41. &queue,
  42. &opencl_program,
  43. "variable_opencl",
  44. devid);
  45. if (err != CL_SUCCESS)
  46. STARPU_OPENCL_REPORT_ERROR(err);
  47. err = clSetKernelArg(kernel, 0, sizeof(val), &val);
  48. if (err != CL_SUCCESS)
  49. STARPU_OPENCL_REPORT_ERROR(err);
  50. err = clSetKernelArg(kernel, 1, sizeof(fail), &fail);
  51. if (err)
  52. STARPU_OPENCL_REPORT_ERROR(err);
  53. err = clSetKernelArg(kernel, 2, sizeof(factor), &factor);
  54. if (err)
  55. STARPU_OPENCL_REPORT_ERROR(err);
  56. {
  57. size_t global = 1;
  58. size_t local;
  59. size_t s;
  60. cl_device_id device;
  61. starpu_opencl_get_device(devid, &device);
  62. err = clGetKernelWorkGroupInfo (kernel,
  63. device,
  64. CL_KERNEL_WORK_GROUP_SIZE,
  65. sizeof(local),
  66. &local,
  67. &s);
  68. if (err != CL_SUCCESS)
  69. STARPU_OPENCL_REPORT_ERROR(err);
  70. if (local > global)
  71. local = global;
  72. err = clEnqueueNDRangeKernel(queue,
  73. kernel,
  74. 1,
  75. NULL,
  76. &global,
  77. &local,
  78. 0,
  79. NULL,
  80. &event);
  81. if (err != CL_SUCCESS)
  82. STARPU_OPENCL_REPORT_ERROR(err);
  83. }
  84. err = clEnqueueReadBuffer(queue,
  85. fail,
  86. CL_TRUE,
  87. 0,
  88. sizeof(int),
  89. &variable_config.copy_failed,
  90. 0,
  91. NULL,
  92. NULL);
  93. if (err != CL_SUCCESS)
  94. STARPU_OPENCL_REPORT_ERROR(err);
  95. clFinish(queue);
  96. starpu_opencl_collect_stats(event);
  97. clReleaseEvent(event);
  98. starpu_opencl_release_kernel(kernel);
  99. starpu_opencl_unload_opencl(&opencl_program);
  100. return;
  101. }