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

[WIP] Some memory improvements #6883

Merged
merged 20 commits into from Dec 19, 2018
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -112,6 +112,8 @@ public abstract class BaseNDArray implements INDArray, Iterable {
//protected transient DataBuffer stride;
protected transient boolean compressed = false;

protected transient boolean released = false;

// this field holds jvm copy of shapeInfo
protected transient JvmShapeInfo jvmShapeInfo;

Expand Down Expand Up @@ -6463,4 +6465,33 @@ protected void validateNumericalArray(String opName){
if(dataType() == DataType.BOOL || dataType() == DataType.UTF8)
throw new IllegalStateException("Cannot apply operation " + opName + " to array with " + dataType() + " datatype. Array shape: " + Arrays.toString(shape()));
}

@Override
public boolean closeable() {
if (released || isAttached())
return false;

// empty arrays have no buffer at all
if (isEmpty())
return true;

if (isView())
return false;

return data.closeable();
}

@Override
public void close() {
// empty arrays have no buffer at all
if (released || isEmpty())
return;

if (!closeable())
throw new ND4JIllegalStateException("Can't release this INDArray");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be great to know why it is not closeable.


data.close();

released = true;
}
}
Expand Up @@ -1308,6 +1308,16 @@ public boolean none() {
return false;
}

@Override
public boolean closeable() {
return false;
}

@Override
public void close() {

}

