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

Fix/separate image bin file #265

Merged
merged 2 commits into from
Apr 9, 2024
Merged
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
58 changes: 20 additions & 38 deletions src/bb/image-io/bb.h
Original file line number Diff line number Diff line change
Expand Up @@ -1112,13 +1112,11 @@ class BinarySaver : public ion::BuildingBlock<BinarySaver<T, D>> {
public:
BuildingBlockParam<std::string> output_directory_ptr{ "output_directory", "." };
BuildingBlockParam<int32_t> num_devices{"num_devices", 2};
BuildingBlockParam<std::string> prefix_ptr{"prefix", "raw-"};

Input<Halide::Func[]> input_images{ "input_images", Halide::type_of<T>(), D };

Input<Halide::Func[]> input_deviceinfo{ "input_deviceinfo", Halide::type_of<uint8_t>(), 1 };
Input<Halide::Func> input_images{"input", Halide::type_of<T>(), D};
Input<Halide::Func> input_deviceinfo{ "input_deviceinfo", Halide::type_of<uint8_t>(), 1 };
Input<Halide::Func> frame_count{ "frame_count", Halide::type_of<uint32_t>(), 1 };


Input<int32_t> width{ "width" };
Input<int32_t> height{ "height" };

Expand All @@ -1134,49 +1132,33 @@ class BinarySaver : public ion::BuildingBlock<BinarySaver<T, D>> {
output_directory_buf.fill(0);
std::memcpy(output_directory_buf.data(), output_directory.c_str(), output_directory.size());

const std::string prefix(prefix_ptr);
Halide::Buffer<uint8_t> prefix_buf(static_cast<int>(prefix.size() + 1));
prefix_buf.fill(0);
std::memcpy(prefix_buf.data(), prefix.c_str(), prefix.size());

Func fc;
fc(_) = frame_count(_);
fc.compute_root();

int32_t dim = D;
int32_t byte_depth = sizeof(T);

Buffer<uint8_t> id_buf = this->get_id();
if (num_devices==1){
Func image;
image(_) = input_images(_);
image.compute_root();

Func deviceinfo;
deviceinfo(_) = input_deviceinfo(_);
deviceinfo.compute_root();
Buffer<uint8_t> id_buf = this->get_id();

std::vector<ExternFuncArgument> params = {id_buf, image, deviceinfo, fc, width, height, dim, byte_depth, output_directory_buf };
Func ion_bb_image_io_binary_image_saver;
ion_bb_image_io_binary_image_saver.define_extern("ion_bb_image_io_binary_1image_saver", params, Int(32), 0);
ion_bb_image_io_binary_image_saver.compute_root();
output() = ion_bb_image_io_binary_image_saver();
}else if (num_devices==2){
Func image0, image1;
image0(_) = input_images[0](_);
image1(_) = input_images[1](_);
image0.compute_root();
image1.compute_root();
Func image;
image(_) = input_images(_);
image.compute_root();

Func deviceinfo0, deviceinfo1;
deviceinfo0(_) = input_deviceinfo[0](_);
deviceinfo1(_) = input_deviceinfo[1](_);
deviceinfo0.compute_root();
deviceinfo1.compute_root();
Func deviceinfo;
deviceinfo(_) = input_deviceinfo(_);
deviceinfo.compute_root();

std::vector<ExternFuncArgument> params = {id_buf, image0, image1, deviceinfo0, deviceinfo1, fc, width, height, dim, byte_depth, output_directory_buf };
Func ion_bb_image_io_binary_image_saver;
ion_bb_image_io_binary_image_saver.define_extern("ion_bb_image_io_binary_2image_saver", params, Int(32), 0);
ion_bb_image_io_binary_image_saver.compute_root();
output() = ion_bb_image_io_binary_image_saver();
}else{
std::runtime_error("device number > 2 is not supported");
}
std::vector<ExternFuncArgument> params = {id_buf, image, deviceinfo, fc, width, height, dim, byte_depth, output_directory_buf, prefix_buf };
Func ion_bb_image_io_binary_image_saver;
ion_bb_image_io_binary_image_saver.define_extern("ion_bb_image_io_binary_image_saver", params, Int(32), 0);
ion_bb_image_io_binary_image_saver.compute_root();
output(_) = ion_bb_image_io_binary_image_saver(_);

this->register_disposer("writer_dispose");
}
Expand Down
106 changes: 16 additions & 90 deletions src/bb/image-io/rt_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,11 @@ namespace {

class Writer {
public:
static Writer& get_instance(const std::string& id, std::vector<int32_t>& payload_size, const ::std::string& output_directory, bool write_framecount)
static Writer& get_instance(const std::string& id, std::vector<int32_t>& payload_size, const ::std::string& output_directory, bool write_framecount, const std::string& prefix = "raw-")
{

if (instances.count(id) == 0) {
instances[id] = std::unique_ptr<Writer>(new Writer(payload_size, output_directory, write_framecount));
instances[id] = std::unique_ptr<Writer>(new Writer(payload_size, output_directory, write_framecount, prefix ));
}
return *instances[id];
}
Expand All @@ -137,7 +137,7 @@ class Writer {

}

void post_images(std::vector<void *>& outs, std::vector<size_t>& size,
void post_image(std::vector<void *>& outs, std::vector<size_t>& size,
std::vector<ion::bb::image_io::rawHeader>& header_infos, void* framecounts)
{
if (with_header_){
Expand Down Expand Up @@ -229,8 +229,8 @@ class Writer {
}

private:
Writer(std::vector<int32_t>& payload_size, const ::std::string& output_directory, bool write_framecount)
: keep_running_(true), output_directory_(output_directory), with_header_(true), disposed_(false)
Writer(std::vector<int32_t>& payload_size, const std::string& output_directory, bool write_framecount, const std::string& prefix)
: keep_running_(true), output_directory_(output_directory), with_header_(true), disposed_(false), prefix_(prefix)
{
int total_payload_size = 0;
for (auto s : payload_size){
Expand All @@ -246,7 +246,8 @@ class Writer {
buf_queue_.push(buffers_[i].data());
}
thread_ = ::std::make_shared<::std::thread>(entry_point, this);
ofs_ = ::std::ofstream(output_directory_ / "raw-0.bin", ::std::ios::binary);
auto filename = prefix_ + std::to_string(0) + ".bin";
ofs_ = ::std::ofstream(output_directory_ / filename, ::std::ios::binary);
}

int get_buffer_num(int width, int height, int num_sensor = 2, int data_in_byte = 2) {
Expand Down Expand Up @@ -294,7 +295,7 @@ class Writer {

if (i == rotate_limit) {
i = 0;
ofs_ = ::std::ofstream(output_directory_ / ("raw-" + ::std::to_string(file_idx++) + ".bin"), ::std::ios::binary);
ofs_ = ::std::ofstream(output_directory_ / (prefix_ + ::std::to_string(file_idx++) + ".bin"), ::std::ios::binary);
}

ofs_.write(reinterpret_cast<const char*>(buffer), size);
Expand All @@ -321,7 +322,7 @@ class Writer {

if (i == rotate_limit) {
i = 0;
ofs_ = ::std::ofstream(output_directory_ / ("raw-" + ::std::to_string(file_idx++) + ".bin"), ::std::ios::binary);
ofs_ = ::std::ofstream(output_directory_ / (prefix_ + ::std::to_string(file_idx++) + ".bin"), ::std::ios::binary);
}


Expand Down Expand Up @@ -353,6 +354,7 @@ class Writer {
uint32_t width_;
uint32_t height_;
std::filesystem::path output_directory_;
std::string prefix_;
bool disposed_;

bool with_header_;
Expand Down Expand Up @@ -474,10 +476,11 @@ int ion_bb_image_io_binary_1gendc_saver( halide_buffer_t * id_buf, halide_buffer
ION_REGISTER_EXTERN(ion_bb_image_io_binary_1gendc_saver);

extern "C" ION_EXPORT
int ion_bb_image_io_binary_1image_saver(
int ion_bb_image_io_binary_image_saver(
halide_buffer_t * id_buf,
halide_buffer_t * image, halide_buffer_t * deviceinfo, halide_buffer_t * frame_count,
int width, int height, int dim, int byte_depth, halide_buffer_t* output_directory_buf,
halide_buffer_t* prefix_buf,
halide_buffer_t * out)
{
try {
Expand All @@ -486,7 +489,8 @@ int ion_bb_image_io_binary_1image_saver(
int32_t frame_size = dim == 2 ? width * height * byte_depth : width * height * 3 * byte_depth;
std::vector<int32_t>frame_size_list{frame_size};
const ::std::string output_directory(reinterpret_cast<const char*>(output_directory_buf->host));
auto& w(Writer::get_instance(id, frame_size_list, output_directory, true));
const ::std::string prefix(reinterpret_cast<const char*>(prefix_buf->host));
auto& w(Writer::get_instance(id, frame_size_list, output_directory, true, prefix));

if (image->is_bounds_query() || deviceinfo->is_bounds_query() || frame_count->is_bounds_query()) {
if (image->is_bounds_query()) {
Expand Down Expand Up @@ -518,7 +522,7 @@ int ion_bb_image_io_binary_1image_saver(

std::vector<void *> obufs{image->host};
std::vector<size_t> size_in_bytes{image->size_in_bytes()};
w.post_images(obufs, size_in_bytes, header_infos, frame_count->host);
w.post_image(obufs, size_in_bytes, header_infos, frame_count->host);
}

return 0;
Expand All @@ -533,85 +537,7 @@ int ion_bb_image_io_binary_1image_saver(
}
}

ION_REGISTER_EXTERN(ion_bb_image_io_binary_1image_saver);

extern "C" ION_EXPORT
int ion_bb_image_io_binary_2image_saver(
halide_buffer_t * id_buf,
halide_buffer_t * image0, halide_buffer_t * image1,
halide_buffer_t * deviceinfo0, halide_buffer_t * deviceinfo1, halide_buffer_t * frame_count,
int32_t width, int32_t height, int32_t dim, int byte_depth, halide_buffer_t* output_directory_buf,
halide_buffer_t * out)
{
try {
int num_output = 2;
const std::string id(reinterpret_cast<const char *>(id_buf->host));
int32_t frame_size = dim == 2 ? width * height * byte_depth : width * height * 3 * byte_depth;
std::vector<int32_t>frame_size_list{frame_size, frame_size};
const ::std::string output_directory(reinterpret_cast<const char*>(output_directory_buf->host));
auto& w(Writer::get_instance(id, frame_size_list, output_directory, true));

if (image0->is_bounds_query() || deviceinfo0->is_bounds_query() ||
image1->is_bounds_query() || deviceinfo1->is_bounds_query() || frame_count->is_bounds_query()) {
if (image0->is_bounds_query()) {
image0->dim[0].min = 0;
image0->dim[0].extent = width;
image0->dim[1].min = 0;
image0->dim[1].extent = height;
if (dim == 3){
image0->dim[2].min = 0;
image0->dim[2].extent = 3;
}
}
if (deviceinfo0->is_bounds_query()) {
deviceinfo0->dim[0].min = 0;
deviceinfo0->dim[0].extent = sizeof(ion::bb::image_io::rawHeader);
}
if (image1->is_bounds_query()) {
image1->dim[0].min = 0;
image1->dim[0].extent = width;
image1->dim[1].min = 0;
image1->dim[1].extent = height;
if (dim == 3){
image1->dim[2].min = 0;
image1->dim[2].extent = 3;
}
}
if (deviceinfo1->is_bounds_query()) {
deviceinfo1->dim[0].min = 0;
deviceinfo1->dim[0].extent = sizeof(ion::bb::image_io::rawHeader);
}
if (frame_count->is_bounds_query()) {
frame_count->dim[0].min = 0;
frame_count->dim[0].extent = num_output;
}
return 0;
}
else {

ion::bb::image_io::rawHeader header_info0, header_info1;
::memcpy(&header_info0, deviceinfo0->host, sizeof(ion::bb::image_io::rawHeader));
::memcpy(&header_info1, deviceinfo1->host, sizeof(ion::bb::image_io::rawHeader));
std::vector<ion::bb::image_io::rawHeader> header_infos{header_info0, header_info1};

std::vector<void *> obufs{image0->host, image1->host};
std::vector<size_t> size_in_bytes{image0->size_in_bytes(), image1->size_in_bytes()};
w.post_images(obufs, size_in_bytes, header_infos, frame_count->host);
}

return 0;
}
catch (const ::std::exception& e) {
::std::cerr << e.what() << ::std::endl;
return -1;
}
catch (...) {
::std::cerr << "Unknown error" << ::std::endl;
return -1;
}
}

ION_REGISTER_EXTERN(ion_bb_image_io_binary_2image_saver);
ION_REGISTER_EXTERN(ion_bb_image_io_binary_image_saver);

namespace {

Expand Down
Loading