Skip to content

Commit

Permalink
Test simulation parallel task updated.
Browse files Browse the repository at this point in the history
  • Loading branch information
Smoren committed May 13, 2024
1 parent 918d7b0 commit 0e6ece5
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 23 deletions.
13 changes: 11 additions & 2 deletions src/lib/analysis/compounds.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { AtomInterface, LinkInterface } from '../types/atomic';
import type {
Compound,
CompoundsAnalyzerInterface,
CompoundsAnalyzerSummary,
CompoundsCollectorInterface,
CompoundsSummary,
} from '../types/analysis';
Expand Down Expand Up @@ -51,7 +51,7 @@ export class CompoundsCollector implements CompoundsCollectorInterface {
}
}

export class CompoundsAnalyzer implements CompoundsAnalyzerInterface {
export class CompoundsAnalyzer implements CompoundsAnalyzerSummary {
private readonly compounds: Array<Compound>;
private readonly compoundsTypesMap: Array<Compound[]>;
private readonly atoms: Array<AtomInterface>;
Expand Down Expand Up @@ -83,6 +83,15 @@ export class CompoundsAnalyzer implements CompoundsAnalyzerInterface {
));
}

get summary(): CompoundsAnalyzerSummary {
return {
length: this.length,
lengthByTypes: this.lengthByTypes,
itemLengthSummary: this.itemLengthSummary,
itemLengthByTypesSummary: this.itemLengthByTypesSummary,
}
}

private groupCompoundsByTypes(): Array<Compound[]> {
const typesMap: Record<number, Compound[]> = {};

Expand Down
2 changes: 1 addition & 1 deletion src/lib/types/analysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export interface CompoundsCollectorInterface {
getCompounds(): Array<Compound>;
}

export interface CompoundsAnalyzerInterface {
export type CompoundsAnalyzerSummary = {
length: number;
lengthByTypes: number[];
itemLengthSummary: CompoundsSummary;
Expand Down
53 changes: 35 additions & 18 deletions src/scripts/actions/test-simulation-parallel.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
import os from 'os';
import { Pool } from "multiprocess-pool";
import type { InitialConfig, TypesConfig, WorldConfig } from '@/lib/types/config';
import { createBaseWorldConfig } from '@/lib/config/world';
import { createBaseTypesConfig } from '@/lib/config/types';
import { create2dBaseInitialConfig } from '@/lib/config/initial';

export const simulationTask = async (id: number) => {
export const simulationTask = async (
[id, worldConfig, typesConfig, initialConfig, steps]: [number, WorldConfig, TypesConfig, InitialConfig, number],
) => {
console.log(`-> task ${id} started`);
const ts = Date.now();

worldConfig.TEMPERATURE_FUNCTION = () => 1;

const dirName = __dirname.replace('/node_modules/multiprocess-pool/dist', '/src');
const { createBaseWorldConfig } = await import(`${dirName}/lib/config/world`);
const { createBaseTypesConfig } = await import(`${dirName}/lib/config/types`);
const { create2dBaseInitialConfig } = await import(`${dirName}/lib/config/initial`);
const { createPhysicModel } = await import(`${dirName}/lib/utils/functions`);
const { create2dRandomDistribution } = await import(`${dirName}/lib/config/atoms`);
const { createDummyDrawer } = await import(`${dirName}/lib/drawer/dummy`);
const { Simulation } = await import(`${dirName}/lib/simulation`);
const { Runner } = await import(`${dirName}/lib/runner`);
const { CompoundsAnalyzer } = await import(`${dirName}/lib/analysis/compounds`);

const worldConfig = createBaseWorldConfig();
const typesConfig = createBaseTypesConfig();
const initialConfig = create2dBaseInitialConfig();

const sim = new Simulation({
viewMode: '2d',
worldConfig: worldConfig,
Expand All @@ -30,27 +32,42 @@ export const simulationTask = async (id: number) => {
});

const runner = new Runner(sim);
runner.runSteps(500);

// console.log(sim.summary);
runner.runSteps(steps);

const compounds = new CompoundsAnalyzer(sim.exportCompounds(), sim.atoms);
// console.log(compounds.lengthByTypes);
// console.log(compounds.itemLengthSummary);
// console.log(compounds.itemLengthByTypesSummary);

console.log(`<- task ${id} finished in ${Date.now() - ts} ms`);

return compounds.lengthByTypes;
return {
world: sim.summary,
compounds: compounds.summary,
};
}

export const actionTestParallelSimulation = async (...args: string[]) => {
export const actionTestSimulationParallel = async (...args: string[]) => {
console.log('[START] test parallel simulation action', args);
const ts = Date.now();

const inputs = [1, 2, 3, 4, 5, 6, 7, 8];
const worldConfig = createBaseWorldConfig();
const typesConfig = createBaseTypesConfig();
const initialConfig = create2dBaseInitialConfig();
const stepsCount = 300;

const inputs = [
[1, worldConfig, typesConfig, initialConfig, stepsCount],
[2, worldConfig, typesConfig, initialConfig, stepsCount],
[3, worldConfig, typesConfig, initialConfig, stepsCount],
[4, worldConfig, typesConfig, initialConfig, stepsCount],
[5, worldConfig, typesConfig, initialConfig, stepsCount],
[6, worldConfig, typesConfig, initialConfig, stepsCount],
[7, worldConfig, typesConfig, initialConfig, stepsCount],
[8, worldConfig, typesConfig, initialConfig, stepsCount],
];

const cpuCount = os.cpus().length;
console.log('CPUs:', cpuCount);

const pool = new Pool(20);
const pool = new Pool(cpuCount);
const result = await pool.map(inputs, simulationTask);
console.log(result);

Expand Down
4 changes: 2 additions & 2 deletions src/scripts/tool.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { createRouter } from '@/scripts/lib/helpers';
import { actionTestSimulation } from '@/scripts/actions/test-simulation';
import { actionTestParallel } from "@/scripts/actions/test-parallel";
import { actionTestParallelSimulation } from "@/scripts/actions/test-simulation-parallel";
import { actionTestSimulationParallel } from "@/scripts/actions/test-simulation-parallel";

const router = createRouter();

router.add('test-simulation', actionTestSimulation);
router.add('test-parallel', actionTestParallel);
router.add('test-parallel-simulation', actionTestParallelSimulation);
router.add('test-simulation-parallel', actionTestSimulationParallel);

router.run(process.argv);

0 comments on commit 0e6ece5

Please sign in to comment.