#! /usr/bin/perl

#
# Author: Sebastian Enger, M.Sc.
# Date: 3/13/2016
# Website: www.OneTIPP.com
# Email: Sebastian.Enger@gmail.com
# Topic: Install Host Image Requirements for Ubuntu in Docker Image 
# Based: https://blog.giantswarm.io/moving-docker-container-images-around/
# Version: 1.0.0
#https://www.quantstart.com/articles/Installing-Nvidia-cuda-on-Ubuntu-14-04-for-Linux-GPU-Computing
# https://github.com/NVIDIA/nvidia-docker/wiki/Installation

use strict;

my $cpu_count = "";
open CPU, "/proc/cpuinfo" or die "Can't open cpuinfo: $!\n";
#printf "CPUs: %d\n", scalar (map /^processor/, <CPU>) ; 
$cpu_count = scalar (map /^processor/, <CPU>);
close CPU;

# apt-get 
system("apt-get update");
system("apt-get upgrade");
#system("apt-get dist-upgrade");
system("apt-get -o Dpkg::Options::='--force-confold' --force-yes -fuy dist-upgrade");

system("apt-get install -y build-essential");
system("apt-get install -y openssl libssl-dev");
system("apt-get install -y libopenblas-dev");

system("apt-get install -y nasm");
system("apt-get install -y g++ c++"); 
system("apt-get install -y cpp"); 
system("apt-get install -y curl");
system("apt-get install -y wget");
system("apt-get install -y joe");
system("apt-get install -y screen");

system("apt-get install -y python-setuptools python-dev");
system("apt-get install -y python-pip");
system("apt-get install -y perl");
system("apt-get install -y bzip2 gzip pbzip2");
system("apt-get install -y libhdf5-dev");

# install and upgrade PIP
system("pip install -U pip");

# uninstall current version of numpy
system("pip uninstall numpy");		

# install numpy again, compile it with openblast support
# install requirements for Chainer		
system("pip install -U numpy");
system("pip install -U Cython");
system("pip install -U h5py");
system("pip install -U six");

# install chainer latest version
system("pip install -U Chainer");

# set the cpu cores to take for openblast to use -> this count of cores will then Chainer use
system("export OPENBLAS_NUM_THREADS=$cpu_count");

# joe /root/.bashrc -> export OPENBLAS_NUM_THREADS=32

my $end =<<<
If you use a newer version of Docker, you can use --cpuset="" in docker run to specify the CPU cores you want to allocate:

docker run --cpuset="0" [...]
If you use an older version of Docker (< 0.9), which uses LXC as the default execution environment, you can use --lxc-conf to configure the allocated CPU cores:

docker run --lxc-conf="lxc.cgroup.cpuset.cpus = 0" [...]
In both of those cases, only the first CPU core will be available to the docker container. Both of these options are documented here.
END;