Start.R 149 KB
Newer Older
print(str(second_round_indices))
print("-> STRUCTURE OF STORE INDICES FOR THIS WORK PIECE:")
print(str(store_indices))
}
}
    ## Converting array indices to vector indices
    store_dims <- work_piece[['store_dims']]
    #store_indices <- do.call('expand.grid', store_indices)
    #### Given a matrix where each row is a set of array indices of an element
    #### the vector indices are computed
    #weights <- sapply(1:length(store_dims), 
    #                  function(i) prod(c(1, store_dims)[1:i]))
    #matrix_indices <- 1 + colSums(t(store_indices - 1) * weights)
    ####
    #data_array <- bigmemory::attach.big.matrix(shared_matrix_pointer)
    #data_array[matrix_indices] <- as.vector(sub_array)
    #rm(data_array)
    #gc()
###########################
#split the work in parts
wl <- which.max(store_dims)
partition <- function(x,n) split(x, cut(seq_along(x), n, labels = FALSE))
#less than 5 parts: increase of RAM consumption
  #mor than 5 parts: non-significant reduction of RAM consumption
parts <- partition(store_indices[[wl]], 5)
for(i in 1:5){
  #print(paste(i, 'of 5'))
  #compute indices in data_array
  stind <- store_indices
  stind[[wl]] <- parts[[i]]
  stind <- do.call("expand.grid", stind)
  stdims <- store_dims
  stdims[[wl]] <- length(parts[[i]])
  weights <- sapply(1:length(stdims), function(i) prod(c(1, stdims)[1:i]))
  matrix_indices <- 1 + colSums(t(stind - 1) * weights)
  #compute length of indices in sub_array
  stdims_sa <- sapply(first_round_indices, length)
  #indices in sub_array
  stind_sa <- stind[,match(names(first_round_indices),names(stdims))]
  weights_sa <- sapply(1:length(stdims_sa), function(i) prod(c(1, stdims_sa)[1:i]))
  matrix_indices_sa <- 1 + colSums(t(stind_sa - 1) * weights_sa)
  #load data_array and assign data from sub_array
  data_array <- bigmemory::attach.big.matrix(shared_matrix_pointer)
  data_array[matrix_indices] <- as.vector(sub_array)[matrix_indices_sa]
  rm(data_array)
  gc()

    if (!is.null(work_piece[['save_metadata_in']])) {
      saveRDS(metadata, file = work_piece[['save_metadata_in']])
    }
  }
  if (!is.null(work_piece[['progress_amount']]) && !silent) {
    message(work_piece[['progress_amount']], appendLF = FALSE)
  }
  is.null(sub_array)
}