Skip to content
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 19 additions & 0 deletions docs/guide/enabling-gpu-acceleration.md
Original file line number Diff line number Diff line change
@@ -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);
```
4 changes: 2 additions & 2 deletions docs/guide/known-limitations.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions docs/guide/performance.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
23 changes: 16 additions & 7 deletions src/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
Expand Down Expand Up @@ -145,6 +145,14 @@ export interface ConfigParams {
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
functionPlugins: any[],
/**
* A GPU.js constructor used by matrix functions. When not provided, plain cpu implementation will be used.
*
* @default undefined
*
* @category Engine
*/
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.
Expand Down Expand Up @@ -390,6 +398,7 @@ export class Config implements ConfigParams, ParserConfig {
language: 'enGB',
licenseKey: '',
functionPlugins: [],
gpujs: undefined,
gpuMode: 'gpu',
leapYear1900: false,
smartRounding: true,
Expand Down Expand Up @@ -442,6 +451,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
Expand Down Expand Up @@ -530,6 +541,7 @@ export class Config implements ConfigParams, ParserConfig {
language,
licenseKey,
functionPlugins,
gpujs,
gpuMode,
ignorePunctuation,
leapYear1900,
Expand Down Expand Up @@ -572,6 +584,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')
Expand Down Expand Up @@ -615,10 +628,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
Expand Down
29 changes: 15 additions & 14 deletions src/interpreter/Interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* Copyright (c) 2020 Handsoncode. All rights reserved.
*/

import GPU from 'gpu.js'
import {AbsoluteCellRange, AbsoluteColumnRange, AbsoluteRowRange} from '../AbsoluteCellRange'
import {
CellError,
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -64,6 +64,7 @@ export class Interpreter {
}
return wrapperForAddress(val, formulaAddress)
}

/**
* Calculates cell value from formula abstract syntax tree
*
Expand Down Expand Up @@ -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
}
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -313,25 +314,25 @@ function passErrors(left: InterpreterValue, right: InterpreterValue): Maybe<Cell
}

function wrapperUnary<T extends InterpreterValue>(op: (a: T) => InterpreterValue, a: T | CellError): InterpreterValue {
if(a instanceof CellError) {
if (a instanceof CellError) {
return a
} else {
return op(a)
}
}

function wrapperBinary<T extends InterpreterValue>(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)
}
}

function wrapperForAddress(val: InterpreterValue, adr: SimpleCellAddress): InterpreterValue {
if(val instanceof CellError) {
if (val instanceof CellError) {
return val.attachAddress(adr)
}
return val
Expand Down
2 changes: 1 addition & 1 deletion src/interpreter/plugin/MatrixPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
5 changes: 3 additions & 2 deletions test/interpreter/matrix-plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down Expand Up @@ -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', () => {
Expand Down