Skip to content

[Hexagon] Host API for changing stack size#5708

Open
aankit-ca wants to merge 10 commits intohalide:mainfrom
aankit-ca:aankit/stack_size
Open

[Hexagon] Host API for changing stack size#5708
aankit-ca wants to merge 10 commits intohalide:mainfrom
aankit-ca:aankit/stack_size

Conversation

@aankit-ca
Copy link

@dsharletg @pranavb-ca Users will have to call halide_hexagon_set_thread_params with thread priority and stack size before any RPC call.

@aankit-ca aankit-ca changed the title [hexagon] Host API for setting stack size [Hexagon] Host API for changing stack size Feb 5, 2021
* @param datalen, length of data
* @retval, 0 on success
*/
extern "C" __QAIC_REMOTE_EXPORT int __QAIC_REMOTE(remote_session_control)(uint32_t req, void *data, uint32_t datalen) __QAIC_REMOTE_ATTRIBUTE;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this code just resolving to the definition in libadsprpc_shim.cpp? In that case, I think most of the stuff in this header is unnecessary (and may be wrong, e.g. dllexport stuff), we can just make a libadsprpc_shim.h and declare it there.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Corrected


debug(user_context) << "halide_hexagon_set_thread_params\n";
if (!remote_set_thread_params) {
// This runtime doesn't support changing the performance target.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

performance target -> thread params?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

__android_log_print(ANDROID_LOG_ERROR, "halide", "Error: Could not set stack_size to %d. remote_session_control returned %d.",
stack_size, retval);
}
return retval;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think maybe this should return either halide_error_code_success or halide_error_code_generic_error, I think the current code is returning a Qualcomm error code as something that will probably be interpreted as a Halide error code. The other remote runtime functions are different because we implement them (that said, I've seen qualcomm error codes returned in crash conditions, so this is a bit janky for the other functions too).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've moved this code to hexagon_host.cpp.

// This is deprecated.
typedef halide_hexagon_power_t halide_hvx_power_perf_t;

/** Set fastRPC thread priority and staksize. Set thread params before making
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stack size (in bytes?)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Updated the comment


/** Set fastRPC thread priority and staksize. Set thread params before making
* any RPC calls. halide_hexagon_set_thread_params. */
extern int halide_hexagon_set_thread_params(void *user_context, int priority, int stack_size);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this and halide_hexagon_set_thread_priority should be documented together so they can share the more detailed comment about priorities?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if halide_hexagon_set_thread_priority mentions that lower int values mean higher priority. We should mention it in the comments here regardless.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

halide_hexagon_set_thread_priority does mention about this: "Smaller number for higher priority"

typedef halide_hexagon_power_t halide_hvx_power_perf_t;

/** Set fastRPC thread priority and staksize. Set thread params before making
* any RPC calls. halide_hexagon_set_thread_params. */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove stray function name here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/staksize/stack size/


/** Set fastRPC thread priority and staksize. Set thread params before making
* any RPC calls. halide_hexagon_set_thread_params. */
extern int halide_hexagon_set_thread_params(void *user_context, int priority, int stack_size);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if halide_hexagon_set_thread_priority mentions that lower int values mean higher priority. We should mention it in the comments here regardless.

halide_hexagon_power_hvx_off(user_context);
}

WEAK int halide_hexagon_set_thread_params(void *user_context, int priority, int stack_size) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed offline just now, renaming this to halide_hexagon_set_remote_thread_params makes more sense?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Thanks

*/

#define HALIDE_RUNTIME_HEXAGON
#define FASTRPC_THREAD_PARAMS (1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This (and remote_rpc_thread_params) shouldn't be in this header, they aren't used in the Halide API. I think they should be in hexagon_host.cpp

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Also I noticed somewhere the default priority value for fastrpc threads was 192 and not 100. I'll check with @pranavb-ca once.

int stack_size; // user thread stack size, pass -1 to use default
};

