nd4j-backends/nd4j-backend-impls/nd4j-cuda/src/main/java/org/nd4j/jita/memory/impl/CudaDirectProvider.java
@Override
    public PointersPair malloc(AllocationShape shape, AllocationPoint point, AllocationStatus location) {

        //log.info("shape onCreate: {}, target: {}", shape, location);

        switch (location) {
            case HOST: {
                Pointer devicePointer = new Pointer();
                long reqMem = AllocationUtils.getRequiredMemory(shape);

                // FIXME: this is WRONG, and directly leads to memleak
                if (reqMem < 1)
                    reqMem = 1;

                Pointer pointer = nativeOps.mallocHost(reqMem, 0);
                if (pointer == null)
                    throw new RuntimeException("Can't allocate [HOST] memory: " + reqMem + "; threadId: "
                                    + Thread.currentThread().getId());

                //                log.info("Host allocation, Thread id: {}, ReqMem: {}, Pointer: {}", Thread.currentThread().getId(), reqMem, pointer != null ? pointer.address() : null);

                Pointer hostPointer = new CudaPointer(pointer);

                PointersPair devicePointerInfo = new PointersPair();
                devicePointerInfo.setDevicePointer(new CudaPointer(hostPointer, reqMem));
                devicePointerInfo.setHostPointer(new CudaPointer(hostPointer, reqMem));

                point.setPointers(devicePointerInfo);

                point.setAllocationStatus(AllocationStatus.HOST);
                return devicePointerInfo;
            }
            case DEVICE: {
                // cudaMalloc call
                int deviceId = AtomicAllocator.getInstance().getDeviceId();
                long reqMem = AllocationUtils.getRequiredMemory(shape);

                // FIXME: this is WRONG, and directly leads to memleak
                if (reqMem < 1)
                    reqMem = 1;

                //                if (CudaEnvironment.getInstance().getConfiguration().getDebugTriggered() == 119)
                //                    throw new RuntimeException("Device allocation happened");


                Pointer pointer = nativeOps.mallocDevice(reqMem, null, 0);
                //log.info("Device [{}] allocation, Thread id: {}, ReqMem: {}, Pointer: {}", AtomicAllocator.getInstance().getDeviceId(), Thread.currentThread().getId(), reqMem, pointer != null ? pointer.address() : null);


                if (pointer == null)
                    return null;
                //throw new RuntimeException("Can't allocate [DEVICE] memory!");

                Pointer devicePointer = new CudaPointer(pointer);

                PointersPair devicePointerInfo = point.getPointers();
                if (devicePointerInfo == null)
                    devicePointerInfo = new PointersPair();
                devicePointerInfo.setDevicePointer(new CudaPointer(devicePointer, reqMem));

                point.setAllocationStatus(AllocationStatus.DEVICE);
                point.setDeviceId(deviceId);

                return devicePointerInfo;
            }
            default:
                throw new IllegalStateException("Unsupported location for malloc: [" + location + "]");
        }
    }
@Override
    public PointersPair malloc(AllocationShape shape, AllocationPoint point, AllocationStatus location) {

        //log.info("shape onCreate: {}, target: {}", shape, location);

        switch (location) {
            case HOST: {
                Pointer devicePointer = new Pointer();
                long reqMem = AllocationUtils.getRequiredMemory(shape);

                // FIXME: this is WRONG, and directly leads to memleak
                if (reqMem < 1)
                    reqMem = 1;

                Pointer pointer = nativeOps.mallocHost(reqMem, 0);
                if (pointer == null)
                    throw new RuntimeException("Can't allocate [HOST] memory: " + reqMem + "; threadId: "
                                    + Thread.currentThread().getId());

                //                log.info("Host allocation, Thread id: {}, ReqMem: {}, Pointer: {}", Thread.currentThread().getId(), reqMem, pointer != null ? pointer.address() : null);

                Pointer hostPointer = new CudaPointer(pointer);

                PointersPair devicePointerInfo = new PointersPair();
                devicePointerInfo.setDevicePointer(new CudaPointer(hostPointer, reqMem));
                devicePointerInfo.setHostPointer(new CudaPointer(hostPointer, reqMem));

                point.setPointers(devicePointerInfo);

                point.setAllocationStatus(AllocationStatus.HOST);
                return devicePointerInfo;
            }
            case DEVICE: {
                // cudaMalloc call
                int deviceId = AtomicAllocator.getInstance().getDeviceId();
                long reqMem = AllocationUtils.getRequiredMemory(shape);

                // FIXME: this is WRONG, and directly leads to memleak
                if (reqMem < 1)
                    reqMem = 1;

                //                if (CudaEnvironment.getInstance().getConfiguration().getDebugTriggered() == 119)
                //                    throw new RuntimeException("Device allocation happened");


                Pointer pointer = nativeOps.mallocDevice(reqMem, null, 0);
                //log.info("Device [{}] allocation, Thread id: {}, ReqMem: {}, Pointer: {}", AtomicAllocator.getInstance().getDeviceId(), Thread.currentThread().getId(), reqMem, pointer != null ? pointer.address() : null);


                if (pointer == null)
                    return null;
                //throw new RuntimeException("Can't allocate [DEVICE] memory!");

                Pointer devicePointer = new CudaPointer(pointer);

                PointersPair devicePointerInfo = point.getPointers();
                if (devicePointerInfo == null)
                    devicePointerInfo = new PointersPair();
                devicePointerInfo.setDevicePointer(new CudaPointer(devicePointer, reqMem));

                point.setAllocationStatus(AllocationStatus.DEVICE);
                point.setDeviceId(deviceId);

                return devicePointerInfo;
            }
            default:
                throw new IllegalStateException("Unsupported location for malloc: [" + location + "]");
        }
    }
