Skip to content

Commit

Permalink
Merge pull request #193 from clEsperanto/add-JIT-cache
Browse files Browse the repository at this point in the history
add JIT cache on disk
  • Loading branch information
StRigaud authored Oct 18, 2023
2 parents 7220d1e + 414c1ff commit 88b4dd1
Show file tree
Hide file tree
Showing 12 changed files with 386 additions and 172 deletions.
3 changes: 0 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,3 @@ if(BUILD_TESTING)
include(CTest)
add_subdirectory(${PROJECT_SOURCE_DIR}/tests)
endif(BUILD_TESTING)



14 changes: 2 additions & 12 deletions clic/include/backend.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef __INCLUDE_BACKEND_HPP
#define __INCLUDE_BACKEND_HPP

#include "cache.hpp"
#include "clic.hpp"
#include "device.hpp"
#include "utils.hpp"
Expand Down Expand Up @@ -128,10 +129,7 @@ class Backend
const std::string & kernel_source,
const std::string & kernel_name,
void * kernel) const -> void = 0;
virtual auto
loadProgramFromCache(const Device::Pointer & device, const std::string & hash, void * program) const -> void = 0;
virtual auto
saveProgramToCache(const Device::Pointer & device, const std::string & hash, void * program) const -> void = 0;

virtual auto
executeKernel(const Device::Pointer & device,
const std::string & kernel_source,
Expand Down Expand Up @@ -296,10 +294,6 @@ class CUDABackend : public Backend
const dType & dtype,
const float & value) -> void;

auto
loadProgramFromCache(const Device::Pointer & device, const std::string & hash, void * program) const -> void override;
auto
saveProgramToCache(const Device::Pointer & device, const std::string & hash, void * program) const -> void override;
auto
buildKernel(const Device::Pointer & device,
const std::string & kernel_source,
Expand Down Expand Up @@ -476,10 +470,6 @@ class OpenCLBackend : public Backend
const dType & dtype,
const float & value) -> void;

auto
loadProgramFromCache(const Device::Pointer & device, const std::string & hash, void * program) const -> void override;
auto
saveProgramToCache(const Device::Pointer & device, const std::string & hash, void * program) const -> void override;
auto
buildKernel(const Device::Pointer & device,
const std::string & kernel_source,
Expand Down
69 changes: 69 additions & 0 deletions clic/include/cache.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#ifndef __INCLUDE_CACHE_HPP
#define __INCLUDE_CACHE_HPP

#include <cstdlib>
#include <filesystem>
#include <iostream>
#include <string>

#include "device.hpp"

#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
# define NOMINMAX
# include <ShlObj.h>
# include <windows.h>
#endif

namespace cle
{

static const std::string CACHE_FOLDER = "clesperanto";
static const std::string CACHE_DIR_WIN = "AppData\\Local";
static const std::string CACHE_DIR_UNIX = ".cache";

static auto
get_path_with_cache_folder(const std::filesystem::path & base_path) -> std::filesystem::path
{
return base_path / std::filesystem::path(CACHE_FOLDER);
}

#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
static auto
get_cache_directory_path() -> std::filesystem::path
{
TCHAR path[MAX_PATH];
if (FAILED(SHGetFolderPath(NULL, CSIDL_LOCAL_APPDATA, NULL, 0, path)))
{
std::cerr << "Failed to get " << CACHE_DIR_WIN << " directory\n";
return get_path_with_cache_folder(std::filesystem::current_path());
}
return get_path_with_cache_folder(std::filesystem::path(path));
}
#else
static auto
get_cache_directory_path() -> std::filesystem::path
{
char * home_dir = std::getenv("HOME");
if (home_dir != nullptr)
{
return get_path_with_cache_folder(std::filesystem::path(home_dir) / std::filesystem::path(CACHE_DIR_UNIX));
}
std::cerr << "Failed to get user home directory\n";
return get_path_with_cache_folder(std::filesystem::current_path() / std::filesystem::path(CACHE_DIR_UNIX));
}
#endif

static auto
is_cache_enabled() -> bool
{
const char * env_var = std::getenv("CLESPERANTO_NO_CACHE");
bool cache_disabled = (env_var != nullptr);
return !cache_disabled;
}


static const auto CACHE_FOLDER_PATH = get_cache_directory_path();

} // namespace cle

#endif // __INCLUDE_CACHE_HPP
14 changes: 2 additions & 12 deletions clic/include/device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ class Device
}

friend auto
operator<<(std::ostream & out, const Device & device) -> std::ostream &
operator<<(std::ostream & out, const Device::Pointer & device) -> std::ostream &
{
out << "(" << device.getType() << ") " << device.getName();
out << "(" << device->getType() << ") " << device->getName();
return out;
}
};
Expand All @@ -69,8 +69,6 @@ class Device
class OpenCLDevice : public Device
{
public:
using CacheType = std::unordered_map<std::string, cl_program>;

OpenCLDevice(const cl_platform_id & platform, const cl_device_id & device);
~OpenCLDevice() override;

Expand Down Expand Up @@ -99,15 +97,12 @@ class OpenCLDevice : public Device
getName() const -> std::string override;
[[nodiscard]] auto
getInfo() const -> std::string override;
[[nodiscard]] auto
getCache() -> CacheType &;

private:
cl_device_id clDevice;
cl_platform_id clPlatform;
cl_context clContext;
cl_command_queue clCommandQueue;
CacheType cache;
bool initialized = false;
bool waitFinish = false;
};
Expand All @@ -117,8 +112,6 @@ class OpenCLDevice : public Device
class CUDADevice : public Device
{
public:
using CacheType = std::unordered_map<std::string, CUmodule>;

explicit CUDADevice(int deviceIndex);
~CUDADevice() override;

Expand Down Expand Up @@ -149,8 +142,6 @@ class CUDADevice : public Device
getInfo() const -> std::string override;
[[nodiscard]] auto
getArch() const -> std::string;
[[nodiscard]] auto
getCache() -> CacheType &;

private:
int cudaDeviceIndex;
Expand All @@ -159,7 +150,6 @@ class CUDADevice : public Device
CUstream cudaStream;
bool initialized = false;
bool waitFinish = false;
CacheType cache;
};
#endif // USE_CUDA

Expand Down
Loading

0 comments on commit 88b4dd1

Please sign in to comment.