Skip to content

Commit 8dbcfee

Browse files
authored
option owns vulkan device index (#5973)
1 parent da08ec3 commit 8dbcfee

File tree

5 files changed

+72
-32
lines changed

5 files changed

+72
-32
lines changed

src/gpu.cpp

+27-14
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,8 @@ class GpuInfoPrivate
266266
void query_extension_properties();
267267

268268
public:
269+
int device_index;
270+
269271
// physical device
270272
VkPhysicalDevice physicalDevice;
271273

@@ -1141,11 +1143,21 @@ GpuInfo& GpuInfo::operator=(const GpuInfo&)
11411143
return *this;
11421144
}
11431145

1146+
int GpuInfo::device_index() const
1147+
{
1148+
return d->device_index;
1149+
}
1150+
11441151
VkPhysicalDevice GpuInfo::physicalDevice() const
11451152
{
11461153
return d->physicalDevice;
11471154
}
11481155

1156+
VkPhysicalDevice GpuInfo::physical_device() const
1157+
{
1158+
return d->physicalDevice;
1159+
}
1160+
11491161
const VkPhysicalDeviceFeatures& GpuInfo::physicalDevicefeatures() const
11501162
{
11511163
return d->physicalDevicefeatures;
@@ -1161,6 +1173,11 @@ const VkPhysicalDeviceMemoryProperties& GpuInfo::physicalDeviceMemoryProperties(
11611173
return d->physicalDeviceMemoryProperties;
11621174
}
11631175

1176+
const VkPhysicalDeviceMemoryProperties& GpuInfo::physical_device_memory_properties() const
1177+
{
1178+
return d->physicalDeviceMemoryProperties;
1179+
}
1180+
11641181
const std::vector<VkExtensionProperties>& GpuInfo::deviceExtensionProperties() const
11651182
{
11661183
return d->deviceExtensionProperties;
@@ -2209,10 +2226,11 @@ int create_gpu_instance(const char* driver_path)
22092226
const VkPhysicalDevice& physicalDevice = physicalDevices[i];
22102227
delete g_gpu_infos[gpu_info_index];
22112228
g_gpu_infos[gpu_info_index] = new GpuInfo;
2212-
// GpuInfoPrivate& gpu_info = *(g_gpu_infos[gpu_info_index]->d);
22132229

22142230
GpuInfo& gpu_info = *g_gpu_infos[gpu_info_index];
22152231

2232+
gpu_info.d->device_index = gpu_info_index;
2233+
22162234
gpu_info.d->physicalDevice = physicalDevice;
22172235

22182236
gpu_info.d->query_features();
@@ -2591,12 +2609,11 @@ const ncnn::Packing_vulkan* VulkanDevicePrivate::get_utility_operator(int storag
25912609

25922610
opt.use_vulkan_compute = true;
25932611

2594-
opt.blob_vkallocator = opt.workspace_vkallocator = vkdev->acquire_blob_allocator();
2595-
opt.staging_vkallocator = vkdev->acquire_staging_allocator();
2596-
25972612
// cache uop pipeline as device member explicitly
25982613
opt.pipeline_cache = 0;
25992614

2615+
opt.vulkan_device_index = vkdev->info.device_index();
2616+
26002617
ncnn::Packing_vulkan* uop = new ncnn::Packing_vulkan;
26012618
uop->vkdev = vkdev;
26022619

@@ -2613,9 +2630,6 @@ const ncnn::Packing_vulkan* VulkanDevicePrivate::get_utility_operator(int storag
26132630

26142631
uop_packing[storage_type_from][storage_type_to][cast_type_from_index][cast_type_to_index][packing_type_to_index] = uop;
26152632

2616-
vkdev->reclaim_blob_allocator(opt.blob_vkallocator);
2617-
vkdev->reclaim_staging_allocator(opt.staging_vkallocator);
2618-
26192633
return uop;
26202634
}
26212635

@@ -2627,6 +2641,7 @@ void VulkanDevicePrivate::destroy_utility_operator()
26272641
opt.use_int8_arithmetic = false;
26282642
opt.use_cooperative_matrix = false;
26292643
opt.pipeline_cache = 0;
2644+
opt.vulkan_device_index = vkdev->info.device_index();
26302645

26312646
// from buffer | image
26322647
// to buffer | image
@@ -4576,11 +4591,13 @@ int compile_spirv_module(const char* comp_data, int comp_data_size, const Option
45764591

45774592
bool support_shader_int64 = false;
45784593

4579-
if (opt.blob_vkallocator)
4594+
// fill device macros
45804595
{
4581-
const VulkanDevice* vkdev = opt.blob_vkallocator->vkdev;
4596+
int device_index = opt.vulkan_device_index;
4597+
if (device_index < 0 || device_index >= get_gpu_count())
4598+
device_index = get_default_gpu_index();
45824599

4583-
const GpuInfo& info = vkdev->info;
4600+
const GpuInfo& info = get_gpu_info(device_index);
45844601

45854602
support_shader_int64 = info.physicalDevicefeatures().shaderInt64;
45864603

@@ -4935,10 +4952,6 @@ int compile_spirv_module(const char* comp_data, int comp_data_size, const Option
49354952

49364953
#undef DD_APPEND_PROPERTY
49374954
}
4938-
else
4939-
{
4940-
NCNN_LOGE("opt.blob_vkallocator is null");
4941-
}
49424955

49434956
std::string define_macro_data;
49444957

src/gpu.h

+4
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,11 @@ class NCNN_EXPORT GpuInfo
193193
explicit GpuInfo();
194194
virtual ~GpuInfo();
195195

196+
int device_index() const;
197+
196198
// vulkan physical device
197199
VkPhysicalDevice physicalDevice() const;
200+
VkPhysicalDevice physical_device() const; // api compatibility
198201

199202
// features
200203
const VkPhysicalDeviceFeatures& physicalDevicefeatures() const;
@@ -204,6 +207,7 @@ class NCNN_EXPORT GpuInfo
204207

205208
// memory properties
206209
const VkPhysicalDeviceMemoryProperties& physicalDeviceMemoryProperties() const;
210+
const VkPhysicalDeviceMemoryProperties& physical_device_memory_properties() const; // api compatibility
207211

208212
// extension properties
209213
const std::vector<VkExtensionProperties>& deviceExtensionProperties() const;

src/net.cpp

+18-2
Original file line numberDiff line numberDiff line change
@@ -1380,7 +1380,14 @@ int Net::load_param(const DataReader& dr)
13801380

13811381
if (opt.use_vulkan_compute)
13821382
{
1383-
if (!d->vkdev) d->vkdev = get_gpu_device();
1383+
if (!d->vkdev)
1384+
{
1385+
int device_index = opt.vulkan_device_index;
1386+
if (device_index < 0 || device_index >= get_gpu_count())
1387+
device_index = get_default_gpu_index();
1388+
1389+
d->vkdev = get_gpu_device(device_index);
1390+
}
13841391
if (!d->vkdev || !d->vkdev->is_valid()) opt.use_vulkan_compute = false; // no valid vulkan device, fallback to cpu
13851392
}
13861393
if (opt.use_vulkan_compute)
@@ -1683,7 +1690,14 @@ int Net::load_param_bin(const DataReader& dr)
16831690

16841691
if (opt.use_vulkan_compute)
16851692
{
1686-
if (!d->vkdev) d->vkdev = get_gpu_device();
1693+
if (!d->vkdev)
1694+
{
1695+
int device_index = opt.vulkan_device_index;
1696+
if (device_index < 0 || device_index >= get_gpu_count())
1697+
device_index = get_default_gpu_index();
1698+
1699+
d->vkdev = get_gpu_device(device_index);
1700+
}
16871701
if (!d->vkdev || !d->vkdev->is_valid()) opt.use_vulkan_compute = false; // no valid vulkan device, fallback to cpu
16881702
}
16891703
if (opt.use_vulkan_compute)
@@ -2305,11 +2319,13 @@ std::vector<Layer*>& Net::mutable_layers()
23052319
#if NCNN_VULKAN
23062320
void Net::set_vulkan_device(int device_index)
23072321
{
2322+
opt.vulkan_device_index = device_index;
23082323
d->vkdev = get_gpu_device(device_index);
23092324
}
23102325

23112326
void Net::set_vulkan_device(const VulkanDevice* _vkdev)
23122327
{
2328+
opt.vulkan_device_index = _vkdev->info.device_index();
23132329
d->vkdev = _vkdev;
23142330
}
23152331

src/option.cpp

+11-8
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ namespace ncnn {
2121
Option::Option()
2222
{
2323
lightmode = true;
24+
use_shader_pack8 = false;
25+
use_subgroup_ops = false;
26+
use_reserved_0 = false;
27+
2428
num_threads = get_physical_big_cpu_count();
2529
blob_allocator = 0;
2630
workspace_allocator = 0;
@@ -50,18 +54,13 @@ Option::Option()
5054

5155
use_packing_layout = true;
5256

53-
use_shader_pack8 = false;
54-
55-
use_subgroup_ops = false;
56-
57-
use_subgroup_reserved_1 = false;
58-
use_subgroup_reserved_2 = false;
59-
use_subgroup_reserved_3 = false;
57+
vulkan_device_index = -1;
58+
use_reserved_1 = false;
6059

6160
use_image_storage = false;
6261
use_tensor_storage = false;
6362

64-
use_reserved_0 = false;
63+
use_reserved_2 = false;
6564

6665
flush_denormals = 3;
6766

@@ -78,6 +77,10 @@ Option::Option()
7877

7978
use_fp16_uniform = true;
8079
use_int8_uniform = true;
80+
81+
use_reserved_9 = false;
82+
use_reserved_10 = false;
83+
use_reserved_11 = false;
8184
}
8285

8386
} // namespace ncnn

src/option.h

+12-8
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ class NCNN_EXPORT Option
3737
// enabled by default
3838
bool lightmode;
3939

40+
// use pack8 shader
41+
bool use_shader_pack8;
42+
43+
// enable subgroup in shader
44+
bool use_subgroup_ops;
45+
46+
bool use_reserved_0;
47+
4048
// thread count
4149
// default value is the one returned by get_cpu_count()
4250
int num_threads;
@@ -105,20 +113,16 @@ class NCNN_EXPORT Option
105113
// enabled by default
106114
bool use_packing_layout;
107115

108-
bool use_shader_pack8;
116+
// the vulkan device
117+
int vulkan_device_index;
109118

110-
// subgroup option
111-
bool use_subgroup_ops;
112-
113-
bool use_subgroup_reserved_1;
114-
bool use_subgroup_reserved_2;
115-
bool use_subgroup_reserved_3;
119+
bool use_reserved_1;
116120

117121
// turn on for adreno
118122
bool use_image_storage;
119123
bool use_tensor_storage;
120124

121-
bool use_reserved_0;
125+
bool use_reserved_2;
122126

123127
// enable DAZ(Denormals-Are-Zero) and FTZ(Flush-To-Zero)
124128
// default value is 3

0 commit comments

Comments
 (0)