Skip to content

Commit e30bd29

Browse files
Add UR USM allocation scenarios
Signed-off-by: Michał Staniewski <[email protected]>
1 parent 9369275 commit e30bd29

13 files changed

+555
-5
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (C) 2025 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: MIT
5+
*
6+
*/
7+
8+
#pragma once
9+
10+
#include "framework/argument/basic_argument.h"
11+
#include "framework/argument/enum/allocation_measure_mode_argument.h"
12+
#include "framework/argument/enum/usm_runtime_memory_placement_argument.h"
13+
#include "framework/test_case/test_case.h"
14+
15+
struct UsmBatchMemoryAllocationArguments : TestCaseArgumentContainer {
16+
UsmRuntimeMemoryPlacementArgument usmMemoryPlacement;
17+
PositiveIntegerArgument allocationCount;
18+
ByteSizeArgument size;
19+
AllocationMeasureModeArgument measureMode;
20+
21+
UsmBatchMemoryAllocationArguments()
22+
: usmMemoryPlacement(*this, "type", "Type of memory being allocated"),
23+
allocationCount(*this, "allocationCount", "Number of allocations done"),
24+
size(*this, "size", "Size per single allocation"),
25+
measureMode(*this, "measureMode", "Specifies which APIs to measure") {}
26+
};
27+
28+
struct UsmBatchMemoryAllocation : TestCase<UsmBatchMemoryAllocationArguments> {
29+
using TestCase<UsmBatchMemoryAllocationArguments>::TestCase;
30+
31+
std::string getTestCaseName() const override {
32+
return "UsmBatchMemoryAllocation";
33+
}
34+
35+
std::string getHelp() const override {
36+
return "measures time spent in USM memory allocation APIs, when many allocations are requested. ";
37+
}
38+
};
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright (C) 2025 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: MIT
5+
*
6+
*/
7+
8+
#pragma once
9+
10+
#include "framework/argument/basic_argument.h"
11+
#include "framework/argument/enum/allocation_measure_mode_argument.h"
12+
#include "framework/argument/enum/distribution_kind_argument.h"
13+
#include "framework/argument/enum/usm_runtime_memory_placement_argument.h"
14+
#include "framework/test_case/test_case.h"
15+
16+
struct UsmRandomMemoryAllocationArguments : TestCaseArgumentContainer {
17+
UsmRuntimeMemoryPlacementArgument usmMemoryPlacement;
18+
PositiveIntegerArgument operationCount;
19+
ByteSizeArgument minSize;
20+
ByteSizeArgument maxSize;
21+
DistributionKindArgument sizeDistribution;
22+
23+
UsmRandomMemoryAllocationArguments()
24+
: usmMemoryPlacement(*this, "type", "Type of memory being allocated"),
25+
operationCount(*this, "operationCount", "Total number of alloc and free operations"),
26+
minSize(*this, "minSize", "Minimum size per single allocation"),
27+
maxSize(*this, "maxSize", "Maximum size per single allocation"),
28+
sizeDistribution(*this, "sizeDistribution", "Specifies randomization method for allocation size") {}
29+
};
30+
31+
struct UsmRandomMemoryAllocation : TestCase<UsmRandomMemoryAllocationArguments> {
32+
using TestCase<UsmRandomMemoryAllocationArguments>::TestCase;
33+
34+
std::string getTestCaseName() const override {
35+
return "UsmRandomMemoryAllocation";
36+
}
37+
38+
std::string getHelp() const override {
39+
return "measures time spent in USM memory allocation APIs, with a randomized scenario. ";
40+
}
41+
};
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright (C) 2025 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: MIT
5+
*
6+
*/
7+
8+
#include "definitions/usm_batch_memory_allocation.h"
9+
10+
#include "framework/test_case/register_test_case.h"
11+
#include "framework/utility/common_gtest_args.h"
12+
#include "framework/utility/memory_constants.h"
13+
14+
#include <gtest/gtest.h>
15+
16+
[[maybe_unused]] static const inline RegisterTestCase<UsmBatchMemoryAllocation> registerTestCase{};
17+
18+
class UsmBatchMemoryAllocationTest : public ::testing::TestWithParam<std::tuple<Api, UsmRuntimeMemoryPlacement, size_t, size_t, AllocationMeasureMode>> {
19+
};
20+
21+
TEST_P(UsmBatchMemoryAllocationTest, Test) {
22+
UsmBatchMemoryAllocationArguments args{};
23+
args.api = std::get<0>(GetParam());
24+
args.usmMemoryPlacement = std::get<1>(GetParam());
25+
args.allocationCount = std::get<2>(GetParam());
26+
args.size = std::get<3>(GetParam());
27+
args.measureMode = std::get<4>(GetParam());
28+
UsmBatchMemoryAllocation test;
29+
test.run(args);
30+
}
31+
32+
INSTANTIATE_TEST_SUITE_P(
33+
UsmBatchMemoryAllocationTest,
34+
UsmBatchMemoryAllocationTest,
35+
::testing::Combine(
36+
::CommonGtestArgs::allApis(),
37+
::testing::Values(UsmRuntimeMemoryPlacement::Host, UsmRuntimeMemoryPlacement::Device, UsmRuntimeMemoryPlacement::Shared),
38+
::testing::Values(32, 512),
39+
::testing::Values(4 * MemoryConstants::kiloByte,
40+
64 * MemoryConstants::kiloByte,
41+
512 * MemoryConstants::kiloByte,
42+
4 * MemoryConstants::megaByte),
43+
::testing::Values(AllocationMeasureMode::Allocate, AllocationMeasureMode::Free, AllocationMeasureMode::Both)));
44+
45+
INSTANTIATE_TEST_SUITE_P(
46+
UsmBatchMemoryAllocationTestLIMITED,
47+
UsmBatchMemoryAllocationTest,
48+
::testing::Combine(
49+
::CommonGtestArgs::allApis(),
50+
::testing::Values(UsmRuntimeMemoryPlacement::Shared),
51+
::testing::Values(512),
52+
::testing::Values(4 * MemoryConstants::megaByte),
53+
::testing::Values(AllocationMeasureMode::Both)));

