Debian Codegraph @

Tests/Cuda/Complex/mixed.cu

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