Java Code Examples for org.bytedeco.javacpp.Pointer#memset()

The following examples show how to use org.bytedeco.javacpp.Pointer#memset() . 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: CudaMemoryManager.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public void memset(INDArray array) {
    if (array.isView()) {
        array.assign(0.0);

        // we don't want any mGRID activations here
        Nd4j.getExecutioner().commit();
        return;
    }

    // we want to be sure we have no trails left in mGRID
    Nd4j.getExecutioner().push();

    AllocationPoint point = AtomicAllocator.getInstance().getAllocationPoint(array);

    if (point.getAllocationStatus() == AllocationStatus.DEVICE) {
        CudaContext context = (CudaContext) AtomicAllocator.getInstance().getDeviceContext().getContext();
        NativeOpsHolder.getInstance().getDeviceNativeOps().memsetAsync(AtomicAllocator.getInstance().getPointer(array, context),0, array.data().length() * Nd4j.sizeOfDataType(array.data().dataType()),0, context.getOldStream());

        // better be safe then sorry
        context.getOldStream().synchronize();
        point.tickDeviceWrite();
    } else if (point.getAllocationStatus() == AllocationStatus.HOST) {
        Nd4j.getExecutioner().commit();

        // just casual memset
        Pointer.memset(AtomicAllocator.getInstance().getHostPointer(array), 0, array.data().length() * Nd4j.sizeOfDataType(array.data().dataType()));
        point.tickHostWrite();
    }
}
 
Example 2
Source File: CpuMemoryManager.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public void memset(INDArray array) {
    if (array.isView()) {
        array.assign(0.0);
        return;
    }

    Pointer.memset(array.data().addressPointer(), 0, array.data().length() * Nd4j.sizeOfDataType(array.data().dataType()));
}
 
Example 3
Source File: CudaMemoryManager.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public void memset(INDArray array) {
    if (array.isView()) {
        array.assign(0.0);

        // we don't want any mGRID activations here
        Nd4j.getExecutioner().commit();
        return;
    }

    // we want to be sure we have no trails left in mGRID
    Nd4j.getExecutioner().push();

    AllocationPoint point = AtomicAllocator.getInstance().getAllocationPoint(array);

    if (point.getAllocationStatus() == AllocationStatus.DEVICE) {
        CudaContext context = AtomicAllocator.getInstance().getDeviceContext();
        NativeOpsHolder.getInstance().getDeviceNativeOps().memsetAsync(AtomicAllocator.getInstance().getPointer(array, context),0, array.data().length() * Nd4j.sizeOfDataType(array.data().dataType()),0, context.getOldStream());

        // we also memset host pointer
        Pointer.memset(AtomicAllocator.getInstance().getHostPointer(array), 0, array.data().length() * Nd4j.sizeOfDataType(array.data().dataType()));

        // better be safe then sorry
        context.getOldStream().synchronize();
        point.tickDeviceWrite();
        point.tickHostRead();
    } else if (point.getAllocationStatus() == AllocationStatus.HOST) {
        Nd4j.getExecutioner().commit();

        // just casual memset
        Pointer.memset(AtomicAllocator.getInstance().getHostPointer(array), 0, array.data().length() * Nd4j.sizeOfDataType(array.data().dataType()));
        point.tickHostWrite();
    }
}
 
Example 4
Source File: CpuMemoryManager.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * This method returns
 * PLEASE NOTE: Cache options depend on specific implementations
 *
 * @param bytes
 * @param kind
 * @param initialize
 */
@Override
public Pointer allocate(long bytes, MemoryKind kind, boolean initialize) {
    Pointer ptr = NativeOpsHolder.getInstance().getDeviceNativeOps().mallocHost(bytes, 0);

    if (ptr == null || ptr.address() == 0L)
        throw new OutOfMemoryError("Failed to allocate [" + bytes + "] bytes");

    //log.info("Allocating {} bytes at MemoryManager", bytes);

    if (initialize)
        Pointer.memset(ptr, 0, bytes);

    return ptr;
}
 
Example 5
Source File: CpuMemoryManager.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public void memset(INDArray array) {
    if (array.isView()) {
        array.assign(0.0);
        return;
    }

    Pointer.memset(array.data().addressPointer(), 0, array.data().length() * Nd4j.sizeOfDataType(array.data().dataType()));
}
 
Example 6
Source File: CudaCachingZeroProvider.java    From nd4j with Apache License 2.0 4 votes vote down vote up
/**
 * This method frees specific chunk of memory, described by AllocationPoint passed in.
 *
 * PLEASE NOTE: This method can actually ignore free, and keep released memory chunk for future reuse.
 *
 * @param point
 */
@Override
public void free(AllocationPoint point) {
    if (point.getAllocationStatus() == AllocationStatus.DEVICE) {
        super.free(point);
    } else {
        AllocationShape shape = point.getShape();
        long reqMemory = AllocationUtils.getRequiredMemory(shape);

        // we don't cache too big objects
        if (reqMemory > CudaEnvironment.getInstance().getConfiguration().getMaximumHostCacheableLength() || zeroCachedAmount.get() >= CudaEnvironment.getInstance().getConfiguration().getMaximumHostCache()) {
            //log.info("HOST memory purging: {} bytes; MS: {}; MT: {}", reqMemory, MAX_SINGLE_ALLOCATION, MAX_CACHED_MEMORY);
            super.free(point);
            return;
        }

        ensureCacheHolder(shape);

        //log.info("Saving DEVICE memory into cache...");

        /*
            Now we should decide if this object can be cached or not
         */
        CacheHolder cache = zeroCache.get(shape);

        // memory chunks < threshold will be cached no matter what
        if (reqMemory <= FORCED_CACHE_THRESHOLD) {
            Pointer.memset(point.getHostPointer(), 0, reqMemory);
            cache.put(new CudaPointer(point.getHostPointer().address()));
        } else {
            long cacheEntries = cache.size();
            long cacheHeight = zeroCache.size();

            // total memory allocated within this bucket
            long cacheDepth = cacheEntries * reqMemory;

            //   if (cacheDepth < MAX_CACHED_MEMORY / cacheHeight) {
            Pointer.memset(point.getHostPointer(), 0, reqMemory);
            cache.put(new CudaPointer(point.getHostPointer().address()));
            //    } else {
            //       super.free(point);
            //    }
        }
    }
}
 
Example 7
Source File: CpuMemoryManager.java    From nd4j with Apache License 2.0 4 votes vote down vote up
/**
 * This method returns
 * PLEASE NOTE: Cache options depend on specific implementations
 *
 * @param bytes
 * @param kind
 * @param initialize
 */
@Override
public Pointer allocate(long bytes, MemoryKind kind, boolean initialize) {
    Pointer ptr = NativeOpsHolder.getInstance().getDeviceNativeOps().mallocHost(bytes, 0);

    if (ptr == null || ptr.address() == 0L)
        throw new ND4JIllegalStateException("Failed to allocate [" + bytes + "] bytes");

    //log.info("Allocating {} bytes at MemoryManager", bytes);


    if (initialize)
        Pointer.memset(ptr, 0, bytes);

    return ptr;
}