Skip to content

Instantly share code, notes, and snippets.

@javierluraschi
Last active January 10, 2020 22:54
Show Gist options
  • Save javierluraschi/f505beb5452846fd0bb70b2682cd42fe to your computer and use it in GitHub Desktop.
Save javierluraschi/f505beb5452846fd0bb70b2682cd42fe to your computer and use it in GitHub Desktop.
Initial setup to use Spark and TensorFlow Distributed using EMR

This script requires an Amazon EMR cluster with one master and three nodes:

library(sparklyr)
sc <- spark_connect(master = "yarn", spark_home = "/usr/lib/spark/", config = list(
    spark.dynamicAllocation.enabled = FALSE,
    `sparklyr.shell.executor-cores` = 8,
    `sparklyr.shell.num-executors` = 3,
    sparklyr.apply.env.WORKON_HOME = "/tmp/.virtualenvs"))

sdf_len(sc, 3, repartition = 3) %>%
    spark_apply(function(df, barrier) { tryCatch({
        library(tensorflow)
        install_tensorflow()
        
        nodes <- sort(as.character(barrier$address))
        cluster <- tf$train$ClusterSpec(list(ps = list(nodes[1]), worker = as.list(nodes[-1])))
        
        server <- tf$distribute$Server(cluster, job_name = "ps")
        
        as.character(tf$constant("Hello World")) }, error = function(e) e$message)
    }, barrier = TRUE, columns = c(address = "character")) %>%
    collect()

References:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment