From e29fda1b1c71862c856eaa08e2a54eab2ab05f2e Mon Sep 17 00:00:00 2001 From: Philippe Tillet Date: Thu, 30 Apr 2015 02:25:14 -0400 Subject: [PATCH] test --- python/src/{core.cpp => core.hpp} | 0 python/src/driver.hpp | 165 ++++++++++++++++++++++++++++ python/src/{model.cpp => model.hpp} | 0 3 files changed, 165 insertions(+) rename python/src/{core.cpp => core.hpp} (100%) create mode 100644 python/src/driver.hpp rename python/src/{model.cpp => model.hpp} (100%) diff --git a/python/src/core.cpp b/python/src/core.hpp similarity index 100% rename from python/src/core.cpp rename to python/src/core.hpp diff --git a/python/src/driver.hpp b/python/src/driver.hpp new file mode 100644 index 000000000..e6b03bbc4 --- /dev/null +++ b/python/src/driver.hpp @@ -0,0 +1,165 @@ +#include +#include +#include + +#include "isaac/array.h" +#include "isaac/model/model.h" + +#include "common.hpp" +#include "driver.h" + + +bp::list nv_compute_capability(isc::driver::Device const & device) +{ + bp::list res; + std::pair cc = device.nv_compute_capability(); + res.append(cc.first); + res.append(cc.second); + return res; +} + +bp::list get_platforms() +{ + std::vector platforms(isc::driver::Platform::get()); + return detail::to_list(platforms.begin(), platforms.end()); +} + +bp::list get_devices(isc::driver::Platform const & platform) +{ + std::vector devices(platform.devices()); + return detail::to_list(devices.begin(), devices.end()); +} + +struct model_map_indexing +{ + static isc::model& get_item(isc::model_map_t& container, bp::tuple i_) + { + isc::expression_type expression = detail::extract_template_type(i_[0]); + isc::numeric_type dtype = detail::extract_dtype(i_[1]); + isc::model_map_t::iterator i = container.find(std::make_pair(expression, dtype)); + if (i == container.end()) + { + PyErr_SetString(PyExc_KeyError, "Invalid key"); + bp::throw_error_already_set(); + } + return *i->second; + } + + static void set_item(isc::model_map_t& container, bp::tuple i_, isc::model const & v) + { + isc::expression_type expression = detail::extract_template_type(i_[0]); + isc::numeric_type dtype = detail::extract_dtype(i_[1]); + container[std::make_pair(expression, dtype)].reset(new isc::model(v)); + } +}; + +std::string to_string(isc::driver::device_type type) +{ + if(type==isc::driver::DEVICE_TYPE_CPU) return "CPU"; + if(type==isc::driver::DEVICE_TYPE_GPU) return "GPU"; + if(type==isc::driver::DEVICE_TYPE_ACCELERATOR) return "ACCELERATOR"; + throw; +} + +std::shared_ptr make_context(isc::driver::Device const & dev) +{ return std::shared_ptr(new isc::driver::Context(dev)); } + +bp::tuple flush(isc::array_expression const & expression, unsigned int queue_id, bp::list dependencies, bool tune, int label, std::string const & program_name, bool force_recompile) +{ + std::list events; + std::vector cdependencies = detail::to_vector(dependencies); + std::shared_ptr parray(new isc::array(isc::control(expression, + isc::execution_options_type(queue_id, &events, &cdependencies), + isc::dispatcher_options_type(tune, label), + isc::compilation_options_type(program_name, force_recompile)))); + return bp::make_tuple(parray, detail::to_list(events.begin(), events.end())); +} + + +struct state_type{ }; +state_type state; + +void export_driver() +{ + typedef std::vector queues_t; + bp::class_("queues") + .def("__len__", &queues_t::size) + .def("__getitem__", &bp::vector_indexing_suite::get_item, bp::return_internal_reference<>()) + .def("__setitem__", &bp::vector_indexing_suite::set_item, bp::with_custodian_and_ward<1,2>()) + .def("append", &bp::vector_indexing_suite::append) + + ; + + bp::class_("models") + .def("__getitem__", &model_map_indexing::get_item, bp::return_internal_reference<>()) + .def("__setitem__", &model_map_indexing::set_item, bp::with_custodian_and_ward<1,2>()) + ; + + bp::enum_ + ("backend_type") + .value("OPENCL", isc::driver::OPENCL) + #ifdef ISAAC_WITH_CUDA + .value("CUDA", isc::driver::CUDA) + #endif + ; + + bp::enum_ + ("device_type") + .value("DEVICE_TYPE_GPU", isc::driver::DEVICE_TYPE_GPU) + .value("DEVICE_TYPE_CPU", isc::driver::DEVICE_TYPE_CPU) + ; + + + bp::class_("platform", bp::no_init) + .def("get_devices", &get_devices) + .add_property("name",&isc::driver::Platform::name) + ; + + bp::enum_ + ("vendor") + .value("AMD", isc::driver::Device::AMD) + .value("INTEL", isc::driver::Device::INTEL) + .value("NVIDIA", isc::driver::Device::NVIDIA) + .value("UNKNOWN", isc::driver::Device::UNKNOWN) + ; + + bp::class_("device", bp::no_init) + .add_property("clock_rate", &isc::driver::Device::clock_rate) + .add_property("name", &isc::driver::Device::name) + .add_property("type", &isc::driver::Device::type) + .add_property("platform", &isc::driver::Device::platform) + .add_property("vendor", &isc::driver::Device::vendor) + .add_property("nv_compute_capability", &nv_compute_capability) + ; + + bp::class_("context", bp::no_init) + .def("__init__", bp::make_constructor(&make_context)) + .add_property("queues", bp::make_function(static_cast & (*)(const isc::driver::Context&)>( [](const isc::driver::Context & ctx) -> std::vector & { return isc::driver::queues[ctx]; }) , bp::return_internal_reference<>())) + .add_property("backend", &isc::driver::Context::backend) + ; + + bp::class_("command_queue", bp::init()) + .def("synchronize", &isc::driver::CommandQueue::synchronize) + .add_property("models", bp::make_function(&isc::get_model_map, bp::return_internal_reference<>())) + .add_property("device", bp::make_function(&isc::driver::CommandQueue::device, bp::return_internal_reference<>())) + ; + + bp::class_("event", bp::init()) + .add_property("elapsed_time", &isc::driver::Event::elapsed_time) + ; + + bp::def("device_type_to_string", &to_string); + + bp::def("get_platforms", &get_platforms); + + bp::def("flush", &flush, (bp::arg("expression"), bp::arg("queue_id") = 0, bp::arg("dependencies")=bp::list(), bp::arg("tune") = false, bp::arg("label")=-1, bp::arg("program_name")="", bp::arg("recompile") = false)); + + bp::class_("state_type") + .def_readwrite("queue_properties",&isc::driver::queues.queue_properties) + ; + + bp::scope().attr("state") = bp::object(bp::ptr(&state)); + + bp::scope().attr("CL_QUEUE_PROFILING_ENABLE") = CL_QUEUE_PROFILING_ENABLE; + bp::scope().attr("CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE") = CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE; +} diff --git a/python/src/model.cpp b/python/src/model.hpp similarity index 100% rename from python/src/model.cpp rename to python/src/model.hpp