Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
practicing function overloading
  • Loading branch information
crcrpar committed Jun 23, 2022
commit 4409a33bde13634def04e21eee972441b5a619ae
57 changes: 40 additions & 17 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,34 @@
#include <cstdint>
#include <iostream>

enum class ReduceOp : uint8_t {
SUM = 0,
PRODUCT,
MIN,
MAX,
BAND, // Bitwise AND
BOR, // Bitwise OR
BXOR, // Bitwise XOR
UNUSED,
struct _SupplementBase {};

struct NcclPreMulSumSupplement : _SupplementBase {};

struct ReduceOp {
enum Kind : uint8_t {
SUM = 0,
PRODUCT,
MIN,
MAX,
BAND, // Bitwise AND
BOR, // Bitwise OR
BXOR, // Bitwise XOR
UNUSED,
};

ReduceOp() = delete;
ReduceOp(Kind op) : op{op} {}

Kind op;
};

void func(const ReduceOp a, const ReduceOp b) {
return;
}

void func(const ReduceOp::Kind a, const ReduceOp::Kind b) { return; }

#define STRINGIFY(x) #x
#define MACRO_STRINGIFY(x) STRINGIFY(x)

Expand All @@ -36,24 +49,34 @@ PYBIND11_MODULE(python_example, m) {
ReduceOp
)pbdoc";

m.def("func", &func, R"pbdoc(
// Reference: https://pybind11.readthedocs.io/en/stable/classes.html#overloaded-methods
m.def("func", py::overload_cast<const ReduceOp, const ReduceOp>(&func), R"pbdoc(
Takes two ReduceOp
)pbdoc");
m.def("func", py::overload_cast<const ReduceOp::Kind, const ReduceOp::Kind>(&func), R"pbdoc(
Takes two ReduceOp
)pbdoc");

// m.def("func", static_cast<void ()(const ReduceOp::Kind, const ReduceOp::Kind)>(&func), R"pbdoc(
// Takes two ReduceOp
// )pbdoc");

py::class_<ReduceOp> reduce_op(m, "ReduceOp");

reduce_op.def(
py::init<ReduceOp::Kind>()
).def_readonly("op", &ReduceOp::op);

py::enum_<ReduceOp>(m, "ReduceOp", R"pbdoc(
An enum-like class for available reduction operations: ``SUM``, ``PRODUCT``,
``MIN``, ``MAX``, ``BAND``, ``BOR``, and ``BXOR``.
The values of this class can be accessed as attributes, e.g., ``ReduceOp.SUM``.
They are used in specifying strategies for reduction collectives, e.g.,
:func:`reduce`, :func:`all_reduce_multigpu`, etc.)pbdoc")
py::enum_<ReduceOp::Kind>(reduce_op, "Kind")
.value("SUM", ReduceOp::SUM)
.value("PRODUCT", ReduceOp::PRODUCT)
.value("MIN", ReduceOp::MIN)
.value("MAX", ReduceOp::MAX)
.value("BAND", ReduceOp::BAND)
.value("BOR", ReduceOp::BOR)
.value("BXOR", ReduceOp::BXOR)
.value("UNUSED", ReduceOp::UNUSED);
.value("UNUSED", ReduceOp::UNUSED)
.export_values();

#ifdef VERSION_INFO
m.attr("__version__") = MACRO_STRINGIFY(VERSION_INFO);
Expand Down
3 changes: 3 additions & 0 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
class SimpleTest(unittest.TestCase):

def test_func(self):
m.func(m.ReduceOp(m.ReduceOp.SUM), m.ReduceOp(m.ReduceOp.UNUSED))

def test_func_auto_call_ctor(self):
m.func(m.ReduceOp.SUM, m.ReduceOp.UNUSED)


Expand Down