/** Set the default priority/stack_size for Halide Hexagon user threads:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does set_remote_thread_params apply to all FastRPC threads, or just the calling thread? At first I thought it was the latter, but now the docs make it sound like the former...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about this. Will confirm with Pranav and get back to you.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will apply to all fastrpc threads spawned by the process and not just he calling thread.

* - Priority 0 is reserved for OS usage
* If this routine is not called, the priority will default to 100.
* This is intended to be called before dispatching any pipeline. */
* If halide_hexagon_set_thread_priority routine is not called, the priority
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aankit-ca - With PipelineContext removed halide_remote.cpp, I don't think the plumbing for halide_hexagon_set_thread_priority to get the priority set for each thread in the parallel thread pool exists any more. We should either completely remove this function or change it to use remote_session_control where available and fall back to its current approach now.(I don't know if many people use it though). We should also use the new thread_params mechanism to set the thread priority and/ or stack size.

// to set only the priority
halide_hexagon_set_remote_thread_params(NULL, 10, -1);
// to set only the stack size
halide_hexagon_set_remote_thread_params(NULL, -1, 1024 * 32);
// to set both
halide_hexagon_set_remote_thread_params(NULL, 10, 1024 * 32);

debug(user_context) << " " << result << "\n";
if (result != 0) {
error(user_context) << "set_remote_thread_params faiiled.\n";
return result;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this fastrcp error code not get conflated with a halide runtime error code?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it will, this should return -1 or some other halide error code.

@steven-johnson
Copy link
Contributor

Looks like this PR still needs some cleanup before it can land?

@alexreinking alexreinking added this to the v12.0.0 milestone Feb 13, 2021
@steven-johnson
Copy link
Contributor

Where does this PR stand?

@aankit-ca
Copy link
Author

@steven-johnson The PR needs some changes. Unfortunately my dev machine is down due to power outage since last week. I'll update the PR soon.

@pranavb-ca
Copy link
Contributor

@steven-johnson & @dsharletg - Due to the winter storm that Texas saw last week, our office where all our hardware for building and testing is saw a massive power outage. As if that wasn't enough, a sprinkler pipe burst caused flooding in our office. As such we are scrambling to bring up some testing infrastructure for our group. @aankit-ca will post an update on this PR once we have some capability restored.

@steven-johnson
Copy link
Contributor

Thanks for the update!

@alexreinking alexreinking modified the milestones: v12.0.0, v13.0.0 Apr 16, 2021
@steven-johnson
Copy link
Contributor

This PR has been sitting around for a few months now; is it still active?

@aankit-ca
Copy link
Author

@steven-johnson Sorry this PR fell through the cracks. But I think we need this PR. I'll resume working on this.

@steven-johnson
Copy link
Contributor

@steven-johnson Sorry this PR fell through the cracks. But I think we need this PR. I'll resume working on this.

Thanks for the update!

@dsharletg
Copy link
Contributor

We now have another stack usage regression causing crashes. Is this PR something that is ready to merge early this week?

debug(user_context) << "halide_hexagon_set_remote_thread_params\n";
if (!set_remote_thread_params) {
// This runtime doesn't support changing the thread params.
return -1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In other similar "optional" cases where the runtime function is missing, we return 0.

debug(user_context) << " " << result << "\n";
if (result != 0) {
error(user_context) << "set_remote_thread_params faiiled.\n";
return result;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it will, this should return -1 or some other halide error code.

@aankit-ca
Copy link
Author

We now have another stack usage regression causing crashes. Is this PR something that is ready to merge early this week?

Saw this late. I've most of the changes ready. Will put them up for review soon.

@aankit-ca
Copy link
Author

@dsharletg @pranavb-ca PTAL

@aankit-ca
Copy link
Author

@dsharletg @pranavb-ca Ping

WEAK remote_power_mode_fn remote_set_performance_mode = nullptr;
WEAK remote_thread_priority_fn remote_set_thread_priority = nullptr;
WEAK remote_set_thread_params_fn remote_set_thread_params = nullptr;
WEAK remote_session_control_fn remote_thread_session_control = nullptr;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should thread be included in the name here? According to the Hexagon SDK docs,remote_session_control can be used for more than just setting thread parameters. We may want to reuse this function for setting other non-thread parameters in the future.

In fact, I've been playing around with using "unsigned" execution mode (discussed here: #5200 (comment)). Using this branch, I was able to reuse remote_thread_session_control to enable said mode.

@alexreinking
Copy link
Member

@aankit-ca - is there any path forward to merging this functionality? How were the binary files generated? In a manner equivalent to #7741?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants