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

Method CreateThreadCtx

oneflow/core/vm/virtual_machine.cpp:425–462  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

423}
424
425Maybe<vm::ThreadCtx*> VirtualMachine::CreateThreadCtx(Symbol<Device> device, StreamType stream_type,
426 size_t thread_uid) {
427 std::unique_lock<std::recursive_mutex> lock(stream_and_thread_ctx_mutex_);
428 // thread_ctx_ptr may be used after timout.
429 auto thread_ctx_ptr = std::make_shared<vm::ThreadCtx*>(nullptr);
430 {
431 auto bc = std::make_shared<BlockingCounter>(1);
432 engine_->InsertProbe([thread_ctx_ptr, bc](vm::VirtualMachineEngine* engine) {
433 auto thread_ctx = intrusive::make_shared<vm::ThreadCtx>();
434 engine->mut_thread_ctx_list()->PushBack(thread_ctx.Mutable());
435 *thread_ctx_ptr = thread_ctx.Mutable();
436 bc->Decrease();
437 return true;
438 });
439 JUST(NotifyOrRunScheduler());
440 JUST(bc->WaitUntilCntEqualZero(VirtualMachine::GetPredicatorNoMoreInstructionsFinished()));
441 }
442 auto* thread_ctx = *thread_ctx_ptr;
443 {
444 const std::string thread_tag = [&] {
445 std::string device_tag = *CHECK_JUST(DeviceTag4DeviceType(device->enum_type()));
446 if (StreamOnIndependentThread::Visit(stream_type)) {
447 return device_tag + GetStreamTypeName::Visit(stream_type);
448 } else {
449 return std::to_string(thread_uid);
450 }
451 }();
452 const auto& WorkerInitializer = [thread_tag](vm::ThreadCtx* thread_ctx) {
453 OF_PROFILER_NAME_THIS_HOST_THREAD("_VM::Worker_" + thread_tag);
454 };
455 auto thread = std::make_unique<std::thread>(&WorkerLoop, thread_ctx, WorkerInitializer);
456 {
457 std::unique_lock<std::mutex> lock(worker_threads_mutex_);
458 worker_threads_.push_back(std::move(thread));
459 }
460 }
461 return thread_ctx;
462}
463
464Maybe<vm::Stream*> VirtualMachine::CreateStream(vm::ThreadCtx* thread_ctx, Symbol<Stream> stream) {
465 std::unique_lock<std::recursive_mutex> lock(stream_and_thread_ctx_mutex_);

Callers

nothing calls this directly

Calls 10

DeviceTag4DeviceTypeFunction · 0.85
to_stringFunction · 0.85
InsertProbeMethod · 0.80
mut_thread_ctx_listMethod · 0.80
enum_typeMethod · 0.80
push_backMethod · 0.80
PushBackMethod · 0.45
MutableMethod · 0.45
DecreaseMethod · 0.45
WaitUntilCntEqualZeroMethod · 0.45

Tested by

no test coverage detected