本文主要是介绍c++runtime,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
namespace dl {
struct Params {
std::unordered_map<std::string, std::string> s;
std::unordered_map<std::string, int> i;
std::unordered_map<std::string, float> f;
std::unordered_map<std::string, std::vector<int>> vi;
std::unordered_map<std::string, std::vector<float>> vf;
};
struct Tensor {
std::string dtype;
std::vector<int> shape;
void *data;
int numel;
int device;
};
std::unordered_map<std::string, void *> _func_d;
std::unordered_map<std::string, Tensor> _tensor_d;
#define REGISTER(FUNC, NAME) _func_d[NAME] = (void *)FUNC
#define EXEC(NAME, PARAMS) \
(*((void (*)(Params &))_func_d[NAME]))(PARAMS)
#define TYPE(DATATYPE) typeid(DATATYPE).name()
#define GLOBAL_TENSOR _tensor_d
#define HANDLE_DTYPE(T, TYPENAME, FUNC, ...) \
if (TYPE(T) == TYPENAME) { \
using scalar_t = T; \
FUNC<scalar_t>(__VA_ARGS__); \
}
#define HANDLE_DTYPE2(T1, T2, ...) \
HANDLE_DTYPE(T1, __VA_ARGS__) \
HANDLE_DTYPE(T2, __VA_ARGS__)
#define HANDLE_DTYPE3(T1, T2, T3, ...) \
HANDLE_DTYPE(T1, __VA_ARGS__) \
HANDLE_DTYPE(T2, __VA_ARGS__) \
HANDLE_DTYPE(T3, __VA_ARGS__)
template <typename scalar_t>
void empty_cpu(std::string name, std::vector<int> shape) {
Tensor t;
t.dtype = TYPE(scalar_t);
t.shape = shape;
t.numel = 1;
for (auto d : shape) t.numel *= d;
t.data = (void *)(new char[t.numel * sizeof(scalar_t)]);
GLOBAL_TENSOR[name] = t;
}
void empty(Params &p) {
HANDLE_DTYPE2(int, float, p.s["dtype"], empty_cpu, p.s["name"], p.vi["shape"])
}
template <typename scalar_t>
void linspace_cpu_(scalar_t *data, int numel) {
for (int i = 0; i < numel; i++) data[i] = (scalar_t)i;
}
void linspace_(Params &p) {
auto self = GLOBAL_TENSOR[p.s["name"]];
auto numel = self.numel;
HANDLE_DTYPE2(int, float, self.dtype, linspace_cpu_, (scalar_t *)self.data, numel)
}
} // namespace dl
int main() {
using namespace std;
using namespace dl;
REGISTER(empty, "empty");
REGISTER(linspace_, "linspace_");
Params p;
p.s["name"] = "Tensor0";
p.s["dtype"] = TYPE(float);
p.vi["shape"] = std::vector<int>{2, 2};
EXEC("empty", p);
Params p2;
p2.s["name"] = "Tensor0";
EXEC("linspace_", p2);
Tensor &t = GLOBAL_TENSOR["Tensor0"];
float *ptr = (float *)t.data;
for (int i = 0; i < t.numel; i++) {
cout << ptr[i] << endl;
}
}
这篇关于c++runtime的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!