Skip to content

Commit

Permalink
[#204][Kernel] Kernel compilation now creates .success files to safel…
Browse files Browse the repository at this point in the history
…y avoid locks
  • Loading branch information
dmed256 committed Sep 2, 2018
1 parent 780a06a commit ce46013
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 65 deletions.
6 changes: 6 additions & 0 deletions include/occa/io/cache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ namespace occa {
const hash_t &hash,
const std::string &header = "");

void markCachedFileComplete(const std::string &hashDir,
const std::string &filename);

bool cachedFileIsComplete(const std::string &hashDir,
const std::string &filename);

void setBuildProps(occa::json &props);

void writeBuildFile(const std::string &filename,
Expand Down
1 change: 0 additions & 1 deletion include/occa/io/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ namespace occa {

namespace io {
const std::string& cachePath();

const std::string& libraryPath();

void endWithSlash(std::string &dir);
Expand Down
19 changes: 19 additions & 0 deletions src/io/cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,25 @@ namespace occa {
return sourceFile;
}

void markCachedFileComplete(const std::string &hashDir,
const std::string &filename) {
std::string successFile = hashDir;
successFile += ".success/";
sys::mkpath(successFile);

successFile += filename;
io::write(successFile, "");
}

bool cachedFileIsComplete(const std::string &hashDir,
const std::string &filename) {
std::string successFile = hashDir;
successFile += ".success/";
successFile += filename;

return io::exists(successFile);
}

void setBuildProps(occa::json &props) {
props["date"] = sys::date();
props["human_date"] = sys::humanDate();
Expand Down
41 changes: 24 additions & 17 deletions src/mode/cuda/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,12 @@ namespace occa {
bool foundBinary = true;
bool usingOKL = kernelProps.get("okl", true);

io::lock_t lock(kernelHash, "cuda-kernel");
if (lock.isMine()) {
if (io::isFile(binaryFilename)) {
lock.release();
} else {
// Check if binary exists and is finished
io::lock_t lock;
if (!io::cachedFileIsComplete(hashDir, kc::binaryFile) ||
!io::isFile(binaryFilename)) {
lock = io::lock_t(kernelHash, "cuda-kernel");
if (lock.isMine()) {
foundBinary = false;
}
}
Expand Down Expand Up @@ -309,6 +310,7 @@ namespace occa {
lock);

// Regular CUDA Kernel
modeKernel_t *k = NULL;
if (!launcherKernel) {
CUmodule cuModule;
CUfunction cuFunction;
Expand All @@ -328,20 +330,25 @@ namespace occa {
OCCA_CUDA_ERROR("Kernel [" + kernelName + "]: Loading Function",
error);
}
return new kernel(this,
kernelName,
sourceFilename,
cuModule,
cuFunction,
kernelProps);
k = new kernel(this,
kernelName,
sourceFilename,
cuModule,
cuFunction,
kernelProps);
} else {
k = buildOKLKernelFromBinary(hashDir,
kernelName,
hostMetadata,
deviceMetadata,
kernelProps,
lock);
}

return buildOKLKernelFromBinary(hashDir,
kernelName,
hostMetadata,
deviceMetadata,
kernelProps,
lock);
if (k) {
io::markCachedFileComplete(hashDir, kc::binaryFile);
}
return k;
}

void device::setArchCompilerFlags(occa::properties &kernelProps) {
Expand Down
41 changes: 24 additions & 17 deletions src/mode/hip/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,12 @@ namespace occa {
bool foundBinary = true;
bool usingOKL = kernelProps.get("okl", true);

io::lock_t lock(kernelHash, "hip-kernel");
if (lock.isMine()) {
if (io::isFile(binaryFilename)) {
lock.release();
} else {
// Check if binary exists and is finished
io::lock_t lock;
if (!io::cachedFileIsComplete(hashDir, kc::binaryFile) ||
!io::isFile(binaryFilename)) {
lock = io::lock_t(kernelHash, "hip-kernel");
if (lock.isMine()) {
foundBinary = false;
}
}
Expand Down Expand Up @@ -315,6 +316,7 @@ namespace occa {
lock);

// Regular HIP Kernel
modeKernel_t *k = NULL;
if (!launcherKernel) {
hipModule_t hipModule;
hipFunction_t hipFunction;
Expand All @@ -334,20 +336,25 @@ namespace occa {
OCCA_HIP_ERROR("Kernel [" + kernelName + "]: Loading Function",
error);
}
return new kernel(this,
kernelName,
sourceFilename,
hipModule,
hipFunction,
kernelProps);
k = new kernel(this,
kernelName,
sourceFilename,
hipModule,
hipFunction,
kernelProps);
} else {
k = buildOKLKernelFromBinary(hashDir,
kernelName,
hostMetadata,
deviceMetadata,
kernelProps,
lock);
}

return buildOKLKernelFromBinary(hashDir,
kernelName,
hostMetadata,
deviceMetadata,
kernelProps,
lock);
if (k) {
io::markCachedFileComplete(hashDir, kc::binaryFile);
}
return k;
}

void device::setArchCompilerFlags(occa::properties &kernelProps) {
Expand Down
43 changes: 25 additions & 18 deletions src/mode/opencl/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,11 +233,12 @@ namespace occa {
clInfo.clDevice = clDevice;
clInfo.clContext = clContext;

io::lock_t lock(kernelHash, "opencl-kernel");
if (lock.isMine()) {
if (io::isFile(binaryFilename)) {
lock.release();
} else {
// Check if binary exists and is finished
io::lock_t lock;
if (!io::cachedFileIsComplete(hashDir, kc::binaryFile) ||
!io::isFile(binaryFilename)) {
lock = io::lock_t(kernelHash, "opencl-kernel");
if (lock.isMine()) {
foundBinary = false;
}
}
Expand Down Expand Up @@ -331,25 +332,31 @@ namespace occa {
lock);

// Regular OpenCL Kernel
modeKernel_t *k = NULL;
if (!launcherKernel) {
opencl::buildKernelFromProgram(clInfo,
kernelName,
lock);
return new kernel(this,
kernelName,
sourceFilename,
clDevice,
clInfo.clKernel,
kernelProps);
k = new kernel(this,
kernelName,
sourceFilename,
clDevice,
clInfo.clKernel,
kernelProps);
} else {
k = buildOKLKernelFromBinary(clInfo,
hashDir,
kernelName,
hostMetadata,
deviceMetadata,
kernelProps,
lock);
}

return buildOKLKernelFromBinary(clInfo,
hashDir,
kernelName,
hostMetadata,
deviceMetadata,
kernelProps,
lock);
if (k) {
io::markCachedFileComplete(hashDir, kc::binaryFile);
}
return k;
}

modeKernel_t* device::buildOKLKernelFromBinary(info_t &clInfo,
Expand Down
27 changes: 15 additions & 12 deletions src/mode/serial/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,20 +188,22 @@ namespace occa {
const hash_t kernelHash,
const occa::properties &kernelProps) {
const std::string hashDir = io::hashDir(filename, kernelHash);
std::string binaryFilename = hashDir + kc::binaryFile;
// Binary name depends if this is being used as the launcher kernel for
// GPU-style kernels
const std::string &kcBinaryFile = (
(filename != (hashDir + kc::hostSourceFile))
? kc::binaryFile
: kc::hostBinaryFile
);
std::string binaryFilename = hashDir + kcBinaryFile;
bool foundBinary = true;

// This is a launcher kernel
// TODO: Clean this up
if (startsWith(filename, hashDir)) {
binaryFilename = io::dirname(filename) + kc::hostBinaryFile;
}

io::lock_t lock(kernelHash, "serial-kernel");
if (lock.isMine()) {
if (io::isFile(binaryFilename)) {
lock.release();
} else {
// Check if binary exists and is finished
io::lock_t lock;
if (!io::cachedFileIsComplete(hashDir, kcBinaryFile) ||
!io::isFile(binaryFilename)) {
lock = io::lock_t(kernelHash, "serial-kernel");
if (lock.isMine()) {
foundBinary = false;
}
}
Expand Down Expand Up @@ -300,6 +302,7 @@ namespace occa {
kernelName,
kernelProps);
if (k) {
io::markCachedFileComplete(hashDir, kcBinaryFile);
k->sourceFilename = filename;
}
return k;
Expand Down

0 comments on commit ce46013

Please sign in to comment.