life_cuda.cu 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. #include <starpu_cuda.h>
  19. /* Heart of the stencil computation: compute a new state from an old one. */
  20. extern "C" __global__ void
  21. cuda_life_update(int bz, const TYPE *old, TYPE *newp, int nx, int ny, int nz, int ldy, int ldz, int iter)
  22. {
  23. unsigned idx = threadIdx.x + blockIdx.x * blockDim.x;
  24. unsigned idy = threadIdx.y + blockIdx.y * blockDim.y;
  25. //unsigned idz = threadIdx.z + blockIdx.z * blockDim.z;
  26. unsigned idz = 0;
  27. unsigned stepx = blockDim.x * gridDim.x;
  28. unsigned stepy = blockDim.y * gridDim.y;
  29. //unsigned stepz = blockDim.z * gridDim.z;
  30. unsigned stepz = 1;
  31. unsigned x, y, z;
  32. unsigned num, alive;
  33. for (z = iter + idz; z < nz - iter; z += stepz)
  34. for (y = K + idy; y < ny - K; y += stepy) {
  35. for (x = K + idx; x < nx - K; x += stepx) {
  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
  54. 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. }