"vscode:/vscode.git/clone" did not exist on "314a75f367a0d41e158edfc2edbb54eb0a1ae235"
sddmm.cc 6.44 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
/*!
 *  Copyright (c) 2020 by Contributors
 * \file aten/cpu/sddmm.cc
 * \brief SDDMM C APIs and definitions.
 */
#include "./sddmm.h"
#include <dgl/array.h>

namespace dgl {
namespace aten {

12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#define SWITCH_RHS(rhs_target, RhsTarget, ...)                        \
  do {                                                                \
    if ((rhs_target) == 0) {                                          \
      constexpr int RhsTarget = 0;                                    \
      { __VA_ARGS__ }                                                 \
    } else if ((rhs_target) == 1) {                                   \
      constexpr int RhsTarget = 1;                                    \
      { __VA_ARGS__ }                                                 \
    } else if ((rhs_target) == 2) {                                   \
      constexpr int RhsTarget = 2;                                    \
      { __VA_ARGS__ }                                                 \
    } else {                                                          \
      LOG(INFO) << "Invalid rhs target: " << (rhs_target);            \
    }                                                                 \
  } while (0)

#define SWITCH_TARGET(lhs_target, rhs_target, LhsTarget, RhsTarget, ...)\
  do {                                                                  \
    if ((lhs_target) == 0) {                                            \
      constexpr int LhsTarget = 0;                                      \
      SWITCH_RHS(rhs_target, RhsTarget, __VA_ARGS__);                   \
    } else if ((lhs_target) == 1) {                                     \
      constexpr int LhsTarget = 1;                                      \
      SWITCH_RHS(rhs_target, RhsTarget, __VA_ARGS__);                   \
    } else if ((lhs_target) == 2) {                                     \
      constexpr int LhsTarget = 2;                                      \
      SWITCH_RHS(rhs_target, RhsTarget, __VA_ARGS__);                   \
    } else {                                                            \
      LOG(INFO) << "Invalid lhs target: " << (lhs_target);              \
    }                                                                   \
  } while (0)

44
45
46
47
48
49
50
51
52
#define SWITCH_BITS(bits, DType, ...)                           \
  do {                                                          \
    if ((bits) == 16 || (bits) == 32) {                         \
      typedef float DType;                                      \
      { __VA_ARGS__ }                                           \
    } else if ((bits) == 64) {                                  \
      typedef double DType;                                     \
      { __VA_ARGS__ }                                           \
    } else {                                                    \
53
      LOG(FATAL) << "Data type not recognized with bits " << bits; \
54
55
56
    }                                                           \
  } while (0)

57

58
/*! \brief Generalized SDDMM on Csr format. */
59
template <int XPU, typename IdType, int bits>
60
61
62
void SDDMMCsr(const std::string& op,
              const BcastOff& bcast,
              const CSRMatrix& csr,
63
64
65
66
67
              NDArray lhs,
              NDArray rhs,
              NDArray out,
              int lhs_target,
              int rhs_target) {
68
69
70
71
72
  SWITCH_BITS(bits, DType, {
    SWITCH_OP(op, Op, {
      SWITCH_TARGET(lhs_target, rhs_target, LhsTarget, RhsTarget, {
        cpu::SDDMMCsr<IdType, DType, Op, LhsTarget, RhsTarget>(bcast, csr, lhs, rhs, out);
      });
73
    });
74
75
76
  });
}

77
78
79
80
81
82
83
84
85
template void SDDMMCsr<kDLCPU, int32_t, 16>(
    const std::string& op, const BcastOff& bcast, const CSRMatrix& csr,
    NDArray lhs, NDArray rhs, NDArray out,
    int lhs_target, int rhs_target);
template void SDDMMCsr<kDLCPU, int64_t, 16>(
    const std::string& op, const BcastOff& bcast, const CSRMatrix& csr,
    NDArray lhs, NDArray rhs, NDArray out,
    int lhs_target, int rhs_target);
template void SDDMMCsr<kDLCPU, int32_t, 32>(
86
    const std::string& op, const BcastOff& bcast, const CSRMatrix& csr,
87
88
    NDArray lhs, NDArray rhs, NDArray out,
    int lhs_target, int rhs_target);
89
template void SDDMMCsr<kDLCPU, int64_t, 32>(
90
    const std::string& op, const BcastOff& bcast, const CSRMatrix& csr,
91
92
    NDArray lhs, NDArray rhs, NDArray out,
    int lhs_target, int rhs_target);
93
template void SDDMMCsr<kDLCPU, int32_t, 64>(
94
    const std::string& op, const BcastOff& bcast, const CSRMatrix& csr,
95
96
    NDArray lhs, NDArray rhs, NDArray out,
    int lhs_target, int rhs_target);
97
template void SDDMMCsr<kDLCPU, int64_t, 64>(
98
    const std::string& op, const BcastOff& bcast, const CSRMatrix& csr,
99
100
    NDArray lhs, NDArray rhs, NDArray out,
    int lhs_target, int rhs_target);
101

102

103
/*! \brief Generalized SDDMM on Coo format. */
104
template <int XPU, typename IdType, int bits>
105
106
107
void SDDMMCoo(const std::string& op,
              const BcastOff& bcast,
              const COOMatrix& coo,
108
109
110
111
112
              NDArray lhs,
              NDArray rhs,
              NDArray out,
              int lhs_target,
              int rhs_target) {
113
114
115
116
117
  SWITCH_BITS(bits, DType, {
    SWITCH_OP(op, Op, {
      SWITCH_TARGET(lhs_target, rhs_target, LhsTarget, RhsTarget, {
        cpu::SDDMMCoo<IdType, DType, Op, LhsTarget, RhsTarget>(bcast, coo, lhs, rhs, out);
      });
118
    });
119
120
121
  });
}

122
123
124
125
126
template void SDDMMCoo<kDLCPU, int32_t, 16>(
    const std::string& op, const BcastOff& bcast, const COOMatrix& coo,
    NDArray lhs, NDArray rhs, NDArray out,
    int lhs_target, int rhs_target);
template void SDDMMCoo<kDLCPU, int64_t, 16>(
127
    const std::string& op, const BcastOff& bcast, const COOMatrix& coo,
128
129
    NDArray lhs, NDArray rhs, NDArray out,
    int lhs_target, int rhs_target);
130
template void SDDMMCoo<kDLCPU, int32_t, 32>(
131
    const std::string& op, const BcastOff& bcast, const COOMatrix& coo,
132
133
    NDArray lhs, NDArray rhs, NDArray out,
    int lhs_target, int rhs_target);
134
template void SDDMMCoo<kDLCPU, int64_t, 32>(
135
    const std::string& op, const BcastOff& bcast, const COOMatrix& coo,
136
137
    NDArray lhs, NDArray rhs, NDArray out,
    int lhs_target, int rhs_target);
138
template void SDDMMCoo<kDLCPU, int32_t, 64>(
139
    const std::string& op, const BcastOff& bcast, const COOMatrix& coo,
140
141
    NDArray lhs, NDArray rhs, NDArray out,
    int lhs_target, int rhs_target);
142
143
144
145
146
template void SDDMMCoo<kDLCPU, int64_t, 64>(
    const std::string& op, const BcastOff& bcast, const COOMatrix& coo,
    NDArray lhs, NDArray rhs, NDArray out,
    int lhs_target, int rhs_target);

147
148
149

}  // namespace aten
}  // namespace dgl