tensor_cuda.cu 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  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 tensor_config;
  19. static __global__ void tensor_cuda(int *tensor,
  20. int nx, int ny, int nz, int nt,
  21. unsigned ldy, unsigned ldz, unsigned ldt,
  22. int factor, int *err)
  23. {
  24. int i, j, k, l;
  25. int val = 0;
  26. for (l = 0; l < nt ;l++)
  27. {
  28. for (k = 0; k < nz ;k++)
  29. {
  30. for (j = 0; j < ny ;j++)
  31. {
  32. for(i = 0; i < nx ;i++)
  33. {
  34. if (tensor[(l*ldt)+(k*ldz)+(j*ldy)+i] != factor * val)
  35. {
  36. *err = 1;
  37. return;
  38. }
  39. else
  40. {
  41. tensor[(l*ldt)+(k*ldz)+(j*ldy)+i] *= -1;
  42. val++;
  43. }
  44. }
  45. }
  46. }
  47. }
  48. }
  49. extern "C" void test_tensor_cuda_func(void *buffers[], void *args)
  50. {
  51. cudaError_t error;
  52. int *ret;
  53. error = cudaMalloc(&ret, sizeof(int));
  54. if (error != cudaSuccess)
  55. STARPU_CUDA_REPORT_ERROR(error);
  56. error = cudaMemcpyAsync(ret, &tensor_config.copy_failed, sizeof(int), cudaMemcpyHostToDevice, starpu_cuda_get_local_stream());
  57. if (error != cudaSuccess)
  58. STARPU_CUDA_REPORT_ERROR(error);
  59. int nx = STARPU_TENSOR_GET_NX(buffers[0]);
  60. int ny = STARPU_TENSOR_GET_NY(buffers[0]);
  61. int nz = STARPU_TENSOR_GET_NZ(buffers[0]);
  62. int nt = STARPU_TENSOR_GET_NT(buffers[0]);
  63. unsigned ldy = STARPU_TENSOR_GET_LDY(buffers[0]);
  64. unsigned ldz = STARPU_TENSOR_GET_LDZ(buffers[0]);
  65. unsigned ldt = STARPU_TENSOR_GET_LDT(buffers[0]);
  66. int *tensor = (int *) STARPU_TENSOR_GET_PTR(buffers[0]);
  67. int factor = *(int*) args;
  68. tensor_cuda<<<1,1, 0, starpu_cuda_get_local_stream()>>>
  69. (tensor, nx, ny, nz, nt, ldy, ldz, ldt, factor, ret);
  70. error = cudaGetLastError();
  71. if (error != cudaSuccess) STARPU_CUDA_REPORT_ERROR(error);
  72. error = cudaMemcpyAsync(&tensor_config.copy_failed, ret, sizeof(int), cudaMemcpyDeviceToHost, starpu_cuda_get_local_stream());
  73. if (error != cudaSuccess)
  74. STARPU_CUDA_REPORT_ERROR(error);
  75. cudaFree(ret);
  76. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  77. }