block_cuda.cu 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011 Institut National de Recherche en Informatique et Automatique
  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. #include <starpu.h>
  17. #include "../test_interfaces.h"
  18. extern struct test_config block_config;
  19. static __global__ void block_cuda(int *block,
  20. int nx, int ny, int nz,
  21. unsigned ldy, unsigned ldz,
  22. float factor, int *err)
  23. {
  24. int i, j, k;
  25. int val = 0;
  26. for (k = 0; k < nz ;k++)
  27. {
  28. for (j = 0; j < ny ;j++)
  29. {
  30. for(i = 0; i < nx ;i++)
  31. {
  32. if (block[(k*ldz)+(j*ldy)+i] != factor * val)
  33. {
  34. *err = 1;
  35. return;
  36. }
  37. else
  38. {
  39. block[(k*ldz)+(j*ldy)+i] *= -1;
  40. val++;
  41. }
  42. }
  43. }
  44. }
  45. }
  46. extern "C" void test_block_cuda_func(void *buffers[], void *args)
  47. {
  48. cudaError_t error;
  49. int *ret;
  50. error = cudaMalloc(&ret, sizeof(int));
  51. if (error != cudaSuccess)
  52. STARPU_CUDA_REPORT_ERROR(error);
  53. error = cudaMemcpy(ret, &block_config.copy_failed, sizeof(int), cudaMemcpyHostToDevice);
  54. if (error != cudaSuccess)
  55. STARPU_CUDA_REPORT_ERROR(error);
  56. int nx = STARPU_BLOCK_GET_NX(buffers[0]);
  57. int ny = STARPU_BLOCK_GET_NY(buffers[0]);
  58. int nz = STARPU_BLOCK_GET_NZ(buffers[0]);
  59. unsigned ldy = STARPU_BLOCK_GET_LDY(buffers[0]);
  60. unsigned ldz = STARPU_BLOCK_GET_LDZ(buffers[0]);
  61. int *block = (int *) STARPU_BLOCK_GET_PTR(buffers[0]);
  62. int factor = *(int*) args;
  63. block_cuda<<<1,1, 0, starpu_cuda_get_local_stream()>>>
  64. (block, nx, ny, nz, ldy, ldz, factor, ret);
  65. error = cudaMemcpy(&block_config.copy_failed, ret, sizeof(int), cudaMemcpyDeviceToHost);
  66. if (error != cudaSuccess)
  67. STARPU_CUDA_REPORT_ERROR(error);
  68. cudaFree(ret);
  69. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  70. }