+
+
+
+#TODO: si parll, getMedoids + serialization, pass only getMedoids to nodes
+# --> BOF... chaque node chargera tous les medoids (efficacité) :/ ==> faut que ça tienne en RAM
+#au pire :: C-ifier et charger medoids 1 by 1...
+
+ #MIEUX :: medoids DOIT etre une big.matrix partagée !
+
+ computeSynchronesChunk = function(indices)
+ {
+ if (verbose)
+ cat(paste("--- Compute synchrones for ",length(indices)," lines\n", sep=""))
+ ref_series = getRefSeries(indices)
+ #get medoids indices for this chunk of series
+ for (i in seq_len(nrow(ref_series)))
+ {
+ j = which.min( rowSums( sweep(medoids, 2, ref_series[i,], '-')^2 ) )
+ if (parll)
+ synchronicity::lock(m)
+ synchrones[j,] = synchrones[j,] + ref_series[i,]
+ counts[j,1] = counts[j,1] + 1
+ if (parll)
+ synchronicity::unlock(m)
+ }
+ }
+
+ K = nrow(medoids)
+ # Use bigmemory (shared==TRUE by default) + synchronicity to fill synchrones in //
+ # TODO: if size > RAM (not our case), use file-backed big.matrix
+ synchrones = bigmemory::big.matrix(nrow=K,ncol=ncol(medoids),type="double",init=0.)
+ counts = bigmemory::big.matrix(nrow=K,ncol=1,type="double",init=0)
+ # synchronicity is only for Linux & MacOS; on Windows: run sequentially
+ parll = (requireNamespace("synchronicity",quietly=TRUE)
+ && parll && Sys.info()['sysname'] != "Windows")
+ if (parll)
+ m <- synchronicity::boost.mutex()
+
+ if (parll)
+ {
+ cl = parallel::makeCluster(ncores_clust)
+ parallel::clusterExport(cl,
+ varlist=c("synchrones","counts","verbose","medoids","getRefSeries"),
+ envir=environment())
+ }
+
+ indices_workers = .spreadIndices(seq_len(nb_ref_curves), nb_series_per_chunk)
+ ignored <-
+ if (parll)
+ parallel::parLapply(indices_workers, computeSynchronesChunk)
+ else
+ lapply(indices_workers, computeSynchronesChunk)
+
+ if (parll)
+ parallel::stopCluster(cl)
+
+ #TODO: can we avoid this loop? ( synchrones = sweep(synchrones, 1, counts, '/') )
+ for (i in seq_len(K))
+ synchrones[i,] = synchrones[i,] / counts[i,1]
+ #NOTE: odds for some clusters to be empty? (when series already come from stage 2)
+ # ...maybe; but let's hope resulting K1' be still quite bigger than K2
+ noNA_rows = sapply(seq_len(K), function(i) all(!is.nan(synchrones[i,])))
+ if (all(noNA_rows))
+ return (synchrones)
+ # Else: some clusters are empty, need to slice synchrones
+ synchrones[noNA_rows,]