MCPcopy Create free account
hub / github.com/Oneflow-Inc/oneflow / Compute

Method Compute

oneflow/user/kernels/eager_p_to_b_kernel.cpp:81–119  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

79
80 private:
81 void Compute(user_op::KernelComputeContext* ctx, user_op::OpKernelState*,
82 const user_op::OpKernelCache* cache) const override {
83 auto* kernel_cache = dynamic_cast<const EagerPToBOpKernelCache*>(cache);
84 CHECK(kernel_cache != nullptr);
85 const user_op::Tensor* in = ctx->Tensor4ArgNameAndIndex("in", 0);
86 user_op::Tensor* out = ctx->Tensor4ArgNameAndIndex("out", 0);
87 user_op::Tensor* tmp_buffer = ctx->Tensor4ArgNameAndIndex("tmp_buffer", 0);
88 const void* in_ptr = in->dptr();
89 void* tmp_buffer_ptr = tmp_buffer->mut_dptr();
90
91 const int64_t total_elem_cnt = ctx->Attr<Shape>("shape").elem_cnt();
92 const auto& p2p_pair = kernel_cache->p2p_pair();
93
94 DeviceType device_type = ctx->device_type();
95
96 std::unique_ptr<ep::primitive::Memset> memset_primitive =
97 ep::primitive::NewPrimitive<ep::primitive::MemsetFactory>(device_type);
98 CHECK(memset_primitive) << "Can not create Memset primitive for device type " << device_type;
99 memset_primitive->Launch(ctx->stream(), out->mut_dptr(), 0,
100 total_elem_cnt * GetSizeOfDataType(out->data_type()));
101
102 std::unique_ptr<ep::primitive::Add> add_primitive =
103 ep::primitive::NewPrimitive<ep::primitive::AddFactory>(ctx->device_type(), in->data_type());
104 CHECK(add_primitive);
105 for (const auto& pair : p2p_pair) {
106 int64_t src = pair.first;
107 int64_t dst = pair.second;
108
109 if (GlobalProcessCtx::Rank() == src) {
110 CHECK_JUST(Send(in_ptr, total_elem_cnt, in->data_type(), dst, device_type, ctx->stream()));
111 }
112 if (GlobalProcessCtx::Rank() == dst) {
113 CHECK_JUST(Recv(tmp_buffer_ptr, total_elem_cnt, out->data_type(), src, device_type,
114 ctx->stream()));
115 add_primitive->Launch(ctx->stream(), out->dptr(), tmp_buffer_ptr, out->mut_dptr(),
116 total_elem_cnt);
117 }
118 }
119 };
120 bool AlwaysComputeWhenAllOutputsEmpty() const override { return false; }
121};
122

Callers

nothing calls this directly

Calls 11

GetSizeOfDataTypeFunction · 0.85
SendFunction · 0.70
RecvFunction · 0.70
dptrMethod · 0.45
mut_dptrMethod · 0.45
elem_cntMethod · 0.45
device_typeMethod · 0.45
LaunchMethod · 0.45
streamMethod · 0.45
data_typeMethod · 0.45

Tested by

no test coverage detected