コード例 #1
0
def quiet_runs(nPts_list, nDim_list, nClusters_list, nRep_list):
    # quiet_runs(nTest_list, nPts_list, nDim_list, nClusters_list [, print_it]):
    # when number of tests is -1, it will be calculated based on the size of the problem
    print "mpi_kmeans timing runs"
    for pts in nPts_list:
        for dim in nDim_list:
            if dim >= pts:
                continue
            for clst in nClusters_list:
                if clst >= pts:
                    continue
                for rep in nRep_list:
                    data = N.random.rand(pts, dim)
                    t1 = time.time()
                    py_kmeans.kmeans(data, clst, rep, 0)
                    t2 = time.time()
                    print "[MPIKMEANS]({0:8},{1:5},{2:5},{3:5})...".format(pts, dim, clst, rep),
                    print 1000.*(t2-t1)
コード例 #2
0
ファイル: verify.py プロジェクト: dbelll/bell_d_project
def run_labels(data, nClusters, nReps, seed=SEED):
    random.seed(seed)
    # run py_kmeans.kmeans once to get a starting label assignment,
    # which will be used by the scipy routine and others
    clusters, dist, labels = py_kmeans.kmeans(data, nClusters, 1, 0)
    if VERBOSE:
        print "data"
        print data
        print "initial clusters:"
        print clusters
 
    (nPts, nDim) = data.shape
    nClusters = clusters.shape[0] 
    print "[nPts:{0:6}][nDim:{1:4}][nClusters:{2:4}][nReps:{3:3}]...".format(nPts, nDim, 
                                                                            nClusters, nReps),

    data2 = np.swapaxes(data, 0, 1).astype(np.float32).copy('C')
    clusters2 = np.swapaxes(clusters, 0, 1).astype(np.float32).copy('C')

    if VERBOSE:
        print "data2"
        print data2
        print "clusters2"
        print clusters2

    """
    t1 = time.time()
    (cuda_clusters, cuda_labels) = cuda_kmeans.kmeans_gpu(data2, clusters2, nReps+1)
    if VERBOSE:
        print "cuda_kmeans labels:"
        print cuda_labels
    t2 = time.time()
    if PRINT_TIMES:
        print "\ncuda ", t2-t1
    """
    
    t1 = time.time()
    (tri_clusters, tri_labels) = cuda_kmeans_tri.trikmeans_gpu(data2, clusters2, nReps+1)
    if VERBOSE:
        print "cuda_kmeans_tri labels:"
        print tri_labels
    t2 = time.time()
    if PRINT_TIMES:
        print "tri  ", t2-t1

    t1 = time.time()
    labels_mpi = mpi_labels(data, nClusters, nReps+1, seed)
    if VERBOSE:
        print "mpi labels:"
        print labels_mpi[0]
    t2 = time.time()
    if PRINT_TIMES:
        print "mpi  ", t2-t1

    if scipyFlag:
        t1 = time.time()
        labels_scipy = scipy_labels(data, clusters, nReps)
        if VERBOSE:
            print "scipy labels:"
            print labels_scipy[0]
        t2 = time.time()
        if PRINT_TIMES:
            print "scipy", t2-t1
    
    t1 = time.time()
    (cpu_clusters, cpu_labels) = cpu_kmeans.kmeans_cpu(data2, clusters2, nReps+1)
    if VERBOSE:
        print "cpu_kmeans labels:"
        print cpu_labels
    t2 = time.time()
    if PRINT_TIMES:
        print "cpu  ", t2-t1

    error = 0
    
    if scipyFlag:
        try:
            np.testing.assert_array_equal(labels_mpi[0], labels_scipy[0])
        except AssertionError:
            print "mpi<>scipy",
            error = 1
    
    try:
        np.testing.assert_array_equal(labels_mpi[0], cpu_labels)
    except AssertionError:
        print "mpi<>cpu",
        error = 1
    
    """
    try:
        np.testing.assert_array_equal(cuda_labels, tri_labels)
    except AssertionError:
        print "cuda<>tri",
        error = 1
    """
    
    try:
        np.testing.assert_array_equal(tri_labels, cpu_labels)
    except AssertionError:
        print "tri<>cpu",
        error = 1

    try:
        np.testing.assert_array_equal(labels_mpi[0], tri_labels)
    except AssertionError:
        print "tri<>mpi",
        error = 1

    if error == 0:
        print "Labels OK ..."
    else:
        print ""
