diff --git a/mace/core/BUILD b/mace/core/BUILD index 63a30357240f99df964d1c23d2884739f61827db..5adf9010ec01e7d69292a92de492eaaf64f4654c 100644 --- a/mace/core/BUILD +++ b/mace/core/BUILD @@ -21,26 +21,46 @@ cc_library( ]), copts = ["-std=c++11"], deps = [ - "core", + ":logging", "@opencl_headers//:opencl20_headers", ], alwayslink = 1, ) + cc_library( - name = "core", - srcs = glob([ - "*.cc", - ]), - hdrs = glob([ - "*.h", - ]), + name = "logging", + srcs = [ + "logging.cc", + ], + hdrs = [ + "logging.h", + ], copts = ["-std=c++11"], linkopts = if_android([ "-llog", + ]), +) + +cc_library( + name = "core", + srcs = glob( + ["*.cc",], + exclude=[ + "logging.cc" + ]), + hdrs = glob( + ["*.h"], + exclude=[ + "logging.h" + ]), + copts = ["-std=c++11"], + linkopts = if_android([ "-pie", ]), deps = [ + ":logging", + ":opencl_runtime", "//mace/proto:cc_proto", "//mace/proto:stats_proto", "//mace/utils", diff --git a/mace/core/allocator.cc b/mace/core/allocator.cc index d05c45b352e37e2e7c67226aee28441a15c665b8..707ea4cb0e0a3dd267e229b6a7f52e39d42e9773 100644 --- a/mace/core/allocator.cc +++ b/mace/core/allocator.cc @@ -3,6 +3,7 @@ // #include "mace/core/allocator.h" +#include "mace/core/opencl_allocator.h" namespace mace { @@ -22,5 +23,6 @@ Allocator *GetDeviceAllocator(DeviceType type) { MACE_REGISTER_ALLOCATOR(DeviceType::CPU, new CPUAllocator()); MACE_REGISTER_ALLOCATOR(DeviceType::NEON, new CPUAllocator()); +MACE_REGISTER_ALLOCATOR(DeviceType::OPENCL, new OpenCLAllocator()); } // namespace mace diff --git a/mace/core/net.cc b/mace/core/net.cc index 22a2fd11ba014dcebe0cd03cd1e031dc251d1a49..2707ec5145b3c9dc3f94e0d2e17db6f7d2682d90 100644 --- a/mace/core/net.cc +++ b/mace/core/net.cc @@ -4,6 +4,7 @@ #include "mace/core/net.h" #include "mace/utils/utils.h" +#include "mace/core/runtime/opencl/opencl_runtime.h" namespace mace { @@ -15,7 +16,7 @@ NetBase::NetBase(const std::shared_ptr &net_def, SimpleNet::SimpleNet(const std::shared_ptr &net_def, Workspace *ws, DeviceType type) - : NetBase(net_def, ws, type) { + : NetBase(net_def, ws, type), device_type_(type){ VLOG(1) << "Constructing SimpleNet " << net_def->name(); for (int idx = 0; idx < net_def->op_size(); ++idx) { const auto &operator_def = net_def->op(idx); @@ -47,6 +48,8 @@ bool SimpleNet::Run(RunMetadata *run_metadata) { LOG(ERROR) << "Operator failed: " << ProtoDebugString(op->debug_def()); return false; } + if (device_type_ == DeviceType::OPENCL) + OpenCLRuntime::Get()->command_queue().finish(); if (op_stats) { op_stats->set_op_end_rel_micros(NowInMicroSec() - op_stats->all_start_micros()); diff --git a/mace/core/net.h b/mace/core/net.h index 541f1b8292eb67a60cd20a80b5526e2d993b4c63..013ca715cafce82242ad148d8ff12c7df8fd9fb4 100644 --- a/mace/core/net.h +++ b/mace/core/net.h @@ -40,6 +40,7 @@ class SimpleNet : public NetBase { protected: vector > operators_; + DeviceType device_type_; DISABLE_COPY_AND_ASSIGN(SimpleNet); }; diff --git a/mace/core/runtime/opencl/opencl_allocator.cc b/mace/core/opencl_allocator.cc similarity index 93% rename from mace/core/runtime/opencl/opencl_allocator.cc rename to mace/core/opencl_allocator.cc index d40432e2a2bb18b264d641d55f66868a26dafc22..5d96fe7dd7bb2e8c62dc2990e50a2dcf6b5e47ed 100644 --- a/mace/core/runtime/opencl/opencl_allocator.cc +++ b/mace/core/opencl_allocator.cc @@ -3,7 +3,7 @@ // #include "mace/core/runtime/opencl/cl2_header.h" -#include "mace/core/runtime/opencl/opencl_allocator.h" +#include "mace/core/opencl_allocator.h" #include "mace/core/runtime/opencl/opencl_runtime.h" namespace mace { @@ -49,6 +49,5 @@ void OpenCLAllocator::Unmap(void *buffer, void *mapped_ptr) { bool OpenCLAllocator::OnHost() { return false; } -MACE_REGISTER_ALLOCATOR(DeviceType::OPENCL, new OpenCLAllocator()); } // namespace mace diff --git a/mace/core/runtime/opencl/opencl_allocator.h b/mace/core/opencl_allocator.h similarity index 100% rename from mace/core/runtime/opencl/opencl_allocator.h rename to mace/core/opencl_allocator.h diff --git a/mace/kernels/BUILD b/mace/kernels/BUILD index e002b1418576be3ddbb6f7feaa3584dff18a17ae..7a3f3007e31915e1652b7e8e72b0e16d58e99bcd 100644 --- a/mace/kernels/BUILD +++ b/mace/kernels/BUILD @@ -20,7 +20,6 @@ cc_library( linkopts = if_android(["-lm"]), deps = [ "//mace/core", - "//mace/core:opencl_runtime", "//mace/utils", "//mace/utils:tuner", ], diff --git a/mace/ops/BUILD b/mace/ops/BUILD index e823136d965670cc198ed14c0f682cbf0b152d00..683d87abfbb5c013a758147298643eeadd0cf4b5 100644 --- a/mace/ops/BUILD +++ b/mace/ops/BUILD @@ -17,7 +17,6 @@ cc_library( ], deps = [ "//mace/core", - "//mace/core:opencl_runtime", "@gtest//:gtest", ], ) diff --git a/mace/tools/benchmark/benchmark_model.cc b/mace/tools/benchmark/benchmark_model.cc index 6ecfc1f4ed417e13d91c161a1bf9149be147684d..d4ae7b5d3bf754bca7d3d369b966ccea22ffdab4 100644 --- a/mace/tools/benchmark/benchmark_model.cc +++ b/mace/tools/benchmark/benchmark_model.cc @@ -42,6 +42,7 @@ bool SplitAndParseToInts(const string &str, tmp = tmp.substr(next_offset + 1); } } + return true; } } // namespace str_util @@ -254,6 +255,10 @@ int Main(int argc, char **argv) { stats_options.show_summary = show_summary; stats.reset(new StatSummarizer(stats_options)); + DeviceType device_type; + DeviceType_Parse(device, &device_type); + VLOG(0) << device_type; + // load model std::ifstream model_file_stream(model_file, std::ios::in | std::ios::binary); if (!model_file_stream.is_open()) { @@ -265,29 +270,30 @@ int Main(int argc, char **argv) { model_file_stream.close(); Workspace ws; - ws.LoadModelTensor(net_def, DeviceType::CPU); + ws.LoadModelTensor(net_def, device_type); // Load inputs for (size_t i = 0; i < inputs_count; ++i) { Tensor *input_tensor = - ws.CreateTensor(input_layers[i], GetDeviceAllocator(DeviceType::CPU), DT_FLOAT); + ws.CreateTensor(input_layers[i], GetDeviceAllocator(device_type), DT_FLOAT); vector shapes; str_util::SplitAndParseToInts(input_layer_shapes[i], ',', &shapes); input_tensor->Resize(shapes); - float *input_data = input_tensor->mutable_data(); - - // load input - if (i < input_layer_files.size()) { - std::ifstream in_file(input_layer_files[i], - std::ios::in | std::ios::binary); - in_file.read(reinterpret_cast(input_data), - input_tensor->size() * sizeof(float)); - in_file.close(); + { + Tensor::MappingGuard input_guard(input_tensor); + float *input_data = input_tensor->mutable_data(); + + // load input + if (i < input_layer_files.size()) { + std::ifstream in_file(input_layer_files[i], + std::ios::in | std::ios::binary); + in_file.read(reinterpret_cast(input_data), + input_tensor->size() * sizeof(float)); + in_file.close(); + } } } // create net - DeviceType device_type; - DeviceType_Parse(device, &device_type); auto net = CreateNet(net_def, &ws, device_type); int64_t warmup_time_us = 0;