test_vector_opencl.c 3.3 KB

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