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

Load balancing nvJPEG work #217

Merged
merged 1 commit into from
Oct 15, 2018
Merged
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
18 changes: 14 additions & 4 deletions dali/pipeline/operators/decoder/nvjpeg_decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
#include <array>
#include <map>
#include <vector>
#include <algorithm>
#include <utility>
#include <functional>

#include "dali/pipeline/operators/operator.h"
#include "dali/pipeline/util/thread_pool.h"
Expand Down Expand Up @@ -154,6 +157,7 @@ class nvJPEGDecoder : public Operator<MixedBackend> {

// Get dimensions
int idx_in_batch = 0;
std::vector<std::pair<size_t, size_t>> image_order(batch_size_);
for (int i = 0; i < batch_size_; ++i) {
auto& in = ws->Input<CPUBackend>(0, i);
auto in_size = in.size();
Expand Down Expand Up @@ -193,6 +197,7 @@ class nvJPEGDecoder : public Operator<MixedBackend> {
const int image_depth = (output_type_ == DALI_GRAY) ? 1 : 3;
output_shape_[i] = Dims({info.heights[0], info.widths[0], image_depth});
output_info_[i] = info;
image_order[i] = std::make_pair(Product(output_shape_[i]), i);
}

// Resize the output (contiguous)
Expand Down Expand Up @@ -256,14 +261,19 @@ class nvJPEGDecoder : public Operator<MixedBackend> {
batched_output_.data(),
streams_[0]));
} else {
// Set the order of images so the largest are processed first
// (for load balancing)
std::sort(image_order.begin(), image_order.end(),
std::greater<std::pair<size_t, size_t>>());
// Loop over images again and decode
for (int i = 0; i < batch_size_; ++i) {
auto& in = ws->Input<CPUBackend>(0, i);
size_t j = image_order[i].second;
auto& in = ws->Input<CPUBackend>(0, j);
auto in_size = in.size();
const auto *data = in.data<uint8_t>();
auto *output_data = output->mutable_tensor<uint8_t>(i);
auto *output_data = output->mutable_tensor<uint8_t>(j);

auto info = output_info_[i];
auto info = output_info_[j];

thread_pool_.DoWorkWithID(std::bind(
[this, info, data, in_size, output_data](int idx, int tid) {
Expand All @@ -276,7 +286,7 @@ class nvJPEGDecoder : public Operator<MixedBackend> {
data, in_size,
output_data,
streams_[stream_idx]);
}, i, std::placeholders::_1));
}, j, std::placeholders::_1));
}
// Make sure work is finished being submitted
thread_pool_.WaitForWork();
Expand Down