MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / pinned_host_input

Function pinned_host_input

lite/example/cpp_example/mge/device_io.cpp:129–173  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

127}
128
129bool pinned_host_input(const Args& args) {
130 std::string network_path = args.model_path;
131 std::string input_path = args.input_path;
132
133 //! config the network running in CUDA device
134 lite::Config config{LiteDeviceType::LITE_CUDA};
135
136 //! create and load the network
137 std::shared_ptr<Network> network = std::make_shared<Network>(config);
138 network->load_model(network_path);
139
140 std::shared_ptr<Tensor> input_tensor = network->get_input_tensor(0);
141 Layout input_layout = input_tensor->get_layout();
142
143 //! read data from numpy data file
144 auto src_tensor = parse_npy(input_path);
145 //! malloc the pinned host memory
146 bool is_pinned_host = true;
147 auto tensor_pinned_input =
148 Tensor(LiteDeviceType::LITE_CUDA, input_layout, is_pinned_host);
149 //! copy to the pinned memory
150 tensor_pinned_input.copy_from(*src_tensor);
151 //! set the pinned host memory to the network as input
152 input_tensor->reset(tensor_pinned_input.get_memory_ptr(), input_layout);
153
154 //! forward
155 network->forward();
156 network->wait();
157
158 //! get the output data or read tensor set in network_in
159 std::shared_ptr<Tensor> output_tensor = network->get_output_tensor(0);
160 void* out_data = output_tensor->get_memory_ptr();
161 size_t out_length = output_tensor->get_tensor_total_size_in_byte() /
162 output_tensor->get_layout().get_elem_size();
163 float max = -1.0f;
164 float sum = 0.0f;
165 for (size_t i = 0; i < out_length; i++) {
166 float data = static_cast<float*>(out_data)[i];
167 sum += data;
168 if (max < data)
169 max = data;
170 }
171 printf("max=%e, sum=%e\n", max, sum);
172 return true;
173}
174} // namespace
175
176REGIST_EXAMPLE("device_input", device_input);

Callers

nothing calls this directly

Calls 12

get_elem_sizeMethod · 0.80
TensorClass · 0.50
load_modelMethod · 0.45
get_input_tensorMethod · 0.45
get_layoutMethod · 0.45
copy_fromMethod · 0.45
resetMethod · 0.45
get_memory_ptrMethod · 0.45
forwardMethod · 0.45
waitMethod · 0.45
get_output_tensorMethod · 0.45

Tested by

no test coverage detected