gpujs / gpu.js

GPU Accelerated JavaScript
https://gpu.rocks
MIT License
15.14k stars 658 forks source link

JSON prebuild export and import broken? #599

Open CipSoft-Components opened 4 years ago

CipSoft-Components commented 4 years ago

What is wrong?

To make the loading time faster, I thought to prebuild my kernels once as JSON with toJSON(), and use this JSON to create my MapKernels. But the toJSON() is out of date (not handling native functions correct), and also the intern fromJSON(...) do not work. I tried to patch them, and now I can export, but I don't get the import working. Additional information: I use a map function with some map return values, and custom native functions.

Where does it happen?

Not specific to a system.

How do we replicate the issue?

How important is this (1-5)?

2

Expected behavior (i.e. solution)

Should work

Other Comments

After some tests to patch fromJSON(...) I got it working, but the compiled fragment shader complains about complains about the missing map result variables. Here are my patched functions:

toJSON = function() {
  return this.traceFunctionCalls(this.rootNode.name).reverse().map(name => {
    const nativeIndex = this.nativeFunctionNames.indexOf(name);
    if (nativeIndex > -1) {
      return {
        name,
        source: this.nativeFunctions[nativeIndex].source
      };
    } else if (this.functionMap[name]) {
      return this.functionMap[name].toJSON();
    } else {
      throw new Error(`function ${ name } not found`);
    }
  });
}
fromJSON = function(jsonFunctionNodes, FunctionNode) {
  this.functionMap = {};
    for (let i = 0; i < jsonFunctionNodes.length; i++) {
      const jsonFunctionNode = jsonFunctionNodes[i];
      if (jsonFunctionNode.settings) {
        const functionNode = new FunctionNode(jsonFunctionNode.ast, jsonFunctionNode.settings);
        functionNode.triggerImplyArgumentType = this.assignArgumentType.bind(this);
        // For some reason the FunctionNode object is missing some members, which I found in the FunctionBuilder.
        // I'm not sure, if this is correct, but it works somehow.
        for (const key of Object.keys(functionNode)) {
          if (functionNode[key] === null && this[key] && typeof this[key] === 'function') {
            functionNode[key] = this[key].bind(this);
          }
        }
        this.addFunctionNode(functionNode);
        if (functionNode.isSubKernel) {
          this.subKernelNodes.push(functionNode);
        }
        this.functionNodes.push(functionNode);
      } else {
        this.nativeFunctions.push(jsonFunctionNode);
      }
    }
    return this;
};
GirkovArpa commented 4 years ago

You may find this workaround helpful:

// save kernel to file as a module
require('fs').writeFileSync('kernel.js', 'module.exports = ' + kernel.toString(...kernelArguments));
// execute the saved kernel
const kernelOutput = require('./kernel.js')({})(...kernelArguments)

EDIT: Actually this won't work unless your arguments are the same size and dimensions when you save it as when you load it.

y4nnick commented 4 years ago

Any updates on this? :)

evbo commented 3 years ago

Just copy and pasted examples from the readme and it appears v2.10.0 createKernelMap fails for both Object Outputs and Array Outputs with:

Uncaught Error: Identifier is not defined on line 2, position 1


const gpu = new GPU();

const megaKernel = gpu.createKernelMap([ function add(a, b) { return a + b; }, function multiply(a, b) { return a * b; } ], function(a, b, c) { return multiply(add(a[this.thread.x], b[this.thread.x]), c[this.thread.x]); }, { output: [10] });

const json = megaKernel.toJSON();