---
title: "R Notebook"
output: html_notebook
---
Parallel R Workshop

In the following notebook, we will be utilizing the 'parallel' library in R to do linear regressions on a dataset. 

Our first step will be to load our parallel library.

```{r}
library(parallel)

```

Now we will load the iris example dataset that is included in every R installation! 

```{r}
data("iris")
```

Lets take a look at this data by using the summary command. 

```{r}
summary(iris)
```

Now let's perform an action to our data by performing a regression between two of our variables 

```{r}

# 
model <- lm(Sepal.Length ~ Sepal.Width, data = iris)
summary(model)
```

To observe the parallelization advantages and drawbacks we wantv to be abel to see the amount of time that it takes for us to run our code.

```{r}
singleregression_time<- system.time({
  model <- lm(Sepal.Length ~ Sepal.Width, data = iris)
})
print(singleregression_time)
```

As we can see the time elapsed for a single calculation of a regression is very small. But what happens if we want to run every possible regression in our dataset. For this small set that means 28 combinations for all variables setting one of them as the result variable. The following code produces that list of possible models for us.

```{r}
# All possible regressions


numeric_columns <- colnames(iris)[1:4]

allModelsList <- list()

# Loop through each numeric column and create a regression formula
for (dependent_var in numeric_columns) {
  
  predictors <- setdiff(numeric_columns, dependent_var)
  
  combs <- unlist(lapply(1:3, function(i) combn(predictors, i, simplify = FALSE)), recursive = FALSE)
  
  for (comb in combs) {
    model_formula <- as.formula(paste(c(paste(dependent_var, "~ 1"), comb), collapse = " + "))
    
    
    allModelsList[[paste(dependent_var, paste(comb, collapse = "_"), sep = "_")]] <- model_formula
  }
}
```

After running the above selection of code we have a list of all possible regression models that we want to run. But to replicate our process and not have to write our code 28 times we have to use a formula definition.

```{r}
run_regression <- function(formula) {
  model <- lm(formula, data = iris)  # Run linear model
  return(summary(model))  # Return summary of the model
}
```

First, we will run all 28 models in sequence with no parallelization.


```{r}
time_lapply <- system.time({
  results_lapply <- lapply(allModelsList, run_regression)
})
print(time_lapply)
```

No matter what time it states in user it is important to note that for a sequential application the system time ( the time spent coordinating system resources remains at 0). Now lets see how this differs from a parallel application. 

Before we apply it parallely let's see how many cores we have available which should match our Open OnDemand job submission. 
```{r}
num_cores <- detectCores() 
print(num_cores)


allocated_cores <- as.numeric(Sys.getenv("SLURM_JOB_CPUS_PER_NODE", unset = 1))
print(allocated_cores)


```

Here we can see that RStudio detects all the cores on the machine it is currently on (resulting on the 96 of our original command). But you will be limited to what you requested on Open OnDemand (in this case 9)

```{r}
time_mclapply <- system.time({
  results_mclapply <- mclapply(allModelsList, run_regression, mc.cores = 2)
})
print(time_mclapply)
```

This last selection of code allows us to easily distribute our function and model list over 2 or more cores. This is a shared memory implementation of parallel computing because all the cores are in a single node and all have access to the allModelslist.






