forked from dmlc/dgl
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Sparse] Compact C++ API (dmlc#6334)
- Loading branch information
1 parent
f00afc5
commit 52356a2
Showing
4 changed files
with
114 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/** | ||
* Copyright (c) 2023 by Contributors | ||
* @file macro.h | ||
* @brief DGL C++ sparse API macros. | ||
*/ | ||
#ifndef DGL_SPARSE_MACRO_H_ | ||
#define DGL_SPARSE_MACRO_H_ | ||
|
||
namespace dgl { | ||
namespace sparse { | ||
|
||
/** | ||
* Dispatch an operator to a templated implementation function | ||
* according to its device: | ||
* | ||
* DGL_SPARSE_XPU_SWITCH(tensor.device().type(), XPU, { | ||
* // Now XPU is a placeholder for tensor.device().type() | ||
* DeviceSpecificImplementation<XPU>(...); | ||
* }); | ||
*/ | ||
#define DGL_SPARSE_XPU_SWITCH(device, XPU, op, ...) \ | ||
do { \ | ||
if ((device) == c10::DeviceType::CPU) { \ | ||
constexpr auto XPU = c10::DeviceType::CPU; \ | ||
{ __VA_ARGS__ } \ | ||
} else { \ | ||
LOG(FATAL) << "Operator " << (op) << " does not support " \ | ||
<< c10::DeviceTypeName(device) << " device."; \ | ||
} \ | ||
} while (0) | ||
|
||
/** | ||
* Dispatch according to ID type (either int32 or int64): | ||
* | ||
* DGL_SPARSE_ID_TYPE_SWITCH(tensor.dtype(), IdType, { | ||
* // Now IdType is the type corresponding to data type of the tensor. | ||
* // For instance, one can do this for a CPU array: | ||
* IdType *data = static_cast<IdType *>(array.data_ptr()); | ||
* }); | ||
*/ | ||
#define DGL_SPARSE_ID_TYPE_SWITCH(dtype, IdType, op, ...) \ | ||
do { \ | ||
if ((dtype) == torch::kInt32) { \ | ||
typedef int32_t IdType; \ | ||
{ __VA_ARGS__ } \ | ||
} else if ((dtype) == torch::kInt64) { \ | ||
typedef int64_t IdType; \ | ||
{ __VA_ARGS__ } \ | ||
} else { \ | ||
LOG(FATAL) << "Operator " << (op) << " does not support " \ | ||
<< (dtype).name() << " as ID dtype."; \ | ||
} \ | ||
} while (0) | ||
|
||
// Macro to dispatch according to device and index type. | ||
#define DGL_SPARSE_COO_SWITCH(coo, XPU, IdType, op, ...) \ | ||
DGL_SPARSE_XPU_SWITCH(coo->indices.device().type(), XPU, op, { \ | ||
DGL_SPARSE_ID_TYPE_SWITCH( \ | ||
(coo)->indices.dtype(), IdType, op, {{__VA_ARGS__}}); \ | ||
}); | ||
|
||
} // namespace sparse | ||
} // namespace dgl | ||
|
||
#endif // DGL_SPARSE_MACRO_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters