Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed caching logic for interpolation #449

Merged
merged 1 commit into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import { ProjectProperties } from "."
import { numberSocket, numericDataSocket } from "../socket_types"
import { workerPool } from '../../../modelling/workerPool'
import { maskFromExtentAndShape } from "../bounding_box"
import { NumericTileGrid } from "../tile_grid"
import { NumericTileGrid, TileGridJSON } from "../tile_grid"
import { SelectControl, SelectControlOptions } from "../controls/select"
import { InterpolationType } from "../../../modelling/worker/interpolation"
import { NumericConstant } from "../numeric_constant"
import { isEqual } from "lodash"

interface InterpolationMethodOption extends SelectControlOptions {
value: InterpolationType
Expand All @@ -32,16 +33,21 @@ export class InterpolationComponent extends BaseComponent {
maxdist : number
p : number
closest_points : number
cache : Map<number, Map<NumericTileGrid, NumericTileGrid>>
cachedInputs: NumericTileGrid[]
cachedOutputs: Map<string, NumericTileGrid>


constructor(projectProps : ProjectProperties) {
super("Interpolation")
this.category = "Calculations"
this.projectProps = projectProps
this.cachedInputs = []
this.cachedOutputs = new Map()

// default values
this.maxdist = 50
this.p = 2
this.closest_points = 10
this.cache = new Map()
}

async builder(node: Node) {
Expand Down Expand Up @@ -76,27 +82,29 @@ export class InterpolationComponent extends BaseComponent {
const maxDist = inputs['maxdist'].length > 0 ? (inputs['maxdist'][0] as NumericConstant).value : this.maxdist
const p = inputs['p'].length > 0 ? (inputs['p'][0] as NumericConstant).value : this.p
const closest_points = inputs['closest_points'].length > 0 ? (inputs['closest_points'][0] as NumericConstant).value : this.closest_points

const input = inputs['input'][0] as NumericTileGrid

// TODO: Caching doesn't work
if(this.cache.has(maxDist)){

if(this.cache.get(maxDist)?.has(input)){
let cacheIdx = this.cachedInputs.findIndex(cachedInput => isEqual(cachedInput.getData(), input.getData()))

outputs['output'] = this.cache.get(maxDist)?.get(input)
if(cacheIdx === -1) {
this.cachedInputs.push(input)
cacheIdx = this.cachedInputs.length - 1
}else{
const code = `${cacheIdx}_${method.value}_${maxDist}_${p}_${closest_points}`
if(this.cachedOutputs.has(code)){
outputs['output'] = this.cachedOutputs.get(code)
return
}
}

const out = await workerPool.queue(async worker =>
worker.interpolateGrid(inputs['input'][0], mask, method.value, maxDist, p, closest_points)
)

const map = this.cache.get(maxDist) || new Map()
map.set(input, out)
this.cache.set(maxDist, map)

outputs['output'] = out
if(outputs['output'] === undefined) {
const out = await workerPool.queue(async worker =>
worker.interpolateGrid(input, mask, method.value, maxDist, p, closest_points)
)
this.cachedOutputs.set(`${cacheIdx}_${method.value}_${maxDist}_${p}_${closest_points}`, out)
outputs['output'] = out
}
}

}
4 changes: 4 additions & 0 deletions app/javascript/projects/modelling/tile_grid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,10 @@ export class NumericTileGrid extends TileGrid {
this.minMax = null
}

getData(): Float32Array {
return this.data
}

iterate(callback: (x: number, y: number, value: number) => void) {
const { x, y, width, height } = this
for (let i = x; i < x + width; i++) {
Expand Down
Loading