From 9e86ec1b5f63c51b3e38bc56c5b3953003f635ec Mon Sep 17 00:00:00 2001 From: cianciosa Date: Tue, 14 Jan 2025 16:40:17 -0500 Subject: [PATCH] Make the spline indicies a build option. Enableing this negatively affects cuda kernels. --- CMakeLists.txt | 1 + graph_framework/CMakeLists.txt | 1 + graph_framework/piecewise.hpp | 54 ++++++++++++++++++++++++++++++++-- 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6e6f3d2..ec76921 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,6 +10,7 @@ option (SAVE_KERNEL_SOURCE "Writes the kernel source code to a file." OFF) option (USE_INPUT_CACHE "Cache the values kernel input values." OFF) option (USE_CONSTANT_CACHE "Cache the value of constantants in kernel registers." OFF) option (SHOW_USE_COUNT "Add a comment showing the use count in kernel sources." OFF) +option (USE_INDEX_CACHE "Cache index values instead of computing them every time." OFF) #------------------------------------------------------------------------------- # Set the cmake module path. diff --git a/graph_framework/CMakeLists.txt b/graph_framework/CMakeLists.txt index 50ac557..270bde1 100644 --- a/graph_framework/CMakeLists.txt +++ b/graph_framework/CMakeLists.txt @@ -24,6 +24,7 @@ target_compile_definitions (rays $<$:SAVE_KERNEL_SOURCE> $<$:USE_CONSTANT_CACHE> $<$:SHOW_USE_COUNT> + $<$:USE_INDEX_CACHE> ) target_include_directories (rays diff --git a/graph_framework/piecewise.hpp b/graph_framework/piecewise.hpp index 7229509..c1faaa4 100644 --- a/graph_framework/piecewise.hpp +++ b/graph_framework/piecewise.hpp @@ -293,12 +293,15 @@ void compile_index(std::ostringstream &stream, jit::register_map &indices, const jit::register_usage &usage) { if (registers.find(this) == registers.end()) { +#ifdef USE_INDEX_CACHE if (indices.find(this->arg.get()) == indices.end()) { +#endif const size_t length = leaf_node::backend_cache[data_hash].size(); shared_leaf a = this->arg->compile(stream, registers, indices, usage); +#ifdef USE_INDEX_CACHE indices[a.get()] = jit::to_string('i', a.get()); stream << " const " << jit::smallest_int_type (length) << " " @@ -306,6 +309,7 @@ void compile_index(std::ostringstream &stream, compile_index (stream, registers[a.get()], length); a->endline(stream, usage); } +#endif registers[this] = jit::to_string('r', this); stream << " const "; @@ -326,22 +330,39 @@ void compile_index(std::ostringstream &stream, #endif stream << registers[leaf_node::backend_cache[data_hash].data()]; if constexpr (jit::use_metal ()) { +#ifdef USE_INDEX_CACHE stream << ".read(" - << indices[this->arg.get()]; + << indices[this->arg.get()] + << ").r"; +#else + stream << ".read("; + compile_index (stream, registers[a.get()], length); stream << ").r"; +#endif #ifdef USE_CUDA_TEXTURES } else if constexpr (jit::use_cuda()) { +#ifdef USE_INDEX_CACHE stream << ", " << indices[this->arg.get()]; +#else + stream << ", "; + compile_index (stream, registers[a.get()], length); +#endif if constexpr (jit::is_complex () || jit::is_double ()) { stream << ")"; } stream << ")"; #endif } else { +#ifdef USE_INDEX_CACHE stream << "[" << indices[this->arg.get()] << "]"; +#else + stream << "["; + compile_index (stream, registers[a.get()], length); + stream << "]"; +#endif } this->endline(stream, usage); } @@ -839,11 +860,14 @@ void compile_index(std::ostringstream &stream, const size_t length = leaf_node::backend_cache[data_hash].size(); const size_t num_rows = length/num_columns; +#ifdef USE_INDEX_CACHE if (indices.find(this->left.get()) == indices.end()) { +#endif shared_leaf x = this->left->compile(stream, registers, indices, usage); +#ifdef USE_INDEX_CACHE indices[x.get()] = jit::to_string('i', x.get()); stream << " const " << jit::smallest_int_type (num_rows) << " " @@ -852,10 +876,12 @@ void compile_index(std::ostringstream &stream, x->endline(stream, usage); } if (indices.find(this->right.get()) == indices.end()) { +#endif shared_leaf y = this->right->compile(stream, registers, indices, usage); +#ifdef USE_INDEX_CACHE indices[y.get()] = jit::to_string('i', y.get()); stream << " const " << jit::smallest_int_type (num_columns) << " " @@ -882,7 +908,8 @@ void compile_index(std::ostringstream &stream, << ";" << std::endl; } } - +#endif + registers[this] = jit::to_string('r', this); stream << " const "; jit::add_type (stream); @@ -902,6 +929,7 @@ void compile_index(std::ostringstream &stream, #endif stream << registers[leaf_node::backend_cache[data_hash].data()]; if constexpr (jit::use_metal ()) { +#ifdef USE_INDEX_CACHE stream << ".read(" << jit::smallest_int_type (std::max(num_rows, num_columns)) @@ -910,21 +938,43 @@ void compile_index(std::ostringstream &stream, << "," << indices[this->left.get()] << ")).r"; +#else + stream << ".read(uint2("; + compile_index (stream, registers[y.get()], num_columns); + stream << ","; + compile_index (stream, registers[x.get()], num_rows); + stream << ")).r"; +#endif #ifdef USE_CUDA_TEXTURES } else if constexpr (jit::use_cuda()) { +#ifdef USE_INDEX_CACHE stream << ", " << indices[this->right.get()] << ", " << indices[this->left.get()]; +#else + stream << ", "; + compile_index (stream, registers[y.get()], num_columns); + stream << ", "; + compile_index (stream, registers[x.get()], num_rows); +#endif if constexpr (jit::is_complex () || jit::is_double ()) { stream << ")"; } stream << ")"; #endif } else { +#ifdef USE_INDEX_CACHE stream << "[" << indices[temp.get()] << "]"; +#else + stream << "["; + compile_index (stream, registers[x.get()], num_rows); + stream << "*" << num_columns << " + "; + compile_index (stream, registers[y.get()], num_columns); + stream << "]"; +#endif } this->endline(stream, usage); } -- GitLab