coo_cuda.cu 1.8 KB

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