Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose JitifyCache via Python interface. #1151

Merged
merged 2 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions include/flamegpu/detail/JitifyCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
#include "jitify/jitify.hpp"
#endif

using jitify::experimental::KernelInstantiation;

namespace flamegpu {
namespace detail {

Expand Down Expand Up @@ -50,7 +48,7 @@ class JitifyCache {
* @param dynamic_header Dynamic header source generated by curve rtc
* @return A jitify RTC kernel instance of the provided kernel sources
*/
std::unique_ptr<KernelInstantiation> loadKernel(
std::unique_ptr<jitify::experimental::KernelInstantiation> loadKernel(
const std::string &func_name,
const std::vector<std::string> &template_args,
const std::string &kernel_src,
Expand Down Expand Up @@ -97,7 +95,7 @@ class JitifyCache {
* @param dynamic_header Dynamic header source generated by curve rtc
* @return A jitify RTC kernel instance of the provided kernel sources
*/
static std::unique_ptr<KernelInstantiation> compileKernel(
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,
Expand Down
10 changes: 5 additions & 5 deletions src/flamegpu/detail/JitifyCache.cu
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ bool confirmFLAMEGPUHeaderVersion(const std::string &flamegpuIncludeDir, const s
} // namespace

std::mutex JitifyCache::instance_mutex;
std::unique_ptr<KernelInstantiation> JitifyCache::compileKernel(const std::string &func_name, const std::vector<std::string> &template_args, const std::string &kernel_src, const std::string &dynamic_header) {
std::unique_ptr<jitify::experimental::KernelInstantiation> JitifyCache::compileKernel(const std::string &func_name, const std::vector<std::string> &template_args, const std::string &kernel_src, const std::string &dynamic_header) {
flamegpu::util::nvtx::Range range{"JitifyCache::compileKernel"};
// find and validate the cuda include directory via CUDA_PATH or CUDA_HOME.
static const std::string cuda_include_dir = getCUDAIncludeDir();
Expand Down Expand Up @@ -416,7 +416,7 @@ std::unique_ptr<KernelInstantiation> JitifyCache::compileKernel(const std::strin
auto program = jitify::experimental::Program(kernel_src, headers, options);
assert(template_args.size() == 1 || template_args.size() == 3); // Add this assertion incase template args change
auto kernel = program.kernel(template_args.size() > 1 ? "flamegpu::agent_function_wrapper" : "flamegpu::agent_function_condition_wrapper");
return std::make_unique<KernelInstantiation>(kernel, template_args);
return std::make_unique<jitify::experimental::KernelInstantiation>(kernel, template_args);
} catch (std::runtime_error const&) {
// jitify does not have a method for getting compile logs so rely on JITIFY_PRINT_LOG defined in cmake
THROW exception::InvalidAgentFunc("Error compiling runtime agent function (or function condition) ('%s'): function had compilation errors (see std::cout), "
Expand Down Expand Up @@ -497,7 +497,7 @@ void JitifyCache::getKnownHeaders(std::vector<std::string>& headers) {
headers.push_back("type_traits");
}

std::unique_ptr<KernelInstantiation> JitifyCache::loadKernel(const std::string &func_name, const std::vector<std::string> &template_args, const std::string &kernel_src, const std::string &dynamic_header) {
std::unique_ptr<jitify::experimental::KernelInstantiation> JitifyCache::loadKernel(const std::string &func_name, const std::vector<std::string> &template_args, const std::string &kernel_src, const std::string &dynamic_header) {
flamegpu::util::nvtx::Range range{"JitifyCache::loadKernel"};
std::lock_guard<std::mutex> lock(cache_mutex);
// Detect current compute capability=
Expand Down Expand Up @@ -534,7 +534,7 @@ std::unique_ptr<KernelInstantiation> JitifyCache::loadKernel(const std::string &
if (it != cache.end()) {
// Check long reference
if (it->second.long_reference == long_reference) {
return std::make_unique<KernelInstantiation>(KernelInstantiation::deserialize(it->second.serialised_kernelinst));
return std::make_unique<jitify::experimental::KernelInstantiation>(jitify::experimental::KernelInstantiation::deserialize(it->second.serialised_kernelinst));
}
}
}
Expand All @@ -551,7 +551,7 @@ std::unique_ptr<KernelInstantiation> JitifyCache::loadKernel(const std::string &
// Add it to cache for later loads
cache.emplace(short_reference, CachedProgram{long_reference, serialised_kernelinst});
// Deserialize and return program
return std::make_unique<KernelInstantiation>(KernelInstantiation::deserialize(serialised_kernelinst));
return std::make_unique<jitify::experimental::KernelInstantiation>(jitify::experimental::KernelInstantiation::deserialize(serialised_kernelinst));
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions swig/python/flamegpu.i
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,10 @@ class ModelVis;
// Because of how _ prefixes are handeled, this must be called via pyflamegpu._pyflamegpu.__TestSuiteTelemetry_sendResults()
%rename (__TestSuiteTelemetry) flamegpu::detail::TestSuiteTelemetry;
%include "flamegpu/detail/TestSuiteTelemetry.h"
// Expose jitifycache for override cache settings
%ignore flamegpu::detail::JitifyCache::loadKernel;
%rename(JitifyCache) flamegpu::detail::JitifyCache;
%include "flamegpu/detail/JitifyCache.h"
// Ignore detail agian?
%ignore flamegpu::detail;

Expand Down
34 changes: 34 additions & 0 deletions tests/python/detail/test_jitify_cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import pytest
from unittest import TestCase
from pyflamegpu import *

class JitifyCacheTest(TestCase):
"""
Test the now exposed flamegpu::detail::JitifyCache methods are exposed to python
This does not test the clear*cache methods, as that would have grim side-effects
"""
def test_memorycache(self):
"""
Test setting and checking the state of the jitify memory cache
"""
rtc_cache = pyflamegpu.JitifyCache.getInstance()
originally_enabled = rtc_cache.useMemoryCache()
rtc_cache.useMemoryCache(True)
assert rtc_cache.useMemoryCache() == True
rtc_cache.useMemoryCache(False)
assert rtc_cache.useMemoryCache() == False
rtc_cache.useMemoryCache(originally_enabled)
assert rtc_cache.useMemoryCache() == originally_enabled

def test_diskcache(self):
"""
Test setting and checking the state of the jitify disk cache
"""
rtc_cache = pyflamegpu.JitifyCache.getInstance()
originally_enabled = rtc_cache.useDiskCache()
rtc_cache.useDiskCache(True)
assert rtc_cache.useDiskCache() == True
rtc_cache.useDiskCache(False)
assert rtc_cache.useDiskCache() == False
rtc_cache.useDiskCache(originally_enabled)
assert rtc_cache.useDiskCache() == originally_enabled