Java Code Examples for java.lang.Math#min()

The following examples show how to use java.lang.Math#min() . 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: LibMatrixCUDA.java    From systemds with Apache License 2.0 6 votes vote down vote up
/**
 * Get threads, blocks and shared memory for a reduce all operation
 * @param gCtx a valid {@link GPUContext}
 * @param n size of input array
 * @return integer array containing {blocks, threads, shared memory}
 */
private static int[] getKernelParamsForReduceAll(GPUContext gCtx, int n) {
	final int MAX_THREADS = getMaxThreads(gCtx);
	final int MAX_BLOCKS = getMaxBlocks(gCtx);
	final int WARP_SIZE = getWarpSize(gCtx);
	int threads = (n < MAX_THREADS *2) ? nextPow2((n + 1)/ 2) : MAX_THREADS;

	int blocks = (n + (threads * 2 - 1)) / (threads * 2);
	blocks = Math.min(MAX_BLOCKS, blocks);

	int sharedMemSize = threads * sizeOfDataType;
	if (threads <= WARP_SIZE) {
		sharedMemSize *= 2;
	}
	return new int[] {blocks, threads, sharedMemSize};
}
 
Example 2
Source File: LibMatrixCUDA.java    From systemds with Apache License 2.0 6 votes vote down vote up
/**
 * Get threads, blocks and shared memory for a reduce all operation
 * @param gCtx a valid {@link GPUContext}
 * @param n size of input array
 * @return integer array containing {blocks, threads, shared memory}
 */
private static int[] getKernelParamsForReduceAll(GPUContext gCtx, int n) {
	final int MAX_THREADS = getMaxThreads(gCtx);
	final int MAX_BLOCKS = getMaxBlocks(gCtx);
	final int WARP_SIZE = getWarpSize(gCtx);
	int threads = (n < MAX_THREADS *2) ? nextPow2((n + 1)/ 2) : MAX_THREADS;

	int blocks = (n + (threads * 2 - 1)) / (threads * 2);
	blocks = Math.min(MAX_BLOCKS, blocks);

	int sharedMemSize = threads * sizeOfDataType;
	if (threads <= WARP_SIZE) {
		sharedMemSize *= 2;
	}
	return new int[] {blocks, threads, sharedMemSize};
}
 
Example 3
Source File: SnmpStringFixed.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructs a new <CODE>SnmpStringFixed</CODE> from the specified <CODE>Bytes</CODE> array
 * with the specified length.
 * @param l The length of the fixed-string.
 * @param v The <CODE>Bytes</CODE> composing the fixed-string value.
 * @exception IllegalArgumentException Either the length or the <CODE>Byte</CODE> array is not valid.
 */
public SnmpStringFixed(int l, Byte[] v) throws IllegalArgumentException {
    if ((l <= 0) || (v == null)) {
        throw new IllegalArgumentException() ;
    }
    int length = Math.min(l, v.length);
    value = new byte[l] ;
    for (int i = 0 ; i < length ; i++) {
        value[i] = v[i].byteValue() ;
    }
    for (int i = length ; i < l ; i++) {
        value[i] = 0 ;
    }
}
 
Example 4
Source File: SnmpStringFixed.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructs a new <CODE>SnmpStringFixed</CODE> from the specified <CODE>Bytes</CODE> array
 * with the specified length.
 * @param l The length of the fixed-string.
 * @param v The <CODE>Bytes</CODE> composing the fixed-string value.
 * @exception IllegalArgumentException Either the length or the <CODE>Byte</CODE> array is not valid.
 */
public SnmpStringFixed(int l, Byte[] v) throws IllegalArgumentException {
    if ((l <= 0) || (v == null)) {
        throw new IllegalArgumentException() ;
    }
    int length = Math.min(l, v.length);
    value = new byte[l] ;
    for (int i = 0 ; i < length ; i++) {
        value[i] = v[i].byteValue() ;
    }
    for (int i = length ; i < l ; i++) {
        value[i] = 0 ;
    }
}
 
