summaryrefslogtreecommitdiff
path: root/src/caffe/util
diff options
context:
space:
mode:
authorMariusz Moczala <mariusz.moczala@intel.com>2016-01-20 09:28:35 +0100
committerMariusz Moczala <mariusz.moczala@intel.com>2016-01-20 09:28:35 +0100
commit581c1cc3fd6c04640c4b89e5ed003a40cd67e855 (patch)
tree63e74f8a09201e668c3da62403abf843ff9eac98 /src/caffe/util
parentcff6f3d997616fa1201923fbfde77d24d0d395ad (diff)
downloadcaffeonacl-581c1cc3fd6c04640c4b89e5ed003a40cd67e855.tar.gz
caffeonacl-581c1cc3fd6c04640c4b89e5ed003a40cd67e855.tar.bz2
caffeonacl-581c1cc3fd6c04640c4b89e5ed003a40cd67e855.zip
Performance related update of im2col() and col2im() functions
Diffstat (limited to 'src/caffe/util')
-rw-r--r--src/caffe/util/im2col.cpp93
1 files changed, 61 insertions, 32 deletions
diff --git a/src/caffe/util/im2col.cpp b/src/caffe/util/im2col.cpp
index 6e5ea875..114a86cb 100644
--- a/src/caffe/util/im2col.cpp
+++ b/src/caffe/util/im2col.cpp
@@ -5,6 +5,16 @@
namespace caffe {
+// Function uses casting from int to unsigned to compare if value of
+// parameter a is greater or equal to zero and lower than value of
+// parameter b. The b parameter is of type signed and is always positive,
+// therefore its value is always lower than 0x800... where casting
+// negative value of a parameter converts it to value higher than 0x800...
+// The casting allows to use one condition instead of two.
+inline bool is_a_ge_zero_and_a_lt_b(int a, int b) {
+ return static_cast<unsigned>(a) < static_cast<unsigned>(b);
+}
+
template <typename Dtype>
void im2col_cpu(const Dtype* data_im, const int channels,
const int height, const int width, const int kernel_h, const int kernel_w,
@@ -12,22 +22,33 @@ void im2col_cpu(const Dtype* data_im, const int channels,
const int stride_h, const int stride_w,
const int dilation_h, const int dilation_w,
Dtype* data_col) {
- const int height_col = (height + 2 * pad_h -
- (dilation_h * (kernel_h - 1) + 1)) / stride_h + 1;
- const int width_col = (width + 2 * pad_w -
- (dilation_w * (kernel_w - 1) + 1)) / stride_w + 1;
- const int channels_col = channels * kernel_h * kernel_w;
- for (int c_col = 0; c_col < channels_col; ++c_col) {
- int w_offset = c_col % kernel_w;
- int h_offset = (c_col / kernel_w) % kernel_h;
- int c_im = c_col / kernel_h / kernel_w;
- for (int h_col = 0; h_col < height_col; ++h_col) {
- for (int w_col = 0; w_col < width_col; ++w_col) {
- int h_im = h_col * stride_h - pad_h + h_offset * dilation_h;
- int w_im = w_col * stride_w - pad_w + w_offset * dilation_w;
- data_col[(c_col * height_col + h_col) * width_col + w_col] =
- (h_im >= 0 && w_im >= 0 && h_im < height && w_im < width) ?
- data_im[(c_im * height + h_im) * width + w_im] : 0;
+ const int output_h = (height + 2 * pad_h -
+ (dilation_h * (kernel_h - 1) + 1)) / stride_h + 1;
+ const int output_w = (width + 2 * pad_w -
+ (dilation_w * (kernel_w - 1) + 1)) / stride_w + 1;
+ const int channel_size = height * width;
+ for (int channel = channels; channel--; data_im += channel_size) {
+ for (int kernel_row = 0; kernel_row < kernel_h; kernel_row++) {
+ for (int kernel_col = 0; kernel_col < kernel_w; kernel_col++) {
+ int input_row = -pad_h + kernel_row * dilation_h;
+ for (int output_rows = output_h; output_rows; output_rows--) {
+ if (!is_a_ge_zero_and_a_lt_b(input_row, height)) {
+ for (int output_cols = output_w; output_cols; output_cols--) {
+ *(data_col++) = 0;
+ }
+ } else {
+ int input_col = -pad_w + kernel_col * dilation_w;
+ for (int output_col = output_w; output_col; output_col--) {
+ if (is_a_ge_zero_and_a_lt_b(input_col, width)) {
+ *(data_col++) = data_im[input_row * width + input_col];
+ } else {
+ *(data_col++) = 0;
+ }
+ input_col += stride_w;
+ }
+ }
+ input_row += stride_h;
+ }
}
}
}
@@ -146,22 +167,30 @@ void col2im_cpu(const Dtype* data_col, const int channels,
const int dilation_h, const int dilation_w,
Dtype* data_im) {
caffe_set(height * width * channels, Dtype(0), data_im);
- const int height_col = (height + 2 * pad_h -
- (dilation_h * (kernel_h - 1) + 1)) / stride_h + 1;
- const int width_col = (width + 2 * pad_w -
- (dilation_w * (kernel_w - 1) + 1)) / stride_w + 1;
- const int channels_col = channels * kernel_h * kernel_w;
- for (int c_col = 0; c_col < channels_col; ++c_col) {
- int w_offset = c_col % kernel_w;
- int h_offset = (c_col / kernel_w) % kernel_h;
- int c_im = c_col / kernel_h / kernel_w;
- for (int h_col = 0; h_col < height_col; ++h_col) {
- for (int w_col = 0; w_col < width_col; ++w_col) {
- int h_im = h_col * stride_h - pad_h + h_offset * dilation_h;
- int w_im = w_col * stride_w - pad_w + w_offset * dilation_w;
- if (h_im >= 0 && h_im < height && w_im >= 0 && w_im < width)
- data_im[(c_im * height + h_im) * width + w_im] +=
- data_col[(c_col * height_col + h_col) * width_col + w_col];
+ const int output_h = (height + 2 * pad_h -
+ (dilation_h * (kernel_h - 1) + 1)) / stride_h + 1;
+ const int output_w = (width + 2 * pad_w -
+ (dilation_w * (kernel_w - 1) + 1)) / stride_w + 1;
+ const int channel_size = height * width;
+ for (int channel = channels; channel--; data_im += channel_size) {
+ for (int kernel_row = 0; kernel_row < kernel_h; kernel_row++) {
+ for (int kernel_col = 0; kernel_col < kernel_w; kernel_col++) {
+ int input_row = -pad_h + kernel_row * dilation_h;
+ for (int output_rows = output_h; output_rows; output_rows--) {
+ if (!is_a_ge_zero_and_a_lt_b(input_row, height)) {
+ data_col += output_w;
+ } else {
+ int input_col = -pad_w + kernel_col * dilation_w;
+ for (int output_col = output_w; output_col; output_col--) {
+ if (is_a_ge_zero_and_a_lt_b(input_col, width)) {
+ data_im[input_row * width + input_col] += *data_col;
+ }
+ data_col++;
+ input_col += stride_w;
+ }
+ }
+ input_row += stride_h;
+ }
}
}
}