Compare commits

...
Author SHA1 Message Date
Julian Andrej 18e636fa54 trying some things 2025-12-03 12:56:24 -08:00
7 changed files with 30996 additions and 0 deletions
+1
View File
@@ -89,6 +89,7 @@ if (MFEM_USE_MPI)
ex37p.cpp
ex39p.cpp
ex40p.cpp
ex999p.cpp
)
endif()
+159
View File
@@ -0,0 +1,159 @@
#include <mfem.hpp>
#include "nlohmann/json.hpp"
#include "minja.hpp"
#include "myqfunction.hpp"
using namespace mfem;
using namespace mfem::future;
template<class T>
struct remove_cvref
{
using type = std::remove_cv_t<std::remove_reference_t<T>>;
};
template <typename qf_t>
auto process(qf_t qf)
{
using qfsig = typename create_function_signature<qf_t>::type;
using qfpar_t = typename qfsig::parameter_ts;
using qfout_t = typename qfsig::return_t;
auto qfparams = decay_tuple<qfpar_t> {};
auto in_str = apply([](auto&&... arg)
{
return std::vector<std::string>
{
std::string(get_type_name<typename remove_cvref<decltype(arg)>::type>())...
};
}, qfparams);
std::vector<std::string> out_str
{
std::string(get_type_name<typename remove_cvref<qfout_t>::type>())
};
return std::tuple{in_str, out_str};
}
int main()
{
// load the kernel template
std::ifstream
kernel_istream("/Users/andrej1/repos/mfem/examples/kernel_skeleton.jinja");
if (!kernel_istream.is_open())
{
std::cerr << "error opening jinja template file" << std::endl;
return 1;
}
std::stringstream buffer;
buffer << kernel_istream.rdbuf();
std::string fileContent = buffer.str();
auto kernel_tmpl = minja::Parser::parse(buffer.str(), /* options= */ {});
auto [in_str, out_str] = process(myqfunction0);
for (auto &v : in_str)
{
std::cout << v << " ";
}
std::cout << std::endl;
const size_t DUMMY_STRIDE = 64*32*32;
const size_t basis_p_1d = 2;
json context_json{};
context_json["kernel_name"] = "demo";
context_json["spaces"].push_back(
{
{"P_1D", basis_p_1d},
{"dim", 3},
{"needs_value", true},
{"needs_grad", true},
});
context_json["spaces"].push_back(
{
{"P_1D", basis_p_1d},
});
context_json["inputs"].push_back(
{
{"name", "potential"},
{"space_idx", 0},
{"num_comp", 1},
{"comp_stride", DUMMY_STRIDE},
{"eval_grad", true},
});
context_json["inputs"].push_back(
{
{"name", "weights"},
{"space_idx", 0},
{"num_comp", 1},
{"comp_stride", DUMMY_STRIDE},
{"is_qdata", true},
});
context_json["outputs"].push_back(
{
{"name", "solution"},
{"space_idx", 0},
{"num_comp", 1},
{"comp_stride", DUMMY_STRIDE},
{"eval_grad", true},
});
const size_t nqf = 1;
const std::vector<std::string> qfunc_names = {"myqfunction0"};
const std::vector<std::vector<size_t>> qfunc_inputs = {{0, 1, 2}};
for (size_t i = 0; i < nqf; i++)
{
json inarr = json::array();
for (size_t j = 0; j < qfunc_inputs[i].size(); j++)
{
inarr.push_back(
{
{"index", j},
{"datatype", in_str[j]}
});
}
context_json["qfuncs"].push_back(
{
{"name", qfunc_names[i]},
{"inputs", inarr}
});
}
std::cout << context_json.dump(2) << std::endl;
auto context = minja::Context::make(context_json);
auto kernel_source = kernel_tmpl->render(context);
std::cout << ">>> generated kernel source\n"
<< kernel_source
<< "\n<<< generated kernel source\n"
<< std::endl;
{
// test casting
std::vector<real_t> d(4);
int i = 0;
for (auto &v : d)
{
v = ++i;
}
mfem::future::tensor<real_t, 2, 2> *dudxi =
reinterpret_cast<mfem::future::tensor<real_t, 2, 2> *>(d.data());
std::cout << *dudxi << std::endl;
}
return 0;
}
+21
View File
@@ -0,0 +1,21 @@
#include "util.hpp"
#define NUM_SPACES {{ spaces | count }}
#define NUM_INPUTS {{ inputs | count }}
#define NUM_OUTPUTS {{ outputs | count }}
extern "C" __global__ void dfem_jit_{{kernel_name}}(int num_entities, const real_t *fields[NUM_INPUTS], real_t *outputs[NUM_OUTPUTS], const real_t *B[NUM_SPACES]) {
// transform fields
const real_t *inputs = ...;
// call qfunctions
{% for qf in qfuncs -%}
{
{%- for qfinput in qf.inputs %}
{{ qfinput.datatype }}* in{{ loop.index0 }} =
reinterpret_cast<{{ qfinput.datatype }}>(inputs[{{ qfinput.index }}]);
{% endfor %}
{{ qf.name }}({% for qfinput in qf.inputs %}*in{{ loop.index0 }}{{ "," if not loop.last else "" }}{% endfor %});
}
{% endfor %}
}
+4137
View File
File diff suppressed because it is too large Load Diff
+17
View File
@@ -0,0 +1,17 @@
#include <mfem.hpp>
using namespace mfem;
using mfem::future::tensor;
constexpr int dim = 2;
tensor<real_t, dim, dim> myqfunction0(
const tensor<real_t, dim, dim> &dvdxi,
const tensor<real_t, dim, dim> &J,
const real_t &w)
{
const auto invJ = inv(J);
const auto dvdx = dvdxi * invJ;
const auto test_function_terms = inv(J);
return dot(dvdx, J) * det(J) * w * test_function_terms;
}
File diff suppressed because it is too large Load Diff
+183
View File
@@ -0,0 +1,183 @@
// __ _____ _____ _____
// __| | __| | | | JSON for Modern C++
// | | |__ | | | | | | version 3.12.0
// |_____|_____|_____|_|___| https://github.com/nlohmann/json
//
// SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
// SPDX-License-Identifier: MIT
#ifndef INCLUDE_NLOHMANN_JSON_FWD_HPP_
#define INCLUDE_NLOHMANN_JSON_FWD_HPP_
#include <cstdint> // int64_t, uint64_t
#include <map> // map
#include <memory> // allocator
#include <string> // string
#include <vector> // vector
// #include <nlohmann/detail/abi_macros.hpp>
// __ _____ _____ _____
// __| | __| | | | JSON for Modern C++
// | | |__ | | | | | | version 3.12.0
// |_____|_____|_____|_|___| https://github.com/nlohmann/json
//
// SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
// SPDX-License-Identifier: MIT
// This file contains all macro definitions affecting or depending on the ABI
#ifndef JSON_SKIP_LIBRARY_VERSION_CHECK
#if defined(NLOHMANN_JSON_VERSION_MAJOR) && \
defined(NLOHMANN_JSON_VERSION_MINOR) && \
defined(NLOHMANN_JSON_VERSION_PATCH)
#if NLOHMANN_JSON_VERSION_MAJOR != 3 || NLOHMANN_JSON_VERSION_MINOR != 12 || \
NLOHMANN_JSON_VERSION_PATCH != 0
#warning "Already included a different version of the library!"
#endif
#endif
#endif
#define NLOHMANN_JSON_VERSION_MAJOR 3 // NOLINT(modernize-macro-to-enum)
#define NLOHMANN_JSON_VERSION_MINOR 12 // NOLINT(modernize-macro-to-enum)
#define NLOHMANN_JSON_VERSION_PATCH 0 // NOLINT(modernize-macro-to-enum)
#ifndef JSON_DIAGNOSTICS
#define JSON_DIAGNOSTICS 0
#endif
#ifndef JSON_DIAGNOSTIC_POSITIONS
#define JSON_DIAGNOSTIC_POSITIONS 0
#endif
#ifndef JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
#define JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON 0
#endif
#if JSON_DIAGNOSTICS
#define NLOHMANN_JSON_ABI_TAG_DIAGNOSTICS _diag
#else
#define NLOHMANN_JSON_ABI_TAG_DIAGNOSTICS
#endif
#if JSON_DIAGNOSTIC_POSITIONS
#define NLOHMANN_JSON_ABI_TAG_DIAGNOSTIC_POSITIONS _dp
#else
#define NLOHMANN_JSON_ABI_TAG_DIAGNOSTIC_POSITIONS
#endif
#if JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
#define NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON _ldvcmp
#else
#define NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON
#endif
#ifndef NLOHMANN_JSON_NAMESPACE_NO_VERSION
#define NLOHMANN_JSON_NAMESPACE_NO_VERSION 0
#endif
// Construct the namespace ABI tags component
#define NLOHMANN_JSON_ABI_TAGS_CONCAT_EX(a, b, c) json_abi##a##b##c
#define NLOHMANN_JSON_ABI_TAGS_CONCAT(a, b, c) \
NLOHMANN_JSON_ABI_TAGS_CONCAT_EX(a, b, c)
#define NLOHMANN_JSON_ABI_TAGS \
NLOHMANN_JSON_ABI_TAGS_CONCAT( \
NLOHMANN_JSON_ABI_TAG_DIAGNOSTICS, \
NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON, \
NLOHMANN_JSON_ABI_TAG_DIAGNOSTIC_POSITIONS)
// Construct the namespace version component
#define NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT_EX(major, minor, patch) \
_v##major##_##minor##_##patch
#define NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT(major, minor, patch) \
NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT_EX(major, minor, patch)
#if NLOHMANN_JSON_NAMESPACE_NO_VERSION
#define NLOHMANN_JSON_NAMESPACE_VERSION
#else
#define NLOHMANN_JSON_NAMESPACE_VERSION \
NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT(NLOHMANN_JSON_VERSION_MAJOR, \
NLOHMANN_JSON_VERSION_MINOR, \
NLOHMANN_JSON_VERSION_PATCH)
#endif
// Combine namespace components
#define NLOHMANN_JSON_NAMESPACE_CONCAT_EX(a, b) a##b
#define NLOHMANN_JSON_NAMESPACE_CONCAT(a, b) \
NLOHMANN_JSON_NAMESPACE_CONCAT_EX(a, b)
#ifndef NLOHMANN_JSON_NAMESPACE
#define NLOHMANN_JSON_NAMESPACE \
nlohmann::NLOHMANN_JSON_NAMESPACE_CONCAT(NLOHMANN_JSON_ABI_TAGS, \
NLOHMANN_JSON_NAMESPACE_VERSION)
#endif
#ifndef NLOHMANN_JSON_NAMESPACE_BEGIN
#define NLOHMANN_JSON_NAMESPACE_BEGIN \
namespace nlohmann { \
inline namespace NLOHMANN_JSON_NAMESPACE_CONCAT( \
NLOHMANN_JSON_ABI_TAGS, NLOHMANN_JSON_NAMESPACE_VERSION) {
#endif
#ifndef NLOHMANN_JSON_NAMESPACE_END
#define NLOHMANN_JSON_NAMESPACE_END \
} /* namespace (inline namespace) NOLINT(readability/namespace) */ \
} // namespace nlohmann
#endif
/*!
@brief namespace for Niels Lohmann
@see https://github.com/nlohmann
@since version 1.0.0
*/
NLOHMANN_JSON_NAMESPACE_BEGIN
/*!
@brief default JSONSerializer template argument
This serializer ignores the template arguments and uses ADL
([argument-dependent lookup](https://en.cppreference.com/w/cpp/language/adl))
for serialization.
*/
template <typename T = void, typename SFINAE = void> struct adl_serializer;
/// a class to store JSON values
/// @sa https://json.nlohmann.me/api/basic_json/
template <template <typename U, typename V, typename... Args> class ObjectType =
std::map,
template <typename U, typename... Args> class ArrayType = std::vector,
class StringType = std::string, class BooleanType = bool,
class NumberIntegerType = std::int64_t,
class NumberUnsignedType = std::uint64_t,
class NumberFloatType = double,
template <typename U> class AllocatorType = std::allocator,
template <typename T, typename SFINAE = void> class JSONSerializer =
adl_serializer,
class BinaryType =
std::vector<std::uint8_t>, // cppcheck-suppress syntaxError
class CustomBaseClass = void>
class basic_json;
/// @brief JSON Pointer defines a string syntax for identifying a specific value
/// within a JSON document
/// @sa https://json.nlohmann.me/api/json_pointer/
template <typename RefStringType> class json_pointer;
/*!
@brief default specialization
@sa https://json.nlohmann.me/api/json/
*/
using json = basic_json<>;
/// @brief a minimal map-like container that preserves insertion order
/// @sa https://json.nlohmann.me/api/ordered_map/
template <class Key, class T, class IgnoredLess, class Allocator>
struct ordered_map;
/// @brief specialization that maintains the insertion order of object keys
/// @sa https://json.nlohmann.me/api/ordered_json/
using ordered_json = basic_json<nlohmann::ordered_map>;
NLOHMANN_JSON_NAMESPACE_END
#endif // INCLUDE_NLOHMANN_JSON_FWD_HPP_