Example #1
0
	bool KMeans::Execute()
	{
		InitCluster();

		for(g_uint iter=0; iter<m_max_iteration; iter++)
		{
			bool exit = true;
			g_uint cluster_id = 0;
			kmean_point_t* pt = m_points;
			for(g_uint i=0; i<m_point_count; i++, pt++)
			{
				cluster_id = FindPointCluster(pt->x, pt->y);
				if(cluster_id != pt->cluster)
				{
					pt->cluster = cluster_id;
					exit = false;
				}
				//printf("%d\n", cluster_id);
			}

			if(exit)
			{
				break;
			}
			else
			{
				UpdateCluster();
			}
		}
		
#ifdef DEBUG

#endif
	return true;
	}
Example #2
0
/*
 * Initialize ConfigDB
 */
tConfigDB*  ConfigInitialize(int mode,char * addr,int port,char * filename)
{
    debug("start ConfigInitialize...\n");
    if(strlen(addr)>NAME_STR_LEN || strlen(filename)>NAME_STR_LEN)
    {
        fprintf(stderr,"Args Error,%s:%d\n",__FILE__,__LINE__);
        return NULL;
    }
    tConfigDB* db = (tConfigDB*)malloc(sizeof(tConfigDB));
    strncpy(db->filename,filename,NAME_STR_LEN);
    strncpy(db->addr,addr,NAME_STR_LEN);
    db->port = port;
    if(mode & LOCAL_MODE)
    {
        /* open config database file */
        
    }
    else if(mode & GRID_MODE)
    {   
        /* start as Master */
        if(strcmp(addr,IP_ADDR) == 0 && PORT == port)
        {
            debug("GRID_MODE:Master.\n");
            mode |= MASTER_MODE;
            db->cluster = (void*)InitCluster();
            AddNode((tCluster*)db->cluster,addr,port);
        }
        else /* start as Data Node */
        { 
            debug("GRID_MODE:Data Node.\n");           
            db->cluster = (void*)RegisterAndLoadClusterNodes(addr,port);
        }   
    }
    else
    {
        goto ERROR;
    }
    /* create local database */
    db->db = DBCreate(filename);
    /* start Service Engine */
    debug("start Service Engine.\n");
    if(pthread_create(&(db->engine),NULL,(void*)ServiceEngine,(void*)db) != 0)
    {
        fprintf(stderr,"Service Engine pthread_create Error,%s:%d\n",__FILE__,__LINE__);
        goto ERROR;
    }
    if(ConnectDataNode((tCluster*)db->cluster,addr,port,filename) != 0)
    {
        goto ERROR;
    }
    return db;
ERROR:
    ConfigDestroy(db);
    return NULL;
}