Skip to content

[wasm] Add flag WASM_THREAD_POOL_SIZE #4942

Closed
axinging wants to merge 1 commit intotensorflow:masterfrom
axinging:wasm_flagthreadpoolsize
Closed

[wasm] Add flag WASM_THREAD_POOL_SIZE #4942
axinging wants to merge 1 commit intotensorflow:masterfrom
axinging:wasm_flagthreadpoolsize

Conversation

@axinging
Copy link
Copy Markdown
Contributor

@axinging axinging commented Apr 16, 2021

This flag indicates how many threads can be used in multi-thread mode, it helps us understand the performance difference between wasm and webgpu/webgl.


This change is Reviewable

@google-cla google-cla Bot added the cla: yes label Apr 16, 2021
@axinging axinging force-pushed the wasm_flagthreadpoolsize branch from 9f2bb46 to 867e5c9 Compare April 16, 2021 07:06
Copy link
Copy Markdown
Contributor

@qjia7 qjia7 left a comment

Choose a reason for hiding this comment

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

@lina128 @jinjingforever take a look, thanks.

Comment thread tfjs-backend-wasm/src/backend_wasm.ts Outdated
disposeData: module.cwrap('dispose_data', voidReturnType, ['number']),
dispose: module.cwrap('dispose', voidReturnType, []),
getThreadPoolSize:
module.cwrap('get_thread_pool_size', 'number', [voidType]),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: Does it work to change [voidType] -> [] so that you don't need to change voidType name?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, this also works.

this.wasm.tfjs.init();
// Register the used thread pool size flag. Done it here to avoid circular
// import.
env().registerFlag('WASM_THREAD_POOL_SIZE', () => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Usually, we register flag in flags_wasm.ts. And call env().set('WASM_THREAD_POOL_SIZE', this.getThreadPoolSize());. But I am not sure if it is always the rules.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Currently backend_wasm.ts deppends on flags_wasm.ts.
If we put those code in flags_wasm.ts, flags_wasm.ts will also depends on backend_wasm.ts, the test case will pass, but the yarn lint will fail due to circular imports.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Is there a good way to pass variable in .ts file to .cc? I'm not sure if this can work: move calculation logic to flags_wasm.ts, and register flag there. In backend.cc, provide a set method. Then in backend_wasm.ts, set the threads count by calling the set method.

@qjia7 qjia7 requested review from jinjingforever and lina128 April 16, 2021 08:12
@axinging axinging force-pushed the wasm_flagthreadpoolsize branch 2 times, most recently from 7b9d5c1 to d1715b4 Compare April 16, 2021 10:59
this.wasm.tfjs.init();
// Register the used thread pool size flag. Done it here to avoid circular
// import.
env().registerFlag('WASM_THREAD_POOL_SIZE', () => {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Is there a good way to pass variable in .ts file to .cc? I'm not sure if this can work: move calculation logic to flags_wasm.ts, and register flag there. In backend.cc, provide a set method. Then in backend_wasm.ts, set the threads count by calling the set method.

// Register the used thread pool size flag. Done it here to avoid circular
// import.
env().registerFlag('WASM_THREAD_POOL_SIZE', () => {
return this.getThreadPoolSize();
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actually this is not the size of pool, but count of threads in pool. According to https://github.com/Maratyszcza/pthreadpool/blob/master/include/pthreadpool.h#L77, please change the name to threads_count.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

rename done

@axinging axinging force-pushed the wasm_flagthreadpoolsize branch from f3363a7 to f574ce7 Compare April 19, 2021 06:00
@axinging axinging changed the title [wasm] Add flag WASM_THREAD_POOL_SIZE [wasm] Add flag WASM_THREADS_COUNT Apr 19, 2021
@axinging axinging force-pushed the wasm_flagthreadpoolsize branch 2 times, most recently from bd4edd8 to a30f6d3 Compare April 19, 2021 06:07
Comment thread tfjs-backend-wasm/src/cc/backend.cc Outdated
#ifdef __EMSCRIPTEN__
EMSCRIPTEN_KEEPALIVE
#endif
const size_t get_threads_count() { return backend::threads_count; }
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

FYI. There is a pthreadpool API to query the number of threads in a thread pool

https://github.com/Maratyszcza/pthreadpool/blob/master/include/pthreadpool.h#L86

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done.

@axinging axinging force-pushed the wasm_flagthreadpoolsize branch from a30f6d3 to 042c9e6 Compare April 21, 2021 02:48
@lina128 lina128 requested a review from mattsoulanille April 22, 2021 17:48
Copy link
Copy Markdown
Collaborator

@lina128 lina128 left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 1 of 1 approvals obtained (waiting on @axinging, @gyagp, @mattsoulanille, and @qjia7)


tfjs-backend-wasm/src/backend_wasm.ts, line 49 at r4 (raw file):

    // Register the used threads count flag. Done it here to avoid circular
    // import.
    env().registerFlag('WASM_THREADS_COUNT', () => {

Is this flag tunable?

@axinging axinging force-pushed the wasm_flagthreadpoolsize branch from 042c9e6 to d1715b4 Compare April 27, 2021 01:08
@axinging axinging changed the title [wasm] Add flag WASM_THREADS_COUNT [wasm] Add flag WASM_THREAD_POOL_SIZE Apr 27, 2021
@axinging axinging force-pushed the wasm_flagthreadpoolsize branch from ac8b4ad to f9b635c Compare April 27, 2021 04:10
@axinging
Copy link
Copy Markdown
Contributor Author

Reviewable status: :shipit: complete! 1 of 1 approvals obtained (waiting on @axinging, @gyagp, @mattsoulanille, and @qjia7)

tfjs-backend-wasm/src/backend_wasm.ts, line 49 at r4 (raw file):

    // Register the used threads count flag. Done it here to avoid circular
    // import.
    env().registerFlag('WASM_THREADS_COUNT', () => {

Is this flag tunable?

Current this flag is untuable. The set pthread pool size logic is not done yet.

@axinging axinging force-pushed the wasm_flagthreadpoolsize branch from f9b635c to 0717b84 Compare May 6, 2021 06:41
This flag tells how many threads can be used in multi-thread mode.
@axinging axinging force-pushed the wasm_flagthreadpoolsize branch from 0717b84 to 27db3e1 Compare May 13, 2021 08:01
@axinging
Copy link
Copy Markdown
Contributor Author

I will close this, and will provide a better solution for this.

@axinging axinging closed this May 18, 2021
@axinging axinging deleted the wasm_flagthreadpoolsize branch September 22, 2021 06:55
@axinging axinging restored the wasm_flagthreadpoolsize branch September 22, 2021 06:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants