multiformat_opencl.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. *
  13. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  14. */
  15. #include <config.h>
  16. #include <starpu.h>
  17. #include <starpu_opencl.h>
  18. #include "../test_interfaces.h"
  19. #define KERNEL_LOCATION "tests/datawizard/interfaces/multiformat/multiformat_opencl_kernel.cl"
  20. extern struct test_config multiformat_config;
  21. static struct starpu_opencl_program multiformat_program;
  22. void test_multiformat_opencl_func(void *buffers[], void *args)
  23. {
  24. STARPU_SKIP_IF_VALGRIND;
  25. int id, devid, factor, ret;
  26. unsigned int n;
  27. cl_int err;
  28. cl_kernel kernel;
  29. cl_command_queue queue;
  30. cl_event event;
  31. cl_context context;
  32. cl_mem val, fail;
  33. ret = starpu_opencl_load_opencl_from_file(KERNEL_LOCATION,
  34. &multiformat_program,
  35. NULL);
  36. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_opencl_from_file");
  37. factor = *(int *)args;
  38. n = STARPU_MULTIFORMAT_GET_NX(buffers[0]);
  39. val = (cl_mem)STARPU_MULTIFORMAT_GET_OPENCL_PTR(buffers[0]);
  40. id = starpu_worker_get_id();
  41. devid = starpu_worker_get_devid(id);
  42. starpu_opencl_get_context(devid, &context);
  43. err = starpu_opencl_load_kernel(&kernel,
  44. &queue,
  45. &multiformat_program,
  46. "multiformat_opencl",
  47. devid);
  48. if (err != CL_SUCCESS)
  49. STARPU_OPENCL_REPORT_ERROR(err);
  50. fail = clCreateBuffer(context, CL_MEM_COPY_HOST_PTR,
  51. sizeof(int), &multiformat_config.copy_failed, &err);
  52. if (err != CL_SUCCESS)
  53. STARPU_OPENCL_REPORT_ERROR(err);
  54. /* Setting args */
  55. err = clSetKernelArg(kernel, 0, sizeof(val), &val);
  56. if (err != CL_SUCCESS)
  57. STARPU_OPENCL_REPORT_ERROR(err);
  58. err = clSetKernelArg(kernel, 1, sizeof(n), &n);
  59. if (err)
  60. STARPU_OPENCL_REPORT_ERROR(err);
  61. err = clSetKernelArg(kernel, 2, sizeof(fail), &fail);
  62. if (err)
  63. STARPU_OPENCL_REPORT_ERROR(err);
  64. err = clSetKernelArg(kernel, 3, sizeof(factor), &factor);
  65. if (err)
  66. STARPU_OPENCL_REPORT_ERROR(err);
  67. {
  68. size_t global=n;
  69. size_t local;
  70. size_t s;
  71. cl_device_id device;
  72. starpu_opencl_get_device(devid, &device);
  73. err = clGetKernelWorkGroupInfo (kernel,
  74. device,
  75. CL_KERNEL_WORK_GROUP_SIZE,
  76. sizeof(local),
  77. &local,
  78. &s);
  79. if (err != CL_SUCCESS)
  80. STARPU_OPENCL_REPORT_ERROR(err);
  81. if (local > global)
  82. local = global;
  83. err = clEnqueueNDRangeKernel(queue,
  84. kernel,
  85. 1,
  86. NULL,
  87. &global,
  88. &local,
  89. 0,
  90. NULL,
  91. &event);
  92. if (err != CL_SUCCESS)
  93. STARPU_OPENCL_REPORT_ERROR(err);
  94. }
  95. err = clEnqueueReadBuffer(queue,
  96. fail,
  97. CL_TRUE,
  98. 0,
  99. sizeof(int),
  100. &multiformat_config.copy_failed,
  101. 0,
  102. NULL,
  103. NULL);
  104. if (err != CL_SUCCESS)
  105. STARPU_OPENCL_REPORT_ERROR(err);
  106. clFinish(queue);
  107. starpu_opencl_collect_stats(event);
  108. clReleaseEvent(event);
  109. starpu_opencl_release_kernel(kernel);
  110. ret = starpu_opencl_unload_opencl(&multiformat_program);
  111. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_unload_opencl");
  112. }