Search in sources :

Example 1 with Nd4jNoSuchWorkspaceException

use of org.nd4j.linalg.exception.Nd4jNoSuchWorkspaceException in project nd4j by deeplearning4j.

the class BaseNDArray method leverageTo.

/**
 * This method detaches INDArray from current Workspace, and attaches it to Workspace with a given Id.
 * If enforceExistence == true, and no workspace with the specified ID exists, then an {@link Nd4jNoSuchWorkspaceException}
 * is thrown. Otherwise, if enforceExistance == false and no workspace with the specified ID exists, then the current
 * INDArray is returned unmodified (same as {@link #leverage()}
 *
 * @param id ID of the workspace to leverage to
 * @param enforceExistence If true, and the specified workspace does not exist: an {@link Nd4jNoSuchWorkspaceException}
 *                         will be thrown.
 * @return The INDArray, leveraged to the specified workspace
 * @see #leverageTo(String)
 */
@Override
public INDArray leverageTo(String id, boolean enforceExistence) throws Nd4jNoSuchWorkspaceException {
    if (!isAttached())
        return this;
    if (!Nd4j.getWorkspaceManager().checkIfWorkspaceExists(id)) {
        if (enforceExistence) {
            throw new Nd4jNoSuchWorkspaceException(id);
        } else {
            return this;
        }
    }
    MemoryWorkspace current = Nd4j.getMemoryManager().getCurrentWorkspace();
    MemoryWorkspace target = Nd4j.getWorkspaceManager().getWorkspaceForCurrentThread(id);
    if (current == target)
        return this;
    if (this.data.getParentWorkspace() == target)
        return this;
    Nd4j.getMemoryManager().setCurrentWorkspace(target);
    INDArray copy = null;
    if (!this.isView()) {
        Nd4j.getExecutioner().commit();
        DataBuffer buffer = Nd4j.createBuffer(this.lengthLong(), false);
        Nd4j.getMemoryManager().memcpy(buffer, this.data());
        copy = Nd4j.createArrayFromShapeBuffer(buffer, this.shapeInfoDataBuffer());
    } else {
        copy = this.dup(this.ordering());
        Nd4j.getExecutioner().commit();
    }
    Nd4j.getMemoryManager().setCurrentWorkspace(current);
    return copy;
}
Also used : MemoryWorkspace(org.nd4j.linalg.api.memory.MemoryWorkspace) Nd4jNoSuchWorkspaceException(org.nd4j.linalg.exception.Nd4jNoSuchWorkspaceException) DataBuffer(org.nd4j.linalg.api.buffer.DataBuffer)

Aggregations

DataBuffer (org.nd4j.linalg.api.buffer.DataBuffer)1 MemoryWorkspace (org.nd4j.linalg.api.memory.MemoryWorkspace)1 Nd4jNoSuchWorkspaceException (org.nd4j.linalg.exception.Nd4jNoSuchWorkspaceException)1