Esempio n. 1
0
        CreateUpdateDesc(
            KMeansOptions options,
            KMeansMapPartition[] workers,
            IEnumerable <Vector> vectors,
            int[] clusterIds,
            int nCenters,
            int nRank,
            int nIteration
            )
        {
            KMeansUpdateCenters update = new KMeansUpdateCenters();

            update.m_evtMapWorkComplete     = m_evtMapWorkComplete;
            update.m_evtMapWorkAvailable    = m_evtMapWorkAvailable;
            update.m_evtUpdateWorkComplete  = m_evtUpdateWorkComplete;
            update.m_evtUpdateWorkAvailable = m_evtUpdateWorkAvailable;
            update.m_barrier           = m_barrier;
            update.m_nIterations       = nIteration;
            update.m_nCenters          = nCenters;
            update.m_nRank             = nRank;
            update.m_nPoints           = vectors.Count();
            update.m_vectors           = vectors;
            update.m_sharedCenters     = null;
            update.m_sharedGroupCounts = null;
            update.m_counts            = null;
            update.m_clusterIds        = clusterIds;
            update.m_fDelta            = 0.0f;
            update.m_workers           = workers;
            update.m_options           = options;

            return(update);
        }
Esempio n. 2
0
        ///-------------------------------------------------------------------------------------------------
        /// <summary>   Updates the centers. </summary>
        ///
        /// <remarks>   Chris Rossbach ([email protected]), 8/7/2012. </remarks>
        ///
        /// <param name="update">       The update. </param>
        ///-------------------------------------------------------------------------------------------------

        public static void UpdateCenters(
            object oUpdate
            )
        {
            KMeansUpdateCenters update = oUpdate as KMeansUpdateCenters;

            update.m_nIterations = 0;
            while (!m_bUpdatePhasesComplete)
            {
                update.m_sharedCenters     = KMeansCalculator.CreateCenterAccumulatorList(update.m_nCenters, update.m_nRank);
                update.m_sharedGroupCounts = KMeansCalculator.CreateGroupCountList(update.m_nCenters, update.m_nRank);
                update.m_fDelta            = 0.0f;
                update.m_evtMapWorkComplete.WaitOne();
                if (m_bVerbose)
                {
                    Console.WriteLine("update...");
                }
                for (int w = 0; w < update.m_workers.Count(); w++)
                {
                    KMeansMapPartition worker = update.m_workers[w];
                    update.m_fDelta += worker.m_fDelta;
                    for (int i = 0; i < update.m_nCenters; i++)
                    {
                        update.m_sharedCenters[i]     += worker.m_newCenters[i];
                        update.m_sharedGroupCounts[i] += worker.m_newGroupCounts[i];
                    }
                }
                for (int i = 0; i < update.m_nCenters; i++)
                {
                    update.m_sharedCenters[i] /= update.m_sharedGroupCounts[i];
                }
                update.m_fDelta /= update.m_nPoints;
                update.m_nIterations++;

                if (update.m_nIterations < update.m_options.m_nMaxIterations &&
                    update.m_fDelta >= update.m_options.m_fConvergenceThreshold)
                {
                    for (int w = 0; w < update.m_workers.Count(); w++)
                    {
                        update.m_workers[w].m_oldCenters = update.m_sharedCenters;
                    }
                    update.m_evtMapWorkComplete.Reset();
                    update.m_evtMapWorkAvailable.Set();
                }
                else
                {
                    m_bMapPhasesComplete    = true;
                    m_bUpdatePhasesComplete = true;
                    update.m_evtMapWorkAvailable.Set(); // shouldn't be needed, but harmless
                    return;
                }
            }
        }