Skip to content

Commit 3097688

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

File tree

6 files changed

+45
-24
lines changed

6 files changed

+45
-24
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: 39 additions & 20 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>
@@ -54,7 +55,7 @@ static int CeedCallSystem_Core(Ceed ceed, const char *command, const char *messa
5455
}
5556
CeedDebug(ceed, "Command output:\n%s\n", output);
5657

57-
CeedCheck(pclose(output_stream) == 0, ceed, CEED_ERROR_BACKEND, "Failed to %s with error: %s", message, output);
58+
CeedCheck(pclose(output_stream) == 0, ceed, CEED_ERROR_BACKEND, "Failed to %s with command: %s\nand error: %s", message, command, output);
5859
return CEED_ERROR_SUCCESS;
5960
}
6061

@@ -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,30 @@ 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) {
230+
closedir(dir);
231+
} else {
232+
mkdir("temp", 0777);
233+
chmod("temp", 0777);
234+
}
235+
}
236+
// Write code to temp file
237+
{
238+
std::string filename = std::string("temp/kernel_") + std::to_string(build_id) + std::string("_0_source.cu");
239+
FILE *file = fopen(filename.c_str(), "w");
240+
241+
CeedCheck(file, ceed, CEED_ERROR_BACKEND, "Failed to create file. Write access is required for cuda-clang");
242+
fputs(code.str().c_str(), file);
243+
fclose(file);
244+
}
232245

246+
// Get rust crate directories
233247
const char **rust_source_dirs = nullptr;
234248
int num_rust_source_dirs = 0;
235249

@@ -265,14 +279,17 @@ static int CeedCompileCore_Cuda(Ceed ceed, const char *source, const bool throw_
265279

266280
// Compile wrapper kernel
267281
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 ";
282+
" --cuda-device-only -emit-llvm -S temp/kernel_" + std::to_string(build_id) + "_0_source.cu -o temp/kernel_" +
283+
std::to_string(build_id) + "_1_wrapped.ll";
269284
command += opts[4];
270285
CeedCallSystem(ceed, command.c_str(), "JiT kernel source");
271286

272287
// 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 ";
288+
command = "$(find $(rustup run " + std::string(rust_toolchain) + " rustc --print sysroot) -name llvm-link) temp/kernel_" +
289+
std::to_string(build_id) +
290+
"_1_wrapped.ll --ignore-non-bitcode --internalize --only-needed -S -o "
291+
"temp/kernel_" +
292+
std::to_string(build_id) + "_2_linked.ll";
276293

277294
// Searches for .a files in rust directoy
278295
// Note: this is necessary because rust crate names may not match the folder they are in
@@ -298,18 +315,20 @@ static int CeedCompileCore_Cuda(Ceed ceed, const char *source, const bool throw_
298315
// Link, optimize, and compile final CUDA kernel
299316
// note that the find command is used to find the rust-installed llvm tool
300317
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");
318+
CeedCallSystem(
319+
ceed,
320+
("$(find $(rustup run " + std::string(rust_toolchain) + " rustc --print sysroot) -name opt) --passes internalize,inline temp/kernel_" +
321+
std::to_string(build_id) + "_2_linked.ll -o temp/kernel_" + std::to_string(build_id) + "_3_opt.bc")
322+
.c_str(),
323+
"optimize linked C and Rust source");
306324
CeedCallSystem(ceed,
307325
("$(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")
326+
std::to_string(prop.major) + std::to_string(prop.minor) + " temp/kernel_" + std::to_string(build_id) +
327+
"_3_opt.bc -o temp/kernel_" + std::to_string(build_id) + "_4_final.ptx")
309328
.c_str(),
310329
"compile final CUDA kernel");
311330

312-
ifstream ptxfile("temp_kernel_final.ptx");
331+
ifstream ptxfile("temp/kernel_" + std::to_string(build_id) + "_4_final.ptx");
313332
ostringstream sstr;
314333

315334
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)