file3.cu 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #include <iostream>
  2. #include "file1.h"
  3. #include "file2.h"
  4. result_type __device__ file1_func(int x);
  5. result_type_dynamic __device__ file2_func(int x);
  6. static __global__ void file3_kernel(result_type* r, int x)
  7. {
  8. *r = file1_func(x);
  9. result_type_dynamic rd = file2_func(x);
  10. }
  11. int file3_launch_kernel(int x)
  12. {
  13. result_type* r;
  14. cudaError_t err = cudaMallocManaged(&r, sizeof(result_type));
  15. if (err != cudaSuccess) {
  16. std::cerr << "file3_launch_kernel: cudaMallocManaged failed: "
  17. << cudaGetErrorString(err) << std::endl;
  18. return x;
  19. }
  20. file3_kernel<<<1, 1>>>(r, x);
  21. err = cudaGetLastError();
  22. if (err != cudaSuccess) {
  23. std::cerr << "file3_kernel [SYNC] failed: " << cudaGetErrorString(err)
  24. << std::endl;
  25. return x;
  26. }
  27. err = cudaDeviceSynchronize();
  28. if (err != cudaSuccess) {
  29. std::cerr << "file3_kernel [ASYNC] failed: "
  30. << cudaGetErrorString(cudaGetLastError()) << std::endl;
  31. return x;
  32. }
  33. int result = r->sum;
  34. err = cudaFree(r);
  35. if (err != cudaSuccess) {
  36. std::cerr << "file3_launch_kernel: cudaFree failed: "
  37. << cudaGetErrorString(err) << std::endl;
  38. return x;
  39. }
  40. return result;
  41. }