-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cherry pick 1.17.3 - Round 2 (#20178)
### Description <!-- Describe your changes. --> ### Motivation and Context <!-- - Why is this change required? What problem does it solve? - If it fixes an open issue, please link to the issue here. --> --------- Co-authored-by: Changming Sun <[email protected]> Co-authored-by: kunal-vaishnavi <[email protected]> Co-authored-by: Sai Kishan Pampana <[email protected]> Co-authored-by: rachguo <[email protected]> Co-authored-by: Jian Chen <[email protected]> Co-authored-by: Shubham Bhokare <[email protected]>
- Loading branch information
1 parent
45ff957
commit a61add2
Showing
14 changed files
with
386 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
onnxruntime/core/platform/windows/hardware_core_enumerator.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
#include "hardware_core_enumerator.h" | ||
#include <memory> | ||
#include <Windows.h> | ||
#include <assert.h> | ||
|
||
namespace onnxruntime { | ||
|
||
struct LogicalProcessorInformation { | ||
std::unique_ptr<char[]> Buffer; | ||
size_t Length; | ||
}; | ||
|
||
struct CoreCounter { | ||
uint32_t PhysicalCores = 0; | ||
uint32_t LLCCores = 0; | ||
}; | ||
|
||
static LogicalProcessorInformation GetLogicalProcessorInfos(LOGICAL_PROCESSOR_RELATIONSHIP relationship) { | ||
DWORD length = 0; | ||
DWORD rc = GetLogicalProcessorInformationEx(relationship, nullptr, &length); | ||
|
||
assert(rc == FALSE); | ||
|
||
auto processorInformationBytes = std::make_unique<char[]>(length); | ||
|
||
rc = GetLogicalProcessorInformationEx( | ||
relationship, reinterpret_cast<PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX>(processorInformationBytes.get()), &length); | ||
|
||
assert(rc == TRUE); | ||
|
||
return {std::move(processorInformationBytes), length}; | ||
} | ||
|
||
uint32_t CountSetBits(DWORD input) { | ||
uint32_t c; | ||
for (c = 0; input; c++) { | ||
input &= input - 1; | ||
} | ||
return c; | ||
} | ||
|
||
static CoreCounter GetCoreInfo() { | ||
auto logicalProcessorInformation = GetLogicalProcessorInfos(RelationAll); | ||
|
||
CoreCounter cores; | ||
DWORD dwLevel2GroupMask = 0; | ||
DWORD dwLevel3GroupMask = 0; | ||
size_t read = 0; | ||
PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX currentProcessorInfo = NULL; | ||
|
||
while ((read + FIELD_OFFSET(SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX, Processor)) < logicalProcessorInformation.Length) { | ||
currentProcessorInfo = | ||
reinterpret_cast<PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX>(logicalProcessorInformation.Buffer.get() + read); | ||
if ((read + currentProcessorInfo->Size) > logicalProcessorInformation.Length) { | ||
break; | ||
} | ||
|
||
switch (currentProcessorInfo->Relationship) { | ||
case RelationProcessorCore: | ||
cores.PhysicalCores++; | ||
break; | ||
case RelationCache: | ||
if (currentProcessorInfo->Cache.Level == 2) { | ||
dwLevel2GroupMask |= currentProcessorInfo->Cache.GroupMask.Mask; | ||
} else if (currentProcessorInfo->Cache.Level == 3) { | ||
dwLevel3GroupMask |= currentProcessorInfo->Cache.GroupMask.Mask; | ||
} | ||
break; | ||
} | ||
|
||
read += currentProcessorInfo->Size; | ||
} | ||
// Cores with L2 and LLC cache levels = # Physical Cores - # logical cores without LLC | ||
cores.LLCCores = cores.PhysicalCores - CountSetBits(dwLevel2GroupMask & ~dwLevel3GroupMask); | ||
|
||
return cores; | ||
} | ||
|
||
uint32_t HardwareCoreEnumerator::DefaultIntraOpNumThreads() { | ||
// # of physical cores = # of P cores + # of E Cores + # of Soc Cores. | ||
// # of logical cores = # of P cores x 2 (if hyper threading is enabled) + # of E cores + # of Soc Cores. | ||
auto cores = GetCoreInfo(); | ||
|
||
return cores.LLCCores; | ||
} | ||
|
||
} // namespace onnxruntime |
12 changes: 12 additions & 0 deletions
12
onnxruntime/core/platform/windows/hardware_core_enumerator.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
#pragma once | ||
#include <stdint.h> | ||
|
||
namespace onnxruntime { | ||
struct HardwareCoreEnumerator { | ||
HardwareCoreEnumerator() = delete; | ||
static uint32_t DefaultIntraOpNumThreads(); | ||
}; | ||
} // namespace onnxruntime |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.