source/benchmarks/api_overhead_benchmark/gtest/usm_memory_allocation.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@
1515

1616
[[maybe_unused]] static const inline RegisterTestCase<UsmMemoryAllocation> registerTestCase{};
1717

18-
class UsmMemoryAllocationTest : public ::testing::TestWithParam<std::tuple<UsmRuntimeMemoryPlacement, size_t, AllocationMeasureMode>> {
18+
class UsmMemoryAllocationTest : public ::testing::TestWithParam<std::tuple<Api, UsmRuntimeMemoryPlacement, size_t, AllocationMeasureMode>> {
1919
};
2020

2121
TEST_P(UsmMemoryAllocationTest, Test) {
2222
UsmMemoryAllocationArguments args{};
23-
args.api = Api::L0;
24-
args.usmMemoryPlacement = std::get<0>(GetParam());
25-
args.size = std::get<1>(GetParam());
26-
args.measureMode = std::get<2>(GetParam());
23+
args.api = std::get<0>(GetParam());
24+
args.usmMemoryPlacement = std::get<1>(GetParam());
25+
args.size = std::get<2>(GetParam());
26+
args.measureMode = std::get<3>(GetParam());
2727
UsmMemoryAllocation test;
2828
test.run(args);
2929
}
@@ -32,6 +32,7 @@ INSTANTIATE_TEST_SUITE_P(
3232
UsmMemoryAllocationTest,
3333
UsmMemoryAllocationTest,
3434
::testing::Combine(
35+
::CommonGtestArgs::allApis(),
3536
::testing::Values(UsmRuntimeMemoryPlacement::Host, UsmRuntimeMemoryPlacement::Device, UsmRuntimeMemoryPlacement::Shared),
3637
::testing::Values(4,
3738
64,
@@ -49,6 +50,7 @@ INSTANTIATE_TEST_SUITE_P(
4950
UsmMemoryAllocationTestLIMITED,
5051
UsmMemoryAllocationTest,
5152
::testing::Combine(
53+
::CommonGtestArgs::allApis(),
5254
::testing::Values(UsmRuntimeMemoryPlacement::Shared),
5355
::testing::Values(
5456
4 * MemoryConstants::megaByte),
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright (C) 2025 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: MIT
5+
*
6+
*/
7+
8+
#include "definitions/usm_random_memory_allocation.h"
9+
10+
#include "framework/test_case/register_test_case.h"
11+
#include "framework/utility/common_gtest_args.h"
12+
#include "framework/utility/memory_constants.h"
13+
14+
#include <gtest/gtest.h>
15+
16+
[[maybe_unused]] static const inline RegisterTestCase<UsmRandomMemoryAllocation> registerTestCase{};
17+
18+
class UsmRandomMemoryAllocationTest : public ::testing::TestWithParam<std::tuple<Api, UsmRuntimeMemoryPlacement, size_t, size_t, size_t, DistributionKind>> {
19+
};
20+
21+
TEST_P(UsmRandomMemoryAllocationTest, Test) {
22+
UsmRandomMemoryAllocationArguments args{};
23+
args.api = std::get<0>(GetParam());
24+
args.usmMemoryPlacement = std::get<1>(GetParam());
25+
args.operationCount = std::get<2>(GetParam());
26+
args.minSize = std::get<3>(GetParam());
27+
args.maxSize = std::get<4>(GetParam());
28+
args.sizeDistribution = std::get<5>(GetParam());
29+
UsmRandomMemoryAllocation test;
30+
test.run(args);
31+
}
32+
33+
INSTANTIATE_TEST_SUITE_P(
34+
UsmRandomMemoryAllocationTest,
35+
UsmRandomMemoryAllocationTest,
36+
::testing::Combine(
37+
::CommonGtestArgs::allApis(),
38+
::testing::Values(UsmRuntimeMemoryPlacement::Host, UsmRuntimeMemoryPlacement::Device, UsmRuntimeMemoryPlacement::Shared),
39+
::testing::Values(128, 1024),
40+
::testing::Values(8, MemoryConstants::kiloByte),
41+
::testing::Values(4 * MemoryConstants::kiloByte,
42+
64 * MemoryConstants::kiloByte,
43+
512 * MemoryConstants::kiloByte,
44+
4 * MemoryConstants::megaByte),
45+
::testing::Values(DistributionKind::Uniform, DistributionKind::LogUniform)));
46+
47+
INSTANTIATE_TEST_SUITE_P(
48+
UsmRandomMemoryAllocationTestLIMITED,
49+
UsmRandomMemoryAllocationTest,
50+
::testing::Combine(
51+
::CommonGtestArgs::allApis(),
52+
::testing::Values(UsmRuntimeMemoryPlacement::Shared),
53+
::testing::Values(32),
54+
::testing::Values(1024),
55+
::testing::Values(4 * MemoryConstants::megaByte),
56+
::testing::Values(DistributionKind::LogUniform)));
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright (C) 2025 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: MIT
5+
*
6+
*/
7+
8+
#include "framework/test_case/register_test_case.h"
9+
#include "framework/ur/error.h"
10+
#include "framework/ur/ur.h"
11+
#include "framework/ur/usm_helper.h"
12+
#include "framework/utility/timer.h"
13+
14+
#include "definitions/usm_batch_memory_allocation.h"
15+
16+
#include <gtest/gtest.h>
17+
18+
static TestResult run(const UsmBatchMemoryAllocationArguments &arguments, Statistics &statistics) {
19+
MeasurementFields typeSelector(MeasurementUnit::Microseconds, MeasurementType::Cpu);
20+
21+
if (isNoopRun()) {
22+
statistics.pushUnitAndType(typeSelector.getUnit(), typeSelector.getType());
23+
return TestResult::Nooped;
24+
}
25+
26+
// Setup
27+
UrState ur;
28+
Timer timer;
29+
30+
// Warmup
31+
std::vector<void *> ptrs(arguments.allocationCount);
32+
for (auto i = 0u; i < arguments.allocationCount; i++)
33+
ASSERT_UR_RESULT_SUCCESS(UR::UsmHelper::allocate(arguments.usmMemoryPlacement, ur.context, ur.device, arguments.size, &ptrs[i]));
34+
for (auto i = 0u; i < arguments.allocationCount; i++)
35+
ASSERT_UR_RESULT_SUCCESS(urUSMFree(ur.context, ptrs[i]));
36+
37+
// Benchmark
38+
for (auto j = 0u; j < arguments.iterations; j++) {
39+
if (arguments.measureMode == AllocationMeasureMode::Allocate ||
40+
arguments.measureMode == AllocationMeasureMode::Both) {
41+
timer.measureStart();
42+
}
43+
44+
for (auto i = 0u; i < arguments.allocationCount; i++)
45+
ASSERT_UR_RESULT_SUCCESS(UR::UsmHelper::allocate(arguments.usmMemoryPlacement, ur.context, ur.device, arguments.size, &ptrs[i]));
46+
47+
if (arguments.measureMode == AllocationMeasureMode::Allocate) {
48+
timer.measureEnd();
49+
} else if (arguments.measureMode == AllocationMeasureMode::Free) {
50+
timer.measureStart();
51+
}
52+
53+
for (auto i = 0u; i < arguments.allocationCount; i++)
54+
ASSERT_UR_RESULT_SUCCESS(urUSMFree(ur.context, ptrs[i]));
55+
56+
if (arguments.measureMode == AllocationMeasureMode::Free ||
57+
arguments.measureMode == AllocationMeasureMode::Both) {
58+
timer.measureEnd();
59+
}
60+
statistics.pushValue(timer.get(), typeSelector.getUnit(), typeSelector.getType());
61+
}
62+
return TestResult::Success;
63+
}
64+
65+
static RegisterTestCaseImplementation<UsmBatchMemoryAllocation> registerTestCase(run, Api::UR);
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright (C) 2025 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: MIT
5+
*
6+
*/
7+
8+
#include "framework/test_case/register_test_case.h"
9+
#include "framework/ur/error.h"
10+
#include "framework/ur/ur.h"
11+
#include "framework/ur/usm_helper.h"
12+
#include "framework/utility/timer.h"
13+
14+
#include "definitions/usm_memory_allocation.h"
15+
16+
#include <gtest/gtest.h>
17+
18+
static TestResult run(const UsmMemoryAllocationArguments &arguments, Statistics &statistics) {
19+
MeasurementFields typeSelector(MeasurementUnit::Microseconds, MeasurementType::Cpu);
20+
21+
if (isNoopRun()) {
22+
statistics.pushUnitAndType(typeSelector.getUnit(), typeSelector.getType());
23+
return TestResult::Nooped;
24+
}
25+
26+
// Setup
27+
UrState ur;
28+
Timer timer;
29+
30+
// Warmup
31+
void *ptr{};
32+
ASSERT_UR_RESULT_SUCCESS(UR::UsmHelper::allocate(arguments.usmMemoryPlacement, ur.context, ur.device, arguments.size, &ptr));
33+
ASSERT_UR_RESULT_SUCCESS(urUSMFree(ur.context, ptr));
34+
35+
// Benchmark
36+
for (auto j = 0u; j < arguments.iterations; j++) {
37+
if (arguments.measureMode == AllocationMeasureMode::Allocate ||
38+
arguments.measureMode == AllocationMeasureMode::Both) {
39+
timer.measureStart();
40+
}
41+
42+
ASSERT_UR_RESULT_SUCCESS(UR::UsmHelper::allocate(arguments.usmMemoryPlacement, ur.context, ur.device, arguments.size, &ptr));
43+
44+
if (arguments.measureMode == AllocationMeasureMode::Allocate) {
45+
timer.measureEnd();
46+
} else if (arguments.measureMode == AllocationMeasureMode::Free) {
47+
timer.measureStart();
48+
}
49+
50+
ASSERT_UR_RESULT_SUCCESS(urUSMFree(ur.context, ptr));
51+
52+
if (arguments.measureMode == AllocationMeasureMode::Free ||
53+
arguments.measureMode == AllocationMeasureMode::Both) {
54+
timer.measureEnd();
55+
}
56+
statistics.pushValue(timer.get(), typeSelector.getUnit(), typeSelector.getType());
57+
}
58+
return TestResult::Success;
59+
}
60+
61+
static RegisterTestCaseImplementation<UsmMemoryAllocation> registerTestCase(run, Api::UR);

0 commit comments

Comments
 (0)