-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[type] [refactor] Promote DataType to a class #1906
Merged
Merged
Changes from 12 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
8c67501
Promote DataType to a class
yuanming-hu 55f9452
pass most tests
yuanming-hu 12bb841
DataType as a handle
yuanming-hu 0cf2ab5
fix comparison
yuanming-hu c8518de
fix tests
yuanming-hu 0c88cba
format
yuanming-hu 3ab42da
class Type
yuanming-hu cc2f9ce
clean
yuanming-hu 9fcf7e1
fix pickling of DataType
TH3CHARLie a71d55d
use existing functions to create datatype-int mapping
TH3CHARLie d88b9d8
format
yuanming-hu 133238a
remove unused get_primitive_type_node
yuanming-hu 2a0e9ce
apply review suggestions
yuanming-hu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
// Definitions of utility functions and enums | ||
|
||
#include "lang_util.h" | ||
#include "taichi/lang_util.h" | ||
|
||
#include "taichi/math/linalg.h" | ||
#include "taichi/program/arch.h" | ||
|
@@ -29,6 +29,37 @@ real get_cpu_frequency() { | |
|
||
real default_measurement_time = 1; | ||
|
||
// Note: these primitive types should never be freed. They are supposed to live | ||
// together with the process. | ||
#define PER_TYPE(x) \ | ||
DataType DataType::x = \ | ||
DataType(new PrimitiveType(PrimitiveType::primitive_type::x)); | ||
#include "taichi/inc/data_type.inc.h" | ||
#undef PER_TYPE | ||
|
||
DataType PrimitiveType::get(PrimitiveType::primitive_type t) { | ||
if (false) { | ||
} | ||
#define PER_TYPE(x) else if (t == primitive_type::x) return DataType::x; | ||
k-ye marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#include "taichi/inc/data_type.inc.h" | ||
#undef PER_TYPE | ||
else { | ||
TI_NOT_IMPLEMENTED | ||
} | ||
} | ||
|
||
DataType::operator std::size_t() const { | ||
if (auto primitive = dynamic_cast<const PrimitiveType *>(ptr_)) { | ||
return (std::size_t)primitive->type; | ||
} else { | ||
TI_NOT_IMPLEMENTED | ||
} | ||
} | ||
|
||
std::string PrimitiveType::serialize() const { | ||
return data_type_name(DataType(this)); | ||
} | ||
|
||
real measure_cpe(std::function<void()> target, | ||
int64 elements_per_call, | ||
real time_second) { | ||
|
@@ -65,30 +96,26 @@ real measure_cpe(std::function<void()> target, | |
} | ||
|
||
std::string data_type_name(DataType t) { | ||
switch (t) { | ||
#define REGISTER_DATA_TYPE(i, j) \ | ||
case DataType::i: \ | ||
return #j; | ||
|
||
REGISTER_DATA_TYPE(f16, float16); | ||
REGISTER_DATA_TYPE(f32, float32); | ||
REGISTER_DATA_TYPE(f64, float64); | ||
REGISTER_DATA_TYPE(u1, int1); | ||
REGISTER_DATA_TYPE(i8, int8); | ||
REGISTER_DATA_TYPE(i16, int16); | ||
REGISTER_DATA_TYPE(i32, int32); | ||
REGISTER_DATA_TYPE(i64, int64); | ||
REGISTER_DATA_TYPE(u8, uint8); | ||
REGISTER_DATA_TYPE(u16, uint16); | ||
REGISTER_DATA_TYPE(u32, uint32); | ||
REGISTER_DATA_TYPE(u64, uint64); | ||
REGISTER_DATA_TYPE(gen, generic); | ||
REGISTER_DATA_TYPE(unknown, unknown); | ||
#define REGISTER_DATA_TYPE(i, j) else if (t == DataType::i) return #j | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, switch-or-if nits can be put iapr. |
||
if (false) { | ||
} | ||
REGISTER_DATA_TYPE(f16, float16); | ||
REGISTER_DATA_TYPE(f32, float32); | ||
REGISTER_DATA_TYPE(f64, float64); | ||
REGISTER_DATA_TYPE(u1, int1); | ||
REGISTER_DATA_TYPE(i8, int8); | ||
REGISTER_DATA_TYPE(i16, int16); | ||
REGISTER_DATA_TYPE(i32, int32); | ||
REGISTER_DATA_TYPE(i64, int64); | ||
REGISTER_DATA_TYPE(u8, uint8); | ||
REGISTER_DATA_TYPE(u16, uint16); | ||
REGISTER_DATA_TYPE(u32, uint32); | ||
REGISTER_DATA_TYPE(u64, uint64); | ||
REGISTER_DATA_TYPE(gen, generic); | ||
REGISTER_DATA_TYPE(unknown, unknown); | ||
|
||
#undef REGISTER_DATA_TYPE | ||
default: | ||
TI_NOT_IMPLEMENTED | ||
} | ||
else TI_NOT_IMPLEMENTED | ||
} | ||
|
||
std::string data_type_format(DataType dt) { | ||
|
@@ -110,48 +137,42 @@ std::string data_type_format(DataType dt) { | |
} | ||
|
||
int data_type_size(DataType t) { | ||
switch (t) { | ||
case DataType::f16: | ||
return 2; | ||
case DataType::gen: | ||
return 0; | ||
case DataType::unknown: | ||
return -1; | ||
|
||
#define REGISTER_DATA_TYPE(i, j) \ | ||
case DataType::i: \ | ||
return sizeof(j); | ||
|
||
REGISTER_DATA_TYPE(f32, float32); | ||
REGISTER_DATA_TYPE(f64, float64); | ||
REGISTER_DATA_TYPE(i8, int8); | ||
REGISTER_DATA_TYPE(i16, int16); | ||
REGISTER_DATA_TYPE(i32, int32); | ||
REGISTER_DATA_TYPE(i64, int64); | ||
REGISTER_DATA_TYPE(u8, uint8); | ||
REGISTER_DATA_TYPE(u16, uint16); | ||
REGISTER_DATA_TYPE(u32, uint32); | ||
REGISTER_DATA_TYPE(u64, uint64); | ||
if (false) { | ||
} else if (t == DataType::f16) | ||
return 2; | ||
else if (t == DataType::gen) | ||
return 0; | ||
else if (t == DataType::unknown) | ||
return -1; | ||
|
||
#define REGISTER_DATA_TYPE(i, j) else if (t == DataType::i) return sizeof(j) | ||
|
||
REGISTER_DATA_TYPE(f32, float32); | ||
REGISTER_DATA_TYPE(f64, float64); | ||
REGISTER_DATA_TYPE(i8, int8); | ||
REGISTER_DATA_TYPE(i16, int16); | ||
REGISTER_DATA_TYPE(i32, int32); | ||
REGISTER_DATA_TYPE(i64, int64); | ||
REGISTER_DATA_TYPE(u8, uint8); | ||
REGISTER_DATA_TYPE(u16, uint16); | ||
REGISTER_DATA_TYPE(u32, uint32); | ||
REGISTER_DATA_TYPE(u64, uint64); | ||
|
||
#undef REGISTER_DATA_TYPE | ||
default: | ||
TI_NOT_IMPLEMENTED | ||
else { | ||
TI_NOT_IMPLEMENTED | ||
} | ||
} | ||
|
||
std::string data_type_short_name(DataType t) { | ||
switch (t) { | ||
#define PER_TYPE(i) \ | ||
case DataType::i: \ | ||
return #i; | ||
|
||
if (false) { | ||
} | ||
#define PER_TYPE(i) else if (t == DataType::i) return #i; | ||
#include "taichi/inc/data_type.inc.h" | ||
|
||
#undef PER_TYPE | ||
default: | ||
TI_NOT_IMPLEMENTED | ||
} | ||
} | ||
else | ||
TI_NOT_IMPLEMENTED | ||
} // namespace lang | ||
|
||
std::string snode_type_name(SNodeType t) { | ||
switch (t) { | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please don't include these OFT nits in an already-error-prone PR, they increased review difficulty.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While I agree off-the-topic changes should be mostly avoided, note that this change is not really off the topic. The old switch-case implementation no longer works after refactoring, and the modifications are necessary for the build to pass.
The same for other places.