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

Method scn_do_execute

src/serialization/impl/extern_c_opr.cpp:383–430  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

381}
382
383void ExternCOprRunner::scn_do_execute() {
384 SmallVector<MGBTensor> c_inp(input().size()), c_out(output().size());
385 SmallVector<HostTensorND> cpu_inp, cpu_out;
386 check_param();
387
388 bool need_copy = false;
389 if (comp_node().device_type() == CompNode::DeviceType::CPU) {
390 for (size_t i = 0; i < input().size(); ++i) {
391 c_inp[i] = tensor_to_c(input(i)->dev_tensor());
392 }
393 for (size_t i = 0; i < output().size(); ++i) {
394 c_out[i] = tensor_to_c(output(i)->dev_tensor());
395 }
396 } else {
397 need_copy = true;
398 mgb_log_debug(
399 "copy is needed to execute extern C "
400 "opr `%s' on comp node `%s'",
401 cname(), comp_node().to_string().c_str());
402 cpu_inp.resize(input().size());
403 cpu_out.resize(output().size());
404 for (size_t i = 0; i < input().size(); ++i) {
405 cpu_inp[i].copy_from(input(i)->dev_tensor());
406 c_inp[i] = tensor_to_c(cpu_inp[i]);
407 }
408 for (size_t i = 0; i < output().size(); ++i) {
409 cpu_out[i]
410 .comp_node(comp_node())
411 .dtype(output(i)->dtype())
412 .resize(output(i)->shape());
413 c_out[i] = tensor_to_c(cpu_out[i]);
414 }
415 }
416
417 if (need_copy) {
418 comp_node().sync();
419 m_desc->execute(m_desc.get(), c_inp.data(), c_out.data());
420
421 for (size_t i = 0; i < output().size(); ++i)
422 output(i)->dev_tensor().copy_from_fixlayout(cpu_out[i]).sync();
423 } else {
424 CompNodeEnv::from_comp_node(comp_node())
425 .cpu_env()
426 .dispatch([this, c_inp, c_out]() mutable {
427 m_desc->execute(m_desc.get(), c_inp.data(), c_out.data());
428 });
429 }
430}
431
432void ExternCOprRunner::add_input_layout_constraint() {
433 for (auto i : input())

Callers

nothing calls this directly

Calls 15

check_paramFunction · 0.85
comp_nodeFunction · 0.85
tensor_to_cFunction · 0.85
resizeMethod · 0.80
copy_from_fixlayoutMethod · 0.80
sizeMethod · 0.45
device_typeMethod · 0.45
dev_tensorMethod · 0.45
to_stringMethod · 0.45
copy_fromMethod · 0.45
dtypeMethod · 0.45
comp_nodeMethod · 0.45

Tested by

no test coverage detected