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