Example 5
Source File: SnmpStringFixed.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
/**
 * Constructs a new <CODE>SnmpStringFixed</CODE> from the specified <CODE>String</CODE>
 * with the specified length.
 * @param l The length of the fixed-string.
 * @param s The <CODE>String</CODE> composing the fixed-string value.
 * @exception IllegalArgumentException Either the length or the <CODE>String</CODE> is not valid.
 */
public SnmpStringFixed(int l, String s) throws IllegalArgumentException {
    if ((l <= 0) || (s == null)) {
        throw new IllegalArgumentException() ;
    }
    byte[] v = s.getBytes();
    int length = Math.min(l, v.length);
    value = new byte[l] ;
    for (int i = 0 ; i < length ; i++) {
        value[i] = v[i] ;
    }
    for (int i = length ; i < l ; i++) {
        value[i] = 0 ;
    }
}
 
Example 6
Source File: Raw.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public int read( byte b[], int off, int len ) throws IOException 
{
	dataAvailable=0;
	int i=0, Minimum=0;
	int intArray[] = 
	{
		b.length,
		InputBuffer, 
		len
	};
/*
	find the lowest nonzero value
	timeout and threshold are handled on the native side
	see  NativeEnableReceiveTimeoutThreshold in
	RawImp.c
*/
	while(intArray[i]==0 && i < intArray.length) i++;
	Minimum=intArray[i];
	while( i < intArray.length )
	{
		if(intArray[i] > 0 )
		{
			Minimum=Math.min(Minimum,intArray[i]);
		}
		i++;
	}
	Minimum=Math.min(Minimum,threshold);
	if(Minimum == 0) Minimum=1;
	int Available=available();
	int Ret = readArray( b, off, Minimum);
	return Ret;
}
 
Example 7
Source File: SnmpStringFixed.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructs a new <CODE>SnmpStringFixed</CODE> from the specified <CODE>bytes</CODE> array
 * with the specified length.
 * @param l The length of the fixed-string.
 * @param v The <CODE>bytes</CODE> composing the fixed-string value.
 * @exception IllegalArgumentException Either the length or the <CODE>byte</CODE> array is not valid.
 */
public SnmpStringFixed(int l, byte[] v) throws IllegalArgumentException {
    if ((l <= 0) || (v == null)) {
        throw new IllegalArgumentException() ;
    }
    int length = Math.min(l, v.length);
    value = new byte[l] ;
    for (int i = 0 ; i < length ; i++) {
        value[i] = v[i] ;
    }
    for (int i = length ; i < l ; i++) {
        value[i] = 0 ;
    }
}
 
Example 8
Source File: SnmpStringFixed.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructs a new <CODE>SnmpStringFixed</CODE> from the specified <CODE>String</CODE>
 * with the specified length.
 * @param l The length of the fixed-string.
 * @param s The <CODE>String</CODE> composing the fixed-string value.
 * @exception IllegalArgumentException Either the length or the <CODE>String</CODE> is not valid.
 */
public SnmpStringFixed(int l, String s) throws IllegalArgumentException {
    if ((l <= 0) || (s == null)) {
        throw new IllegalArgumentException() ;
    }
    byte[] v = s.getBytes();
    int length = Math.min(l, v.length);
    value = new byte[l] ;
    for (int i = 0 ; i < length ; i++) {
        value[i] = v[i] ;
    }
    for (int i = length ; i < l ; i++) {
        value[i] = 0 ;
    }
}
 
Example 9
Source File: SnmpStringFixed.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructs a new <CODE>SnmpStringFixed</CODE> from the specified <CODE>Bytes</CODE> array
 * with the specified length.
 * @param l The length of the fixed-string.
 * @param v The <CODE>Bytes</CODE> composing the fixed-string value.
 * @exception IllegalArgumentException Either the length or the <CODE>Byte</CODE> array is not valid.
 */
public SnmpStringFixed(int l, Byte[] v) throws IllegalArgumentException {
    if ((l <= 0) || (v == null)) {
        throw new IllegalArgumentException() ;
    }
    int length = Math.min(l, v.length);
    value = new byte[l] ;
    for (int i = 0 ; i < length ; i++) {
        value[i] = v[i].byteValue() ;
    }
    for (int i = length ; i < l ; i++) {
        value[i] = 0 ;
    }
}
 
