multiformat_cuda.cu 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011,2012 Inria
  4. * Copyright (C) 2011-2013,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 "multiformat_types.h"
  19. #include "../test_interfaces.h"
  20. #include "../../../helper.h"
  21. extern struct test_config multiformat_config;
  22. static __global__ void multiformat_cuda(struct struct_of_arrays *soa, unsigned n,
  23. int *err, int factor)
  24. {
  25. unsigned i = blockIdx.x*blockDim.x + threadIdx.x;
  26. if (i >= n)
  27. return;
  28. if (soa->x[i] != i * factor || soa->y[i] != i * factor)
  29. {
  30. *err = 1;
  31. }
  32. else
  33. {
  34. soa->x[i] = -soa->x[i];
  35. soa->y[i] = -soa->y[i];
  36. }
  37. }
  38. extern "C" void test_multiformat_cuda_func(void *buffers[], void *args)
  39. {
  40. FPRINTF(stderr, "Entering %s\n", __starpu_func__);
  41. int factor;
  42. int *ret;
  43. cudaError_t error;
  44. unsigned int n = STARPU_MULTIFORMAT_GET_NX(buffers[0]);
  45. struct struct_of_arrays *soa;
  46. soa = (struct struct_of_arrays *) STARPU_MULTIFORMAT_GET_CUDA_PTR(buffers[0]);
  47. unsigned threads_per_block = 64;
  48. unsigned nblocks = (n + threads_per_block-1) / threads_per_block;
  49. factor = *(int *) args;
  50. error = cudaMalloc(&ret, sizeof(int));
  51. if (error != cudaSuccess)
  52. STARPU_CUDA_REPORT_ERROR(error);
  53. error = cudaMemcpyAsync(ret,
  54. &multiformat_config.copy_failed,
  55. sizeof(int),
  56. cudaMemcpyHostToDevice, starpu_cuda_get_local_stream());
  57. if (error != cudaSuccess)
  58. STARPU_CUDA_REPORT_ERROR(error);
  59. multiformat_cuda<<<nblocks,threads_per_block,2,starpu_cuda_get_local_stream()>>>(soa, n, ret, factor);
  60. error = cudaMemcpyAsync(&multiformat_config.copy_failed,
  61. ret,
  62. sizeof(int),
  63. cudaMemcpyDeviceToHost, starpu_cuda_get_local_stream());
  64. if (error != cudaSuccess)
  65. STARPU_CUDA_REPORT_ERROR(error);
  66. cudaFree(ret);
  67. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  68. }