life_cuda.cu 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * StarPU
  3. * Copyright (C) Université Bordeaux 1, CNRS 2009-2010 (see AUTHORS file)
  4. *
  5. * This program 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. * This program 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. for (x = K + idx; x < nx - K; x += stepx) {
  35. unsigned index = x + y*ldy + z*ldz;
  36. num = 0
  37. + old[index+1*ldy+0*ldz]
  38. + old[index+1*ldy+1*ldz]
  39. + old[index+0*ldy+1*ldz]
  40. + old[index-1*ldy+1*ldz]
  41. + old[index-1*ldy+0*ldz]
  42. + old[index-1*ldy-1*ldz]
  43. + old[index+0*ldy-1*ldz]
  44. + old[index+1*ldy-1*ldz]
  45. ;
  46. alive = old[index];
  47. alive = (alive && num == 2) || num == 3;
  48. newp[index] = alive;
  49. }
  50. }
  51. }
  52. extern "C" void
  53. 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>>> (bz, old, newp, nx, ny, nz, ldy, ldz, iter);
  71. }