/** * Returns the number of threads in the thread pool. 0 and 1 mean no thread pool. * @return the number of threads in the thread pool */ public static int getThreadPoolSize() { createThreadPool(); return nprocs; }
/** * Constructor. Clustering data into k clusters. * @param data the dataset for clustering. * @param distance the distance/dissimilarity measure. * @param k the number of clusters. * @param maxNeighbor the maximum number of neighbors examined during a random search of local minima. */ public CLARANS(T[] data, Distance<T> distance, int k, int maxNeighbor) { this(data, distance, k, maxNeighbor, Math.max(2, MulticoreExecutor.getThreadPoolSize())); }
for (double fi : MulticoreExecutor.run(ftasks)) { f += fi;
/** Compute the Gaussian kernel (search the width for given perplexity. */ private double[][] expd(double[][] D, double perplexity, double tol) { int n = D.length; double[][] P = new double[n][n]; double[] DiSum = Math.rowSums(D); int nprocs = MulticoreExecutor.getThreadPoolSize(); int chunk = n / nprocs; List<PerplexityTask> tasks = new ArrayList<>(); for (int i = 0; i < nprocs; i++) { int start = i * chunk; int end = i == nprocs-1 ? n : (i+1) * chunk; PerplexityTask task = new PerplexityTask(start, end, D, P, DiSum, perplexity, tol); tasks.add(task); } try { MulticoreExecutor.run(tasks); } catch (Exception e) { logger.error("t-SNE Gaussian kernel width search task fails: {}", e); } return P; }
@Override public RBFNetwork<T> train(T[] x, double[] y) { @SuppressWarnings("unchecked") T[] centers = (T[]) java.lang.reflect.Array.newInstance(x.getClass().getComponentType(), m); GaussianRadialBasis gaussian = SmileUtils.learnGaussianRadialBasis(x, centers, distance); if (rbf == null) { return new RBFNetwork<>(x, y, distance, gaussian, centers, normalized); } else { return new RBFNetwork<>(x, y, distance, rbf, centers, normalized); } }
/** Creates the worker thread pool. */ private static void createThreadPool() { if (nprocs == -1) { int n = -1; try { String env = System.getProperty("smile.threads"); if (env != null) { n = Integer.parseInt(env); } } catch (Exception ex) { logger.error("Failed to create multi-core execution thread pool", ex); } if (n < 1) { nprocs = Runtime.getRuntime().availableProcessors(); } else { nprocs = n; } if (nprocs > 1) { threads = (ThreadPoolExecutor) Executors.newFixedThreadPool(nprocs, new SimpleDeamonThreadFactory()); } } }
int m = MulticoreExecutor.getThreadPoolSize(); if (n < 1000 || m < 2) { for (int i = 0; i < n; i++) { for (double fi : MulticoreExecutor.run(tasks)) { f += fi;
for (double[] gi : MulticoreExecutor.run(gtasks)) { f += gi[w.length]; for (int i = 0; i < w.length; i++) {
/** * Mines the frequent item sets. The discovered frequent item sets * will be printed out to the provided stream. * @param out a print stream for output of frequent item sets. * @return the number of discovered frequent item sets. */ private long learn(PrintStream out, List<ItemSet> list, TotalSupportTree ttree) { if (MulticoreExecutor.getThreadPoolSize() > 1) { return grow(out, list, ttree, T0, null, null, null); } else { return grow(out, list, ttree, T0, null); } }
@Override public RBFNetwork<T> train(T[] x, int[] y) { @SuppressWarnings("unchecked") T[] centers = (T[]) java.lang.reflect.Array.newInstance(x.getClass().getComponentType(), m); GaussianRadialBasis gaussian = SmileUtils.learnGaussianRadialBasis(x, centers, distance); if (rbf == null) { return new RBFNetwork<>(x, y, distance, gaussian, centers, normalized); } else { return new RBFNetwork<>(x, y, distance, rbf, centers, normalized); } }
for (double fi : MulticoreExecutor.run(ftasks)) { f += fi;
/** * Constructor. */ MultiClassObjectiveFunction(double[][] x, int[] y, int k, double lambda) { this.x = x; this.y = y; this.k = k; this.lambda = lambda; int n = x.length; int m = MulticoreExecutor.getThreadPoolSize(); if (n >= 1000 && m >= 2) { ftasks = new ArrayList<>(m + 1); gtasks = new ArrayList<>(m + 1); int step = n / m; if (step < 100) { step = 100; } int start = 0; int end = step; for (int i = 0; i < m - 1; i++) { ftasks.add(new FTask(start, end)); gtasks.add(new GTask(start, end)); start += step; end += step; } ftasks.add(new FTask(start, n)); gtasks.add(new GTask(start, n)); } }
/** * Process support vectors until converge. */ public void finish() { if (k == 2) { svm.finish(); } else { List<ProcessTask> tasks = new ArrayList<>(svms.size()); for (LASVM s : svms) { tasks.add(new ProcessTask(s)); } try { MulticoreExecutor.run(tasks); } catch (Exception e) { logger.error("Failed to train SVM on multi-core", e); } } }
/** * Constructor. */ BinaryObjectiveFunction(double[][] x, int[] y, double lambda) { this.x = x; this.y = y; this.lambda = lambda; int n = x.length; int m = MulticoreExecutor.getThreadPoolSize(); if (n >= 1000 && m >= 2) { ftasks = new ArrayList<>(m + 1); gtasks = new ArrayList<>(m + 1); int step = n / m; if (step < 100) { step = 100; } int start = 0; int end = step; for (int i = 0; i < m - 1; i++) { ftasks.add(new FTask(start, end)); gtasks.add(new GTask(start, end)); start += step; end += step; } ftasks.add(new FTask(start, n)); gtasks.add(new GTask(start, n)); } }
for (double[] gi : MulticoreExecutor.run(gtasks)) { f += gi[w.length]; for (int i = 0; i < w.length; i++) {
/** * Constructor. Clustering the data. * @param data the dataset for clustering. * @param distance the distance measure for neighborhood search. * @param k the number of clusters. Note that this is just a hint. The final * number of clusters may be less. * @param radius the neighborhood radius. */ public MEC(T[] data, Distance<T> distance, int k, double radius) { if (k < 2) { throw new IllegalArgumentException("Invalid k: " + k); } if (radius <= 0.0) { throw new IllegalArgumentException("Invalid radius: " + radius); } LinearSearch<T> naive = new LinearSearch<>(data, distance); naive.setIdenticalExcluded(false); // Initialize clusters with KMeans/CLARANS. if (data[0] instanceof double[] && distance instanceof EuclideanDistance) { KMeans kmeans = new KMeans((double[][]) data, k, 10, Math.max(1, MulticoreExecutor.getThreadPoolSize())); y = kmeans.getClusterLabel(); } else { CLARANS<T> clarans = new CLARANS<>(data, distance, k, Math.min(100, (int) Math.round(0.01 * k * (data.length - k))), Math.max(1, MulticoreExecutor.getThreadPoolSize())); y = clarans.getClusterLabel(); } learn(data, naive, k, radius, y); }
public CRF train(double[][][] sequences, int[][] labels) { CRF crf = new CRF(numClasses, eta); double[][] scaling = new double[sequences.length][]; TrellisNode[][][] trellis = new TrellisNode[sequences.length][][]; for (int i = 0; i < sequences.length; i++) { scaling[i] = new double[sequences[i].length]; trellis[i] = crf.getTrellis(sequences[i]); } List<GradientTask> gradientTasks = new ArrayList<>(); for (int i = 0; i < sequences.length; i++) { gradientTasks.add(new GradientTask(crf, trellis[i], scaling[i], labels[i])); } List<BoostingTask> boostingTasks = new ArrayList<>(); for (int i = 0; i < numClasses; i++) { boostingTasks.add(new BoostingTask(crf.potentials[i], trellis, i)); } for (int iter = 0; iter < iters; iter++) { try { MulticoreExecutor.run(gradientTasks); MulticoreExecutor.run(boostingTasks); } catch (Exception e) { logger.error("Failed to train CRF on multi-core", e); } } return crf; }
/** * Constructor. Clustering the data. * @param data the dataset for clustering. * @param distance the distance measure for neighborhood search. * @param k the number of clusters. Note that this is just a hint. The final * number of clusters may be less. * @param radius the neighborhood radius. */ public MEC(T[] data, Metric<T> distance, int k, double radius) { if (k < 2) { throw new IllegalArgumentException("Invalid k: " + k); } if (radius <= 0.0) { throw new IllegalArgumentException("Invalid radius: " + radius); } CoverTree<T> cover = new CoverTree<>(data, distance); cover.setIdenticalExcluded(false); // Initialize clusters with KMeans/CLARANS. if (data[0] instanceof double[] && distance instanceof EuclideanDistance) { KMeans kmeans = new KMeans((double[][]) data, k, 10, Math.max(1, MulticoreExecutor.getThreadPoolSize())); y = kmeans.getClusterLabel(); } else { CLARANS<T> clarans = new CLARANS<>(data, distance, k, Math.min(100, (int) Math.round(0.01 * k * (data.length - k))), Math.max(1, MulticoreExecutor.getThreadPoolSize())); y = clarans.getClusterLabel(); } learn(data, cover, k, radius, y); }
public CRF train(int[][][] sequences, int[][] labels) { CRF crf = new CRF(numFeatures, numClasses, eta); double[][] scaling = new double[sequences.length][]; TrellisNode[][][] trellis = new TrellisNode[sequences.length][][]; for (int i = 0; i < sequences.length; i++) { scaling[i] = new double[sequences[i].length]; trellis[i] = crf.getTrellis(sequences[i]); } List<GradientTask> gradientTasks = new ArrayList<>(); for (int i = 0; i < sequences.length; i++) { gradientTasks.add(new GradientTask(crf, trellis[i], scaling[i], labels[i])); } List<BoostingTask> boostingTasks = new ArrayList<>(); for (int i = 0; i < numClasses; i++) { boostingTasks.add(new BoostingTask(crf.potentials[i], trellis, i)); } for (int iter = 0; iter < iters; iter++) { try { MulticoreExecutor.run(gradientTasks); MulticoreExecutor.run(boostingTasks); } catch (Exception e) { logger.error("Failed to train CRF on multi-core", e); } } return crf; }