life_cuda.cu 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010 Université de Bordeaux 1
  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
  20. 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
  55. cuda_life_update_host(int bz, const TYPE *old, TYPE *newp, int nx, int ny, int nz, int ldy, int ldz, int iter)
  56. {
  57. unsigned max_parallelism = 512;
  58. unsigned threads_per_dim_x = max_parallelism;
  59. while (threads_per_dim_x / 2 >= nx)
  60. threads_per_dim_x /= 2;
  61. unsigned threads_per_dim_y = max_parallelism / threads_per_dim_x;
  62. while (threads_per_dim_y / 2 >= ny)
  63. threads_per_dim_y /= 2;
  64. #if 0
  65. unsigned threads_per_dim_z = 4;
  66. dim3 dimBlock(threads_per_dim_x, threads_per_dim_y, threads_per_dim_z);
  67. dim3 dimGrid(nx / threads_per_dim_x, ny / threads_per_dim_y, nz / threads_per_dim_z);
  68. #else
  69. dim3 dimBlock(threads_per_dim_x, threads_per_dim_y);
  70. dim3 dimGrid((nx + threads_per_dim_x-1) / threads_per_dim_x, (ny + threads_per_dim_y-1) / threads_per_dim_y);
  71. #endif
  72. cuda_life_update <<<dimGrid, dimBlock, 0, starpu_cuda_get_local_stream()>>> (bz, old, newp, nx, ny, nz, ldy, ldz, iter);
  73. }