Example 10
Source File: RS485.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public int read( byte b[], int off, int len ) throws IOException 
{
	dataAvailable=0;
	int i=0, Minimum=0;
	int intArray[] = 
	{
		b.length,
		InputBuffer, 
		len
	};
/*
	find the lowest nonzero value
	timeout and threshold are handled on the native side
	see  NativeEnableReceiveTimeoutThreshold in
	RS485Imp.c
*/
	while(intArray[i]==0 && i < intArray.length) i++;
	Minimum=intArray[i];
	while( i < intArray.length )
	{
		if(intArray[i] > 0 )
		{
			Minimum=Math.min(Minimum,intArray[i]);
		}
		i++;
	}
	Minimum=Math.min(Minimum,threshold);
	if(Minimum == 0) Minimum=1;
	int Available=available();
	int Ret = readArray( b, off, Minimum);
	return Ret;
}
 
Example 11
Source File: LibMatrixCUDA.java    From systemds with Apache License 2.0 5 votes vote down vote up
private static int[] getKernelParamsForReduceByCol(GPUContext gCtx, int rows, int cols) {
	final int MAX_THREADS = getMaxThreads(gCtx);
	final int MAX_BLOCKS = getMaxBlocks(gCtx);
	final int WARP_SIZE = getWarpSize(gCtx);
	int threads = Math.min(cols, MAX_THREADS);
	int blocks = Math.min(cols/MAX_THREADS, MAX_BLOCKS);
	if (cols % MAX_THREADS != 0) blocks++;
	int sharedMemSize = threads * sizeOfDataType;
	if (threads <= WARP_SIZE) {
		sharedMemSize *=2;
	}
	return new int[] {blocks, threads, sharedMemSize};
}
 
Example 12
Source File: SnmpStringFixed.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructs a new <CODE>SnmpStringFixed</CODE> from the specified <CODE>bytes</CODE> array
 * with the specified length.
 * @param l The length of the fixed-string.
 * @param v The <CODE>bytes</CODE> composing the fixed-string value.
 * @exception IllegalArgumentException Either the length or the <CODE>byte</CODE> array is not valid.
 */
public SnmpStringFixed(int l, byte[] v) throws IllegalArgumentException {
    if ((l <= 0) || (v == null)) {
        throw new IllegalArgumentException() ;
    }
    int length = Math.min(l, v.length);
    value = new byte[l] ;
    for (int i = 0 ; i < length ; i++) {
        value[i] = v[i] ;
    }
    for (int i = length ; i < l ; i++) {
        value[i] = 0 ;
    }
}
 
Example 13
Source File: CirSim.java    From circuitjs1 with GNU General Public License v2.0 5 votes vote down vote up
void zoomCircuit(int dy) {
double newScale;
   	double oldScale = transform[0];
   	double val = dy*.01;
   	newScale = Math.max(oldScale+val, .2);
   	newScale = Math.min(newScale, 2.5);
   	setCircuitScale(newScale);
   }
 
Example 14
Source File: ApkSmaliDecoder.java    From apk-dependency-graph with Apache License 2.0 4 votes vote down vote up
private int getNumberOfAvailableProcessors() {
    int jobs = Runtime.getRuntime().availableProcessors();
    return Math.min(jobs, MAXIMUM_NUMBER_OF_PROCESSORS);
}
 
Example 15
Source File: ListNavigator.java    From codeu_project_2017 with Apache License 2.0 4 votes vote down vote up
private void moveUp() {
  top = Math.max(top - pageSize, 1);
  bottom = Math.min(top + pageSize - 1, selection.size());
}
 
