-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Make HALIDE_REGISTER_GENERATOR work with multiple template args #6556
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
cb60e9d
Make HALIDE_REGISTER_GENERATOR work with multiple template args
abadams 046eb7e
Add missing generator to cmakefile
abadams a403376
Can't put parens around this usage
abadams cae2cc6
Another attempt at stripping parens from a type
abadams ad489f3
Merge branch 'master' into abadams/templated_generator_macro
steven-johnson 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#include "HalideBuffer.h" | ||
#include "HalideRuntime.h" | ||
|
||
#include <math.h> | ||
#include <stdio.h> | ||
|
||
#include "templated.h" | ||
|
||
using namespace Halide::Runtime; | ||
|
||
int main(int argc, char **argv) { | ||
const int kSize = 1024; | ||
|
||
Buffer<double> output(kSize, kSize); | ||
Buffer<float> input(kSize, kSize); | ||
|
||
input.fill(17.0f); | ||
|
||
templated(input, output); | ||
|
||
auto check = [&](float val, float input_val) { | ||
if (val != input_val + 2) { | ||
printf("Output value was %f instead of %f\n", | ||
val, input_val + 2); | ||
exit(-1); | ||
} | ||
}; | ||
|
||
output.for_each_value(check, input); | ||
|
||
printf("Success!\n"); | ||
return 0; | ||
} |
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,35 @@ | ||
#include "Halide.h" | ||
|
||
using namespace Halide; | ||
|
||
// Define a templated generator. Normally this is a bad idea, and your template | ||
// parameters (e.g. the type of the input) should be GeneratorParams | ||
// instead. Sometimes, however, it's more convenient to have the C++ type | ||
// available as a template parameter. Or maybe you want to template a Generator | ||
// on something not expressible as a GeneratorParam. Or maybe you have a | ||
// deficient build system and it's difficult to specify GeneratorParams in the | ||
// build (note that HALIDE_REGISTER_GENERATOR_ALIAS also exists for this | ||
// purpose). | ||
template<typename T1, typename T2> | ||
class Templated : public Generator<Templated<T1, T2>> { | ||
public: | ||
// A major downside of templated generators is that because we use CRTP, you | ||
// must manually import names of types from the base class. For Input and | ||
// Output you can also just use these equivalent globally-scoped names: | ||
GeneratorInput<Buffer<T1>> input{"input", 2}; | ||
GeneratorOutput<Buffer<T2>> output{"output", 2}; | ||
|
||
void generate() { | ||
Var x, y; | ||
output(x, y) = cast<T2>(input(x, y) + (T1)2); | ||
|
||
// Again, due to CRTP, we must manually tell C++ how to look up a method | ||
// from the base class using this->template. | ||
output.vectorize(x, this->template natural_vector_size<T2>()); | ||
} | ||
}; | ||
|
||
// To pass a comma-separated template parameter list to a macro, we must enclose | ||
// the type argument in parentheses. | ||
HALIDE_REGISTER_GENERATOR((Templated<float, double>), templated) | ||
HALIDE_REGISTER_GENERATOR((Templated<uint8_t, uint16_t>), templated_uint8) |
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.
I helped someone with this exact issue on StackOverflow not too long ago... https://stackoverflow.com/a/70071173/2137996
Maybe it would be helpful to document an example of this somewhere?