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

Method Compute

oneflow/user/kernels/eager_p_to_s_kernel.cpp:150–195  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

148
149 private:
150 void Compute(user_op::KernelComputeContext* ctx, user_op::OpKernelState*,
151 const user_op::OpKernelCache* cache) const override {
152 auto* kernel_cache = dynamic_cast<const EagerPToSOpKernelCache*>(cache);
153 CHECK(kernel_cache != nullptr);
154 const user_op::Tensor* in = ctx->Tensor4ArgNameAndIndex("in", 0);
155 user_op::Tensor* out = ctx->Tensor4ArgNameAndIndex("out", 0);
156 user_op::Tensor* tmp_buffer = ctx->Tensor4ArgNameAndIndex("tmp_buffer", 0);
157 const void* in_ptr = in->dptr();
158 void* tmp_buffer_ptr = tmp_buffer->mut_dptr();
159
160 int64_t elem_cnt_of_this_chunk = kernel_cache->elem_cnt_of_this_chunk();
161 const auto& sorted_elem_cnt2_in_tensor_slice_copier =
162 kernel_cache->sorted_elem_cnt2_in_tensor_slice_copier();
163 const auto& sorted_p2p_pair = kernel_cache->sorted_p2p_pair();
164 CHECK_EQ(sorted_elem_cnt2_in_tensor_slice_copier.size(), sorted_p2p_pair.size());
165
166 DeviceType device_type = ctx->device_type();
167
168 std::unique_ptr<ep::primitive::Memset> memset_primitive =
169 ep::primitive::NewPrimitive<ep::primitive::MemsetFactory>(device_type);
170 CHECK(memset_primitive) << "Can not create Memset primitive for device type " << device_type;
171 memset_primitive->Launch(ctx->stream(), out->mut_dptr(), 0,
172 elem_cnt_of_this_chunk * GetSizeOfDataType(out->data_type()));
173
174 std::unique_ptr<ep::primitive::Add> add_primitive =
175 ep::primitive::NewPrimitive<ep::primitive::AddFactory>(ctx->device_type(), in->data_type());
176 CHECK(add_primitive);
177 for (int64_t i = 0; i < sorted_p2p_pair.size(); ++i) {
178 const auto& p2p_pair = sorted_p2p_pair.at(i);
179 int64_t src = p2p_pair.first;
180 int64_t dst = p2p_pair.second;
181 if (GlobalProcessCtx::Rank() == src) {
182 const auto& tensor_slice_copier = sorted_elem_cnt2_in_tensor_slice_copier.at(i).second;
183 int64_t send_elem_cnt = sorted_elem_cnt2_in_tensor_slice_copier.at(i).first;
184 tensor_slice_copier->Copy(ctx->stream(), tmp_buffer_ptr, in_ptr);
185 CHECK_JUST(Send(reinterpret_cast<const void*>(tmp_buffer_ptr), send_elem_cnt,
186 in->data_type(), dst, device_type, ctx->stream()));
187 }
188 if (GlobalProcessCtx::Rank() == dst) {
189 CHECK_JUST(Recv(tmp_buffer_ptr, elem_cnt_of_this_chunk, out->data_type(), src, device_type,
190 ctx->stream()));
191 add_primitive->Launch(ctx->stream(), out->dptr(), tmp_buffer_ptr, out->mut_dptr(),
192 elem_cnt_of_this_chunk);
193 }
194 }
195 }
196 bool AlwaysComputeWhenAllOutputsEmpty() const override { return false; }
197};
198

Callers

nothing calls this directly

Calls 14

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

Tested by

no test coverage detected