shadow_opencl.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-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 "stencil.h"
  17. /* Perform replication of data on X and Y edges, to fold the domain on
  18. itself through mere replication of the source state. */
  19. #define str(x) #x
  20. #define clsrc(t,k) "__kernel void\n\
  21. #define TYPE " str(t) "\n\
  22. #define K " str(k) "\n\
  23. shadow( int bz, __global TYPE *ptr, int nx, int ny, int nz, int ldy, int ldz, int i)\n\
  24. {\n\
  25. unsigned idx = get_global_id(0);\n\
  26. unsigned idy = get_global_id(1);\n\
  27. //unsigned idz = threadIdx.z + blockIdx.z * blockDim.z;\n\
  28. unsigned idz = 0;\n\
  29. unsigned stepx = get_global_size(0);\n\
  30. unsigned stepy = get_global_size(1);\n\
  31. //unsigned stepz = blockDim.z * gridDim.z;\n\
  32. unsigned stepz = 1;\n\
  33. unsigned x, y, z;\n\
  34. if (idy == 0)\n\
  35. for (z = i-1 + idz; z < nz-(i-1); z += stepz)\n\
  36. for (x = K + idx; x < nx-K; x += stepx) \
  37. {\n \
  38. unsigned index = x+z*ldz;\n\
  39. ptr[index+(K-1)*ldy] = ptr[index+(ny-K-1)*ldy];\n\
  40. ptr[index+(ny-K)*ldy] = ptr[index+K*ldy];\n\
  41. }\n\
  42. \n\
  43. if (idx == 0)\n\
  44. for (z = i-1 + idz; z < nz-(i-1); z += stepz)\n\
  45. for (y = K + idy; y < ny-K; y += stepy) \
  46. {\n \
  47. unsigned index = y*ldy+z*ldz;\n\
  48. ptr[(K-1)+index] = ptr[(nx-K-1)+index];\n\
  49. ptr[(nx-K)+index] = ptr[K+index];\n\
  50. }\n\
  51. \n\
  52. if (idx == 0 && idy == 0)\n\
  53. for (z = i-1 + idz; z < nz-(i-1); z += stepz) \
  54. {\n \
  55. unsigned index = z*ldz;\n\
  56. ptr[K-1+(K-1)*ldy+index] = ptr[(nx-K-1)+(ny-K-1)*ldy+index];\n\
  57. ptr[(nx-K)+(K-1)*ldy+index] = ptr[K+(ny-K-1)*ldy+index];\n\
  58. ptr[(K-1)+(ny-K)*ldy+index] = ptr[(nx-K-1)+K*ldy+index];\n\
  59. ptr[(nx-K)+(ny-K)*ldy+index] = ptr[K+K*ldy+index];\n\
  60. }\n\
  61. }"
  62. static const char * src = clsrc(TYPE,K);
  63. static struct starpu_opencl_program program;
  64. void
  65. opencl_shadow_init(void)
  66. {
  67. starpu_opencl_load_opencl_from_string(src, &program, NULL);
  68. }
  69. void opencl_shadow_free(void)
  70. {
  71. int ret = starpu_opencl_unload_opencl(&program);
  72. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_unload_opencl");
  73. }
  74. void
  75. opencl_shadow_host(int bz, TYPE *ptr, int nx, int ny, int nz, int ldy, int ldz, int i)
  76. {
  77. #if 0
  78. size_t dim[] = {nx, ny, nz};
  79. #else
  80. size_t dim[] = {nx, ny, 1};
  81. #endif
  82. int devid,id;
  83. id = starpu_worker_get_id_check();
  84. devid = starpu_worker_get_devid(id);
  85. cl_kernel kernel;
  86. cl_command_queue cq;
  87. cl_int err;
  88. err = starpu_opencl_load_kernel(&kernel, &cq, &program, "shadow", devid);
  89. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  90. clSetKernelArg(kernel, 0, sizeof(bz), &bz);
  91. clSetKernelArg(kernel, 1, sizeof(ptr), &ptr);
  92. clSetKernelArg(kernel, 2, sizeof(nx), &nx);
  93. clSetKernelArg(kernel, 3, sizeof(ny), &ny);
  94. clSetKernelArg(kernel, 4, sizeof(nz), &nz);
  95. clSetKernelArg(kernel, 5, sizeof(ldy), &ldy);
  96. clSetKernelArg(kernel, 6, sizeof(ldz), &ldz);
  97. clSetKernelArg(kernel, 7, sizeof(i), &i);
  98. err = clEnqueueNDRangeKernel(cq, kernel, 3, NULL, dim, NULL, 0, NULL, NULL);
  99. if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
  100. }