diff --git a/src/command.cpp b/src/command.cpp index 043d7d359..6939a3273 100644 --- a/src/command.cpp +++ b/src/command.cpp @@ -20,7 +20,7 @@ namespace ncnn { -Command::Command(VulkanDevice* _vkdev, uint32_t _queue_index) : vkdev(_vkdev), queue_index(_queue_index) +Command::Command(const VulkanDevice* _vkdev, uint32_t _queue_index) : vkdev(_vkdev), queue_index(_queue_index) { // get queue vkGetDeviceQueue(vkdev->vkdevice(), queue_index, 0, &queue); @@ -161,7 +161,7 @@ int Command::wait_fence() return 0; } -VkCompute::VkCompute(VulkanDevice* _vkdev) : Command(_vkdev, _vkdev->info.compute_queue_index) +VkCompute::VkCompute(const VulkanDevice* _vkdev) : Command(_vkdev, _vkdev->info.compute_queue_index) { if (vkdev->info.support_VK_KHR_push_descriptor) { @@ -701,7 +701,7 @@ void VkCompute::transfer_transfer_barrier(VkBuffer buffer, size_t offset, size_t vkCmdPipelineBarrier(command_buffer, srcStageMask, dstStageMask, 0, 0, 0, 1, &bufferBarrier, 0, 0); } -VkTransfer::VkTransfer(VulkanDevice* _vkdev) : Command(_vkdev, _vkdev->info.transfer_queue_index) +VkTransfer::VkTransfer(const VulkanDevice* _vkdev) : Command(_vkdev, _vkdev->info.transfer_queue_index) { buffer_offset_alignment = vkdev->info.buffer_offset_alignment; staging_data = 0; diff --git a/src/command.h b/src/command.h index b39c6ee8a..c6a6cd79a 100644 --- a/src/command.h +++ b/src/command.h @@ -29,7 +29,7 @@ namespace ncnn { class Command { public: - Command(VulkanDevice* vkdev, uint32_t queue_index); + Command(const VulkanDevice* vkdev, uint32_t queue_index); ~Command(); protected: @@ -43,7 +43,7 @@ protected: int wait_fence(); protected: - VulkanDevice* vkdev; + const VulkanDevice* vkdev; uint32_t queue_index; VkQueue queue; @@ -57,7 +57,7 @@ protected: class VkCompute : public Command { public: - VkCompute(VulkanDevice* vkdev); + VkCompute(const VulkanDevice* vkdev); ~VkCompute(); void record_upload(const VkMat& m); @@ -153,7 +153,7 @@ protected: class VkTransfer : public Command { public: - VkTransfer(VulkanDevice* vkdev); + VkTransfer(const VulkanDevice* vkdev); ~VkTransfer(); void record_upload(const Mat& src, VkMat& dst); diff --git a/src/layer.cpp b/src/layer.cpp index e2a02ecbd..0f363ebbb 100644 --- a/src/layer.cpp +++ b/src/layer.cpp @@ -30,7 +30,7 @@ Option::Option() workspace_allocator = 0; #if NCNN_VULKAN - vulkan_compute = true; + vulkan_compute = false; blob_vkallocator = 0; workspace_vkallocator = 0; staging_vkallocator = 0; diff --git a/src/layer/concat.cpp b/src/layer/concat.cpp index fb3a040b3..b6f74f796 100644 --- a/src/layer/concat.cpp +++ b/src/layer/concat.cpp @@ -13,6 +13,7 @@ // specific language governing permissions and limitations under the License. #include "concat.h" +#include namespace ncnn { diff --git a/src/net.cpp b/src/net.cpp index 319fbe2c3..c9e089031 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -44,6 +44,7 @@ Net::Net() #if NCNN_VULKAN vkdev = 0; + vkdev_local = 0; weight_vkallocator = 0; weight_staging_vkallocator = 0; #endif // NCNN_VULKAN @@ -52,6 +53,10 @@ Net::Net() Net::~Net() { clear(); + +#if NCNN_VULKAN + delete vkdev_local; +#endif // NCNN_VULKAN } #if NCNN_STRING @@ -142,8 +147,10 @@ int Net::load_param(FILE* fp) #if NCNN_VULKAN if (use_vulkan_compute && !vkdev) { - fprintf(stderr, "vulkan device not set, vulkan compute disabled\n"); - use_vulkan_compute = false; + // use default vulkan device + if (!vkdev_local) + vkdev_local = new VulkanDevice; + vkdev = vkdev_local; } #endif // NCNN_VULKAN @@ -311,8 +318,10 @@ int Net::load_param_mem(const char* _mem) #if NCNN_VULKAN if (use_vulkan_compute && !vkdev) { - fprintf(stderr, "vulkan device not set, vulkan compute disabled\n"); - use_vulkan_compute = false; + // use default vulkan device + if (!vkdev_local) + vkdev_local = new VulkanDevice; + vkdev = vkdev_local; } #endif // NCNN_VULKAN @@ -483,8 +492,10 @@ int Net::load_param_bin(FILE* fp) #if NCNN_VULKAN if (use_vulkan_compute && !vkdev) { - fprintf(stderr, "vulkan device not set, vulkan compute disabled\n"); - use_vulkan_compute = false; + // use default vulkan device + if (!vkdev_local) + vkdev_local = new VulkanDevice; + vkdev = vkdev_local; } #endif // NCNN_VULKAN @@ -720,8 +731,10 @@ int Net::load_param(const unsigned char* _mem) #if NCNN_VULKAN if (use_vulkan_compute && !vkdev) { - fprintf(stderr, "vulkan device not set, vulkan compute disabled\n"); - use_vulkan_compute = false; + // use default vulkan device + if (!vkdev_local) + vkdev_local = new VulkanDevice; + vkdev = vkdev_local; } #endif // NCNN_VULKAN @@ -918,7 +931,7 @@ Extractor Net::create_extractor() const } #if NCNN_VULKAN -void Net::set_vulkan_device(VulkanDevice* _vkdev) +void Net::set_vulkan_device(const VulkanDevice* _vkdev) { vkdev = _vkdev; } @@ -1592,6 +1605,8 @@ Extractor::Extractor(const Net* _net, int blob_count) : net(_net) opt = get_default_option(); #if NCNN_VULKAN + opt.vulkan_compute = net->use_vulkan_compute; + if (net->use_vulkan_compute) { blob_mats_gpu.resize(blob_count); diff --git a/src/net.h b/src/net.h index c4b9d1966..6c877e4e9 100644 --- a/src/net.h +++ b/src/net.h @@ -106,7 +106,7 @@ public: #if NCNN_VULKAN - void set_vulkan_device(VulkanDevice* vkdev); + void set_vulkan_device(const VulkanDevice* vkdev); #endif // NCNN_VULKAN @@ -132,7 +132,8 @@ protected: std::vector custom_layer_registry; #if NCNN_VULKAN - VulkanDevice* vkdev; + const VulkanDevice* vkdev; + const VulkanDevice* vkdev_local; VkAllocator* weight_vkallocator; VkAllocator* weight_staging_vkallocator;