@Override
public boolean isS() {
return false;
Expand Down
Expand Up @@ -35,7 +35,7 @@
*
* @author Adam Gibson
*/
public interface INDArray extends Serializable {
public interface INDArray extends Serializable, AutoCloseable {
/**
* Returns the shape information debugging
* information
Expand Down Expand Up @@ -2686,4 +2686,18 @@ public interface INDArray extends Serializable {
* @return
*/
boolean none();

/**
* This method checks, if this INDArray instalce can use close() method
* @return true if array can be released, false otherwise
*/
boolean closeable();

/**
* This method releases exclusive off-heap resources uses by this INDArray instance.
* If INDArray relies on shared resources, exception will be thrown instead
*
* PLEASE NOTE: This method is NOT safe by any means
*/
void close();
}
Expand Up @@ -161,4 +161,14 @@ public boolean any() {
public boolean none() {
return false;
}

@Override
public boolean closeable() {
return false;
}

@Override
public void close() {

}
}
Expand Up @@ -1648,6 +1648,11 @@ public long capacity() {
return pointer.capacity();
}

@Override
protected void release() {
AtomicAllocator.getInstance().freeMemory(allocationPoint);
}

/*
protected short fromFloat( float fval ) {
int fbits = Float.floatToIntBits( fval );
Expand Down
Expand Up @@ -162,6 +162,16 @@ public boolean none() {
throw new UnsupportedOperationException();
}

@Override
public boolean closeable() {
return false;
}

@Override
public void close() {

}

@Override
public boolean isS() {
return false;
Expand Down
@@ -0,0 +1,88 @@
/*******************************************************************************
* Copyright (c) 2015-2018 Skymind, Inc.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
******************************************************************************/

package org.nd4j.linalg.memory;

import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.nd4j.linalg.BaseNd4jTest;
import org.nd4j.linalg.api.memory.conf.WorkspaceConfiguration;
import org.nd4j.linalg.factory.Nd4j;
import org.nd4j.linalg.factory.Nd4jBackend;
import org.nd4j.linalg.indexing.NDArrayIndex;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

/**
* @author raver119@gmail.com
*/
@Slf4j
@RunWith(Parameterized.class)
public class CloseableTests extends BaseNd4jTest {
public CloseableTests(Nd4jBackend backend) {
super(backend);
}

@Test
public void testSimpleRelease_1() {
val array = Nd4j.createFromArray(new float[]{1, 2, 3, 4, 5});
assertTrue(array.closeable());

array.close();

assertFalse(array.closeable());
}

@Test
public void testCyclicRelease_1() {
for (int e = 0; e < 1000; e++) {
try (val array = Nd4j.createFromArray(new float[]{1, 2, 3, 4, 5})) {
array.addi(1.0f);
}
System.gc();
}
}

@Test
public void testViewRelease_1() {
val array = Nd4j.create(5, 5);
assertTrue(array.closeable());

val view = array.get(NDArrayIndex.point(1), NDArrayIndex.all());

assertTrue(array.closeable());
assertFalse(view.closeable());
}

@Test
public void testAttachedRelease_1() {
val wsconf = WorkspaceConfiguration.builder().build();

try (val ws = Nd4j.getWorkspaceManager().getAndActivateWorkspace(wsconf, "haha72yjhfdfs")) {
val array = Nd4j.create(5, 5);
assertFalse(array.closeable());
}
}

@Override
public char ordering() {
return 'c';
}
}
Expand Up @@ -25,6 +25,7 @@
import org.nd4j.linalg.api.buffer.util.AllocUtil;
import org.nd4j.linalg.api.buffer.util.DataTypeUtil;
import org.nd4j.linalg.api.memory.MemoryWorkspace;
import org.nd4j.linalg.primitives.AtomicBoolean;
import org.nd4j.linalg.primitives.AtomicDouble;
import org.nd4j.linalg.primitives.Triple;
import org.nd4j.linalg.util.ArrayUtil;
Expand All @@ -35,6 +36,7 @@
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.LongBuffer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
Expand Down Expand Up @@ -94,6 +96,10 @@ public abstract class BaseDataBuffer implements DataBuffer {
protected transient Long trackingPoint;

protected transient boolean constant = false;
protected transient boolean released = false;

protected transient AtomicBoolean referenced = new AtomicBoolean(false);
protected transient Collection<BaseDataBuffer> references = new ArrayList<>();

public BaseDataBuffer() {}

Expand Down Expand Up @@ -168,6 +174,8 @@ protected BaseDataBuffer(DataBuffer underlyingBuffer, long length, long offset)
this.elementSize = (byte) underlyingBuffer.getElementSize();
this.underlyingLength = underlyingBuffer.underlyingLength();
this.wrappedDataBuffer = underlyingBuffer;
((BaseDataBuffer) underlyingBuffer).referenced.compareAndSet(false, true);
((BaseDataBuffer) underlyingBuffer).references.add(this);

// Adding link to original databuffer
if (underlyingBuffer.originalDataBuffer() == null) {
Expand Down Expand Up @@ -2246,4 +2254,40 @@ public DataBuffer reallocate(long length) {
public long capacity() {
return pointer().capacity();
}

@Override
public boolean closeable() {
if (released || isAttached() || isConstant())
return false;

if (wrappedDataBuffer != null && wrappedDataBuffer != this)
return false;

return true;
}

protected void markReleased() {
this.released = true;

for (val r:references)
r.markReleased();
}

@Override
public void close() {
if (!closeable())
throw new IllegalStateException("Can't release this data buffer");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be great to know why it is not closeable.


// notifying other databuffers that their underlying
for (val r:references)
r.markReleased();

release();
}

protected void release() {
this.pointer.deallocate();
this.indexer = null;
this.pointer = null;
}
}
Expand Up @@ -32,7 +32,7 @@
*
* @author Adam Gibson
*/
public interface DataBuffer extends Serializable {
public interface DataBuffer extends Serializable, AutoCloseable {
enum TypeEx {

}
Expand Down Expand Up @@ -681,4 +681,18 @@ enum AllocationMode {
* @return the capacity of the databuffer
* */
long capacity();

/**
* This method checks, if this DataBuffer instalce can use close() method
* @return true if DataBuffer can be released, false otherwise
*/
boolean closeable();

/**
* This method releases exclusive off-heap resources uses by this DataBuffer instance.
* If DataBuffer relies on shared resources, exception will be thrown instead
*
* PLEASE NOTE: This method is NOT safe by any means
*/
void close();
}