From 9234156f6a17fc6c8d288c138bc156f33884f4e4 Mon Sep 17 00:00:00 2001 From: EdwardRaff Date: Sun, 12 Nov 2017 00:34:48 -0500 Subject: [PATCH] Replicating and fixing 2nd bug reported in issue #68 --- .../jsat/clustering/kmeans/NaiveKMeans.java | 26 ++++++++----------- .../DivisiveGlobalClustererTest.java | 2 +- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/JSAT/src/jsat/clustering/kmeans/NaiveKMeans.java b/JSAT/src/jsat/clustering/kmeans/NaiveKMeans.java index 985dd5d8..c6acb9e4 100644 --- a/JSAT/src/jsat/clustering/kmeans/NaiveKMeans.java +++ b/JSAT/src/jsat/clustering/kmeans/NaiveKMeans.java @@ -8,7 +8,6 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.atomic.AtomicInteger; -import java.util.concurrent.atomic.AtomicIntegerArray; import java.util.logging.Level; import java.util.logging.Logger; import jsat.DataSet; @@ -23,8 +22,8 @@ import jsat.utils.FakeExecutor; import jsat.utils.SystemInfo; import jsat.utils.concurrent.AtomicDoubleArray; +import jsat.utils.concurrent.ParallelUtils; import jsat.utils.random.RandomUtil; -import jsat.utils.random.XORWOW; /** * An implementation of Lloyd's K-Means clustering algorithm using the @@ -40,13 +39,12 @@ public class NaiveKMeans extends KMeans { + private static final long serialVersionUID = 6164910874898843069L; - private static final long serialVersionUID = 6164910874898843069L; - - /** - * Creates a new naive k-Means cluster using - * {@link SeedSelection#KPP k-means++} for the - * seed selection and the {@link EuclideanDistance} + /** + * Creates a new naive k-Means cluster using + * {@link SeedSelection#KPP k-means++} for the seed selection and the + * {@link EuclideanDistance} */ public NaiveKMeans() { @@ -161,17 +159,17 @@ protected Vec[] initialValue() } }; + final int N = dataSet.getSampleSize(); Arrays.fill(assignment, -1); do { changes.set(0); - int extra = dataSet.getSampleSize() % SystemInfo.LogicalCores; - int start = 0; final CountDownLatch latch = new CountDownLatch(SystemInfo.LogicalCores); - while(start < dataSet.getSampleSize()) + + for(int id = 0; id < SystemInfo.LogicalCores; id++) { - final int s = start; - final int end = start + blockSize + (extra-- > 0 ? 1 : 0); + final int s = ParallelUtils.getStartBlock(N, id); + final int end = ParallelUtils.getEndBlock(N, id); threadpool.submit(new Runnable() { @Override @@ -220,8 +218,6 @@ public void run() latch.countDown(); } }); - - start = end; } try diff --git a/JSAT/test/jsat/clustering/hierarchical/DivisiveGlobalClustererTest.java b/JSAT/test/jsat/clustering/hierarchical/DivisiveGlobalClustererTest.java index 96d021d5..b4c53b8c 100644 --- a/JSAT/test/jsat/clustering/hierarchical/DivisiveGlobalClustererTest.java +++ b/JSAT/test/jsat/clustering/hierarchical/DivisiveGlobalClustererTest.java @@ -57,7 +57,7 @@ public static void tearDownClass() throws Exception public void setUp() { DistanceMetric dm = new EuclideanDistance(); - dgc = new DivisiveGlobalClusterer(new HamerlyKMeans(), new DaviesBouldinIndex(dm)); + dgc = new DivisiveGlobalClusterer(new NaiveKMeans(), new DaviesBouldinIndex(dm)); } @After