#!/bin/bash echo "ip container: $(/sbin/ip -o -4 addr list eth0 | awk '{print $4}' | cut -d/ -f1)" echo "ip host: $(curl -s "http://169.254.169.254/latest/meta-data/local-ipv4")" cat << 'EOF' >> /tmp/mpi_hello_world.c // Copyright 2011 www.mpitutorial.com // // An intro MPI hello world program that uses MPI_Init, MPI_Comm_size, // MPI_Comm_rank, MPI_Finalize, and MPI_Get_processor_name. // #include #include #include int main(int argc, char** argv) { // Initialize the MPI environment. The two arguments to MPI Init are not // currently used by MPI implementations, but are there in case future // implementations might need the arguments. MPI_Init(NULL, NULL); // Get the number of processes int world_size; MPI_Comm_size(MPI_COMM_WORLD, &world_size); // Get the rank of the process int world_rank; MPI_Comm_rank(MPI_COMM_WORLD, &world_rank); // Get the name of the processor char processor_name[MPI_MAX_PROCESSOR_NAME]; int name_len; MPI_Get_processor_name(processor_name, &name_len); // Print off a hello world message printf("Hello world from processor %s, rank %d out of %d processors\n", processor_name, world_rank, world_size); // Finalize the MPI environment. No more MPI calls can be made after this MPI_Finalize(); } EOF # get shared dir IFS=',' _shared_dirs=(${PCLUSTER_SHARED_DIRS}) _shared_dir=${_shared_dirs[0]} _job_dir="${_shared_dir}/${AWS_BATCH_JOB_ID%#*}-${AWS_BATCH_JOB_ATTEMPT}" _exit_code_file="${_job_dir}/batch-exit-code" if [[ "${AWS_BATCH_JOB_NODE_INDEX}" -eq "${AWS_BATCH_JOB_MAIN_NODE_INDEX}" ]]; then echo "Hello I'm the main node ${HOSTNAME}! I run the mpi job!" mkdir -p "${_job_dir}" echo "Compiling..." /usr/lib64/openmpi/bin/mpicc -o "${_job_dir}/mpi_hello_world" "/tmp/mpi_hello_world.c" echo "Running..." /usr/lib64/openmpi/bin/mpirun --mca btl_tcp_if_include eth0 --allow-run-as-root --machinefile "${HOME}/hostfile" "${_job_dir}/mpi_hello_world" # Write exit status code echo "0" > "${_exit_code_file}" # Waiting for compute nodes to terminate sleep 30 else echo "Hello I'm the compute node ${HOSTNAME}! I let the main node orchestrate the mpi execution!" # Since mpi orchestration happens on the main node, we need to make sure the containers representing the compute # nodes are not terminated. A simple trick is to wait for a file containing the status code to be created. # All compute nodes are terminated by Batch if the main node exits abruptly. while [ ! -f "${_exit_code_file}" ]; do sleep 2 done exit $(cat "${_exit_code_file}") fi