ceed-hip-compile.cpp (a171b6ef1192a8f5f3031330339aaab2ded4c8fb) ceed-hip-compile.cpp (b13efd58b277efef1db70d6f06eaaf4d415a7642)
1// Copyright (c) 2017-2024, Lawrence Livermore National Security, LLC and other CEED contributors.
2// All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3//
4// SPDX-License-Identifier: BSD-2-Clause
5//
6// This file is part of CEED: http://github.com/ceed
7
8#include "ceed-hip-compile.h"

--- 23 unchanged lines hidden (view full) ---

32
33//------------------------------------------------------------------------------
34// Compile HIP kernel
35//------------------------------------------------------------------------------
36int CeedCompile_Hip(Ceed ceed, const char *source, hipModule_t *module, const CeedInt num_defines, ...) {
37 size_t ptx_size;
38 char *jit_defs_source, *ptx;
39 const char *jit_defs_path;
1// Copyright (c) 2017-2024, Lawrence Livermore National Security, LLC and other CEED contributors.
2// All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3//
4// SPDX-License-Identifier: BSD-2-Clause
5//
6// This file is part of CEED: http://github.com/ceed
7
8#include "ceed-hip-compile.h"

--- 23 unchanged lines hidden (view full) ---

32
33//------------------------------------------------------------------------------
34// Compile HIP kernel
35//------------------------------------------------------------------------------
36int CeedCompile_Hip(Ceed ceed, const char *source, hipModule_t *module, const CeedInt num_defines, ...) {
37 size_t ptx_size;
38 char *jit_defs_source, *ptx;
39 const char *jit_defs_path;
40 const int num_opts = 3;
41 const char *opts[num_opts];
40 const int num_opts = 3;
41 CeedInt num_jit_source_dirs = 0;
42 const char **opts;
42 int runtime_version;
43 hiprtcProgram prog;
44 struct hipDeviceProp_t prop;
45 Ceed_Hip *ceed_data;
46
47 hipFree(0); // Make sure a Context exists for hiprtc
48
49 std::ostringstream code;

--- 29 unchanged lines hidden (view full) ---

79 CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-jit.h", &jit_defs_path));
80 CeedCallBackend(CeedLoadSourceToBuffer(ceed, jit_defs_path, &jit_defs_source));
81 code << jit_defs_source;
82 code << "\n\n";
83 CeedCallBackend(CeedFree(&jit_defs_path));
84 CeedCallBackend(CeedFree(&jit_defs_source));
85
86 // Non-macro options
43 int runtime_version;
44 hiprtcProgram prog;
45 struct hipDeviceProp_t prop;
46 Ceed_Hip *ceed_data;
47
48 hipFree(0); // Make sure a Context exists for hiprtc
49
50 std::ostringstream code;

--- 29 unchanged lines hidden (view full) ---

80 CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-jit.h", &jit_defs_path));
81 CeedCallBackend(CeedLoadSourceToBuffer(ceed, jit_defs_path, &jit_defs_source));
82 code << jit_defs_source;
83 code << "\n\n";
84 CeedCallBackend(CeedFree(&jit_defs_path));
85 CeedCallBackend(CeedFree(&jit_defs_source));
86
87 // Non-macro options
88 CeedCallBackend(CeedCalloc(num_opts, &opts));
87 opts[0] = "-default-device";
88 CeedCallBackend(CeedGetData(ceed, (void **)&ceed_data));
89 CeedCallHip(ceed, hipGetDeviceProperties(&prop, ceed_data->device_id));
90 std::string arch_arg = "--gpu-architecture=" + std::string(prop.gcnArchName);
91 opts[1] = arch_arg.c_str();
92 opts[2] = "-munsafe-fp-atomics";
89 opts[0] = "-default-device";
90 CeedCallBackend(CeedGetData(ceed, (void **)&ceed_data));
91 CeedCallHip(ceed, hipGetDeviceProperties(&prop, ceed_data->device_id));
92 std::string arch_arg = "--gpu-architecture=" + std::string(prop.gcnArchName);
93 opts[1] = arch_arg.c_str();
94 opts[2] = "-munsafe-fp-atomics";
95 {
96 const char **jit_source_dirs;
93
97
98 CeedCallBackend(CeedGetJitSourceRoots(ceed, &num_jit_source_dirs, &jit_source_dirs));
99 CeedCallBackend(CeedRealloc(num_opts + num_jit_source_dirs, &opts));
100 for (CeedInt i = 0; i < num_jit_source_dirs; i++) {
101 std::ostringstream include_dirs_arg;
102
103 include_dirs_arg << "-I" << jit_source_dirs[i];
104 CeedCallBackend(CeedStringAllocCopy(include_dirs_arg.str().c_str(), (char **)&opts[num_opts + i]));
105 }
106 CeedCallBackend(CeedRestoreJitSourceRoots(ceed, &jit_source_dirs));
107 }
108
94 // Add string source argument provided in call
95 code << source;
96
97 // Create Program
98 CeedCallHiprtc(ceed, hiprtcCreateProgram(&prog, code.str().c_str(), NULL, 0, NULL, NULL));
99
100 // Compile kernel
109 // Add string source argument provided in call
110 code << source;
111
112 // Create Program
113 CeedCallHiprtc(ceed, hiprtcCreateProgram(&prog, code.str().c_str(), NULL, 0, NULL, NULL));
114
115 // Compile kernel
101 hiprtcResult result = hiprtcCompileProgram(prog, num_opts, opts);
116 hiprtcResult result = hiprtcCompileProgram(prog, num_opts + num_jit_source_dirs, opts);
102
117
118 for (CeedInt i = 0; i < num_jit_source_dirs; i++) {
119 CeedCallBackend(CeedFree(&opts[num_opts + i]));
120 }
121 CeedCallBackend(CeedFree(&opts));
103 if (result != HIPRTC_SUCCESS) {
104 size_t log_size;
105 char *log;
106
107 CeedDebug256(ceed, CEED_DEBUG_COLOR_ERROR, "---------- CEED JIT SOURCE FAILED TO COMPILE ----------\n");
108 CeedDebug(ceed, "Source:\n%s\n", code.str().c_str());
109 CeedDebug256(ceed, CEED_DEBUG_COLOR_ERROR, "---------- CEED JIT SOURCE FAILED TO COMPILE ----------\n");
110 CeedChk_hiprtc(ceed, hiprtcGetProgramLogSize(prog, &log_size));

--- 50 unchanged lines hidden ---
122 if (result != HIPRTC_SUCCESS) {
123 size_t log_size;
124 char *log;
125
126 CeedDebug256(ceed, CEED_DEBUG_COLOR_ERROR, "---------- CEED JIT SOURCE FAILED TO COMPILE ----------\n");
127 CeedDebug(ceed, "Source:\n%s\n", code.str().c_str());
128 CeedDebug256(ceed, CEED_DEBUG_COLOR_ERROR, "---------- CEED JIT SOURCE FAILED TO COMPILE ----------\n");
129 CeedChk_hiprtc(ceed, hiprtcGetProgramLogSize(prog, &log_size));

--- 50 unchanged lines hidden ---