mixed.cu 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #include <iostream>
  2. #include "file1.h"
  3. #include "file2.h"
  4. #ifdef _WIN32
  5. # define EXPORT __declspec(dllexport)
  6. # define IMPORT __declspec(dllimport)
  7. #else
  8. # define EXPORT
  9. # define IMPORT
  10. #endif
  11. result_type __device__ file1_func(int x);
  12. result_type_dynamic __device__ file2_func(int x);
  13. IMPORT bool __host__ cuda_dynamic_lib_func();
  14. static __global__ void mixed_kernel(result_type* r, int x)
  15. {
  16. *r = file1_func(x);
  17. result_type_dynamic rd = file2_func(x);
  18. }
  19. EXPORT int mixed_launch_kernel(int x)
  20. {
  21. if (!cuda_dynamic_lib_func()) {
  22. return x;
  23. }
  24. result_type* r;
  25. cudaError_t err = cudaMallocManaged(&r, sizeof(result_type));
  26. if (err != cudaSuccess) {
  27. std::cerr << "mixed_launch_kernel: cudaMallocManaged failed: "
  28. << cudaGetErrorString(err) << std::endl;
  29. return x;
  30. }
  31. mixed_kernel<<<1, 1>>>(r, x);
  32. err = cudaGetLastError();
  33. if (err != cudaSuccess) {
  34. std::cerr << "mixed_kernel [SYNC] failed: " << cudaGetErrorString(err)
  35. << std::endl;
  36. return x;
  37. }
  38. err = cudaDeviceSynchronize();
  39. if (err != cudaSuccess) {
  40. std::cerr << "mixed_kernel [ASYNC] failed: "
  41. << cudaGetErrorString(cudaGetLastError()) << std::endl;
  42. return x;
  43. }
  44. int result = r->sum;
  45. err = cudaFree(r);
  46. if (err != cudaSuccess) {
  47. std::cerr << "mixed_launch_kernel: cudaFree failed: "
  48. << cudaGetErrorString(err) << std::endl;
  49. return x;
  50. }
  51. return result;
  52. }