tensor_cuda.cu 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011,2012 Inria
  4. * Copyright (C) 2012,2015,2017 CNRS
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include <starpu.h>
  18. #include "../test_interfaces.h"
  19. extern struct test_config tensor_config;
  20. static __global__ void tensor_cuda(int *tensor,
  21. int nx, int ny, int nz, int nt,
  22. unsigned ldy, unsigned ldz, unsigned ldt,
  23. float factor, int *err)
  24. {
  25. int i, j, k, l;
  26. int val = 0;
  27. for (l = 0; l < nt ;l++)
  28. {
  29. for (k = 0; k < nz ;k++)
  30. {
  31. for (j = 0; j < ny ;j++)
  32. {
  33. for(i = 0; i < nx ;i++)
  34. {
  35. if (tensor[(l*ldt)+(k*ldz)+(j*ldy)+i] != factor * val)
  36. {
  37. *err = 1;
  38. return;
  39. }
  40. else
  41. {
  42. tensor[(l*ldt)+(k*ldz)+(j*ldy)+i] *= -1;
  43. val++;
  44. }
  45. }
  46. }
  47. }
  48. }
  49. }
  50. extern "C" void test_tensor_cuda_func(void *buffers[], void *args)
  51. {
  52. cudaError_t error;
  53. int *ret;
  54. error = cudaMalloc(&ret, sizeof(int));
  55. if (error != cudaSuccess)
  56. STARPU_CUDA_REPORT_ERROR(error);
  57. error = cudaMemcpyAsync(ret, &tensor_config.copy_failed, sizeof(int), cudaMemcpyHostToDevice, starpu_cuda_get_local_stream());
  58. if (error != cudaSuccess)
  59. STARPU_CUDA_REPORT_ERROR(error);
  60. int nx = STARPU_TENSOR_GET_NX(buffers[0]);
  61. int ny = STARPU_TENSOR_GET_NY(buffers[0]);
  62. int nz = STARPU_TENSOR_GET_NZ(buffers[0]);
  63. int nt = STARPU_TENSOR_GET_NT(buffers[0]);
  64. unsigned ldy = STARPU_TENSOR_GET_LDY(buffers[0]);
  65. unsigned ldz = STARPU_TENSOR_GET_LDZ(buffers[0]);
  66. unsigned ldt = STARPU_TENSOR_GET_LDT(buffers[0]);
  67. int *tensor = (int *) STARPU_TENSOR_GET_PTR(buffers[0]);
  68. int factor = *(int*) args;
  69. tensor_cuda<<<1,1, 0, starpu_cuda_get_local_stream()>>>
  70. (tensor, nx, ny, nz, nt, ldy, ldz, ldt, factor, ret);
  71. error = cudaMemcpyAsync(&tensor_config.copy_failed, ret, sizeof(int), cudaMemcpyDeviceToHost, starpu_cuda_get_local_stream());
  72. if (error != cudaSuccess)
  73. STARPU_CUDA_REPORT_ERROR(error);
  74. cudaFree(ret);
  75. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  76. }