@ Override public PointersPair malloc ( AllocationShape shape , AllocationPoint point , AllocationStatus location ) { //log.info("shape onCreate: {}, target: {}", shape, location); switch ( location ) { case HOST : { Pointer devicePointer = new Pointer ( ) ; long reqMem = AllocationUtils . getRequiredMemory ( shape ) ; // FIXME: this is WRONG, and directly leads to memleak if ( reqMem < 1 ) reqMem = 1 ; Pointer pointer = nativeOps . mallocHost ( reqMem , 0 ) ; if ( pointer == null ) throw new RuntimeException ( "Can't allocate [HOST] memory: " + reqMem + "; threadId: " + Thread . currentThread ( ) . getId ( ) ) ; //                log.info("Host allocation, Thread id: {}, ReqMem: {}, Pointer: {}", Thread.currentThread().getId(), reqMem, pointer != null ? pointer.address() : null); Pointer hostPointer = new CudaPointer ( pointer ) ; PointersPair devicePointerInfo = new PointersPair ( ) ; devicePointerInfo . setDevicePointer ( new CudaPointer ( hostPointer , reqMem ) ) ; devicePointerInfo . setHostPointer ( new CudaPointer ( hostPointer , reqMem ) ) ; point . setPointers ( devicePointerInfo ) ; point . setAllocationStatus ( AllocationStatus . HOST ) ; return devicePointerInfo ; } case DEVICE : { // cudaMalloc call int deviceId = AtomicAllocator . getInstance ( ) . getDeviceId ( ) ; long reqMem = AllocationUtils . getRequiredMemory ( shape ) ; // FIXME: this is WRONG, and directly leads to memleak if ( reqMem < 1 ) reqMem = 1 ; //                if (CudaEnvironment.getInstance().getConfiguration().getDebugTriggered() == 119) //                    throw new RuntimeException("Device allocation happened"); Pointer pointer = nativeOps . mallocDevice ( reqMem , null , 0 ) ; //log.info("Device [{}] allocation, Thread id: {}, ReqMem: {}, Pointer: {}", AtomicAllocator.getInstance().getDeviceId(), Thread.currentThread().getId(), reqMem, pointer != null ? pointer.address() : null); if ( pointer == null ) return null ; //throw new RuntimeException("Can't allocate [DEVICE] memory!"); Pointer devicePointer = new CudaPointer ( pointer ) ; PointersPair devicePointerInfo = point . getPointers ( ) ; if ( devicePointerInfo == null ) devicePointerInfo = new PointersPair ( ) ; devicePointerInfo . setDevicePointer ( new CudaPointer ( devicePointer , reqMem ) ) ; point . setAllocationStatus ( AllocationStatus . DEVICE ) ; point . setDeviceId ( deviceId ) ; return devicePointerInfo ; } default : throw new IllegalStateException ( "Unsupported location for malloc: [" + location + "]" ) ; } }
This method provides PointersPair to memory chunk specified by AllocationShape

@param shape shape of desired memory chunk
@param point target AllocationPoint structure
@param location either HOST or DEVICE
@return
This method provides PointersPair to memory chunk specified by AllocationShape