### ABI 1.5 - VDE_PLUGIN_ABI_MINOR 5;VdePluginKind OPTIMIZER=9/IO_BATCH=10; VdeOptimizer*/VdeIoBatch* 结构(JSON 输入输出回调);VdePluginRegistration 尾追加两指针 56→72B(offset 56/64), 旧 1.0–1.4 按 struct_size 安全加载、新 kind 结构不足拒绝; - PluginManager:OptimizerInfo/IoBatchInfo、optimizers()/invoke_optimizer、io_batches()/invoke_io_batch、 校验/失败隔离/卸载清理(IO_BATCH 支持 PARTIAL); - sample_optimizer/sample_io_batch(mock);ABI 矩阵 1.0–1.5 accepted=6+2.0 rejected;--abi/--check 适配; - test_plugin_abi15 6/6 + fixtures;既有 ABI 1.4 断言适配; - 文档 plugin-development §16/§17、plugin-distribution ABI 矩阵、plugin-ecosystem-handoff/ecosystem、plugin-ci-template; ### WP/claim - WP-P4-11 交付并移入 Delivered;claim Released 归档;统计 42=31 Delivered+11 Candidate; - claim check 修正:Released claim 可指向 Delivered WP(仅 Active 需要释放); ### 工程 - 版本 1.0.83;全量构建 0 error(421 targets);全量 ctest 7576 注册、7481/7481 PASS(0 failed,25 skipped,302.66s); - 收录 1.0.82 独立验证报告 80。
154 lines
5.7 KiB
C++
154 lines
5.7 KiB
C++
// 示例 Optimizer 插件(VDE 微内核 P4 / ABI 1.5;纯 C ABI,不链接内核)。
|
||
//
|
||
// mock 梯度优化器:不解析真实模型,返回确定性的 objective/iterations JSON,
|
||
// 用于验证 ABI 1.5 VdeOptimizerPlugin 的注册/回调/JSON 输入输出/错误隔离。
|
||
#include <cstdint>
|
||
#include <cstdio>
|
||
#include <cstdlib>
|
||
#include <cstring>
|
||
#include <string>
|
||
|
||
#include "vde/plugin/vde_plugin_api.h"
|
||
|
||
namespace {
|
||
|
||
int fail(const VdePluginHost* host, int code, const char* message) {
|
||
if (host && host->set_error) host->set_error(message);
|
||
return code;
|
||
}
|
||
|
||
bool parse_double(const char* json, const char* key, double* out) {
|
||
if (!json || !out) return false;
|
||
const std::string pattern = "\"" + std::string(key) + "\"";
|
||
const char* pos = std::strstr(json, pattern.c_str());
|
||
if (!pos) return false;
|
||
const char* colon = std::strchr(pos + pattern.size(), ':');
|
||
if (!colon) return false;
|
||
char* end = nullptr;
|
||
const double value = std::strtod(colon + 1, &end);
|
||
if (end == colon + 1) return false;
|
||
*out = value;
|
||
return true;
|
||
}
|
||
|
||
bool parse_u32(const char* json, const char* key, std::uint32_t* out) {
|
||
double value = 0.0;
|
||
if (!parse_double(json, key, &value) || value < 0.0 || value > 4294967295.0) return false;
|
||
*out = static_cast<std::uint32_t>(value);
|
||
return true;
|
||
}
|
||
|
||
int optimize(const VdePluginHost* host, const char* options_json, const char* input_json,
|
||
size_t input_size, VdeBuffer* output_json, char* error_message,
|
||
size_t error_message_size) {
|
||
const auto set_error = [&](const char* text) {
|
||
if (host && host->set_error) host->set_error(text);
|
||
if (error_message && error_message_size > 0) {
|
||
std::snprintf(error_message, error_message_size, "%s", text);
|
||
}
|
||
};
|
||
if (!host || !host->alloc || !host->free_buffer) {
|
||
set_error("optimizer: host services incomplete");
|
||
return VDE_PLUGIN_ERR_INVALID_ARG;
|
||
}
|
||
if (!input_json || input_size == 0) {
|
||
set_error("optimizer: input JSON is empty");
|
||
return VDE_PLUGIN_ERR_INVALID_ARG;
|
||
}
|
||
if (!output_json) {
|
||
set_error("optimizer: output buffer is null");
|
||
return VDE_PLUGIN_ERR_INVALID_ARG;
|
||
}
|
||
std::uint32_t max_iterations = 64;
|
||
double tolerance = 1e-6;
|
||
if (options_json && *options_json) {
|
||
std::uint32_t parsed_iterations = 0;
|
||
if (parse_u32(options_json, "max_iterations", &parsed_iterations) && parsed_iterations > 0)
|
||
max_iterations = parsed_iterations;
|
||
double parsed_tolerance = 0.0;
|
||
if (parse_double(options_json, "tolerance", &parsed_tolerance) && parsed_tolerance > 0.0)
|
||
tolerance = parsed_tolerance;
|
||
std::uint32_t flags = 0;
|
||
if (parse_u32(options_json, "flags", &flags) && flags != 0) {
|
||
set_error("optimizer: options.flags must be 0");
|
||
return VDE_PLUGIN_ERR_INVALID_ARG;
|
||
}
|
||
}
|
||
const std::uint32_t iterations =
|
||
max_iterations < 3 ? max_iterations : 3;
|
||
const double objective =
|
||
static_cast<double>(input_size) / 100.0 + tolerance * 10.0;
|
||
std::string json;
|
||
char buffer[512];
|
||
std::snprintf(buffer, sizeof(buffer),
|
||
"{\"optimizer_id\":\"optimizer.mock.gradient\",\"ok\":true,"
|
||
"\"objective_value\":%.12g,\"iterations\":%u,\"tolerance\":%.12g,"
|
||
"\"input_bytes\":%zu,\"diagnostics\":[\"mock optimizer; not for production\"]}",
|
||
objective, iterations, tolerance, input_size);
|
||
json += buffer;
|
||
void* out = host->alloc(json.size() + 1);
|
||
if (!out) {
|
||
set_error("optimizer: cannot allocate result buffer");
|
||
return VDE_PLUGIN_ERR_BUFFER;
|
||
}
|
||
std::memcpy(out, json.c_str(), json.size() + 1);
|
||
output_json->data = out;
|
||
output_json->size = json.size();
|
||
return VDE_PLUGIN_OK;
|
||
}
|
||
|
||
const char* describe() {
|
||
return "VDE sample optimizer (mock gradient; ABI 1.5)";
|
||
}
|
||
|
||
const VdeOptimizerPlugin kOptimizer = [] {
|
||
VdeOptimizerPlugin plugin{};
|
||
plugin.struct_size = sizeof(VdeOptimizerPlugin);
|
||
plugin.optimizer_id = "optimizer.mock.gradient";
|
||
plugin.capabilities_json = "{\"methods\":[\"mock_gradient\"],\"mock\":true}";
|
||
plugin.optimize = &optimize;
|
||
plugin.describe = &describe;
|
||
return plugin;
|
||
}();
|
||
|
||
} // namespace
|
||
|
||
extern "C" {
|
||
|
||
VDE_PLUGIN_EXPORT int vde_plugin_query(std::uint32_t host_abi_major, VdePluginInfo* info) {
|
||
if (!info || info->struct_size < sizeof(VdePluginInfo)) return VDE_PLUGIN_ERR_INVALID_ARG;
|
||
if (host_abi_major != VDE_PLUGIN_ABI_MAJOR) return VDE_PLUGIN_ERR_ABI;
|
||
info->id = "com.vde.sample.optimizer";
|
||
info->name = "VDE Sample Optimizer (ABI 1.5)";
|
||
info->version = "0.1.0";
|
||
info->kind = VDE_PLUGIN_KIND_OPTIMIZER;
|
||
info->abi_major = VDE_PLUGIN_ABI_MAJOR;
|
||
info->abi_minor = VDE_PLUGIN_ABI_MINOR;
|
||
info->capabilities = "optimizer:mock";
|
||
info->license = "Apache-2.0";
|
||
return VDE_PLUGIN_OK;
|
||
}
|
||
|
||
VDE_PLUGIN_EXPORT int vde_plugin_init(const VdePluginHost* host, VdePluginRegistration* reg) {
|
||
if (!host || !reg) return VDE_PLUGIN_ERR_INVALID_ARG;
|
||
const std::size_t optimizer_field_end =
|
||
offsetof(VdePluginRegistration, optimizer) + sizeof(reg->optimizer);
|
||
if (reg->struct_size < optimizer_field_end) return VDE_PLUGIN_ERR_ABI;
|
||
if (!host->alloc || !host->free_buffer || !host->set_error)
|
||
return fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "optimizer: host services incomplete");
|
||
reg->kind = VDE_PLUGIN_KIND_OPTIMIZER;
|
||
reg->format = nullptr;
|
||
reg->generic = nullptr;
|
||
reg->cae_solver = nullptr;
|
||
reg->cam_strategy = nullptr;
|
||
reg->gcs_solver = nullptr;
|
||
reg->mesher = nullptr;
|
||
reg->optimizer = &kOptimizer;
|
||
reg->io_batch = nullptr;
|
||
return VDE_PLUGIN_OK;
|
||
}
|
||
|
||
VDE_PLUGIN_EXPORT void vde_plugin_shutdown() {}
|
||
|
||
} // extern "C"
|