test_vector_cuda.cu 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011 Institut National de Recherche en Informatique et Automatique
  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 vector_config;
  19. __global__ void framework_cuda(int *val, unsigned 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] = -val[i];
  28. }
  29. extern "C" void test_vector_cuda_func(void *buffers[], void *args)
  30. {
  31. cudaError_t error;
  32. int *ret;
  33. error = cudaMalloc(&ret, sizeof(int));
  34. if (error != cudaSuccess)
  35. {
  36. fprintf(stderr, "cudaMalloc failed...\n");
  37. return;
  38. }
  39. error = cudaMemcpy(ret, &vector_config.copy_failed, sizeof(int), cudaMemcpyHostToDevice);
  40. if (error != cudaSuccess)
  41. return;
  42. unsigned n = STARPU_VECTOR_GET_NX(buffers[0]);
  43. int *val = (int *)STARPU_VECTOR_GET_PTR(buffers[0]);
  44. int factor = *(int*) args;
  45. unsigned threads_per_block = 64;
  46. unsigned nblocks = (n + threads_per_block-1) / threads_per_block;
  47. framework_cuda<<<nblocks,threads_per_block,0,starpu_cuda_get_local_stream()>>>(val, n, ret, factor);
  48. error = cudaMemcpy(&vector_config.copy_failed, ret, sizeof(int), cudaMemcpyDeviceToHost);
  49. if (error != cudaSuccess)
  50. {
  51. return;
  52. }
  53. cudaFree(ret);
  54. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  55. }