Skip to content

navigator.hardwareConcurrency and native pthread gets different thread pool size #4995

@axinging

Description

@axinging

(On Intel i7-9700)
By navigator.hardwareConcurrency: #4994, the WASM_THREAD_POOL_SIZE is 4. WASM_THREAD_POOL_SIZE is got by:

Math.min(4, Math.max(1, (navigator.hardwareConcurrency || 1) / 2));

By native pthread: #4942, the WASM_THREAD_POOL_SIZE is 1. WASM_THREAD_POOL_SIZE is got by:

// emscripten_num_logical_cores corresponds to navigator.hardwareConcurrency.
// Many x86-64 processors have 2 threads per core, so we are dividing by 2.
#ifdef __EMSCRIPTEN_PTHREADS__
int num_cores = emscripten_num_logical_cores() / 2;
#else
int num_cores = 1;
#endif

int min_num_threads = 1;
int max_num_threads = 4;
int thread_pool_size =  // thread_pool_size is WASM_THREAD_POOL_SIZE
    std::min(std::max(num_cores, min_num_threads), max_num_threads);

Does this possible due to #ifdef EMSCRIPTEN_PTHREADS?

BTW, code has rebased, and #4957 is included.

Reproduce steps:

  1. Use local wasm:
+++ b/e2e/benchmarks/local-benchmark/index.html
@@ -91,7 +91,7 @@ limitations under the License.
   <script src="https://unpkg.com/@tensorflow/tfjs-backend-webgl@latest/dist/tf-backend-webgl.js"></script>
   <script src="https://unpkg.com/@tensorflow/tfjs-layers@latest/dist/tf-layers.js"></script>
   <script src="https://unpkg.com/@tensorflow/tfjs-converter@latest/dist/tf-converter.js"></script>
-  <script src="https://unpkg.com/@tensorflow/tfjs-backend-wasm@latest/dist/tf-backend-wasm.js"></script>
+  <script src="../../../tfjs-backend-wasm/dist/tf-backend-wasm.js"></script>
  1. Build:
    cd tfjs-backend-wasm
    tfjs-backend-wasm$ yarn & yarn build-npm
    cd ../e2e
    e2e$ yarn build-deps
  2. Serup server
    cd ../../
    npx http-server
  3. Open http://127.0.0.1:8080/tfjs/e2e/benchmarks/local-benchmark/

Metadata

Metadata

Assignees

No one assigned

    Labels

    type:bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions