shadow_opencl.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2011, 2013-2014 Université de Bordeaux
  4. * Copyright (C) 2016 CNRS
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include "stencil.h"
  18. /* Perform replication of data on X and Y edges, to fold the domain on
  19. itself through mere replication of the source state. */
  20. #define str(x) #x
  21. #define clsrc(t,k) "__kernel void\n\
  22. #define TYPE " str(t) "\n\
  23. #define K " str(k) "\n\
  24. shadow( int bz, __global TYPE *ptr, int nx, int ny, int nz, int ldy, int ldz, int i)\n\
  25. {\n\
  26. unsigned idx = get_global_id(0);\n\
  27. unsigned idy = get_global_id(1);\n\
  28. //unsigned idz = threadIdx.z + blockIdx.z * blockDim.z;\n\
  29. unsigned idz = 0;\n\
  30. unsigned stepx = get_global_size(0);\n\
  31. unsigned stepy = get_global_size(1);\n\
  32. //unsigned stepz = blockDim.z * gridDim.z;\n\
  33. unsigned stepz = 1;\n\
  34. unsigned x, y, z;\n\
  35. if (idy == 0)\n\
  36. for (z = i-1 + idz; z < nz-(i-1); z += stepz)\n\
  37. for (x = K + idx; x < nx-K; x += stepx) \
  38. {\n \
  39. unsigned index = x+z*ldz;\n\
  40. ptr[index+(K-1)*ldy] = ptr[index+(ny-K-1)*ldy];\n\
  41. ptr[index+(ny-K)*ldy] = ptr[index+K*ldy];\n\
  42. }\n\
  43. \n\
  44. if (idx == 0)\n\
  45. for (z = i-1 + idz; z < nz-(i-1); z += stepz)\n\
  46. for (y = K + idy; y < ny-K; y += stepy) \
  47. {\n \
  48. unsigned index = y*ldy+z*ldz;\n\
  49. ptr[(K-1)+index] = ptr[(nx-K-1)+index];\n\
  50. ptr[(nx-K)+index] = ptr[K+index];\n\
  51. }\n\
  52. \n\
  53. if (idx == 0 && idy == 0)\n\
  54. for (z = i-1 + idz; z < nz-(i-1); z += stepz) \
  55. {\n \
  56. unsigned index = z*ldz;\n\
  57. ptr[K-1+(K-1)*ldy+index] = ptr[(nx-K-1)+(ny-K-1)*ldy+index];\n\
  58. ptr[(nx-K)+(K-1)*ldy+index] = ptr[K+(ny-K-1)*ldy+index];\n\
  59. ptr[(K-1)+(ny-K)*ldy+index] = ptr[(nx-K-1)+K*ldy+index];\n\
  60. ptr[(nx-K)+(ny-K)*ldy+index] = ptr[K+K*ldy+index];\n\
  61. }\n\
  62. }"
  63. static const char * src = clsrc(TYPE,K);
  64. static struct starpu_opencl_program program;
  65. void
  66. opencl_shadow_init(void)
  67. {
  68. starpu_opencl_load_opencl_from_string(src, &program, NULL);
  69. }
  70. void opencl_shadow_free(void)
  71. {
  72. int ret = starpu_opencl_unload_opencl(&program);
  73. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_unload_opencl");
  74. }
  75. void
  76. opencl_shadow_host(int bz, TYPE *ptr, int nx, int ny, int nz, int ldy, int ldz, int i)
  77. {
  78. #if 0
  79. size_t dim[] = {nx, ny, nz};
  80. #else
  81. size_t dim[] = {nx, ny, 1};
  82. #endif
  83. int devid,id;
  84. id = starpu_worker_get_id_check();
  85. devid = starpu_worker_get_devid(id);
  86. cl_kernel kernel;
  87. cl_command_queue cq;
  88. cl_int err;
  89. err = starpu_opencl_load_kernel(&kernel, &cq, &program, "shadow", devid);
  90. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  91. clSetKernelArg(kernel, 0, sizeof(bz), &bz);
  92. clSetKernelArg(kernel, 1, sizeof(ptr), &ptr);
  93. clSetKernelArg(kernel, 2, sizeof(nx), &nx);
  94. clSetKernelArg(kernel, 3, sizeof(ny), &ny);
  95. clSetKernelArg(kernel, 4, sizeof(nz), &nz);
  96. clSetKernelArg(kernel, 5, sizeof(ldy), &ldy);
  97. clSetKernelArg(kernel, 6, sizeof(ldz), &ldz);
  98. clSetKernelArg(kernel, 7, sizeof(i), &i);
  99. err = clEnqueueNDRangeKernel(cq, kernel, 3, NULL, dim, NULL, 0, NULL, NULL);
  100. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  101. }