multiformat_cuda.cu 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 <starpu_cuda.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", __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 = cudaMemcpy(ret,
  54. &multiformat_config.copy_failed,
  55. sizeof(int),
  56. cudaMemcpyHostToDevice);
  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 = cudaMemcpy(&multiformat_config.copy_failed,
  61. ret,
  62. sizeof(int),
  63. cudaMemcpyDeviceToHost);
  64. cudaFree(ret);
  65. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  66. }