Skip to content

Commit

Permalink
[Kernel] Adds hash() method
Browse files Browse the repository at this point in the history
  • Loading branch information
dmed256 committed Jul 6, 2019
1 parent 15733e4 commit 284aff8
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 5 deletions.
2 changes: 2 additions & 0 deletions include/occa/c/kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ OCCA_LFUNC const char* OCCA_RFUNC occaKernelSourceFilename(occaKernel kernel);

OCCA_LFUNC const char* OCCA_RFUNC occaKernelBinaryFilename(occaKernel kernel);

OCCA_LFUNC const char* OCCA_RFUNC occaKernelHash(occaKernel kernel);

OCCA_LFUNC int OCCA_RFUNC occaKernelMaxDims(occaKernel kernel);

OCCA_LFUNC occaDim OCCA_RFUNC occaKernelMaxOuterDims(occaKernel kernel);
Expand Down
11 changes: 7 additions & 4 deletions include/occa/core/kernel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,21 @@ namespace occa {
//---[ modeKernel_t ]---------------------
class modeKernel_t : public gc::ringEntry_t {
public:
// Information about the kernel
occa::modeDevice_t *modeDevice;

std::string name;
std::string sourceFilename, binaryFilename;
occa::properties properties;
hash_t hash;

gc::ring_t<kernel> kernelRing;

// Requirements to launch kernel
dim outerDims, innerDims;

std::vector<kernelArgData> arguments;
lang::kernelMetadata metadata;

// References
gc::ring_t<kernel> kernelRing;

modeKernel_t(modeDevice_t *modeDevice_,
const std::string &name_,
const std::string &sourceFilename_,
Expand Down Expand Up @@ -120,6 +122,7 @@ namespace occa {
const std::string& name();
const std::string& sourceFilename();
const std::string& binaryFilename();
hash_t hash();

int maxDims();
dim maxOuterDims();
Expand Down
15 changes: 15 additions & 0 deletions src/c/kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,21 @@ const char* OCCA_RFUNC occaKernelBinaryFilename(occaKernel kernel) {
return occa::c::kernel(kernel).binaryFilename().c_str();
}

const char* OCCA_RFUNC occaKernelHash(occaKernel kernel) {
occa::hash_t hash = occa::c::kernel(kernel).hash();
if (!hash.isInitialized()) {
return (const char*) NULL;
}

std::string hashStr = hash.toFullString();

const int charCount = (int) hashStr.size();
char *c_str = (char*) ::malloc(charCount);
::memcpy(c_str, hashStr.c_str(), charCount);

return c_str;
}

int OCCA_RFUNC occaKernelMaxDims(occaKernel kernel) {
return occa::c::kernel(kernel).maxDims();
}
Expand Down
4 changes: 3 additions & 1 deletion src/core/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,9 @@ namespace occa {
kernelHash,
allProps);

if (!cachedKernel.isInitialized()) {
if (cachedKernel.isInitialized()) {
cachedKernel.modeKernel->hash = kernelHash;
} else {
sys::rmrf(hashDir);
}

Expand Down
6 changes: 6 additions & 0 deletions src/core/kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,12 @@ namespace occa {
: noBinaryFilename);
}

hash_t kernel::hash() {
return (modeKernel
? modeKernel->hash
: hash_t());
}

void kernel::setRunDims(occa::dim outerDims, occa::dim innerDims) {
if (modeKernel) {
modeKernel->innerDims = innerDims;
Expand Down
4 changes: 4 additions & 0 deletions tests/src/c/kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ void testInfo() {
occa::startsWith(binaryFilename, occa::io::cachePath())
);

const char *hash = occaKernelHash(addVectors);
ASSERT_TRUE(hash != NULL);
::free((void*) hash);

occaKernelMaxDims(addVectors);
occaKernelMaxOuterDims(addVectors);
occaKernelMaxInnerDims(addVectors);
Expand Down
3 changes: 3 additions & 0 deletions tests/src/core/kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ void testInfo() {
ASSERT_EQ(addVectors2.mode(),
"No Mode");

ASSERT_TRUE(addVectors.hash().isInitialized());
ASSERT_FALSE(addVectors2.hash().isInitialized());

addVectors2 = addVectors;

ASSERT_EQ(addVectors.mode(),
Expand Down

0 comments on commit 284aff8

Please sign in to comment.