Example 16
Source File: ReduceTask.java    From RDFS with Apache License 2.0 4 votes vote down vote up
public ReduceCopier(TaskUmbilicalProtocol umbilical, JobConf conf,
                    TaskReporter reporter
                    )throws ClassNotFoundException, IOException {

  configureClasspath(conf);
  this.reporter = reporter;
  this.shuffleClientMetrics = new ShuffleClientMetrics(conf);
  this.umbilical = umbilical;
  this.reduceTask = ReduceTask.this;

  this.scheduledCopies = new ArrayList<MapOutputLocation>(100);
  this.copyResults = new ArrayList<CopyResult>(100);
  this.numCopiers = conf.getInt("mapred.reduce.parallel.copies", 5);
  this.maxInFlight = 4 * numCopiers;
  Counters.Counter combineInputCounter =
    reporter.getCounter(Task.Counter.COMBINE_INPUT_RECORDS);
  this.combinerRunner = CombinerRunner.create(conf, getTaskID(),
                                              combineInputCounter,
                                              reporter, null);
  if (combinerRunner != null) {
    combineCollector =
      new CombineOutputCollector(reduceCombineOutputCounter);
  }

  this.ioSortFactor = conf.getInt("io.sort.factor", 10);

  this.abortFailureLimit = Math.max(30, numMaps / 10);

  this.maxFetchFailuresBeforeReporting = conf.getInt(
      "mapreduce.reduce.shuffle.maxfetchfailures", REPORT_FAILURE_LIMIT);

  this.maxFailedUniqueFetches = Math.min(numMaps,
                                         this.maxFailedUniqueFetches);
  this.maxInMemOutputs = conf.getInt("mapred.inmem.merge.threshold", 1000);
  this.maxInMemCopyPer =
    conf.getFloat("mapred.job.shuffle.merge.percent", 0.66f);
  final float maxRedPer =
    conf.getFloat("mapred.job.reduce.input.buffer.percent", 0f);
  if (maxRedPer > 1.0 || maxRedPer < 0.0) {
    throw new IOException("mapred.job.reduce.input.buffer.percent" +
                          maxRedPer);
  }
  this.maxInMemReduce = (int)Math.min(
      Runtime.getRuntime().maxMemory() * maxRedPer, Integer.MAX_VALUE);

  // Setup the RamManager
  ramManager = new ShuffleRamManager(conf);

  localFileSys = FileSystem.getLocal(conf);

  rfs = ((LocalFileSystem)localFileSys).getRaw();

  // hosts -> next contact time
  this.penaltyBox = new LinkedHashMap<String, Long>();

  // hostnames
  this.uniqueHosts = new HashSet<String>();

  // Seed the random number generator with a reasonably globally unique seed
  long randomSeed = System.nanoTime() +
                    (long)Math.pow(this.reduceTask.getPartition(),
                                   (this.reduceTask.getPartition()%10)
                                  );
  this.random = new Random(randomSeed);
  this.maxMapRuntime = 0;
  this.reportReadErrorImmediately =
    conf.getBoolean("mapreduce.reduce.shuffle.notify.readerror", true);
}
 
