+ if (verbose)
+ cat(paste("--- Compute synchrones\n", sep=""))
+
+ computeSynchronesChunk = function(indices)
+ {
+ if (parll)
+ {
+ require("bigmemory", quietly=TRUE)
+ requireNamespace("synchronicity", quietly=TRUE)
+ require("epclust", quietly=TRUE)
+ synchrones <- bigmemory::attach.big.matrix(synchrones_desc)
+ counts <- bigmemory::attach.big.matrix(counts_desc)
+ medoids <- bigmemory::attach.big.matrix(medoids_desc)
+ m <- synchronicity::attach.mutex(m_desc)
+ }
+
+ ref_series = getRefSeries(indices)
+ nb_series = nrow(ref_series)
+
+ #get medoids indices for this chunk of series
+ mi = computeMedoidsIndices(medoids@address, ref_series)
+
+ for (i in seq_len(nb_series))
+ {
+ if (parll)
+ synchronicity::lock(m)
+ synchrones[ mi[i], ] = synchrones[ mi[i], ] + ref_series[i,]
+ counts[ mi[i] ] = counts[ mi[i] ] + 1 #TODO: remove counts?
+ if (parll)
+ synchronicity::unlock(m)
+ }
+ }
+
+ K = nrow(medoids) ; L = ncol(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=L, 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()
+ m_desc <- synchronicity::describe(m)
+ synchrones_desc = bigmemory::describe(synchrones)
+ counts_desc = bigmemory::describe(counts)
+ medoids_desc = bigmemory::describe(medoids)
+ cl = parallel::makeCluster(ncores_clust)
+ parallel::clusterExport(cl, varlist=c("synchrones_desc","counts_desc","counts",
+ "verbose","m_desc","medoids_desc","getRefSeries"), envir=environment())
+ }
+
+ indices_workers = .spreadIndices(seq_len(nb_ref_curves), nb_series_per_chunk)
+ ignored <-
+ if (parll)
+ parallel::parLapply(cl, indices_workers, computeSynchronesChunk)
+ else
+ lapply(indices_workers, computeSynchronesChunk)
+
+ if (parll)
+ parallel::stopCluster(cl)