life_cuda.cu 3.1 KB

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