MulOpGpu.cu 5.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* Copyright (c) 2016 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. */

#include "hl_base.h"
#include "MulOp.h"
#include "paddle/math/Matrix.h"
#include "paddle/math/SparseMatrix.h"

namespace paddle {
21
/// dense matrix (+)= dense matrix * dense matrix
22 23 24 25
template <>
void MulOp<DEVICE_TYPE_GPU>(GpuMatrix& out,
                            const GpuMatrix& a,
                            const GpuMatrix& b,
X
xutianbing 已提交
26
                            real scaleAB,
X
xutianbing 已提交
27 28 29 30 31
                            real scaleT,
                            bool aTrans,
                            bool bTrans,
                            bool cTrans) {
  CHECK(a.useGpu_ && b.useGpu_) << "matrix device type not match";
32
  hl_matrix_mul(const_cast<real*>(a.getData()),
X
xutianbing 已提交
33
                !aTrans ? HPPL_OP_N : HPPL_OP_T,
34
                const_cast<real*>(b.getData()),
X
xutianbing 已提交
35
                !bTrans ? HPPL_OP_N : HPPL_OP_T,
36
                const_cast<real*>(out.getData()),
X
xutianbing 已提交
37 38
                out.getHeight(),
                out.getWidth(),
X
xutianbing 已提交
39
                !aTrans ? a.getWidth() : a.getHeight(),
X
xutianbing 已提交
40 41 42 43 44
                scaleAB,
                scaleT,
                a.getStride(),
                b.getStride(),
                out.getStride());
45 46
}

47
/// dense matrix (+)= sparse matrix * dense matrix
48 49 50 51
template <>
void MulOp<DEVICE_TYPE_GPU>(GpuMatrix& out,
                            const GpuSparseMatrix& a,
                            const GpuMatrix& b,
X
xutianbing 已提交
52
                            real scaleAB,
X
xutianbing 已提交
53 54 55 56
                            real scaleT,
                            bool aTrans,
                            bool bTrans,
                            bool cTrans) {
57 58
  CHECK(out.isContiguous());
  CHECK(b.isContiguous());
X
xutianbing 已提交
59
  CHECK(a.useGpu_ && b.useGpu_) << "matrix device type not match";
60
  hl_matrix_csr_mul_dense(a.sMatrix_.get(),
X
xutianbing 已提交
61
                          aTrans ? HPPL_OP_T : HPPL_OP_N,
62
                          const_cast<real*>(b.getData()),
63
                          HPPL_OP_N,
64
                          const_cast<real*>(out.getData()),
X
xutianbing 已提交
65 66 67 68 69
                          out.getHeight(),
                          out.getWidth(),
                          b.getHeight(),
                          scaleAB,
                          scaleT);
70 71
}

72
/// dense matrix (+)= dense matrix * sparse matrix
73 74 75 76
template <>
void MulOp<DEVICE_TYPE_GPU>(GpuMatrix& out,
                            const GpuMatrix& a,
                            const GpuSparseMatrix& b,
X
xutianbing 已提交
77
                            real scaleAB,
X
xutianbing 已提交
78 79 80 81
                            real scaleT,
                            bool aTrans,
                            bool bTrans,
                            bool cTrans) {
82 83
  CHECK(out.isContiguous());
  CHECK(a.isContiguous());
X
xutianbing 已提交
84
  CHECK(a.useGpu_ && b.useGpu_) << "matrix device type not match";
X
xutianbing 已提交
85

86
  if (b.format_ == SPARSE_CSC) {
87
    hl_matrix_dense_mul_csc(const_cast<real*>(a.getData()),
88
                            HPPL_OP_N,
89
                            b.sMatrix_.get(),
X
xutianbing 已提交
90
                            bTrans ? HPPL_OP_T : HPPL_OP_N,
91
                            const_cast<real*>(out.getData()),
X
xutianbing 已提交
92 93 94 95 96
                            out.getHeight(),
                            out.getWidth(),
                            a.getWidth(),
                            scaleAB,
                            scaleT);
97
  } else {
98
    hl_matrix_dense_mul_csr(const_cast<real*>(a.getData()),
99
                            HPPL_OP_N,
100
                            b.sMatrix_.get(),
X
xutianbing 已提交
101
                            bTrans ? HPPL_OP_T : HPPL_OP_N,
102
                            const_cast<real*>(out.getData()),
X
xutianbing 已提交
103 104 105 106 107
                            out.getHeight(),
                            out.getWidth(),
                            a.getWidth(),
                            scaleAB,
                            scaleT);
108 109 110
  }
}

111
/// sparse matrix (+)= dense matrix * dense matrix
112 113 114 115
template <>
void MulOp<DEVICE_TYPE_GPU>(GpuSparseMatrix& out,
                            const GpuMatrix& a,
                            const GpuMatrix& b,
X
xutianbing 已提交
116
                            real scaleAB,
X
xutianbing 已提交
117 118 119 120
                            real scaleT,
                            bool aTrans,
                            bool bTrans,
                            bool cTrans) {
X
xutianbing 已提交
121
  CHECK(a.useGpu_ && b.useGpu_) << "matrix device type not match";
122
  hl_sparse_matrix_mul(const_cast<real*>(a.getData()),
X
xutianbing 已提交
123
                       aTrans ? HPPL_OP_T : HPPL_OP_N,
124
                       const_cast<real*>(b.getData()),
X
xutianbing 已提交
125
                       bTrans ? HPPL_OP_T : HPPL_OP_N,
126
                       out.sMatrix_.get(),
X
xutianbing 已提交
127 128 129 130 131
                       out.getHeight(),
                       out.getWidth(),
                       !bTrans ? b.getHeight() : b.getWidth(),
                       scaleAB,
                       scaleT);
132 133
}

134
}  // namespace paddle