| 1 |
package shared; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.io.PrintStream; |
| 5 |
import java.lang.management.ManagementFactory; |
| 6 |
import java.lang.reflect.Method; |
| 7 |
import java.net.UnknownHostException; |
| 8 |
import java.util.ArrayList; |
| 9 |
import java.util.Arrays; |
| 10 |
import java.util.Collections; |
| 11 |
import java.util.Comparator; |
| 12 |
import java.util.List; |
| 13 |
import java.util.Map; |
| 14 |
import java.util.Random; |
| 15 |
import java.util.concurrent.ThreadLocalRandom; |
| 16 |
|
| 17 |
public class Shared { |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
public static boolean ENV=(System.getenv()!=null); |
| 26 |
private static String HOSTNAME; |
| 27 |
public static boolean WINDOWS=envContainsPair("OS", "Win", true); |
| 28 |
public static boolean MAC=envContainsPair("OS", "Mac", true); |
| 29 |
|
| 30 |
public static boolean LINUX=envContainsPair("OS", "nix", true) || envContainsPair("OS", "nux", true) || envContainsPair("OS", "aix", true); |
| 31 |
public static boolean SOLARIS=envContainsPair("OS", "sunos", true); |
| 32 |
public static boolean GENEPOOL=envContainsPair("NERSC_HOST", "genepool", false); |
| 33 |
public static boolean DENOVO=envContainsPair("NERSC_HOST", "denovo", false); |
| 34 |
public static boolean CORI=envContainsPair("NERSC_HOST", "cori", false); |
| 35 |
public static boolean PERLMUTTER=envContainsPair("NERSC_HOST", "perlmutter", false); |
| 36 |
public static boolean LOGIN=envContainsPair("HOSTNAME", "login", true); |
| 37 |
public static boolean NERSC=envContainsKey("NERSC_HOST"); |
| 38 |
public static boolean AWS=envContainsKey("EC2_HOME"); |
| 39 |
public static boolean IGBVM="taxonomy-vm".equals(HOSTNAME()) || "taxonomy-vm-2".equals(HOSTNAME()); |
| 40 |
public static boolean DORI=envContainsPair("SLURM_PARTITION", "dori", false); |
| 41 |
public static boolean AMD64="amd64".equalsIgnoreCase(System.getProperty("os.arch")); |
| 42 |
|
| 43 |
public static void setTaxServer(String path){ |
| 44 |
taxServerNersc=taxServerAws=path; |
| 45 |
} |
| 46 |
|
| 47 |
private static String taxServerNersc="https://taxonomy.jgi.doe.gov/"; |
| 48 |
private static String ntSketchServerNersc="https://nt-sketch.jgi.doe.gov/"; |
| 49 |
private static String riboSketchServerNersc="https://ribo-sketch.jgi.doe.gov/"; |
| 50 |
private static String proteinSketchServerNersc="https://protein-sketch.jgi.doe.gov/"; |
| 51 |
private static String refseqSketchServerNersc="https://refseq-sketch.jgi.doe.gov/"; |
| 52 |
private static String demuxServer="https://demux.jgi.doe.gov/"; |
| 53 |
|
| 54 |
private static String taxServerAws="http://bbtaxonomy.org:3068/"; |
| 55 |
private static String ntSketchServerAws="http://nt-sketch.org:3071/"; |
| 56 |
private static String riboSketchServerAws="http://ribo-sketch.org:3073/"; |
| 57 |
private static String proteinSketchServerAws="http://protein-sketch.org:3074/"; |
| 58 |
private static String refseqSketchServerAws="http://refseq-sketch.org:3072/"; |
| 59 |
|
| 60 |
public static String taxServer(){return awsServers ? taxServerAws : taxServerNersc;} |
| 61 |
public static String ntSketchServer(){return awsServers ? ntSketchServerAws : ntSketchServerNersc;} |
| 62 |
public static String riboSketchServer(){return awsServers ? riboSketchServerAws : riboSketchServerNersc;} |
| 63 |
public static String proteinSketchServer(){return awsServers ? proteinSketchServerAws : proteinSketchServerNersc;} |
| 64 |
public static String refseqSketchServer(){return awsServers ? refseqSketchServerAws : refseqSketchServerNersc;} |
| 65 |
public static String demuxServer(){return demuxServer;} |
| 66 |
|
| 67 |
public static boolean awsServers=false; |
| 68 |
|
| 69 |
public static boolean envContainsPair(String key, String value, boolean loose){ |
| 70 |
Map<String, String> map=System.getenv(); |
| 71 |
String v=map.get(key); |
| 72 |
if(value==null || v==null){return v==value;} |
| 73 |
return loose ? v.contains(value.toLowerCase()) : value.equalsIgnoreCase(v); |
| 74 |
} |
| 75 |
|
| 76 |
public static boolean envContainsKey(String key){ |
| 77 |
Map<String, String> map=System.getenv(); |
| 78 |
return map.containsKey(key); |
| 79 |
} |
| 80 |
|
| 81 |
public static String HOSTNAME(){ |
| 82 |
if(HOSTNAME==null){ |
| 83 |
try { |
| 84 |
java.net.InetAddress localMachine = java.net.InetAddress.getLocalHost(); |
| 85 |
HOSTNAME=localMachine.getHostName(); |
| 86 |
} catch (UnknownHostException e) { |
| 87 |
|
| 88 |
|
| 89 |
HOSTNAME="unknown"; |
| 90 |
} catch (NullPointerException e) { |
| 91 |
|
| 92 |
|
| 93 |
HOSTNAME="unknown"; |
| 94 |
} catch (Throwable e) { |
| 95 |
HOSTNAME="unknown"; |
| 96 |
} |
| 97 |
} |
| 98 |
return HOSTNAME; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
public static void main(String[] args){ |
| 107 |
COMMAND_LINE=args; |
| 108 |
mainClass=Shared.class; |
| 109 |
assert(false) : fullCommandline(); |
| 110 |
} |
| 111 |
|
| 112 |
public static int LOGICAL_PROCESSORS=CALC_LOGICAL_PROCESSORS(); |
| 113 |
private static int THREADS=setThreads(-1); |
| 114 |
|
| 115 |
private static int READ_BUFFER_NUM_BUFFERS=setBuffers(); |
| 116 |
private static int READ_BUFFER_LENGTH=200; |
| 117 |
private static long READ_BUFFER_MAX_DATA=400000; |
| 118 |
|
| 119 |
public static boolean OUTPUT_KMG=true; |
| 120 |
|
| 121 |
|
| 122 |
public static boolean AMINO_IN=false; |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
public static final int GAPBUFFER=64; |
| 127 |
public static final int GAPBUFFER2=2*GAPBUFFER; |
| 128 |
public static final int GAPLEN=128; |
| 129 |
public static final int MINGAP=GAPBUFFER2+GAPLEN; |
| 130 |
public static final int GAPCOST=Tools.max(1, GAPLEN/64); |
| 131 |
public static final byte GAPC='-'; |
| 132 |
|
| 133 |
public static String BBTOOLS_VERSION_STRING="39.20"; |
| 134 |
public static String BBMAP_VERSION_NAME="Grass Blade"; |
| 135 |
|
| 136 |
public static boolean TRIM_READ_COMMENTS=false; |
| 137 |
public static boolean TRIM_RNAME=false; |
| 138 |
|
| 139 |
public static boolean USE_JNI=(CORI || DENOVO || GENEPOOL || NERSC || AWS || (AMD64 && (LINUX || MAC))) && !WINDOWS; |
| 140 |
public static boolean USE_MPI=false; |
| 141 |
public static boolean MPI_KEEP_ALL=true; |
| 142 |
|
| 143 |
public static boolean USE_CRISMPI=true; |
| 144 |
public static int MPI_RANK=0; |
| 145 |
public static int MPI_NUM_RANKS=1; |
| 146 |
|
| 147 |
public static int FASTA_WRAP=70; |
| 148 |
public static byte FAKE_QUAL=30; |
| 149 |
|
| 150 |
public static boolean FIX_EXTENSIONS=true; |
| 151 |
|
| 152 |
|
| 153 |
private static boolean EA=false; |
| 154 |
|
| 155 |
public static boolean EA(){return EA;} |
| 156 |
|
| 157 |
public static String BBMAP_CLASS=null; |
| 158 |
public static Class<?> mainClass=null; |
| 159 |
public static String[] COMMAND_LINE=null; |
| 160 |
|
| 161 |
public static final byte PLUS=0; |
| 162 |
public static final byte MINUS=1; |
| 163 |
|
| 164 |
public static final String[] strandCodes={"+", "-", "?"}; |
| 165 |
public static final char[] strandCodes2={'+', '-', '?'}; |
| 166 |
|
| 167 |
public static List<String> JVM_ARGS(){ |
| 168 |
return ManagementFactory.getRuntimeMXBean().getInputArguments(); |
| 169 |
} |
| 170 |
|
| 171 |
public static String fullCommandline(){return fullCommandline(true, true);} |
| 172 |
public static String fullCommandline(boolean includeCP){return fullCommandline(includeCP, true);} |
| 173 |
public static String fullCommandline(boolean includeCP, boolean includeArgs){ |
| 174 |
StringBuilder sb=new StringBuilder(); |
| 175 |
if(includeArgs) { |
| 176 |
sb.append("java "); |
| 177 |
for(String s : JVM_ARGS()) { |
| 178 |
sb.append(s).append(' '); |
| 179 |
} |
| 180 |
} |
| 181 |
if(includeCP) { |
| 182 |
sb.append("-cp "+System.getProperty("java.class.path")+" "); |
| 183 |
} |
| 184 |
sb.append(mainClass.getCanonicalName()).append(' '); |
| 185 |
for(String s : COMMAND_LINE) { |
| 186 |
sb.append(s).append(' '); |
| 187 |
} |
| 188 |
sb.setLength(sb.length()-1); |
| 189 |
return sb.toString(); |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
private static String TMPDIR=getTmpdir(); |
| 194 |
|
| 195 |
|
| 196 |
private static String getTmpdir(){ |
| 197 |
String s=System.getenv("SLURM_TMP"); |
| 198 |
if(s==null){s=System.getenv("TMPDIR");} |
| 199 |
if(s!=null){s=(s+"/").replaceAll("//", "/").replaceAll("\\\\", "/");} |
| 200 |
return s; |
| 201 |
} |
| 202 |
|
| 203 |
public static String tmpdir(){return TMPDIR;} |
| 204 |
|
| 205 |
public static String setTmpdir(String s){ |
| 206 |
if(s==null){TMPDIR=null;} |
| 207 |
else{ |
| 208 |
s=s.replaceAll("\\\\", "/"); |
| 209 |
if(!s.endsWith("/")){s=s+"/";} |
| 210 |
TMPDIR=s.replaceAll("//", "/"); |
| 211 |
} |
| 212 |
return TMPDIR; |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
public static boolean anomaly=!(System.getProperty("user.dir")+"").contains("/bushnell/") && !WINDOWS; |
| 218 |
|
| 219 |
public static final char[] getTLCB(int len){ |
| 220 |
char[] buffer=TLCB.get(); |
| 221 |
if(buffer==null || buffer.length<len){ |
| 222 |
buffer=new char[len]; |
| 223 |
if(len<1000000){TLCB.set(buffer);} |
| 224 |
} |
| 225 |
return buffer; |
| 226 |
} |
| 227 |
private static final ThreadLocal<char[]> TLCB=new ThreadLocal<char[]>(); |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
public static int capThreads(int t) { |
| 234 |
assert(THREADS>0) : THREADS; |
| 235 |
final int old=THREADS; |
| 236 |
THREADS=Tools.mid(1, t, old); |
| 237 |
assert(THREADS>0) : THREADS; |
| 238 |
return old; |
| 239 |
} |
| 240 |
|
| 241 |
public static int setThreads(String x){ |
| 242 |
int y=LOGICAL_PROCESSORS; |
| 243 |
if(x!=null && !x.equalsIgnoreCase("auto")){ |
| 244 |
if(x.indexOf('.')>=0){ |
| 245 |
double d=Double.parseDouble(x); |
| 246 |
if(d>1){ |
| 247 |
y=(int)d; |
| 248 |
}else{ |
| 249 |
y=(int)Tools.max(1, Math.ceil(d*y)); |
| 250 |
} |
| 251 |
}else{ |
| 252 |
y=Integer.parseInt(x); |
| 253 |
} |
| 254 |
} |
| 255 |
return setThreads(y); |
| 256 |
} |
| 257 |
|
| 258 |
public static int setThreads(int x){ |
| 259 |
if(x>0){ |
| 260 |
THREADS=x; |
| 261 |
}else{ |
| 262 |
THREADS=Tools.max(1, LOGICAL_PROCESSORS); |
| 263 |
} |
| 264 |
setBuffers(); |
| 265 |
assert(THREADS>0) : THREADS; |
| 266 |
return THREADS; |
| 267 |
} |
| 268 |
|
| 269 |
public static int threads(){ |
| 270 |
assert(THREADS>0) : THREADS; |
| 271 |
return THREADS; |
| 272 |
} |
| 273 |
|
| 274 |
public static int CALC_LOGICAL_PROCESSORS(){ |
| 275 |
final int procs=Tools.max(1, Runtime.getRuntime().availableProcessors()); |
| 276 |
int slots=procs; |
| 277 |
if(PERLMUTTER && LOGIN) {slots=Tools.min(slots, 64);} |
| 278 |
Map<String,String> env=System.getenv(); |
| 279 |
String s=env.get("NSLOTS"); |
| 280 |
boolean success=false; |
| 281 |
if(s!=null){ |
| 282 |
int x=slots; |
| 283 |
try { |
| 284 |
x=Tools.max(1, Integer.parseInt(s)); |
| 285 |
success=true; |
| 286 |
} catch (NumberFormatException e) { |
| 287 |
|
| 288 |
} |
| 289 |
if(x<=16){slots=x;} |
| 290 |
} |
| 291 |
if(!success){ |
| 292 |
s=env.get("SLURM_CPUS_ON_NODE"); |
| 293 |
if(s!=null){ |
| 294 |
int x=slots; |
| 295 |
try { |
| 296 |
x=Tools.max(1, Integer.parseInt(s)); |
| 297 |
success=true; |
| 298 |
} catch (NumberFormatException e) { |
| 299 |
|
| 300 |
} |
| 301 |
slots=x; |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
return Tools.min(slots, procs); |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
private static int loadedJNI=-1; |
| 314 |
public static synchronized boolean loadJNI(){ |
| 315 |
return loadJNI("bbtoolsjni"); |
| 316 |
} |
| 317 |
public static synchronized boolean loadJNI(String name){ |
| 318 |
if(loadedJNI<0){ |
| 319 |
boolean success=false; |
| 320 |
|
| 321 |
String libpath=System.getProperty("java.library.path"); |
| 322 |
|
| 323 |
|
| 324 |
if(libpath==null || libpath.length()==0){ |
| 325 |
libpath=System.getProperty("java.class.path").replace("/current", "/jni"); |
| 326 |
}else if(!libpath.contains("/jni")){ |
| 327 |
libpath=libpath+":"+System.getProperty("java.class.path").replace("/current", "/jni"); |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
try{ |
| 332 |
System.loadLibrary(name); |
| 333 |
success=true; |
| 334 |
}catch(UnsatisfiedLinkError e){} |
| 335 |
|
| 336 |
if(!success){ |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
libpath=libpath.replace("-Djava.library.path=",""); |
| 342 |
String[] libpathEntries=libpath.split(File.pathSeparator); |
| 343 |
for(int i=0; i<libpathEntries.length && !success; i++){ |
| 344 |
String lib=libpathEntries[i]+"/"+System.mapLibraryName(name); |
| 345 |
try{ |
| 346 |
System.load(lib); |
| 347 |
success=true; |
| 348 |
}catch(UnsatisfiedLinkError e2){ |
| 349 |
success=false; |
| 350 |
} |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
if(success){ |
| 355 |
loadedJNI=1; |
| 356 |
}else{ |
| 357 |
loadedJNI=0; |
| 358 |
System.err.println("Native library can not be found in java.library.path."); |
| 359 |
new Exception().printStackTrace(); |
| 360 |
System.exit(1); |
| 361 |
} |
| 362 |
} |
| 363 |
return loadedJNI==1; |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
public static int capBuffers(int num){ |
| 371 |
return setBuffers(Tools.min(num, READ_BUFFER_NUM_BUFFERS)); |
| 372 |
} |
| 373 |
|
| 374 |
public static int READ_BUFFER_NUM_BUFFERS() { |
| 375 |
return READ_BUFFER_NUM_BUFFERS; |
| 376 |
} |
| 377 |
|
| 378 |
public static int setBuffers(){ |
| 379 |
return setBuffersFromThreads(THREADS); |
| 380 |
} |
| 381 |
|
| 382 |
public static int setBuffersFromThreads(int threads){ |
| 383 |
return setBuffers(Tools.max(4, (threads*3)/2)); |
| 384 |
} |
| 385 |
|
| 386 |
|
| 387 |
public static int setBuffers(int num){ |
| 388 |
|
| 389 |
num=Tools.max(2, num); |
| 390 |
return READ_BUFFER_NUM_BUFFERS=num; |
| 391 |
} |
| 392 |
|
| 393 |
|
| 394 |
public static int numBuffers(){ |
| 395 |
return READ_BUFFER_NUM_BUFFERS; |
| 396 |
} |
| 397 |
|
| 398 |
public static int bufferLen(){ |
| 399 |
return READ_BUFFER_LENGTH; |
| 400 |
} |
| 401 |
|
| 402 |
public static long bufferData(){ |
| 403 |
return READ_BUFFER_MAX_DATA; |
| 404 |
} |
| 405 |
|
| 406 |
public static void capBufferLen(int x){ |
| 407 |
if(x!=READ_BUFFER_LENGTH){setBufferLen(Tools.min(x, READ_BUFFER_LENGTH));} |
| 408 |
} |
| 409 |
|
| 410 |
public static int setBufferLen(int x){ |
| 411 |
|
| 412 |
assert(x>0); |
| 413 |
return READ_BUFFER_LENGTH=x; |
| 414 |
} |
| 415 |
|
| 416 |
public static long setBufferData(long x){ |
| 417 |
|
| 418 |
assert(x>0); |
| 419 |
return READ_BUFFER_MAX_DATA=x; |
| 420 |
} |
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
public static boolean LOW_MEMORY=false; |
| 428 |
|
| 429 |
|
| 430 |
public static final double xmsRatio(){ |
| 431 |
Runtime rt=Runtime.getRuntime(); |
| 432 |
return rt.totalMemory()*1.0/rt.maxMemory(); |
| 433 |
} |
| 434 |
|
| 435 |
public static long memAvailable(int readThreads){ |
| 436 |
long usableMemory; |
| 437 |
{ |
| 438 |
long memory=Runtime.getRuntime().maxMemory(); |
| 439 |
double xmsRatio=Shared.xmsRatio(); |
| 440 |
usableMemory=(long)Tools.max(((memory-48000000-(Tools.max(readThreads, 4)*400000))*(xmsRatio>0.97 ? 0.82 : 0.72)), memory*0.45); |
| 441 |
} |
| 442 |
return usableMemory; |
| 443 |
} |
| 444 |
|
| 445 |
public static long memTotal(){ |
| 446 |
Runtime rt=Runtime.getRuntime(); |
| 447 |
return rt.maxMemory(); |
| 448 |
} |
| 449 |
|
| 450 |
public static long memFree(){ |
| 451 |
Runtime rt=Runtime.getRuntime(); |
| 452 |
return rt.freeMemory(); |
| 453 |
} |
| 454 |
|
| 455 |
public static long memAvailable(){ |
| 456 |
Runtime rt=Runtime.getRuntime(); |
| 457 |
return rt.maxMemory()-rt.totalMemory()+rt.freeMemory(); |
| 458 |
} |
| 459 |
|
| 460 |
|
| 461 |
public static long memAvailableAdvanced(){ |
| 462 |
Runtime rt=Runtime.getRuntime(); |
| 463 |
final long mmemory=rt.maxMemory(); |
| 464 |
final long tmemory=rt.totalMemory(); |
| 465 |
final long fmemory=rt.freeMemory(); |
| 466 |
final long umemory=tmemory-fmemory; |
| 467 |
|
| 468 |
double xmsRatio=Shared.xmsRatio(); |
| 469 |
double usableMemory=Tools.max(((mmemory-96000000)*(xmsRatio>0.97 ? 0.82 : 0.72)), mmemory*0.45); |
| 470 |
double availableMemory=Tools.max(fmemory*0.5, usableMemory-umemory); |
| 471 |
|
| 472 |
|
| 473 |
return (long)availableMemory; |
| 474 |
} |
| 475 |
|
| 476 |
public static long memUsed(){ |
| 477 |
Runtime rt=Runtime.getRuntime(); |
| 478 |
return rt.maxMemory()-rt.freeMemory(); |
| 479 |
} |
| 480 |
|
| 481 |
|
| 482 |
public static final void printMemory(){ |
| 483 |
try{ |
| 484 |
if(GC_BEFORE_PRINT_MEMORY){ |
| 485 |
System.gc(); |
| 486 |
System.gc(); |
| 487 |
} |
| 488 |
Runtime rt=Runtime.getRuntime(); |
| 489 |
long mmemory=rt.maxMemory()/1000000; |
| 490 |
long tmemory=rt.totalMemory()/1000000; |
| 491 |
long fmemory=rt.freeMemory()/1000000; |
| 492 |
long umemory=tmemory-fmemory; |
| 493 |
System.err.println("Memory: "+"max="+mmemory+"m, total="+tmemory+"m, "+ |
| 494 |
"free="+fmemory+"m, used="+umemory+"m"); |
| 495 |
}catch(Throwable t){} |
| 496 |
} |
| 497 |
|
| 498 |
public static final Random threadLocalRandom(){return threadLocalRandom(-1);} |
| 499 |
public static final Random threadLocalRandom(long seed){ |
| 500 |
Random randy; |
| 501 |
if(seed>=0){return new Random(seed);} |
| 502 |
try { |
| 503 |
randy=ThreadLocalRandom.current(); |
| 504 |
} catch (Throwable e) { |
| 505 |
randy=new Random(); |
| 506 |
} |
| 507 |
return randy; |
| 508 |
} |
| 509 |
|
| 510 |
|
| 511 |
public static boolean GC_BEFORE_PRINT_MEMORY=false; |
| 512 |
|
| 513 |
|
| 514 |
public static String comment; |
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
public static final void sort(int[] array){sort(array, 0, array.length);} |
| 521 |
public static final void sort(int[] array, int from, int to){ |
| 522 |
try { |
| 523 |
if(!parallelSort || array.length<=parallelSortLength){ |
| 524 |
Arrays.sort(array, from, to); |
| 525 |
return; |
| 526 |
} |
| 527 |
Arrays.parallelSort(array, from, to); |
| 528 |
} catch (OutOfMemoryError e) { |
| 529 |
KillSwitch.memKill(e); |
| 530 |
} |
| 531 |
} |
| 532 |
|
| 533 |
public static final void sort(long[] array){sort(array, 0, array.length);} |
| 534 |
public static final void sort(long[] array, int from, int to){ |
| 535 |
try { |
| 536 |
if(!parallelSort || array.length<=parallelSortLength || THREADS<2){ |
| 537 |
Arrays.sort(array, from, to); |
| 538 |
return; |
| 539 |
} |
| 540 |
Arrays.parallelSort(array, from, to); |
| 541 |
} catch (OutOfMemoryError e) { |
| 542 |
KillSwitch.memKill(e); |
| 543 |
} |
| 544 |
} |
| 545 |
|
| 546 |
public static final void sort(float[] array){sort(array, 0, array.length);} |
| 547 |
public static final void sort(float[] array, int from, int to){ |
| 548 |
try { |
| 549 |
if(!parallelSort || array.length<=parallelSortLength){ |
| 550 |
Arrays.sort(array, from, to); |
| 551 |
return; |
| 552 |
} |
| 553 |
Arrays.parallelSort(array, from, to); |
| 554 |
} catch (OutOfMemoryError e) { |
| 555 |
KillSwitch.memKill(e); |
| 556 |
} |
| 557 |
} |
| 558 |
|
| 559 |
public static final void sort(double[] array){sort(array, 0, array.length);} |
| 560 |
public static final void sort(double[] array, int from, int to){ |
| 561 |
try { |
| 562 |
if(!parallelSort || array.length<=parallelSortLength){ |
| 563 |
Arrays.sort(array, from, to); |
| 564 |
return; |
| 565 |
} |
| 566 |
Arrays.parallelSort(array, from, to); |
| 567 |
} catch (OutOfMemoryError e) { |
| 568 |
KillSwitch.memKill(e); |
| 569 |
} |
| 570 |
} |
| 571 |
|
| 572 |
public static final <T extends Comparable<? super T>> void sort(T[] array){sort(array, 0, array.length);} |
| 573 |
public static final <T extends Comparable<? super T>> void sort(T[] array, int from, int to){ |
| 574 |
try { |
| 575 |
if(!parallelSort || array.length<=parallelSortLength || THREADS<2){ |
| 576 |
Arrays.sort(array, from, to); |
| 577 |
return; |
| 578 |
} |
| 579 |
Arrays.parallelSort(array, from, to); |
| 580 |
} catch (OutOfMemoryError e) { |
| 581 |
KillSwitch.memKill(e); |
| 582 |
} |
| 583 |
} |
| 584 |
|
| 585 |
public static final <T extends Comparable<? super T>> void sort(T[] array, Comparator<? super T> comparator){sort(array, 0, array.length, comparator);} |
| 586 |
public static final <T extends Comparable<? super T>> void sort(T[] array, int from, int to, Comparator<? super T> comparator){ |
| 587 |
try { |
| 588 |
if(!parallelSort || array.length<=parallelSortLength || THREADS<2){ |
| 589 |
Arrays.sort(array, from, to, comparator); |
| 590 |
return; |
| 591 |
} |
| 592 |
Arrays.parallelSort(array, from, to, comparator); |
| 593 |
} catch (OutOfMemoryError e) { |
| 594 |
KillSwitch.memKill(e); |
| 595 |
} |
| 596 |
} |
| 597 |
|
| 598 |
public static final <T extends Comparable<? super T>> void sort(ArrayList<T> list){ |
| 599 |
try { |
| 600 |
if(!parallelSort || list.size()<=parallelSortLength || THREADS<2){ |
| 601 |
Collections.sort(list); |
| 602 |
return; |
| 603 |
} |
| 604 |
|
| 605 |
{ |
| 606 |
@SuppressWarnings("unchecked") |
| 607 |
T[] array=list.toArray((T[])new Comparable[0]); |
| 608 |
list.clear(); |
| 609 |
Arrays.parallelSort(array); |
| 610 |
for(T r : array){list.add(r);} |
| 611 |
} |
| 612 |
} catch (OutOfMemoryError e) { |
| 613 |
KillSwitch.memKill(e); |
| 614 |
} |
| 615 |
} |
| 616 |
|
| 617 |
public static final <T> void sort(ArrayList<T> list, Comparator<? super T> comparator){ |
| 618 |
try { |
| 619 |
if(!parallelSort){ |
| 620 |
Collections.sort(list, comparator); |
| 621 |
return; |
| 622 |
} |
| 623 |
|
| 624 |
{ |
| 625 |
if(list.size()<=parallelSortLength || THREADS<2){ |
| 626 |
list.sort(comparator); |
| 627 |
return; |
| 628 |
} |
| 629 |
@SuppressWarnings("unchecked") |
| 630 |
T[] array=list.toArray((T[])new Object[0]); |
| 631 |
list.clear(); |
| 632 |
Arrays.parallelSort(array, comparator); |
| 633 |
for(T r : array){list.add(r);} |
| 634 |
} |
| 635 |
} catch (OutOfMemoryError e) { |
| 636 |
KillSwitch.memKill(e); |
| 637 |
} |
| 638 |
} |
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
public static void closeStream(PrintStream outstream) { |
| 646 |
if(outstream!=null){ |
| 647 |
synchronized(outstream){ |
| 648 |
if(outstream!=System.err && outstream!=System.out){ |
| 649 |
outstream.close(); |
| 650 |
} |
| 651 |
} |
| 652 |
} |
| 653 |
} |
| 654 |
|
| 655 |
public static final int MAX_ARRAY_LEN=Integer.MAX_VALUE-20; |
| 656 |
|
| 657 |
public static final int parallelSortLength=10000; |
| 658 |
private static boolean disableParallelSort=false; |
| 659 |
public static boolean parallelSort=testParallelSort(); |
| 660 |
public static boolean SIMD=false; |
| 661 |
|
| 662 |
public static double javaVersion=parseJavaVersion(); |
| 663 |
|
| 664 |
private static double parseJavaVersion(){ |
| 665 |
String s=System.getProperty("java.version"); |
| 666 |
if(s==null){return 1.6;} |
| 667 |
int dots=0; |
| 668 |
StringBuilder sb=new StringBuilder(); |
| 669 |
for(int i=0; i<s.length() && dots<2; i++){ |
| 670 |
char c=s.charAt(i); |
| 671 |
if(c=='.'){dots++;} |
| 672 |
else if(!Tools.isDigit(c)){break;} |
| 673 |
if(dots>1){break;} |
| 674 |
sb.append(c); |
| 675 |
} |
| 676 |
return Double.parseDouble(sb.toString()); |
| 677 |
} |
| 678 |
|
| 679 |
public static void setParallelSort(boolean x){ |
| 680 |
if(x){ |
| 681 |
disableParallelSort=false; |
| 682 |
parallelSort=testParallelSort(); |
| 683 |
}else{ |
| 684 |
disableParallelSort=true; |
| 685 |
parallelSort=false; |
| 686 |
} |
| 687 |
} |
| 688 |
|
| 689 |
|
| 690 |
private static boolean testParallelSort(){ |
| 691 |
Method m=null; |
| 692 |
try { |
| 693 |
m=Arrays.class.getMethod("parallelSort", new Class[] {Object[].class, Comparator.class}); |
| 694 |
} catch (NoSuchMethodException e) { |
| 695 |
|
| 696 |
|
| 697 |
} catch (SecurityException e) { |
| 698 |
|
| 699 |
|
| 700 |
} catch (Throwable t) { |
| 701 |
|
| 702 |
|
| 703 |
} |
| 704 |
return m!=null; |
| 705 |
} |
| 706 |
|
| 707 |
static{ |
| 708 |
assert(EA=true); |
| 709 |
KillSwitch.addBallast(); |
| 710 |
} |
| 711 |
|
| 712 |
} |