life_cuda.cu 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. #define _externC extern "C"
  17. #include "stencil.h"
  18. /* Heart of the stencil computation: compute a new state from an old one. */
  19. extern "C" __global__ void cuda_life_update(int bz, const TYPE *old, TYPE *newp, int nx, int ny, int nz, int ldy, int ldz, int iter)
  20. {
  21. unsigned idx = threadIdx.x + blockIdx.x * blockDim.x;
  22. unsigned idy = threadIdx.y + blockIdx.y * blockDim.y;
  23. //unsigned idz = threadIdx.z + blockIdx.z * blockDim.z;
  24. unsigned idz = 0;
  25. unsigned stepx = blockDim.x * gridDim.x;
  26. unsigned stepy = blockDim.y * gridDim.y;
  27. //unsigned stepz = blockDim.z * gridDim.z;
  28. unsigned stepz = 1;
  29. unsigned x, y, z;
  30. unsigned num, alive;
  31. for (z = iter + idz; z < nz - iter; z += stepz)
  32. for (y = K + idy; y < ny - K; y += stepy)
  33. {
  34. for (x = K + idx; x < nx - K; x += stepx)
  35. {
  36. unsigned index = x + y*ldy + z*ldz;
  37. num = 0
  38. + old[index+1*ldy+0*ldz]
  39. + old[index+1*ldy+1*ldz]
  40. + old[index+0*ldy+1*ldz]
  41. + old[index-1*ldy+1*ldz]
  42. + old[index-1*ldy+0*ldz]
  43. + old[index-1*ldy-1*ldz]
  44. + old[index+0*ldy-1*ldz]
  45. + old[index+1*ldy-1*ldz]
  46. ;
  47. alive = old[index];
  48. alive = (alive && num == 2) || num == 3;
  49. newp[index] = alive;
  50. }
  51. }
  52. }
  53. extern "C" void cuda_life_update_host(int bz, const TYPE *old, TYPE *newp, int nx, int ny, int nz, int ldy, int ldz, int iter)
  54. {
  55. unsigned max_parallelism = 512;
  56. unsigned threads_per_dim_x = max_parallelism;
  57. while (threads_per_dim_x / 2 >= nx)
  58. threads_per_dim_x /= 2;
  59. unsigned threads_per_dim_y = max_parallelism / threads_per_dim_x;
  60. while (threads_per_dim_y / 2 >= ny)
  61. threads_per_dim_y /= 2;
  62. #if 0
  63. unsigned threads_per_dim_z = 4;
  64. dim3 dimBlock(threads_per_dim_x, threads_per_dim_y, threads_per_dim_z);
  65. dim3 dimGrid(nx / threads_per_dim_x, ny / threads_per_dim_y, nz / threads_per_dim_z);
  66. #else
  67. dim3 dimBlock(threads_per_dim_x, threads_per_dim_y);
  68. dim3 dimGrid((nx + threads_per_dim_x-1) / threads_per_dim_x, (ny + threads_per_dim_y-1) / threads_per_dim_y);
  69. #endif
  70. cuda_life_update <<<dimGrid, dimBlock, 0, starpu_cuda_get_local_stream()>>> (bz, old, newp, nx, ny, nz, ldy, ldz, iter);
  71. }