Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

init image at creation time #720

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,12 @@ using the name of the corresponding environment variable.
can be a work-around when having issues with clang compiling in the
application thread.

* `CLVK_INIT_IMAGE_AT_CREATION` force to initialize OpenCL images created with
`CL_MEM_COPY_HOST_PTR` or `CL_MEM_USE_HOST_PTR` at creation time instead of
initializing them during first use of the image (default: false). It reduces
the memory footprint as clvk needs to keep a buffer with the data to
initialize at first use.

# Limitations

* Only one device per CL context
Expand Down
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ endif()
add_library(OpenCL-objects OBJECT
api.cpp
config.cpp
context.cpp
device.cpp
device_properties.cpp
event.cpp
Expand Down
2 changes: 2 additions & 0 deletions src/config.def
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ OPTION(uint32_t, opencl_version, (uint32_t)CL_MAKE_VERSION(3, 0, 0))

OPTION(bool, build_in_separate_thread, false)

OPTION(bool, init_image_at_creation, false)

#if COMPILER_AVAILABLE
OPTION(std::string, clspv_options, "")
#if !CLSPV_ONLINE_COMPILER
Expand Down
32 changes: 32 additions & 0 deletions src/context.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2024 The clvk authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "context.hpp"
#include "queue.hpp"

cvk_command_queue* cvk_context::get_or_create_image_init_command_queue() {
if (m_queue_image_init != nullptr) {
return m_queue_image_init;
}
std::vector<cl_queue_properties> properties_array;
m_queue_image_init =
new cvk_command_queue(this, m_device, 0, std::move(properties_array));
cl_int ret = m_queue_image_init->init();
if (ret != CL_SUCCESS) {
return nullptr;
}
return m_queue_image_init;
}

void cvk_context::free_image_init_command_queue() { delete m_queue_image_init; }
8 changes: 8 additions & 0 deletions src/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ struct cvk_context_callback {
void* data;
};

struct cvk_command_queue;

struct cvk_context : public _cl_context,
refcounted,
object_magic_header<object_magic::context> {
Expand Down Expand Up @@ -75,6 +77,7 @@ struct cvk_context : public _cl_context,
auto cb = *cbi;
cb.pointer(this, cb.data);
}
free_image_init_command_queue();
}

const std::vector<cl_context_properties>& properties() const {
Expand Down Expand Up @@ -112,6 +115,9 @@ struct cvk_context : public _cl_context,
cvk_printf_callback_t get_printf_callback() { return m_printf_callback; }
void* get_printf_userdata() { return m_user_data; }

cvk_command_queue* get_or_create_image_init_command_queue();
void free_image_init_command_queue();

private:
cvk_device* m_device;
std::mutex m_callbacks_lock;
Expand All @@ -120,6 +126,8 @@ struct cvk_context : public _cl_context,
size_t m_printf_buffersize;
cvk_printf_callback_t m_printf_callback;
void* m_user_data;

cvk_command_queue* m_queue_image_init = nullptr;
};

static inline cvk_context* icd_downcast(cl_context context) {
Expand Down
18 changes: 18 additions & 0 deletions src/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,24 @@ bool cvk_image::init_vulkan_image() {
"Could not copy image host_ptr data to the staging buffer");
return false;
}

if (config.init_image_at_creation()) {
auto queue = m_context->get_or_create_image_init_command_queue();
if (queue == nullptr) {
return false;
}

auto initimage = new cvk_command_image_init(queue, this);
ret = queue->enqueue_command_with_deps(initimage, 0, nullptr,
nullptr);
if (ret != CL_SUCCESS) {
return false;
}
ret = queue->finish();
if (ret != CL_SUCCESS) {
return false;
}
}
}

return true;
Expand Down
4 changes: 4 additions & 0 deletions src/queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ cl_int cvk_command_queue::satisfy_data_dependencies(cvk_command* cmd) {
continue;
}
CVK_ASSERT(mem->is_image_type());
if (config.init_image_at_creation()) {
tracker.set_state(cvk_mem_init_state::completed);
continue;
}
auto initcmd =
new cvk_command_image_init(this, static_cast<cvk_image*>(mem));
_cl_event* initev;
Expand Down