MKLDNNMatrix.h 2.5 KB
Newer Older
T
tensor-tang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* Copyright (c) 2017 PaddlePaddle Authors. All Rights Reserve.

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. */

#pragma once

T
tensor-tang 已提交
17 18
#include <vector>
#include "Matrix.h"
T
tensor-tang 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
#include "mkldnn.hpp"
#include "paddle/parameter/Parameter.h"

namespace paddle {

static const std::map<mkldnn::memory::format, PARAM_FORMAT> PARAM_FOARMAT_MAP =
    {{mkldnn::memory::format::oi, PARAM_FORMAT_MKLDNN_OI}};

class MKLDNNMatrix;
typedef std::shared_ptr<MKLDNNMatrix> MKLDNNMatrixPtr;

/**
 * @brief MKLDNN Matrix.
 *
 */
T
tensor-tang 已提交
34
class MKLDNNMatrix : public CpuMatrix, public mkldnn::memory {
T
tensor-tang 已提交
35
public:
T
tensor-tang 已提交
36 37 38 39 40
  MKLDNNMatrix(real* data,
               size_t height,
               size_t width,
               mkldnn::memory::primitive_desc pd)
      : CpuMatrix(data, height, width, false), mkldnn::memory(pd, data) {}
T
tensor-tang 已提交
41

T
tensor-tang 已提交
42 43 44 45 46
  MKLDNNMatrix(size_t height, size_t width, mkldnn::memory::primitive_desc pd)
      : CpuMatrix(height, width, false), mkldnn::memory(pd) {
    set_data_handle(CpuMatrix::getData());
  }

T
tensor-tang 已提交
47 48
  ~MKLDNNMatrix() {}

T
tensor-tang 已提交
49 50 51 52 53 54 55 56
  static MKLDNNMatrixPtr create(
      const MatrixPtr& m,
      mkldnn::memory::dims dims,
      mkldnn::memory::format fmt,
      mkldnn::engine& eg,
      mkldnn::memory::data_type dtype = mkldnn::memory::data_type::f32);

  /**
T
tensor-tang 已提交
57
   * Get primitive descriptor.
T
tensor-tang 已提交
58 59
   */
  mkldnn::memory::primitive_desc getPD() { return this->get_primitive_desc(); }
T
tensor-tang 已提交
60

T
tensor-tang 已提交
61
  /**
T
tensor-tang 已提交
62
   * Get memory descriptor.
T
tensor-tang 已提交
63 64 65 66
   */
  mkldnn::memory::desc getMD() { return getPD().desc(); }

  /**
T
tensor-tang 已提交
67
   * Get dims.
T
tensor-tang 已提交
68
   */
T
tensor-tang 已提交
69 70 71 72 73 74 75 76 77 78
  mkldnn::memory::dims getDims() {
    mkldnn::memory::dims dst;
    int* src = getMD().data.dims;
    int ndims = getMD().data.ndims;
    dst.resize(ndims);
    for (int i = 0; i < ndims; ++i) {
      dst[i] = src[i];
    }
    return dst;
  }
T
tensor-tang 已提交
79

T
tensor-tang 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92
  /**
   * Get format.
   */
  mkldnn::memory::format getFormat() {
    return (mkldnn::memory::format)(getMD().data.format);
  }

  /**
   * Update the memory data handle.
   * Caution: This will not check the buffer size of the data,
   *          it should be coverd by user.
   */
  void updateData(void* data) { set_data_handle(data); }
T
tensor-tang 已提交
93 94 95
};

}  // namespace paddle