1515#include < nvrtc.h>
1616#include < stdarg.h>
1717#include < string.h>
18+ #include < sys/stat.h>
1819#include < sys/types.h>
1920
2021#include < cstdlib>
@@ -154,8 +155,6 @@ static int CeedCompileCore_Cuda(Ceed ceed, const char *source, const bool throw_
154155 // Add string source argument provided in call
155156 code << source;
156157
157- // Create Program
158-
159158 // Compile kernel
160159 CeedDebug256 (ceed, CEED_DEBUG_COLOR_SUCCESS, " ---------- ATTEMPTING TO COMPILE JIT SOURCE ----------\n " );
161160 CeedDebug (ceed, " Source:\n %s\n " , code.str ().c_str ());
@@ -221,15 +220,26 @@ static int CeedCompileCore_Cuda(Ceed ceed, const char *source, const bool throw_
221220 CeedCallBackend (CeedFree (&ptx));
222221 return CEED_ERROR_SUCCESS;
223222 } else {
224- const char *full_filename = " temp_kernel_source.cu" ;
225- FILE *file = fopen (full_filename, " w" );
223+ const int build_id = rand ();
226224
227- CeedCheck (file, ceed, CEED_ERROR_BACKEND, " Failed to create file. Write access is required for cuda-clang \n " );
228- fputs (code. str (). c_str (), file);
229- fclose (file );
225+ // Create temp dir if needed
226+ {
227+ DIR *dir = opendir ( " temp " );
230228
231- // Get rust crate directories
229+ if (dir) closedir (dir);
230+ else mkdir (" temp" , 000 );
231+ }
232+ // Write code to temp file
233+ {
234+ std::string filename = std::string (" temp/kernel_source_" ) + std::to_string (build_id) + std::string (" .cu" );
235+ FILE *file = fopen (filename.c_str (), " w" );
236+
237+ CeedCheck (file, ceed, CEED_ERROR_BACKEND, " Failed to create file. Write access is required for cuda-clang" );
238+ fputs (code.str ().c_str (), file);
239+ fclose (file);
240+ }
232241
242+ // Get rust crate directories
233243 const char **rust_source_dirs = nullptr ;
234244 int num_rust_source_dirs = 0 ;
235245
@@ -265,14 +275,17 @@ static int CeedCompileCore_Cuda(Ceed ceed, const char *source, const bool throw_
265275
266276 // Compile wrapper kernel
267277 command = " clang++ -flto=thin --cuda-gpu-arch=sm_" + std::to_string (prop.major ) + std::to_string (prop.minor ) +
268- " --cuda-device-only -emit-llvm -S temp_kernel_source.cu -o temp_kernel.ll " ;
278+ " --cuda-device-only -emit-llvm -S temp/kernel_source_" + std::to_string (build_id) + " .cu -o temp/kernel_" + std::to_string (build_id) +
279+ " .ll " ;
269280 command += opts[4 ];
270281 CeedCallSystem (ceed, command.c_str (), " JiT kernel source" );
271282
272283 // the find command finds the rust-installed llvm-link tool and runs it
273- command = " $(find $(rustup run " + std::string (rust_toolchain) +
274- " rustc --print sysroot) -name llvm-link) temp_kernel.ll --ignore-non-bitcode --internalize --only-needed -S -o "
275- " temp_kernel_linked.ll " ;
284+ command = " $(find $(rustup run " + std::string (rust_toolchain) + " rustc --print sysroot) -name llvm-link) temp/kernel_" +
285+ std::to_string (build_id) +
286+ " .ll --ignore-non-bitcode --internalize --only-needed -S -o "
287+ " temp/kernel_linked_" +
288+ std::to_string (build_id) + " .ll " ;
276289
277290 // Searches for .a files in rust directoy
278291 // Note: this is necessary because rust crate names may not match the folder they are in
@@ -298,18 +311,20 @@ static int CeedCompileCore_Cuda(Ceed ceed, const char *source, const bool throw_
298311 // Link, optimize, and compile final CUDA kernel
299312 // note that the find command is used to find the rust-installed llvm tool
300313 CeedCallSystem (ceed, command.c_str (), " link C and Rust source" );
301- CeedCallSystem (ceed,
302- (" $(find $(rustup run " + std::string (rust_toolchain) +
303- " rustc --print sysroot) -name opt) --passes internalize,inline temp_kernel_linked.ll -o temp_kernel_opt.bc" )
304- .c_str (),
305- " optimize linked C and Rust source" );
314+ CeedCallSystem (
315+ ceed,
316+ (" $(find $(rustup run " + std::string (rust_toolchain) + " rustc --print sysroot) -name opt) --passes internalize,inline temp/kernel_linked_" +
317+ std::to_string (build_id) + " .ll -o temp/kernel_opt_" + std::to_string (build_id) + " .bc" )
318+ .c_str (),
319+ " optimize linked C and Rust source" );
306320 CeedCallSystem (ceed,
307321 (" $(find $(rustup run " + std::string (rust_toolchain) + " rustc --print sysroot) -name llc) -O3 -mcpu=sm_" +
308- std::to_string (prop.major ) + std::to_string (prop.minor ) + " temp_kernel_opt.bc -o temp_kernel_final.ptx" )
322+ std::to_string (prop.major ) + std::to_string (prop.minor ) + " temp/kernel_opt_" + std::to_string (build_id) +
323+ " .bc -o temp/kernel_final_" + std::to_string (build_id) + " .ptx" )
309324 .c_str (),
310325 " compile final CUDA kernel" );
311326
312- ifstream ptxfile (" temp_kernel_final .ptx" );
327+ ifstream ptxfile (" temp/kernel_final_ " + std::to_string (build_id) + " .ptx" );
313328 ostringstream sstr;
314329
315330 sstr << ptxfile.rdbuf ();
0 commit comments