-# helper
-.toInteger <- function(x, condition)
-{
- if (!is.integer(x))
- tryCatch(
- {x = as.integer(x)[1]},
- error = function(e) paste("Cannot convert argument",substitute(x),"to integer")
- )
- if (!condition(x))
- stop(paste("Argument",substitute(x),"does not verify condition",body(condition)))
- x
+ if (parll && ntasks>1)
+ parallel::stopCluster(cl)
+
+ # For the last stage, ncores_tasks*(ncores_clusts+1) cores should be available:
+ # - ntasks for level 1 parallelism
+ # - ntasks*ncores_clust for level 2 parallelism,
+ # but since an extension MPI <--> tasks / OpenMP <--> sub-tasks is on the way,
+ # it's better to just re-use ncores_clust
+ ncores_last_stage <- ncores_clust
+
+
+
+#TODO: here, save all inputs to clusteringTask2 and compare :: must have differences...
+
+
+
+ # Run last clustering tasks to obtain only K2 medoids indices
+ if (verbose)
+ cat("...Run final // stage 1 + stage 2\n")
+ indices_medoids <- clusteringTask1(indices_medoids_all, getContribs, K1, algoClust1,
+ nb_items_clust, ncores_tasks*ncores_clust, verbose, parll)
+ indices_medoids <- clusteringTask2(indices_medoids, getContribs, K2, algoClust2,
+ nb_series_per_chunk,smooth_lvl,nvoice,nbytes,endian,ncores_last_stage,verbose,parll)
+
+ # Compute synchrones, that is to say the cumulated power consumptions for each of the K2
+ # final groups.
+ medoids <- getSeries(indices_medoids)
+ synchrones <- computeSynchrones(medoids, getSeries, nb_curves, nb_series_per_chunk,
+ ncores_last_stage, verbose, parll)
+
+ # NOTE: no need to use big.matrix here, since there are only K2 << K1 << N remaining curves
+ list("medoids"=medoids, "ranks"=indices_medoids, "synchrones"=synchrones)