bcsr_cuda.cu 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011, 2012 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 <starpu_cuda.h>
  18. #include "../test_interfaces.h"
  19. extern struct test_config bcsr_config;
  20. __global__ void bcsr_cuda(int *nzval, uint32_t nnz, int *err, int factor)
  21. {
  22. unsigned i = blockIdx.x*blockDim.x + threadIdx.x;
  23. if (i >= nnz)
  24. return;
  25. if (nzval[i] != i*factor)
  26. *err = 1;
  27. else
  28. nzval[i] = -nzval[i];
  29. }
  30. extern "C" void test_bcsr_cuda_func(void *buffers[], void *args)
  31. {
  32. int factor;
  33. int *ret;
  34. int *val;
  35. cudaError_t error;
  36. uint32_t nnz = STARPU_BCSR_GET_NNZ(buffers[0]);
  37. uint32_t r = ((struct starpu_bcsr_interface *)buffers[0])->r;
  38. uint32_t c = ((struct starpu_bcsr_interface *)buffers[0])->c;
  39. nnz *= (r*c);
  40. unsigned threads_per_block = 64;
  41. unsigned nblocks = (nnz + threads_per_block-1) / threads_per_block;
  42. factor = *(int *) args;
  43. val = (int *) STARPU_BCSR_GET_NZVAL(buffers[0]);
  44. error = cudaMalloc(&ret, sizeof(int));
  45. if (error != cudaSuccess)
  46. STARPU_CUDA_REPORT_ERROR(error);
  47. error = cudaMemcpy(ret,
  48. &bcsr_config.copy_failed,
  49. sizeof(int),
  50. cudaMemcpyHostToDevice);
  51. if (error != cudaSuccess)
  52. STARPU_CUDA_REPORT_ERROR(error);
  53. bcsr_cuda<<<nblocks,threads_per_block,2,starpu_cuda_get_local_stream()>>>
  54. (val, nnz, ret, factor);
  55. error = cudaMemcpy(&bcsr_config.copy_failed,
  56. ret,
  57. sizeof(int),
  58. cudaMemcpyDeviceToHost);
  59. cudaFree(ret);
  60. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  61. }