public static Nd4jBackend load ( ) throws NoAvailableBackendException { List < Nd4jBackend > backends = new ArrayList <> ( 1 ) ; ServiceLoader < Nd4jBackend > loader = ServiceLoader . load ( Nd4jBackend . class ) ; try { Iterator < Nd4jBackend > backendIterator = loader . iterator ( ) ; while ( backendIterator . hasNext ( ) ) backends . add ( backendIterator . next ( ) ) ; } catch ( ServiceConfigurationError serviceError ) { // a fatal error due to a syntax or provider construction error. // backends mustn't throw an exception during construction. throw new RuntimeException ( "failed to process available backends" , serviceError ) ; } Collections . sort ( backends , new Comparator < Nd4jBackend > ( ) { @ Override public int compare ( Nd4jBackend o1 , Nd4jBackend o2 ) { // high-priority first return o2 . getPriority ( ) - o1 . getPriority ( ) ; } } ) ; for ( Nd4jBackend backend : backends ) { boolean available = false ; String error = null ; try { available = backend . isAvailable ( ) ; } catch ( Exception e ) { error = e . getMessage ( ) ; } if ( ! available ) { log . warn ( "Skipped [{}] backend (unavailable): {}" , backend . getClass ( ) . getSimpleName ( ) , error ) ; continue ; } try { Nd4jContext . getInstance ( ) . updateProperties ( backend . getConfigurationResource ( ) . getInputStream ( ) ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; } log . info ( "Loaded [{}] backend" , backend . getClass ( ) . getSimpleName ( ) ) ; return backend ; } //need to dynamically load jars and recall, note that we do this right before the backend loads. //An existing backend should take precedence over //ones being dynamically discovered. //Note that we prioritize jvm properties first, followed by environment variables. String [ ] jarUris ; if ( System . getProperties ( ) . containsKey ( ND4JSystemProperties . DYNAMIC_LOAD_CLASSPATH_PROPERTY ) && ! triedDynamicLoad ) { jarUris = System . getProperties ( ) . getProperty ( ND4JSystemProperties . DYNAMIC_LOAD_CLASSPATH_PROPERTY ) . split ( ";" ) ; // Do not call System.getenv(): Accessing all variables requires higher security privileges } else if ( System . getenv ( ND4JEnvironmentVars . BACKEND_DYNAMIC_LOAD_CLASSPATH ) != null && ! triedDynamicLoad ) { jarUris = System . getenv ( ND4JEnvironmentVars . BACKEND_DYNAMIC_LOAD_CLASSPATH ) . split ( ";" ) ; } else throw new NoAvailableBackendException ( "Please ensure that you have an nd4j backend on your classpath. Please see: http://nd4j.org/getstarted.html" ) ; triedDynamicLoad = true ; //load all the discoverable uris and try to load the backend again for ( String uri : jarUris ) { loadLibrary ( new File ( uri ) ) ; } return load ( ) ; }
Loads the best available backend .