Program Listing for File JitifyCache.h

Return to documentation for file (include/flamegpu/detail/JitifyCache.h)

#ifndef INCLUDE_FLAMEGPU_DETAIL_JITIFYCACHE_H_
#define INCLUDE_FLAMEGPU_DETAIL_JITIFYCACHE_H_

#include <map>
#include <mutex>
#include <memory>
#include <string>
#include <vector>

#ifdef _MSC_VER
#pragma warning(push, 2)
#include "jitify/jitify.hpp"
#pragma warning(pop)
#else
#include "jitify/jitify.hpp"
#endif

namespace flamegpu {
namespace detail {

class JitifyCache {
    struct CachedProgram {
        // dynamic header concatenated to kernel
        // We check this is an exact match before loading from cache
        std::string long_reference;
        std::string serialised_kernelinst;
    };

 public:
    std::unique_ptr<jitify::experimental::KernelInstantiation> loadKernel(
        const std::string &func_name,
        const std::vector<std::string> &template_args,
        const std::string &kernel_src,
        const std::string &dynamic_header);
    void useMemoryCache(bool yesno);
    bool useMemoryCache() const;
    void clearMemoryCache();
    void useDiskCache(bool yesno);
    bool useDiskCache() const;
    static void clearDiskCache();

 private:
    static std::unique_ptr<jitify::experimental::KernelInstantiation> compileKernel(
    const std::string &func_name,
    const std::vector<std::string> &template_args,
    const std::string &kernel_src,
    const std::string &dynamic_header);
    static void getKnownHeaders(std::vector<std::string> &headers);

    std::map<std::string, CachedProgram> cache{};
    mutable std::mutex cache_mutex;

    bool use_memory_cache;
    bool use_disk_cache;

    JitifyCache();
    static std::mutex instance_mutex;

 public:
    static JitifyCache& getInstance();
    // Public deleted creates better compiler errors
    JitifyCache(JitifyCache const&) = delete;
    void operator=(JitifyCache const&) = delete;
};

}  // namespace detail
}  // namespace flamegpu

#endif  // INCLUDE_FLAMEGPU_DETAIL_JITIFYCACHE_H_