Skip to content

Commit 37e0a1c

Browse files
committed
clang - keep temp files from overwriting each other
1 parent 02293a7 commit 37e0a1c

File tree

6 files changed

+40
-23
lines changed

6 files changed

+40
-23
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ doc/sphinx/build/
5252
doc/sphinx/source/examples/
5353

5454
# Clang GPU temp files
55-
temp_*
55+
temp/*
5656

5757
# Output files, videos, and compressed archives should not be added accidentally
5858
*.avi

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -915,7 +915,7 @@ cln clean :
915915
$(call quiet,MAKE) -C examples clean NEK5K_DIR="$(abspath $(NEK5K_DIR))"
916916
$(call quiet,MAKE) -C python/tests clean
917917
$(RM) benchmarks/*output.txt
918-
$(RM) -f temp_*
918+
$(RM) -rf temp
919919

920920
distclean : clean
921921
$(RM) -r doc/html doc/sphinx/build $(CONFIG)

backends/cuda/ceed-cuda-compile.cpp

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
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();

examples/ceed/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
ex1-volume
22
ex2-surface
3+
ex3-volume
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
temp_*
1+
ex1-volume
2+
temp/*

examples/rust-qfunctions/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@ ex1-volume: ex1-volume.c
3131

3232
clean:
3333
rm -f *~ $(EXAMPLES)
34-
rm -f temp_*
34+
rm -rf temp/
3535
rm -rf *.dSYM *.TVD.*breakpoints

0 commit comments

Comments
 (0)