nd4j-backends/nd4j-backend-impls/nd4j-cuda/src/main/java/org/nd4j/jita/concurrency/CudaAffinityManager.java
@Override
    public Integer getDeviceForThread(long threadId) {
        if (getNumberOfDevices() == 1)
            return 0;

        Integer aff = affinityMap.get(threadId);

        if (aff == null) {
            Integer deviceId = getNextDevice(threadId);
            affinityMap.put(threadId, deviceId);
            affiliated.set(new AtomicBoolean(false));

            if (threadId == Thread.currentThread().getId()) {
                NativeOpsHolder.getInstance().getDeviceNativeOps().setDevice(new CudaPointer(deviceId));
                //logger.error("setDevice({}) called for thread {}", deviceId, Thread.currentThread().getName());
                affiliated.get().set(true);
            }

            return deviceId;
        } else {

            if (threadId == Thread.currentThread().getId()) {
                if (affiliated.get() == null)
                    affiliated.set(new AtomicBoolean(false));

                if (!affiliated.get().get()) {
                    NativeOpsHolder.getInstance().getDeviceNativeOps().setDevice(new CudaPointer(aff));
                    //logger.error("SCARY setDevice({}) called for thread {}", aff, threadId);
                    affiliated.get().set(true);
                    return aff;
                }
            }

            return aff;
        }
/*


        return affinityMap.get(threadId);
*/
        //return 0;
    }
@Override
    public Integer getDeviceForThread(long threadId) {
        if (getNumberOfDevices() == 1)
            return 0;

        Integer aff = affinityMap.get(threadId);

        if (aff == null) {
            Integer deviceId = getNextDevice(threadId);
            affinityMap.put(threadId, deviceId);
            affiliated.set(new AtomicBoolean(false));

            if (threadId == Thread.currentThread().getId()) {
                NativeOpsHolder.getInstance().getDeviceNativeOps().setDevice(new CudaPointer(deviceId));
                //logger.error("setDevice({}) called for thread {}", deviceId, Thread.currentThread().getName());
                affiliated.get().set(true);
            }

            return deviceId;
        } else {

            if (threadId == Thread.currentThread().getId()) {
                if (affiliated.get() == null)
                    affiliated.set(new AtomicBoolean(false));

                if (!affiliated.get().get()) {
                    NativeOpsHolder.getInstance().getDeviceNativeOps().setDevice(new CudaPointer(aff));
                    //logger.error("SCARY setDevice({}) called for thread {}", aff, threadId);
                    affiliated.get().set(true);
                    return aff;
                }
            }

            return aff;
        }
/*


        return affinityMap.get(threadId);
*/
        //return 0;
    }
@ Override public Integer getDeviceForThread ( long threadId ) { if ( getNumberOfDevices ( ) == 1 ) return 0 ; Integer aff = affinityMap . get ( threadId ) ; if ( aff == null ) { Integer deviceId = getNextDevice ( threadId ) ; affinityMap . put ( threadId , deviceId ) ; affiliated . set ( new AtomicBoolean ( false ) ) ; if ( threadId == Thread . currentThread ( ) . getId ( ) ) { NativeOpsHolder . getInstance ( ) . getDeviceNativeOps ( ) . setDevice ( new CudaPointer ( deviceId ) ) ; //logger.error("setDevice({}) called for thread {}", deviceId, Thread.currentThread().getName()); affiliated . get ( ) . set ( true ) ; } return deviceId ; } else { if ( threadId == Thread . currentThread ( ) . getId ( ) ) { if ( affiliated . get ( ) == null ) affiliated . set ( new AtomicBoolean ( false ) ) ; if ( ! affiliated . get ( ) . get ( ) ) { NativeOpsHolder . getInstance ( ) . getDeviceNativeOps ( ) . setDevice ( new CudaPointer ( aff ) ) ; //logger.error("SCARY setDevice({}) called for thread {}", aff, threadId); affiliated . get ( ) . set ( true ) ; return aff ; } } return aff ; } /*


        return affinityMap.get(threadId);
*/ //return 0; }
This method returns deviceId for given thread, identified by threadId

If no device was assigned to this thread before this call, it'll be assinged here.

@param threadId
@return
This method returns deviceId for given thread identified by threadId