#! /usr/bin/perl

#
# Author: Sebastian Enger, M.Sc.
# Date: 3/6/2016
# Website: BuzzerStar.com
# Email: contact (@) BuzzerStar.com
# Topic: Enable Multi CPU Usage with Chainer
# Based: https://groups.google.com/forum/#!topic/chainer/S3YLJdtuQaw
#

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;

# install openblast -> Chainer uses this library -> it enables Chainer to use multi cpus
system("apt-get install libopenblas-dev");		

# uninstall current version of numpy
system("pip uninstall numpy");		

# install numpy again, compile it with openblast support		
system("pip install -U numpy");

# 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");

# check for numpy support with openblast compiled in:

# python
#import numpy
#numpy.show_config()
### Now numpy should show openblast support, then you can start your chainer process and it will use multiple cpu ressources & processors
