예제 #1
0
 // Allocates memory with cache management
 // Will block until there is enough available memory.
 // Catches OutOfMemory, clears cache & retries.
 public static Object malloc(int elems, long bytes, int type, byte[] orig, int from) {
   // Do not assert on large-size here.  RF's temp internal datastructures are
   // single very large arrays.
   // assert bytes < Value.MAX : "malloc size=0x"+Long.toHexString(bytes);
   while (true) {
     if (!CAN_ALLOC
         && // Not allowing allocations?
         bytes > 256
         && // Allow tiny ones in any case
         // To prevent deadlock, we cannot block the cleaner thread in any
         // case.  This is probably an allocation for logging (ouch! shades of
         // logging-induced deadlock!) which will probably be recycled quickly.
         !(Thread.currentThread() instanceof H2O.Cleaner)) {
       synchronized (_lock) {
         try {
           _lock.wait(3 * 1000);
         } catch (InterruptedException ex) {
         }
       }
     }
     try {
       switch (type) {
         case 1:
           return new byte[elems];
         case 2:
           return new short[elems];
         case 4:
           return new int[elems];
         case 8:
           return new long[elems];
         case -4:
           return new float[elems];
         case -8:
           return new double[elems];
         case 0:
           return new boolean[elems];
         case -1:
           return Arrays.copyOfRange(orig, from, elems);
         default:
           throw H2O.unimpl();
       }
     } catch (OutOfMemoryError e) {
       // Do NOT log OutOfMemory, it is expected and unavoidable and handled
       // in most cases by spilling to disk.
       if (H2O.Cleaner.isDiskFull()) UDPRebooted.suicide(UDPRebooted.T.oom, H2O.SELF);
     }
     set_goals("OOM", true, bytes); // Low memory; block for swapping
   }
 }
예제 #2
0
 /**
  * Simple GLM wrapper to enable launching GLM from command line.
  *
  * <p>Example input: java -jar target/h2o.jar -name=test -runMethod water.util.GLMRunner
  * -file=smalldata/logreg/prostate.csv -y=CAPSULE -family=binomial
  *
  * @param args
  * @throws InterruptedException
  */
 public static void main(String[] args) throws InterruptedException {
   try {
     GLMArgs ARGS = new GLMArgs();
     new Arguments(args).extract(ARGS);
     System.out.println("==================<GLMRunner START>===================");
     ValueArray ary = Utils.loadAndParseKey(ARGS.file);
     int ycol;
     try {
       ycol = Integer.parseInt(ARGS.y);
     } catch (NumberFormatException e) {
       ycol = ary.getColumnIds(new String[] {ARGS.y})[0];
     }
     int ncols = ary.numCols();
     if (ycol < 0 || ycol >= ary.numCols()) {
       System.err.println("invalid y column: " + ycol);
       H2O.exit(-1);
     }
     int[] xcols;
     if (ARGS.xs.equalsIgnoreCase("all")) {
       xcols = new int[ncols - 1];
       for (int i = 0; i < ycol; ++i) xcols[i] = i;
       for (int i = ycol; i < ncols - 1; ++i) xcols[i] = i + 1;
     } else {
       System.out.println("xs = " + ARGS.xs);
       String[] names = ARGS.xs.split(",");
       xcols = new int[names.length];
       try {
         for (int i = 0; i < names.length; ++i) xcols[i] = Integer.valueOf(names[i]);
       } catch (NumberFormatException e) {
         xcols = ary.getColumnIds(ARGS.xs.split(","));
       }
     }
     for (int x : xcols)
       if (x < 0) {
         System.err.println("Invalid predictor specification " + ARGS.xs);
         H2O.exit(-1);
       }
     GLMJob j =
         DGLM.startGLMJob(
             DGLM.getData(ary, xcols, ycol, null, true),
             new ADMMSolver(ARGS.lambda, ARGS._alpha),
             new GLMParams(Family.valueOf(ARGS.family)),
             null,
             ARGS.xval,
             true);
     System.out.print("[GLM] computing model...");
     int progress = 0;
     while (!j.isDone()) {
       int p = (int) (100 * j.progress());
       int dots = p - progress;
       progress = p;
       for (int i = 0; i < dots; ++i) System.out.print('.');
       Thread.sleep(250);
     }
     Log.debug(Sys.GENLM, "DONE.");
     GLMModel m = j.get();
     String[] colnames = ary.colNames();
     System.out.println("Intercept" + " = " + m._beta[ncols - 1]);
     for (int i = 0; i < xcols.length; ++i) {
       System.out.println(colnames[i] + " = " + m._beta[i]);
     }
   } catch (Throwable t) {
     Log.err(t);
   } finally { // we're done. shutdown the cloud
     Log.debug(Sys.GENLM, "==================<GLMRunner DONE>===================");
     UDPRebooted.suicide(UDPRebooted.T.shutdown, H2O.SELF);
   }
 }