MCPcopy Create free account
hub / github.com/ROCm/hip / main

Function main

docs/tools/example_codes/compilation_apis.cpp:58–164  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

56)"};
57
58int main()
59{
60 hiprtcProgram prog;
61 auto rtc_ret_code = hiprtcCreateProgram(&prog, // HIPRTC program handle
62 kernel_source, // kernel source string
63 "vector_add.cpp", // Name of the file
64 0, // Number of headers
65 nullptr, // Header sources
66 nullptr); // Name of header file
67
68 if (rtc_ret_code != HIPRTC_SUCCESS)
69 {
70 std::cerr << "Failed to create program" << std::endl;
71 std::abort();
72 }
73
74 hipDeviceProp_t props;
75 int device = 0;
76 HIP_CHECK(hipGetDeviceProperties(&props, device));
77 auto sarg = std::string{"--gpu-architecture="} + props.gcnArchName; // device for which binary is to be generated
78
79 const char* options[] = {sarg.c_str()};
80
81 rtc_ret_code = hiprtcCompileProgram(prog, // hiprtcProgram
82 1, // Number of options
83 options); // Clang Options
84 if (rtc_ret_code != HIPRTC_SUCCESS)
85 {
86 std::cerr << "Failed to create program" << std::endl;
87 std::abort();
88 }
89
90 std::size_t logSize;
91 HIPRTC_CHECK(hiprtcGetProgramLogSize(prog, &logSize));
92
93 if (logSize)
94 {
95 std::string log(logSize, '\0');
96 HIPRTC_CHECK(hiprtcGetProgramLog(prog, &log[0]));
97 std::cerr << "Compilation failed or produced warnings: " << log << std::endl;
98 std::abort();
99 }
100
101 std::size_t codeSize;
102 HIPRTC_CHECK(hiprtcGetCodeSize(prog, &codeSize));
103
104 std::vector<char> kernel_binary(codeSize);
105 HIPRTC_CHECK(hiprtcGetCode(prog, kernel_binary.data()));
106
107 HIPRTC_CHECK(hiprtcDestroyProgram(&prog));
108
109 hipModule_t module;
110 hipFunction_t kernel;
111
112 HIP_CHECK(hipModuleLoadData(&module, kernel_binary.data()));
113 HIP_CHECK(hipModuleGetFunction(&kernel, module, "vector_add"));
114
115 constexpr std::size_t ele_size = 256; // total number of items to add

Callers

nothing calls this directly

Calls 1

hipMallocFunction · 0.85

Tested by

no test coverage detected