test_vector_cuda.cu 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011,2012 Inria
  4. * Copyright (C) 2011,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 vector_config;
  20. __global__ void framework_cuda(int *val, unsigned n, int *err, int factor)
  21. {
  22. unsigned i = blockIdx.x*blockDim.x + threadIdx.x;
  23. if (i >= n)
  24. return;
  25. if (val[i] != i*factor)
  26. *err = 1;
  27. else
  28. val[i] = -val[i];
  29. }
  30. extern "C" void test_vector_cuda_func(void *buffers[], void *args)
  31. {
  32. cudaError_t error;
  33. int *ret;
  34. error = cudaMalloc(&ret, sizeof(int));
  35. if (error != cudaSuccess)
  36. {
  37. fprintf(stderr, "cudaMalloc failed...\n");
  38. return;
  39. }
  40. error = cudaMemcpyAsync(ret, &vector_config.copy_failed, sizeof(int), cudaMemcpyHostToDevice, starpu_cuda_get_local_stream());
  41. if (error != cudaSuccess)
  42. return;
  43. unsigned n = STARPU_VECTOR_GET_NX(buffers[0]);
  44. int *val = (int *)STARPU_VECTOR_GET_PTR(buffers[0]);
  45. int factor = *(int*) args;
  46. unsigned threads_per_block = 64;
  47. unsigned nblocks = (n + threads_per_block-1) / threads_per_block;
  48. framework_cuda<<<nblocks,threads_per_block,0,starpu_cuda_get_local_stream()>>>(val, n, ret, factor);
  49. error = cudaMemcpyAsync(&vector_config.copy_failed, ret, sizeof(int), cudaMemcpyDeviceToHost, starpu_cuda_get_local_stream());
  50. if (error != cudaSuccess)
  51. {
  52. return;
  53. }
  54. cudaFree(ret);
  55. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  56. }