Skip to content

Commit

Permalink
feat(web): limit max number of threads to 8
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan-aksamentov committed May 10, 2022
1 parent f500f98 commit afae90e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
20 changes: 10 additions & 10 deletions packages_rs/nextclade-web/src/components/Layout/SettingsButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,14 @@ export function SettingsButton() {
const initialValues = useMemo(() => ({ numThreads }), [numThreads])
const onReset = useCallback(() => ({ numThreads }), [numThreads])

const memoryAvailable = useMemo(() => {
return guess.memoryAvailable ? prettyBytes.format(guess.memoryAvailable) : t('unsupported')
}, [guess.memoryAvailable, t])

const memoryAvailablePerThread = useMemo(() => {
return guess.memoryAvailable ? prettyBytes.format(guess.memoryAvailable / numThreads) : t('unsupported')
}, [guess.memoryAvailable, numThreads, t])

return (
<>
<ButtonSettingsBase type="button" onClick={toggleOpen} title={text}>
Expand Down Expand Up @@ -202,20 +210,12 @@ export function SettingsButton() {
<tbody>
<tr>
<td>{t('Memory available*')}</td>
<td>
{guess.memoryAvailable
? prettyBytes.format(guess.memoryAvailable)
: t('unsupported')}
</td>
<td>{memoryAvailable}</td>
</tr>

<tr>
<td>{t('Memory per CPU thread')}</td>
<td>
{guess.memoryAvailable
? prettyBytes.format(guess.memoryAvailable / numThreads)
: t('unsupported')}
</td>
<td>{memoryAvailablePerThread}</td>
</tr>

<tr>
Expand Down
10 changes: 6 additions & 4 deletions packages_rs/nextclade-web/src/helpers/getNumThreads.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { clamp } from 'lodash'
import { useEffect, useState } from 'react'

export const DEFAULT_NUM_THREADS = 4
export const MINIMUM_NUM_THREADS = 2
export const MAXIMUM_NUM_THREADS = 8
export const MEMORY_BYTES_PER_THREAD_MINIMUM = 200 * 1024 * 1024

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
Expand All @@ -26,11 +28,11 @@ export function getMemoryBytesAvailable(): number | undefined {
return undefined
}

export function guessNumThreads(numThreadsBase: number | undefined) {
export function guessNumThreads() {
const memoryBytesAvailable = getMemoryBytesAvailable()
if (memoryBytesAvailable && Number.isFinite(memoryBytesAvailable)) {
const numThreadsMax = Math.floor(memoryBytesAvailable / MEMORY_BYTES_PER_THREAD_MINIMUM)
const numThreads = Math.max(numThreadsMax, MINIMUM_NUM_THREADS)
const numThreads = clamp(numThreadsMax, MINIMUM_NUM_THREADS, MAXIMUM_NUM_THREADS)
return { memoryAvailable: memoryBytesAvailable, numThreads }
}
return undefined
Expand All @@ -45,7 +47,7 @@ export function getNumThreads() {
numThreads = Math.max(numThreads, DEFAULT_NUM_THREADS)

// Detect how much memory is available and adjust number of threads if per-thread memory is too low
const guess = guessNumThreads(numThreads)
const guess = guessNumThreads()
if (guess?.numThreads) {
numThreads = Math.min(numThreads, guess.numThreads)
}
Expand All @@ -59,7 +61,7 @@ export function useGuessNumThreads(numThreadsBase: number | undefined) {

useEffect(() => {
const timer = setInterval(() => {
const guess = guessNumThreads(numThreads)
const guess = guessNumThreads()
if (guess?.numThreads && guess?.memoryAvailable) {
setNumThreads((numThreads) => (numThreads ? Math.min(numThreads, guess.numThreads) : guess.numThreads))
setMemoryAvailable(guess.memoryAvailable)
Expand Down
4 changes: 2 additions & 2 deletions packages_rs/nextclade-web/src/state/settings.state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { mapValues, sum } from 'lodash'
import { atom, selector } from 'recoil'

import { GENE_OPTION_NUC_SEQUENCE } from 'src/constants'
import { getNumThreads } from 'src/helpers/getNumThreads'
import { getNumThreads, guessNumThreads } from 'src/helpers/getNumThreads'
import { COLUMN_WIDTHS, DYNAMIC_COLUMN_WIDTH } from 'src/components/Results/ResultsTableStyle'
import { cladeNodeAttrKeysAtom } from 'src/state/results.state'
import { persistAtom } from 'src/state/persist/localStorage'
Expand All @@ -14,7 +14,7 @@ export const isInitializedAtom = atom<boolean>({

export const numThreadsAtom = atom<number>({
key: 'numThreads',
default: getNumThreads(),
default: guessNumThreads()?.numThreads ?? getNumThreads(),
effects: [persistAtom],
})

Expand Down

0 comments on commit afae90e

Please sign in to comment.