Java Code Examples for org.nd4j.linalg.api.rng.Random#getStatePointer()

The following examples show how to use org.nd4j.linalg.api.rng.Random#getStatePointer() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RandomFactory.java    From nd4j with Apache License 2.0 6 votes vote down vote up
/**
 * This method returns Random implementation instance associated with calling thread
 *
 * @return object implementing Random interface
 */
public Random getRandom() {
    try {
        if (threadRandom.get() == null) {
            Random t = (Random) randomClass.newInstance();
            if (t.getStatePointer() != null) {
                // TODO: attach this thing to deallocator
                // if it's stateless random - we just don't care then
            }
            threadRandom.set(t);
            return t;
        }


        return threadRandom.get();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example 2
Source File: RandomFactory.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
/**
 * This method returns Random implementation instance associated with calling thread
 *
 * @return object implementing Random interface
 */
public Random getRandom() {
    try {
        if (threadRandom.get() == null) {
            Random t = (Random) randomClass.newInstance();
            if (t.getStatePointer() != null) {
                // TODO: attach this thing to deallocator
                // if it's stateless random - we just don't care then
            }
            threadRandom.set(t);
            return t;
        }


        return threadRandom.get();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example 3
Source File: RandomFactory.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/**
 * This method returns new onject implementing Random interface, initialized with seed value
 *
 * @param seed seed for this rng object
 * @return object implementing Random interface
 */
public Random getNewRandomInstance(long seed) {
    try {
        Random t = (Random) randomClass.newInstance();
        if (t.getStatePointer() != null) {
            // TODO: attach this thing to deallocator
            // if it's stateless random - we just don't care then
        }
        t.setSeed(seed);
        return t;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example 4
Source File: RandomFactory.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * This method returns new onject implementing Random interface, initialized with seed value
 *
 * @param seed seed for this rng object
 * @return object implementing Random interface
 */
public Random getNewRandomInstance(long seed) {
    try {
        Random t = (Random) randomClass.newInstance();
        if (t.getStatePointer() != null) {
            // TODO: attach this thing to deallocator
            // if it's stateless random - we just don't care then
        }
        t.setSeed(seed);
        return t;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example 5
Source File: CudaExecutioner.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
public INDArray exec(RandomOp op, OpContext oc, Random rng){
    INDArray x = getX(op, oc);
    INDArray y = getY(op, oc);
    INDArray z = getZ(op, oc);

    if(op instanceof BaseRandomOp && ((BaseRandomOp)op).isTripleArgRngOp() && z != null && x == null && y == null){
        //Ugly hack to ensure the triple arg call occurs
        //See GaussianDistribution.setZ etc
        x = z;
        y = z;
    }

    long st = profilingConfigurableHookIn(op);

    checkForCompression(op);

    //validateDataType(Nd4j.dataType(), op);

    if (rng.getStatePointer() == null)
        throw new IllegalStateException(
                "You should use one of NativeRandom classes for NativeOperations execution");

    if (extraz.get() == null)
        extraz.set(new PointerPointer(32));

    if (CudaEnvironment.getInstance().getConfiguration().isDebug())
        lastOp.set(op.opName());

    val context = AtomicAllocator.getInstance().getDeviceContext();

    PointerPointer extraZZ = extraz.get().put(AddressRetriever.retrieveHostPointer(z.shapeInfoDataBuffer()),
            context.getOldStream(), AtomicAllocator.getInstance().getDeviceIdPointer());

    val hostXShapeInfo = x == null ? null : AddressRetriever.retrieveHostPointer(x.shapeInfoDataBuffer());
    val hostYShapeInfo = y == null ? null : AddressRetriever.retrieveHostPointer(y.shapeInfoDataBuffer());
    val hostZShapeInfo = z == null ? null : AddressRetriever.retrieveHostPointer(z.shapeInfoDataBuffer());

    val xb = x == null ? null : ((BaseCudaDataBuffer) x.data()).getOpaqueDataBuffer();
    val yb = y == null ? null : ((BaseCudaDataBuffer) y.data()).getOpaqueDataBuffer();
    val zb = z == null ? null : ((BaseCudaDataBuffer) z.data()).getOpaqueDataBuffer();

    if (x != null && y != null && z != null) {
        // triple arg call
        nativeOps.execRandom3(extraZZ, op.opNum(), rng.getStatePointer(), // rng state ptr
                    xb, (LongPointer) hostXShapeInfo, (LongPointer) AtomicAllocator.getInstance().getPointer(x.shapeInfoDataBuffer(), context),
                    yb, (LongPointer) hostYShapeInfo, (LongPointer) AtomicAllocator.getInstance().getPointer(y.shapeInfoDataBuffer(), context),
                    zb, (LongPointer) hostZShapeInfo, (LongPointer) AtomicAllocator.getInstance().getPointer(z.shapeInfoDataBuffer(), context),
                    AtomicAllocator.getInstance().getPointer(op.extraArgsDataBuff(z.dataType()), context));

    } else if (x != null && z != null) {
        //double arg call
        nativeOps.execRandom2(extraZZ, op.opNum(), rng.getStatePointer(), // rng state ptr
                    xb, (LongPointer) hostXShapeInfo, (LongPointer) AtomicAllocator.getInstance().getPointer(x.shapeInfoDataBuffer(), context),
                    zb, (LongPointer) hostZShapeInfo, (LongPointer) AtomicAllocator.getInstance().getPointer(z.shapeInfoDataBuffer(), context),
                    AtomicAllocator.getInstance().getPointer(op.extraArgsDataBuff(z.dataType()),context));


    } else {
        // single arg call
        nativeOps.execRandom(extraZZ, op.opNum(), rng.getStatePointer(), // rng state ptr
                    zb, (LongPointer) hostZShapeInfo, (LongPointer) AtomicAllocator.getInstance().getPointer(z.shapeInfoDataBuffer(), context),
                     AtomicAllocator.getInstance().getPointer(op.extraArgsDataBuff(z.dataType()), context));
    }

    if (nativeOps.lastErrorCode() != 0)
        throw new RuntimeException(nativeOps.lastErrorMessage());

    profilingConfigurableHookOut(op, oc, st);

    return z;
}