Assign shared objects to internal objects, using memory allocation algorithms. Usage records for the algorithms are calculated separately for each data type and object type.
| 512 | // algorithms. Usage records for the algorithms are calculated separately for |
| 513 | // each data type and object type. |
| 514 | Status Runtime::AssignInternalObjects(std::vector<Object>* shared_objects) { |
| 515 | // Build tensor usage records, clusterized by object type and data type. |
| 516 | std::map<DataType, CombinedUsageRecords> usage_records_by_data_type; |
| 517 | std::vector<Object*> global_ref_to_object_ptr; |
| 518 | for (size_t i = 0; i < programs_.size(); ++i) { |
| 519 | for (auto& object : programs_[i].refs) { |
| 520 | auto ref = GetRef(object); |
| 521 | if (ref >= global_ref_to_object_ptr.size()) { |
| 522 | global_ref_to_object_ptr.resize(ref + 1, nullptr); |
| 523 | } |
| 524 | if (global_ref_to_object_ptr[ref] == nullptr) { |
| 525 | global_ref_to_object_ptr[ref] = &object; |
| 526 | } |
| 527 | RETURN_IF_ERROR(AddUsageRecord( |
| 528 | &usage_records_by_data_type[object.data_type], object, i)); |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | std::vector<ObjectRef> global_ref_to_shared_ref( |
| 533 | global_ref_to_object_ptr.size(), kInvalidObjectRef); |
| 534 | |
| 535 | // Calculate and apply shared objects assignment for each data type. |
| 536 | for (const auto& it : usage_records_by_data_type) { |
| 537 | const CombinedUsageRecords& usage_records = it.second; |
| 538 | if (!usage_records.buffers.empty()) { |
| 539 | ObjectsAssignment<size_t> buffer_assignment; |
| 540 | RETURN_IF_ERROR(AssignObjectsToTensors( |
| 541 | usage_records.buffers, MemoryStrategy::GREEDY, &buffer_assignment)); |
| 542 | RETURN_IF_ERROR(ApplyBuffersAssignment( |
| 543 | buffer_assignment, usage_records.usage_refs, global_ref_to_object_ptr, |
| 544 | &global_ref_to_shared_ref, shared_objects)); |
| 545 | } |
| 546 | if (!usage_records.textures_1d.empty()) { |
| 547 | ObjectsAssignment<size_t> texture_1d_assignment; |
| 548 | RETURN_IF_ERROR(AssignObjectsToTensors(usage_records.textures_1d, |
| 549 | MemoryStrategy::GREEDY, |
| 550 | &texture_1d_assignment)); |
| 551 | RETURN_IF_ERROR(ApplyTexturesAssignment( |
| 552 | texture_1d_assignment, usage_records.usage_refs, |
| 553 | global_ref_to_object_ptr, &global_ref_to_shared_ref, shared_objects)); |
| 554 | } |
| 555 | if (!usage_records.textures_2d.empty()) { |
| 556 | ObjectsAssignment<uint2> texture_2d_assignment; |
| 557 | RETURN_IF_ERROR(AssignObjectsToTensors(usage_records.textures_2d, |
| 558 | MemoryStrategy::GREEDY, |
| 559 | &texture_2d_assignment)); |
| 560 | RETURN_IF_ERROR(ApplyTexturesAssignment( |
| 561 | texture_2d_assignment, usage_records.usage_refs, |
| 562 | global_ref_to_object_ptr, &global_ref_to_shared_ref, shared_objects)); |
| 563 | } |
| 564 | if (!usage_records.textures_3d.empty()) { |
| 565 | ObjectsAssignment<uint3> texture_3d_assignment; |
| 566 | RETURN_IF_ERROR(AssignObjectsToTensors(usage_records.textures_3d, |
| 567 | MemoryStrategy::GREEDY, |
| 568 | &texture_3d_assignment)); |
| 569 | RETURN_IF_ERROR(ApplyTexturesAssignment( |
| 570 | texture_3d_assignment, usage_records.usage_refs, |
| 571 | global_ref_to_object_ptr, &global_ref_to_shared_ref, shared_objects)); |
nothing calls this directly
no test coverage detected