Example 17
Source File: BumbleBench.java    From bumblebench with Apache License 2.0 4 votes vote down vote up
final boolean runAttempt(boolean lowball) throws InterruptedException {
	float under = _estimate * (1-_uncertainty/2);
	float over  = _estimate * (1+_uncertainty/2);
	float target = lowball? under : over;

	// Run an experiment
	//
	if (VERBOSE)
		out().println("attempt(" + target + ")");
	float result = attempt(target);

	// Analyze the results
	//
	boolean runSucceeded;                    // Was the target score achieved?
	boolean guessWasCorrect;                 // Was runSucceeded what we expected it to be based on the lowball/highball setting?
	boolean newEstimateWasSpecified = false; // Did the run provide an estimate of the score it can achieve?
	float oldEstimate = _estimate;
	if (result >= target && result < Float.POSITIVE_INFINITY) {
		runSucceeded = true;
		recordSuccess(target);
		guessWasCorrect = lowball;
		_estimate = result;
		newEstimateWasSpecified = true;
	} else if (result <= 0F) {
		// UNSPECIFIED_FAILURE
		runSucceeded = false;
		guessWasCorrect = !lowball;
		_estimate = lowball? _estimate * (1-_uncertainty) : _estimate;
	} else if (result < target) {
		runSucceeded = false;
		guessWasCorrect = !lowball;
		_estimate = result; // This is why we can't handle result==0 here.  Estimate hits zero and never recovers
		newEstimateWasSpecified = true;
	} else {
		// UNSPECIFIED_SUCCESS
		runSucceeded = true;
		recordSuccess(target);
		guessWasCorrect = lowball;
		_estimate = lowball? _estimate : _estimate * (1+_uncertainty);
	}
	if (_recentPeak == _maxPeak) {
		if (under <= _maxPeak && _maxPeak <= over)
			_maxPeakUncertainty = Math.min(_maxPeakUncertainty, _uncertainty);
	}
	if (!runSucceeded && target < _recentPeak)
		_recentPeak = Float.NEGATIVE_INFINITY;
	float oldUncertainty = _uncertainty;
	if (runSucceeded && target >= Float.POSITIVE_INFINITY) {
		// lowball guess or not, if we thought it was infinitely fast and the
		// runSucceeded, we were right.  Otherwise, we may never terminate,
		// always attempting to increase the already-infinite target score ever higher.
		guessWasCorrect = true;
	}
	if (newEstimateWasSpecified) {
		float impliedUncertainty = Math.abs(oldEstimate - result) / target;
		if (impliedUncertainty > _uncertainty) {
			if (TAME_UNCERTAINTY) {
				// If the estimate was way off, just bump up the _uncertainty as though our guess was incorrect
				_uncertainty *= INCORRECT_GUESS_ADJUSTMENT;
			} else {
				_uncertainty = impliedUncertainty;
			}
		} else {
			_uncertainty *= guessWasCorrect? CORRECT_GUESS_ADJUSTMENT : INCORRECT_GUESS_ADJUSTMENT;
		}
	} else {
		_uncertainty *= guessWasCorrect? CORRECT_GUESS_ADJUSTMENT : INCORRECT_GUESS_ADJUSTMENT;
	}
	_uncertainty = Math.min(_uncertainty, MAX_UNCERTAINTY); 
	report(target, result, oldUncertainty, lowball, guessWasCorrect, runSucceeded);
	return guessWasCorrect;
}
 
Example 18
Source File: TransferFsImage.java    From RDFS with Apache License 2.0 4 votes vote down vote up
/**
 * A server-side method to respond to a getfile http request
 * Copies the contents of the local file into the output stream.
 */
static void getFileServer(OutputStream outstream, File localfile, DataTransferThrottler throttler) 
  throws IOException {
  byte buf[] = new byte[BUFFER_SIZE];
  FileInputStream infile = null;
  long totalReads = 0, totalSends = 0;
  try {
    infile = new FileInputStream(localfile);
    if (ErrorSimulator.getErrorSimulation(2)
        && localfile.getAbsolutePath().contains("secondary")) {
      // throw exception only when the secondary sends its image
      throw new IOException("If this exception is not caught by the " +
          "name-node fs image will be truncated.");
    }
    
    if (ErrorSimulator.getErrorSimulation(3)
        && localfile.getAbsolutePath().contains("fsimage")) {
        // Test sending image shorter than localfile
        long len = localfile.length();
        buf = new byte[(int)Math.min(len/2, BUFFER_SIZE)];
        // This will read at most half of the image
        // and the rest of the image will be sent over the wire
        infile.read(buf);
    }
    int num = 1;
    while (num > 0) {
      long startRead = System.currentTimeMillis();
      num = infile.read(buf);
      if (num <= 0) {
        break;
      }
      outstream.write(buf, 0, num);
      if (throttler != null) {
        throttler.throttle(num);
      }
    }
  } finally {
    if (infile != null) {
      infile.close();
    }
  }
}
 
Example 19
Source File: SystemFunctions.java    From incubator-retired-mrql with Apache License 2.0 votes vote down vote up
public static MR_double min ( MR_double x, MR_double y ) { return new MR_double(Math.min(x.get(),y.get())); } 
Example 20
Source File: SystemFunctions.java    From incubator-retired-mrql with Apache License 2.0 votes vote down vote up
public static MR_long min ( MR_long x, MR_long y ) { return new MR_long(Math.min(x.get(),y.get())); }