-
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
[CC] [autodiff] Support AdStack on C backend #1752
Changes from all commits
257cfb2
3ebf2ca
6a83777
4143a3e
cadd0aa
bdc1559
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -83,6 +83,50 @@ static inline Ti_f32 Ti_rand_f32(void) { | |
return (Ti_f32) drand48(); // [0.0, 1.0) | ||
} | ||
|
||
// Copied from Metal: | ||
typedef Ti_u8 *Ti_AdStackPtr; | ||
|
||
static inline Ti_u32 *Ti_ad_stack_n(Ti_AdStackPtr stack) { | ||
return (Ti_u32 *)stack; | ||
} | ||
|
||
static inline Ti_AdStackPtr Ti_ad_stack_data(Ti_AdStackPtr stack) { | ||
return stack + sizeof(Ti_u32); | ||
} | ||
|
||
static inline void Ti_ad_stack_init(Ti_AdStackPtr stack) { | ||
Ti_u32 *n = Ti_ad_stack_n(stack); | ||
Ti_i32 *data = (Ti_i32 *)Ti_ad_stack_data(stack); | ||
*n = 0; | ||
} | ||
|
||
static inline Ti_AdStackPtr Ti_ad_stack_top_primal(Ti_AdStackPtr stack, | ||
Ti_u32 element_size) { | ||
Ti_u32 *n = Ti_ad_stack_n(stack); | ||
return Ti_ad_stack_data(stack) + (*n - 1) * 2 * element_size; | ||
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. I copied from 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. I think when std::stack<int> s;
s.top(); // runtime error 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. Exactly, this is just |
||
} | ||
|
||
static inline Ti_AdStackPtr Ti_ad_stack_top_adjoint(Ti_AdStackPtr stack, | ||
Ti_u32 element_size) { | ||
return Ti_ad_stack_top_primal(stack, element_size) + element_size; | ||
} | ||
|
||
static inline void Ti_ad_stack_pop(Ti_AdStackPtr stack) { | ||
Ti_u32 *n = Ti_ad_stack_n(stack); | ||
--(*n); | ||
} | ||
|
||
static inline void Ti_ad_stack_push(Ti_AdStackPtr stack, Ti_u32 element_size) { | ||
Ti_u32 i; | ||
Ti_u32 *n = Ti_ad_stack_n(stack); | ||
++(*n); | ||
|
||
Ti_AdStackPtr data = Ti_ad_stack_top_primal(stack, element_size); | ||
for (i = 0; i < element_size * 2; ++i) { | ||
data[i] = 0; | ||
} | ||
} | ||
|
||
) "\n" STR( | ||
) | ||
|
||
|
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.
Well... I would suggest not using a capital letter as the beginning of a function's name. I haven't taken a look at the C backend before, so is there any reason that the
Ti_
prefix is used? I would suggestcc_
prefix to show that it's the C backend (and probablyCc
for classes).