shadow_opencl.c 3.7 KB

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