From 7eb92547a22c71701d3eb66bcbd7fe427bfc7905 Mon Sep 17 00:00:00 2001 From: Jakub Kowalski Date: Fri, 29 Jan 2021 08:32:30 +0100 Subject: [PATCH 1/6] typescript 3.8 required --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index d5cb3fcaaf..8d3215a8be 100644 --- a/package.json +++ b/package.json @@ -131,7 +131,7 @@ "ts-node": "^8.0.1", "typedoc": "^0.17.3", "typedoc-plugin-markdown": "^2.2.17", - "typescript": "^3.2", + "typescript": "^3.8", "vuepress": "^1.3.1", "vuepress-plugin-clean-urls": "^1.1.1", "weak-napi": "^2.0.2", From ec82a133d434eefc0bba9f92c8865fd127dcf045 Mon Sep 17 00:00:00 2001 From: Jakub Kowalski Date: Fri, 29 Jan 2021 08:38:23 +0100 Subject: [PATCH 2/6] gpu.js constructor passed to config --- src/Config.ts | 19 ++++++++++------- src/interpreter/Interpreter.ts | 29 +++++++++++++------------- src/interpreter/plugin/MatrixPlugin.ts | 2 +- test/interpreter/matrix-plugin.spec.ts | 5 +++-- 4 files changed, 31 insertions(+), 24 deletions(-) diff --git a/src/Config.ts b/src/Config.ts index 6ecadcc1a6..9ea906051a 100644 --- a/src/Config.ts +++ b/src/Config.ts @@ -20,11 +20,11 @@ import {Maybe} from './Maybe' import {ParserConfig} from './parser/ParserConfig' import {checkLicenseKeyValidity, LicenseKeyValidityState} from './helpers/licenseKeyValidator' import {FunctionPluginDefinition} from './interpreter' -import GPU from 'gpu.js' +import type { GPU } from 'gpu.js' -type GPUMode = 'gpu' | 'cpu' | 'dev' | 'fallback' +type GPUMode = 'gpu' | 'cpu' | 'dev' -const PossibleGPUModeString: GPUMode[] = ['gpu', 'cpu', 'dev', 'fallback'] +const PossibleGPUModeString: GPUMode[] = ['gpu', 'cpu', 'dev'] export interface ConfigParams { /** @@ -145,6 +145,10 @@ export interface ConfigParams { */ // eslint-disable-next-line @typescript-eslint/no-explicit-any functionPlugins: any[], + /** + * TODO + */ + gpujs?: typeof GPU, /** * Allows to set GPU or CPU for use in matrix calculations. * When set to 'gpu' it will try to use GPU for matrix calculations. Setting it to 'cpu' will force CPU usage. @@ -390,6 +394,7 @@ export class Config implements ConfigParams, ParserConfig { language: 'enGB', licenseKey: '', functionPlugins: [], + gpujs: undefined, gpuMode: 'gpu', leapYear1900: false, smartRounding: true, @@ -442,6 +447,8 @@ export class Config implements ConfigParams, ParserConfig { // eslint-disable-next-line @typescript-eslint/no-explicit-any public readonly functionPlugins: FunctionPluginDefinition[] /** @inheritDoc */ + public readonly gpujs?: typeof GPU + /** @inheritDoc */ public readonly gpuMode: GPUMode /** @inheritDoc */ public readonly leapYear1900: boolean @@ -530,6 +537,7 @@ export class Config implements ConfigParams, ParserConfig { language, licenseKey, functionPlugins, + gpujs, gpuMode, ignorePunctuation, leapYear1900, @@ -572,6 +580,7 @@ export class Config implements ConfigParams, ParserConfig { this.thousandSeparator = this.valueFromParam(thousandSeparator, ['', ',', ' ', '.'], 'thousandSeparator') this.localeLang = this.valueFromParam(localeLang, 'string', 'localeLang') this.functionPlugins = functionPlugins ?? Config.defaultConfig.functionPlugins + this.gpujs = gpujs ?? Config.defaultConfig.gpujs this.gpuMode = this.valueFromParam(gpuMode, PossibleGPUModeString, 'gpuMode') this.smartRounding = this.valueFromParam(smartRounding, 'boolean', 'smartRounding') this.matrixDetection = this.valueFromParam(matrixDetection, 'boolean', 'matrixDetection') @@ -615,10 +624,6 @@ export class Config implements ConfigParams, ParserConfig { {value: this.functionArgSeparator, name: 'functionArgSeparator'}, {value: this.thousandSeparator, name: 'thousandSeparator'} ) - - if (GPU === undefined) { - this.gpuMode = 'fallback' - } } public getConfig(): ConfigParams { //TODO: avoid pollution diff --git a/src/interpreter/Interpreter.ts b/src/interpreter/Interpreter.ts index ee24f7571b..82f3615c43 100644 --- a/src/interpreter/Interpreter.ts +++ b/src/interpreter/Interpreter.ts @@ -3,7 +3,6 @@ * Copyright (c) 2020 Handsoncode. All rights reserved. */ -import GPU from 'gpu.js' import {AbsoluteCellRange, AbsoluteColumnRange, AbsoluteRowRange} from '../AbsoluteCellRange' import { CellError, @@ -31,9 +30,10 @@ import {ArithmeticHelper, coerceScalarToString, fixNegativeZero, isNumberOverflo import {CriterionBuilder} from './Criterion' import {FunctionRegistry} from './FunctionRegistry' import {InterpreterValue, SimpleRangeValue} from './InterpreterValue' +import type {GPU} from 'gpu.js' export class Interpreter { - private gpu?: GPU.GPU + private gpu?: GPU public readonly arithmeticHelper: ArithmeticHelper public readonly criterionBuilder: CriterionBuilder @@ -64,6 +64,7 @@ export class Interpreter { } return wrapperForAddress(val, formulaAddress) } + /** * Calculates cell value from formula abstract syntax tree * @@ -183,8 +184,8 @@ export class Interpreter { const result = this.evaluateAst(ast.value, formulaAddress) if (result instanceof SimpleRangeValue) { return new CellError(ErrorType.VALUE, ErrorMessage.ScalarExpected) - } else if(typeof result === 'number') { - return this.arithmeticHelper.unaryPlus(result) + } else if (typeof result === 'number') { + return this.arithmeticHelper.unaryPlus(result) } else { return result } @@ -208,7 +209,7 @@ export class Interpreter { } } case AstNodeType.FUNCTION_CALL: { - if(this.config.licenseKeyValidityState !== LicenseKeyValidityState.VALID && !FunctionRegistry.functionIsProtected(ast.procedureName)) { + if (this.config.licenseKeyValidityState !== LicenseKeyValidityState.VALID && !FunctionRegistry.functionIsProtected(ast.procedureName)) { return new CellError(ErrorType.LIC, ErrorMessage.LicenseKey(this.config.licenseKeyValidityState)) } const pluginEntry = this.functionRegistry.getFunction(ast.procedureName) @@ -272,16 +273,16 @@ export class Interpreter { } } - public getGpuInstance(): GPU.GPU { + public getGpuInstance(): GPU { const mode = this.config.gpuMode + const gpujs = this.config.gpujs - if (mode === 'fallback') { - throw Error('Cannot instantiate GPU.js when fallback mode is active.') + if (gpujs === undefined) { + throw Error('Cannot instantiate GPU.js. Constructor not provided.') } if (!this.gpu) { - const GPUConstructor = GPU.GPU || GPU - this.gpu = new GPUConstructor({ mode }) + this.gpu = new gpujs({mode}) } return this.gpu @@ -313,7 +314,7 @@ function passErrors(left: InterpreterValue, right: InterpreterValue): Maybe(op: (a: T) => InterpreterValue, a: T | CellError): InterpreterValue { - if(a instanceof CellError) { + if (a instanceof CellError) { return a } else { return op(a) @@ -321,9 +322,9 @@ function wrapperUnary(op: (a: T) => InterpreterValue } function wrapperBinary(op: (a: T, b: T) => InterpreterValue, a: T | CellError, b: T | CellError): InterpreterValue { - if(a instanceof CellError) { + if (a instanceof CellError) { return a - } else if(b instanceof CellError) { + } else if (b instanceof CellError) { return b } else { return op(a, b) @@ -331,7 +332,7 @@ function wrapperBinary(op: (a: T, b: T) => Interpret } function wrapperForAddress(val: InterpreterValue, adr: SimpleCellAddress): InterpreterValue { - if(val instanceof CellError) { + if (val instanceof CellError) { return val.attachAddress(adr) } return val diff --git a/src/interpreter/plugin/MatrixPlugin.ts b/src/interpreter/plugin/MatrixPlugin.ts index da344808cf..d55fcd7e20 100644 --- a/src/interpreter/plugin/MatrixPlugin.ts +++ b/src/interpreter/plugin/MatrixPlugin.ts @@ -62,7 +62,7 @@ export class MatrixPlugin extends FunctionPlugin { constructor(interpreter: Interpreter) { super(interpreter) - if (this.config.gpuMode === 'fallback') { + if (this.config.gpujs === undefined) { this.createKernel = this.createCpuKernel } else { this.createKernel = this.createGpuJsKernel diff --git a/test/interpreter/matrix-plugin.spec.ts b/test/interpreter/matrix-plugin.spec.ts index f2e47a6b26..5ff764b0f1 100644 --- a/test/interpreter/matrix-plugin.spec.ts +++ b/test/interpreter/matrix-plugin.spec.ts @@ -4,6 +4,7 @@ import {ErrorMessage} from '../../src/error-message' import {MatrixPlugin} from '../../src/interpreter/plugin/MatrixPlugin' import {adr, detailedError} from '../testUtils' import {ConfigParams} from '../../src/Config' +import GPU from 'gpu.js' describe('Matrix plugin', () => { beforeAll(() => { @@ -210,9 +211,9 @@ describe('Matrix plugin', () => { }) } - describe('GPU.js', () => sharedExamples({gpuMode: 'cpu'})) + describe('GPU.js', () => sharedExamples({gpujs: GPU.GPU ?? GPU, gpuMode: 'cpu'})) - describe('CPU fallback', () => sharedExamples({gpuMode: 'fallback'})) + describe('CPU fallback', () => sharedExamples({})) }) describe('Function TRANSPOSE', () => { From a0ed79f75f8308faed8fbe8a16b37d8928905e3e Mon Sep 17 00:00:00 2001 From: Jakub Kowalski Date: Fri, 29 Jan 2021 09:07:12 +0100 Subject: [PATCH 3/6] Config param docs --- src/Config.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Config.ts b/src/Config.ts index 9ea906051a..11a1bcc822 100644 --- a/src/Config.ts +++ b/src/Config.ts @@ -146,7 +146,11 @@ export interface ConfigParams { // eslint-disable-next-line @typescript-eslint/no-explicit-any functionPlugins: any[], /** - * TODO + * A GPU.js constructor used by matrix functions. When not provided, plain cpu implementation will be used. + * + * @default undefined + * + * @category Engine */ gpujs?: typeof GPU, /** From cb259f041edad417b97aff3d25f23d778a147bf6 Mon Sep 17 00:00:00 2001 From: Jakub Kowalski Date: Fri, 29 Jan 2021 09:44:06 +0100 Subject: [PATCH 4/6] adjust test --- test/config.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/config.spec.ts b/test/config.spec.ts index d54f78e19f..de7d9efd23 100644 --- a/test/config.spec.ts +++ b/test/config.spec.ts @@ -96,7 +96,7 @@ describe('Config', () => { expect(() => new Config({dateFormats: {}})).toThrowError('Expected value of type: array for config parameter: dateFormats') // eslint-disable-next-line // @ts-ignore - expect(() => new Config({gpuMode: 'abcd'})).toThrowError('Expected one of \'gpu\' \'cpu\' \'dev\' \'fallback\' for config parameter: gpuMode') + expect(() => new Config({gpuMode: 'abcd'})).toThrowError('Expected one of \'gpu\' \'cpu\' \'dev\' for config parameter: gpuMode') // eslint-disable-next-line // @ts-ignore expect(() => new Config({caseFirst: 'abcd'})).toThrowError('Expected one of \'upper\' \'lower\' \'false\' for config parameter: caseFirst') From ecf7cd4fe604b3218b59cc498552f2094c66720f Mon Sep 17 00:00:00 2001 From: Jakub Kowalski Date: Sat, 30 Jan 2021 10:00:43 +0100 Subject: [PATCH 5/6] Docs --- docs/guide/enabling-gpu-acceleration.md | 19 +++++++++++++++++++ docs/guide/known-limitations.md | 4 ++-- docs/guide/performance.md | 2 ++ 3 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 docs/guide/enabling-gpu-acceleration.md diff --git a/docs/guide/enabling-gpu-acceleration.md b/docs/guide/enabling-gpu-acceleration.md new file mode 100644 index 0000000000..cbba6b8258 --- /dev/null +++ b/docs/guide/enabling-gpu-acceleration.md @@ -0,0 +1,19 @@ +# Enabling GPU acceleration. + +Currently, GPU.js uses ES6 features and does not provide proper ES5 package. +Therefore, to allow the engine to run on browsers like Internet Explorer, it is not bundled with the engine. + +In order to take advantage of the GPU acceleration, the GPU.js constructor has to be passed to the engine configuration +(supported version of GPU.js is 2.3.0): + +```javascript +import GPU from 'gpu.js' + +// define options +const options = { + gpujs: GPU.GPU ?? GPU +}; + +// call the static method to build a new instance +const hfInstance = HyperFormula.buildEmpty(options); +``` diff --git a/docs/guide/known-limitations.md b/docs/guide/known-limitations.md index b92197cb25..3a8b4e95bd 100644 --- a/docs/guide/known-limitations.md +++ b/docs/guide/known-limitations.md @@ -10,8 +10,8 @@ market (~450 functions on average). culture-insensitive strings. HyperFormula requires the full International Components for Unicode (ICU) to be supported. [Learn more](https://nodejs.org/api/intl.html#intl_embed_the_entire_icu_full_icu) -* GPU acceleration is used only by matrix functions: MMULT, -TRANSPOSE, MEDIANPOOL, MAXPOOL. +* GPU acceleration is used only by matrix functions: MMULT, MEDIANPOOL, MAXPOOL. +* GPU.js works only in browsers that support ES6. [Learn more](enabling-gpu-acceleration.md) * Sorting and filtering are not natively supported. However, we simulate sorting using the move operations. * Multiple workbooks are not supported. One instance of HyperFormula diff --git a/docs/guide/performance.md b/docs/guide/performance.md index 908f87bea2..e0fb344c6e 100644 --- a/docs/guide/performance.md +++ b/docs/guide/performance.md @@ -89,6 +89,8 @@ the bigger the performance gain. **For small data sets, the difference between the CPU and GPU is non-significant.** +[See how to enable GPU acceleration →](enabling-gpu-acceleration.md) + ## Benchmarks HyperFormula's performance has been tested on different devices, From b39e23a33f299264ef7560fac9222e5a0894b918 Mon Sep 17 00:00:00 2001 From: Jakub Kowalski Date: Sat, 30 Jan 2021 10:06:27 +0100 Subject: [PATCH 6/6] CHANGELOG --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 836853281f..eb912ecbd4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Changed +- GPU.js constructor needs to be provided directly to engine configuration. (#355) + ### Added - Added support for row and column reordering. (#343) - Added ability to fallback to plain CPU implementation for functions that uses GPU.js (#355)