Skip to content

Commit

Permalink
Merge pull request #45 from yan-ming/new-hash
Browse files Browse the repository at this point in the history
use FNV-1a for kernel indexing instead of md5
  • Loading branch information
whchung committed Apr 29, 2016
2 parents 5520c23 + 825a3ac commit 84095e1
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,8 @@ set(CPACK_PACKAGE_VERSION_PATCH ${KALMAR_VERSION_PATCH})
set(CPACK_PACKAGE_FILE_NAME ${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}-${CMAKE_SYSTEM_NAME})
set(CPACK_DEBIAN_PACKAGE_DESCRIPTION "HCC: a Heterogeneous C++ to OpenCL/HSA compiler")
set(CPACK_DEBIAN_PACKAGE_MAINTAINER "Siu Chi Chan <siuchi.chan@amd.com>")
set(CPACK_DEBIAN_PACKAGE_SECTION "devel")


set(CPACK_DEBIAN_PACKAGE_CONTROL_EXTRA
"${PROJECT_BINARY_DIR}/packaging/debian/postinst;${PROJECT_BINARY_DIR}/packaging/debian/prerm")
Expand Down
36 changes: 30 additions & 6 deletions lib/hsa/mcwamp_hsa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@
#define ASYNCOPS_VECTOR_GC_SIZE (1024)


// whether to use MD5 as kernel indexing hash function
// default set as 0 (use faster FNV-1a hash instead)
#define USE_MD5_HASH (0)

// cutoff size used in FNV-1a hash function
// default set as 104, which is the larger value between HSA BrigModuleHeader
// and AMD GCN ISA header (Elf64_Ehdr) from Jack's research
#define FNV1A_CUTOFF_SIZE (104)

static const char* getHSAErrorString(hsa_status_t s) {

Expand Down Expand Up @@ -1322,7 +1330,8 @@ class HSADevice final : public KalmarDevice
}

// calculate MD5 checksum
std::string MD5Sum(size_t size, void* source) {
std::string kernel_checksum(size_t size, void* source) {
#if USE_MD5_HASH
unsigned char md5_hash[16];
memset(md5_hash, 0, sizeof(unsigned char) * 16);
MD5_CTX md5ctx;
Expand All @@ -1337,10 +1346,25 @@ class HSADevice final : public KalmarDevice
}

return checksum.str();
#else
// FNV-1a hashing, 64-bit version
const uint64_t FNV_prime = 0x100000001b3;
const uint64_t FNV_basis = 0xcbf29ce484222325;
uint64_t hash = FNV_basis;

const char *str = static_cast<const char *>(source);

size = size > FNV1A_CUTOFF_SIZE ? FNV1A_CUTOFF_SIZE : size;
for (auto i = 0; i < size; ++i) {
hash ^= *str++;
hash *= FNV_prime;
}
return std::to_string(hash);
#endif
}

void BuildProgram(void* size, void* source, bool needsCompilation = true) override {
if (executables.find(MD5Sum((size_t)size, source)) == executables.end()) {
if (executables.find(kernel_checksum((size_t)size, source)) == executables.end()) {
bool use_amdgpu = false;
#ifdef HSA_USE_AMDGPU_BACKEND
const char *km_use_amdgpu = getenv("KM_USE_AMDGPU");
Expand Down Expand Up @@ -1747,7 +1771,7 @@ class HSADevice final : public KalmarDevice
void BuildOfflineFinalizedProgramImpl(void* kernelBuffer, int kernelSize) {
hsa_status_t status;

std::string index = MD5Sum((size_t)kernelSize, kernelBuffer);
std::string index = kernel_checksum((size_t)kernelSize, kernelBuffer);

// load HSA program if we haven't done so
if (executables.find(index) == executables.end()) {
Expand Down Expand Up @@ -1779,7 +1803,7 @@ class HSADevice final : public KalmarDevice
HSAKernel* CreateOfflineFinalizedKernelImpl(void *kernelBuffer, int kernelSize, const char *entryName) {
hsa_status_t status;

std::string index = MD5Sum((size_t)kernelSize, kernelBuffer);
std::string index = kernel_checksum((size_t)kernelSize, kernelBuffer);

// load HSA program if we haven't done so
if (executables.find(index) == executables.end()) {
Expand All @@ -1805,7 +1829,7 @@ class HSADevice final : public KalmarDevice
void BuildProgramImpl(const char* hsailBuffer, int hsailSize) {
hsa_status_t status;

std::string index = MD5Sum((size_t)hsailSize, (void*)hsailBuffer);
std::string index = kernel_checksum((size_t)hsailSize, (void*)hsailBuffer);

// finalize HSA program if we haven't done so
if (executables.find(index) == executables.end()) {
Expand Down Expand Up @@ -1872,7 +1896,7 @@ class HSADevice final : public KalmarDevice
HSAKernel* CreateKernelImpl(const char *hsailBuffer, int hsailSize, const char *entryName) {
hsa_status_t status;

std::string index = MD5Sum((size_t)hsailSize, (void*)hsailBuffer);
std::string index = kernel_checksum((size_t)hsailSize, (void*)hsailBuffer);

// finalize HSA program if we haven't done so
if (executables.find(index) == executables.end()) {
Expand Down
4 changes: 1 addition & 3 deletions packaging/debian/postinst.in
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ INSTALL_PATH=@INSTALL_PATH@

ROCM_PATH="/opt/rocm"

# NOTE: if you modify this table, also update the same table in postrm
# NOTE: if you modify this table, also update the same table in prerm
SOFTLINKS=(
# <DIRECTORY> <FILE>
"bin" "clamp-config"
"bin" "clang"
"bin" "clang++"
"bin" "extractkernel"
"bin" "hcc"
"bin" "hcc-config"
Expand Down
2 changes: 0 additions & 2 deletions packaging/debian/prerm.in
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ ROCM_PATH="/opt/rocm"
SOFTLINKS=(
# <DIRECTORY> <FILE>
"bin" "clamp-config"
"bin" "clang"
"bin" "clang++"
"bin" "extractkernel"
"bin" "hcc"
"bin" "hcc-config"
Expand Down

0 comments on commit 84095e1

Please sign in to comment.