life_opencl.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2011 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. /* Heart of the stencil computation: compute a new state from an old one. */
  17. /* #define _externC extern "C" */
  18. #include <stencil.h>
  19. #include <CL/cl.h>
  20. #include <starpu.h>
  21. #define str(x) #x
  22. #define clsrc(t,k) "__kernel void\n\
  23. #define TYPE " str(t) "\n\
  24. #define K " str(k) "\n\
  25. life_update(int bz, __global const TYPE *old, __global TYPE *newp, int nx, int ny, int nz, int ldy, int ldz, int iter)\n\
  26. {\n\
  27. unsigned idx = get_global_id(0);\n\
  28. unsigned idy = get_global_id(1);\n\
  29. //unsigned idz = threadIdx.z + blockIdx.z * blockDim.z;\n\
  30. unsigned idz = 0;\n\
  31. unsigned stepx = get_global_size(0);\n\
  32. unsigned stepy = get_global_size(1);\n\
  33. //unsigned stepz = blockDim.z * gridDim.z;\n\
  34. unsigned stepz = 1;\n\
  35. unsigned x, y, z;\n\
  36. unsigned num, alive;\n\
  37. \n\
  38. for (z = iter + idz; z < nz - iter; z += stepz)\n\
  39. for (y = K + idy; y < ny - K; y += stepy) \n\
  40. {\n \
  41. for (x = K + idx; x < nx - K; x += stepx) \
  42. {\n \
  43. unsigned index = x + y*ldy + z*ldz;\n\
  44. num = 0\n\
  45. + old[index+1*ldy+0*ldz]\n\
  46. + old[index+1*ldy+1*ldz]\n\
  47. + old[index+0*ldy+1*ldz]\n\
  48. + old[index-1*ldy+1*ldz]\n\
  49. + old[index-1*ldy+0*ldz]\n\
  50. + old[index-1*ldy-1*ldz]\n\
  51. + old[index+0*ldy-1*ldz]\n\
  52. + old[index+1*ldy-1*ldz]\n\
  53. ;\n\
  54. alive = old[index];\n\
  55. alive = (alive && num == 2) || num == 3;\n\
  56. newp[index] = alive;\n\
  57. }\n\
  58. }\n\
  59. }"
  60. static const char * src = clsrc(TYPE,K);
  61. static struct starpu_opencl_program program;
  62. void
  63. opencl_life_init(void)
  64. {
  65. starpu_opencl_load_opencl_from_string(src, &program, NULL);
  66. }
  67. void opencl_life_free(void)
  68. {
  69. starpu_opencl_unload_opencl(&program);
  70. }
  71. void
  72. opencl_life_update_host(int bz, const TYPE *old, TYPE *newp, int nx, int ny, int nz, int ldy, int ldz, int iter)
  73. {
  74. unsigned max_parallelism = 512;
  75. unsigned threads_per_dim_x = max_parallelism;
  76. while (threads_per_dim_x / 2 >= nx)
  77. threads_per_dim_x /= 2;
  78. unsigned threads_per_dim_y = max_parallelism / threads_per_dim_x;
  79. while (threads_per_dim_y / 2 >= ny)
  80. threads_per_dim_y /= 2;
  81. #if 0
  82. unsigned threads_per_dim_z = 4;
  83. size_t dimBlock[] = {threads_per_dim_x, threads_per_dim_y, threads_per_dim_z};
  84. size_t dimGrid[] = {nx / threads_per_dim_x, ny / threads_per_dim_y, nz / threads_per_dim_z};
  85. #else
  86. size_t dimBlock[] = {threads_per_dim_x, threads_per_dim_y, 1};
  87. size_t dimGrid[] = {((nx + threads_per_dim_x-1) / threads_per_dim_x)*threads_per_dim_x, ((ny + threads_per_dim_y-1) / threads_per_dim_y)*threads_per_dim_y, 1};
  88. #endif
  89. int devid,id;
  90. id = starpu_worker_get_id();
  91. devid = starpu_worker_get_devid(id);
  92. cl_kernel kernel;
  93. cl_command_queue cq;
  94. starpu_opencl_load_kernel(&kernel, &cq, &program, "life_update", devid);
  95. clSetKernelArg(kernel, 0, sizeof(bz), &bz);
  96. clSetKernelArg(kernel, 1, sizeof(old), &old);
  97. clSetKernelArg(kernel, 2, sizeof(newp), &newp);
  98. clSetKernelArg(kernel, 3, sizeof(nx), &nx);
  99. clSetKernelArg(kernel, 4, sizeof(ny), &ny);
  100. clSetKernelArg(kernel, 5, sizeof(nz), &nz);
  101. clSetKernelArg(kernel, 6, sizeof(ldy), &ldy);
  102. clSetKernelArg(kernel, 7, sizeof(ldz), &ldz);
  103. clSetKernelArg(kernel, 8, sizeof(iter), &iter);
  104. cl_event ev;
  105. clEnqueueNDRangeKernel(cq, kernel, 3, NULL, dimGrid, dimBlock, 0, NULL, &ev);
  106. clWaitForEvents(1, &ev);
  107. starpu_opencl_collect_stats(ev);
  108. clReleaseEvent(ev);
  109. }