コード例 #3
0
ファイル: verify.py プロジェクト: dbelll/bell_d_project
def mpi_labels(data, num_clusters, nReps, seed = SEED):
    # reset the random seed so the first cluster assignments will be the same
    # as calculated at the beginning of run_labels()
    random.seed(seed)
    clusters, dist, labels = py_kmeans.kmeans(data, num_clusters, nReps, 0)
    return labels-1, clusters
コード例 #4
0
def run_labels(data, nClusters, nReps, skip_cpu = SKIP_CPU, seed=SEED):
    random.seed(seed)
    # run py_kmeans.kmeans once to get a starting label assignment,
    # which will be used by the scipy routine and others
    clusters, dist, labels = py_kmeans.kmeans(data, nClusters, 1, 0)
    if VERBOSE:
        print "data"
        print data
        print "initial clusters:"
        print clusters
 
    (nPts, nDim) = data.shape
    nClusters = clusters.shape[0] 
    print "[nPts:{0:6}][nDim:{1:4}][nClusters:{2:4}][nReps:{3:3}]...".format(nPts, nDim, 
                                                                            nClusters, nReps),

    data2 = np.swapaxes(data, 0, 1).astype(np.float32).copy('C')
    clusters2 = np.swapaxes(clusters, 0, 1).astype(np.float32).copy('C')

    if VERBOSE:
        print "data2"
        print data2
        print "clusters2"
        print clusters2

    """
    t1 = time.time()
    (cuda_clusters, cuda_labels) = cuda_kmeans.kmeans_gpu(data2, clusters2, nReps+1)
    if VERBOSE:
        print "cuda_kmeans labels:"
        print cuda_labels
    t2 = time.time()
    if PRINT_TIMES:
        print "\ncuda ", t2-t1
    """
    
    t1 = time.time()
    (tri_clusters, tri_labels) = cuda_kmeans_tri.trikmeans_gpu(data2, clusters2, nReps+1)
    if VERBOSE:
        print "\ncuda_kmeans_tri labels:"
        print tri_labels
    t2 = time.time()
    if PRINT_TIMES:
        print "\ntri  ", t2-t1

    t1 = time.time()
    labels_mpi = mpi_labels(data, nClusters, nReps+1, seed)
    if VERBOSE:
        print "mpi labels:"
        print labels_mpi[0]
    t2 = time.time()
    if PRINT_TIMES:
        print "mpi  ", t2-t1

    if scipyFlag:
        t1 = time.time()
        labels_scipy = scipy_labels(data, clusters, nReps)
        if VERBOSE:
            print "scipy labels:"
            print labels_scipy[0]
        t2 = time.time()
        if PRINT_TIMES:
            print "scipy", t2-t1
    
    t1 = time.time()
    if not skip_cpu:
        (cpu_clusters, cpu_labels) = cpu_kmeans.kmeans_cpu(data2, clusters2, nReps+1)
        if VERBOSE:
            print "cpu_kmeans labels:"
            print cpu_labels
        t2 = time.time()
        if PRINT_TIMES:
            print "cpu  ", t2-t1

    error = 0
    
    if scipyFlag:
        try:
            np.testing.assert_array_equal(labels_mpi[0], labels_scipy[0])
        except AssertionError:
            print "mpi<>scipy",
            error = 1
    
    if not skip_cpu:
        try:
            np.testing.assert_array_equal(labels_mpi[0], cpu_labels)
        except AssertionError:
            print "mpi<>cpu",
            error = 1
    
        try:
            np.testing.assert_array_equal(tri_labels, cpu_labels)
        except AssertionError:
            print "tri<>cpu",
            error = 1

    """
    try:
        np.testing.assert_array_equal(cuda_labels, tri_labels)
    except AssertionError:
        print "cuda<>tri",
        error = 1
    """
    
    try:
        np.testing.assert_array_equal(labels_mpi[0], tri_labels)
    except AssertionError:
        print "tri<>mpi",
        error = 1


    if error == 0:
        print "Labels OK ..."
    else:
        print ""
コード例 #5
0
def mpi_labels(data, num_clusters, nReps, seed = SEED):
    # reset the random seed so the first cluster assignments will be the same
    # as calculated at the beginning of run_labels()
    random.seed(seed)
    clusters, dist, labels = py_kmeans.kmeans(data, num_clusters, nReps, 0)
    return labels-1, clusters