3

I am writing a GPU database and looking at using javascript as language to query against using node.js.

I have been writing a node addon as I have written the GPU database in C++. However I have problem with my node.js addon as my c++ objects are not being destructed but only when I am not explicitly using the new operator. If I am using the new operator, it's fine, it's just when a call a method that creates a new method - like copy(), etc. I am using V8::AdjustAmountOfExternalAllocatedMemory(size()) as indication to V8 that I have allocated exernal memory (on the GPU).

Please could I get some advice.

1. Code that correctly frees GPU memory

This piece of code correctly frees the GPU memory by calling the object destructor, which makes a call to free the GPU memory:

var gpudb = require('./build/Release/gpudb');

var n = 1000000;
for (var i = 0; i < 10000; ++i) {
    var col = new gpudb.GpuArray(n);
}

2. However, this piece of code doesn't call the objects' destructor to free GPU memory.

var gpudb = require('./build/Release/gpudb');

var n = 1000000;
var col = new gpudb.GpuArray(n);
for (var i = 0; i < 10000; ++i) {
        var copyOfCol = col.copy();
}

3. Now, here's the functions for the constructor and copy function respectively.

Handle<Value> GpuVector::New(const Arguments& args) {
  HandleScope scope;

  if (args.IsConstructCall()) {
    // Invoked as constructor: `new GpuVector(...)`
    int value = args[0]->IsUndefined() ? 0 : args[0]->NumberValue();
    GpuVector* obj = new GpuVector(value);
    obj->Wrap(args.This());
    return args.This();
  } else {
    // Invoked as plain function `GpuVector(...)`, turn into construct call.
    const int argc = 1;
    Local<Value> argv[argc] = { args[0] };
    return scope.Close(constructor->NewInstance(argc, argv));
  }
}

Handle<Value> GpuArray::Copy(const Arguments& args) {
    HandleScope scope;

    GpuArray* in = ObjectWrap::Unwrap<GpuVector>(args.This());
    GpuArray* out = new GpuArray(in); // creates new gpu memory slot and copies the data over

    out->Wrap(args.This());
    return args.This();
}
user3199023
  • 433
  • 2
  • 8

1 Answers1

0

Without the GpuArray constructor it's a little harder to tell what is wrong.

But I can see some stuff that is not right:

Handle<Value> GpuArray::Copy(const Arguments& args) {
    //...
    GpuArray* in = ObjectWrap::Unwrap<GpuVector>(args.This());

    //...
}

You are unwraping a GpuVector object, from a GpuArray object, which is wrong.

The Wrap/Unwrap methods should be used to make a connection between c++ objects and their respective js objects, not between different objects.

From the code you posted, it looks like like (but I might be wrong about that) that you are trying to clone the object, if I'm correct, it should be done like that:

Handle<Value> GpuArray::Copy(const Arguments& args) {
    HandleScope scope;

    GpuArray* in = ObjectWrap::Unwrap<GpuArray>( args.This() );

    //build argc and argv here

    Local<Object> outJs = constructorOfGpuArray->NewInstance( argc, argv );
    GpuArray* out = ObjectWrap::Unwrap<GpuArray>( outJs );

    //set members/etc of the "out" object so it looks identical to the "in" object

    //return the js object to the caller.
    return scope.Close( outJs );
}

I didn't tested the code, but it should workin theory.

jonathancardoso
  • 11,737
  • 7
  • 53
  • 72