Skip to content

Commit

Permalink
add memmory slider
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscoBSalgueiro committed Nov 16, 2023
1 parent dffecea commit d4564fe
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 28 deletions.
25 changes: 25 additions & 0 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ log = "0.4.20"
oauth2 = "4.4.2"
axum = "0.6.20"
tar = "0.4.40"
sysinfo = "0.29.10"

[features]
# by default Tauri runs in production mode
Expand Down
5 changes: 5 additions & 0 deletions src-tauri/src/chess.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ impl EngineProcess {
if options.multipv != self.options.multipv {
self.set_option("MultiPV", &options.multipv).await?;
}
if options.hash != self.options.hash {
self.set_option("Hash", options.hash).await?;
}

for option in &options.extra_options {
if !self.options.extra_options.contains(option) {
Expand Down Expand Up @@ -298,6 +301,7 @@ async fn send_command(stdin: &mut ChildStdin, command: impl AsRef<str>) {
pub struct EngineOptions {
pub multipv: u16,
pub threads: u16,
pub hash: u16,
#[derivative(Default(value = "Fen::default().to_string()"))]
pub fen: String,
pub extra_options: Vec<EngineOption>,
Expand Down Expand Up @@ -498,6 +502,7 @@ pub async fn analyze_game(
.set_options(EngineOptions {
threads: 4,
multipv: 1,
hash: 16,
fen: fen.to_string(),
extra_options: Vec::new(),
})
Expand Down
11 changes: 10 additions & 1 deletion src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ mod error;
mod fide;
mod fs;
mod lexer;
mod oauth;
mod opening;
mod pgn;
mod puzzle;
mod oauth;

use std::path::PathBuf;
use std::sync::{Arc, Mutex};
Expand All @@ -25,6 +25,7 @@ use derivative::Derivative;
use fide::FidePlayer;
use log::LevelFilter;
use oauth::AuthState;
use sysinfo::SystemExt;
use tauri::{
api::path::{resolve_path, BaseDirectory},
Manager, Window,
Expand Down Expand Up @@ -116,6 +117,7 @@ fn main() {
stop_engine,
kill_engines,
get_engine_logs,
memory_size
))
.events(tauri_specta::collect_events!(BestMovesPayload));

Expand Down Expand Up @@ -214,3 +216,10 @@ fn is_bmi2_compatible() -> bool {
}
false
}

#[tauri::command]
#[specta::specta]
fn memory_size() -> u32 {
let total_bytes = sysinfo::System::new_all().total_memory();
return (total_bytes / 1024 / 1024) as u32;
}
15 changes: 8 additions & 7 deletions src/atoms/atoms.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GoMode } from "@/bindings";
import { EngineOptions, GoMode } from "@/bindings";
import { Card, buildFromTree } from "@/components/files/opening";
import { LocalOptions } from "@/components/panels/database/DatabasePanel";
import { DatabaseInfo } from "@/utils/db";
Expand Down Expand Up @@ -241,9 +241,7 @@ export const deckAtomFamily = atomFamily(
export type EngineSettings = {
enabled: boolean;
go: GoMode;
cores: number;
numberLines: number;
extraOptions: { name: string, value: string }[];
options: Omit<EngineOptions, "fen">;
};

export const tabEngineSettingsFamily = atomFamily(
Expand All @@ -259,9 +257,12 @@ export const tabEngineSettingsFamily = atomFamily(
t: "Depth",
c: 24,
},
cores: 2,
numberLines: 3,
extraOptions: [],
options: {
threads: 2,
multipv: 3,
hash: 16,
extraOptions: [],
}
})
},
(a, b) => a.tab === b.tab && a.engine === b.engine
Expand Down
5 changes: 4 additions & 1 deletion src/bindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ try {
if(e instanceof Error) throw e;
else return { status: "error", error: e as any };
}
},
async memorySize() : Promise<number> {
return await TAURI_INVOKE("plugin:tauri-specta|memory_size");
}
}

Expand All @@ -66,7 +69,7 @@ export type AnalysisOptions = { fen: string; annotateNovelties: boolean; referen
export type BestMoves = { depth: number; score: Score; uciMoves: string[]; sanMoves: string[]; multipv: number; nps: number }
export type BestMovesPayload = { bestLines: BestMoves[]; engine: string; tab: string }
export type EngineOption = { name: string; value: string }
export type EngineOptions = { multipv: number; threads: number; fen: string; extraOptions: EngineOption[] }
export type EngineOptions = { multipv: number; threads: number; hash: number; fen: string; extraOptions: EngineOption[] }
export type GoMode = { t: "Depth"; c: number } | { t: "Time"; c: number } | { t: "Nodes"; c: number } | { t: "Infinite" }
export type Score = { type: "cp"; value: number } | { type: "mate"; value: number }

Expand Down
12 changes: 6 additions & 6 deletions src/components/panels/analysis/BestMoves.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,10 @@ export default function BestMovesComponent({
commands
.getBestMoves(engine.path, activeTab!, settings.go, {
fen: threat ? swapMove(fen) : fen,
multipv: settings.numberLines,
threads: settings.cores,
extraOptions: settings.extraOptions,
multipv: settings.options.multipv,
hash: settings.options.hash,
threads: settings.options.threads,
extraOptions: settings.options.extraOptions,
})
.then((res) => {
unwrap(res);
Expand All @@ -149,9 +150,8 @@ export default function BestMovesComponent({
50,
[
settings.enabled,
settings.cores,
settings.options,
settings.go,
settings.numberLines,
threat,
fen,
]
Expand Down Expand Up @@ -271,7 +271,7 @@ export default function BestMovesComponent({
</tr>}
{!isGameOver && engineVariations.length === 0 &&
(settings.enabled ? (
[...Array(settings.numberLines)].map((_, i) => (
[...Array(settings.options.multipv)].map((_, i) => (
<tr key={i}>
<td>
<Skeleton height={35} radius="xl" p={5} />
Expand Down
47 changes: 34 additions & 13 deletions src/components/panels/analysis/EngineSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import CoresSlide from "./CoresSlider";
import DepthSlider from "./DepthSlider";
import LinesSlider from "./LinesSlider";
import { IconPlus, IconX } from "@tabler/icons-react";
import HashSlider from "./HashSlider";

interface EngineSettingsProps {
engine: string;
Expand Down Expand Up @@ -48,9 +49,9 @@ function EngineSettings({
Number of Lines
</Text>
<LinesSlider
value={settings.numberLines}
value={settings.options.multipv}
setValue={(v) =>
setSettings((prev) => ({ ...prev, numberLines: v }))
setSettings((prev) => ({ ...prev, options: { ...prev.options, multipv: v } }))
}
/>
{settings.go.t === "Infinite" || settings.go.t === "Depth" ? (
Expand Down Expand Up @@ -90,8 +91,16 @@ function EngineSettings({
Number of cores
</Text>
<CoresSlide
value={settings.cores}
setValue={(v) => setSettings((prev) => ({ ...prev, cores: v }))}
value={settings.options.threads}
setValue={(v) => setSettings((prev) => ({ ...prev, options: { ...prev.options, threads: v } }))}
/>

<Text size="sm" fw="bold">
Size of Hash
</Text>
<HashSlider
value={settings.options.hash}
setValue={(v) => setSettings((prev) => ({ ...prev, options: { ...prev.options, hash: v } }))}
/>
</SimpleGrid>

Expand Down Expand Up @@ -186,9 +195,9 @@ function AdvancedOptions({
<td>
<NumberInput
min={1}
value={settings.cores}
value={settings.options.threads}
onChange={(v) =>
setSettings((prev) => ({ ...prev, cores: v || 1 }))
setSettings((prev) => ({ ...prev, options: { ...prev.options, threads: v || 1 } }))
}
/>
</td>
Expand All @@ -198,20 +207,32 @@ function AdvancedOptions({
<td>
<NumberInput
min={1}
value={settings.numberLines}
value={settings.options.multipv}
onChange={(v) =>
setSettings((prev) => ({ ...prev, options: { ...prev.options, multipv: v || 1 } }))
}
/>
</td>
</tr>
<tr>
<td>Hash Size</td>
<td>
<NumberInput
min={1}
value={settings.options.hash}
onChange={(v) =>
setSettings((prev) => ({ ...prev, numberLines: v || 1 }))
setSettings((prev) => ({ ...prev, options: { ...prev.options, hash: v || 1 } }))
}
/>
</td>
</tr>
{settings.extraOptions.map((option, i) => (
{settings.options.extraOptions.map((option, i) => (
<tr key={i}>
<td>
<TextInput
value={option.name}
onChange={(e) => {
const newOptions = settings.extraOptions;
const newOptions = settings.options.extraOptions;
newOptions[i].name = e.currentTarget.value;
setSettings((prev) => ({
...prev,
Expand All @@ -225,7 +246,7 @@ function AdvancedOptions({
<TextInput
value={option.value}
onChange={(e) => {
const newOptions = settings.extraOptions;
const newOptions = settings.options.extraOptions;
newOptions[i].value = e.currentTarget.value;
setSettings((prev) => ({
...prev,
Expand All @@ -236,7 +257,7 @@ function AdvancedOptions({
{/* Remove button */}
<ActionIcon
onClick={() => {
const newOptions = settings.extraOptions;
const newOptions = settings.options.extraOptions;
newOptions.splice(i, 1);
setSettings((prev) => ({
...prev,
Expand All @@ -259,7 +280,7 @@ function AdvancedOptions({
variant="default"
size="xs"
onClick={() => {
const newOptions = settings.extraOptions;
const newOptions = settings.options.extraOptions;
newOptions.push({ name: "", value: "" });
setSettings((prev) => ({
...prev,
Expand Down
36 changes: 36 additions & 0 deletions src/components/panels/analysis/HashSlider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { commands } from "@/bindings";
import { formatBytes } from "@/utils/format";
import { Slider } from "@mantine/core";
import { useEffect, useState } from "react";
import useSWRImmutable from "swr/immutable";

export default function HashSlider({
value,
setValue,
}: {
value: number;
setValue: (v: number) => void;
}) {
const [tempValue, setTempValue] = useState(Math.log2(value));

useEffect(() => {
setTempValue(Math.log2(value));
}, [value]);

const { data: memorySize } = useSWRImmutable("memory", async () => {
return await commands.memorySize() / 2;
});

return (
<>
<Slider
min={0}
max={Math.log2(memorySize || 16)}
value={tempValue}
onChange={setTempValue}
onChangeEnd={(v) => setValue(2 ** v)}
label={(v) => formatBytes((2 ** v) * 1024 * 1024)}
/>
</>
);
}

0 comments on commit d4564fe

Please sign in to comment.