
Hello,everybody! I am a freshman to the parallel programming of R, it is said that snowfall is easy to understand and easy to use, so I learned it for a short time.  I find it is easy to learn,but not so easy to use. When I wrote some parallel codes to implement the kmeans,I met  some error,but I have no idea how to deal with it, could anybody help me ,please ?


The codes are  :

library(snowfall)
sfInit(parallel=TRUE,cpus=2)
kmeans_label<-function(y){
    cl<-kmeans(x,centers)
    return(cl$cluster)
}
       
cutoff=0.5        #阈值

x <- rbind(matrix(rnorm(100, sd = 0.3), ncol = 2),
           matrix(rnorm(100, mean = 1, sd = 0.3), ncol = 2))
colnames(x) <- c("x", "y")

getDistence<-function(x){
    return(x[1]**2+x[2]**2)

    }

centers<-x[1:5,]#随机选取几个点
sfExport("x")#发送到各个slave
sfExport("centers")

sfClusterEval(ls())
    repeat{
        sfClusterSetupRNG(type="RNGstream")
        label<-sfLapply(1,kmeans_label)
        sfRemove("centers")
        newx=cbind(x,unlist(label))
        newcen=c()
        for(i in 1:nrow(centers)){
            newcen=rbind(newcen,apply(newx[which(newx[,3]==1),][,1:2],2,mean))       
        }           
        mm=newcen-centers
        realcutoff=sqrt(sum(apply(mm,1,getDistence)))
        cat("realcutoff: ",realcutoff,"\n")
        if((realcutoff-cutoff)<0) {

            break
        }
        else
        {
            sfClusterEval(ls())
            centers=newcen;           
            sfExport("centers")           
        }
    }
    print(label)   
sfStop()




The error I got is ＂one node produced an error: initial centers are not distinct
＂
 
   











--
文新胜

方向不对，努力白费

	[[alternative HTML version deleted]]

