{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Import packages"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import ck.kernel as ck\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib as matplotlib\n",
    "import matplotlib.pyplot as plt\n",
    "import json\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "print \"Collective Knowledge: v%s\" % ck.version({})['version_str']\n",
    "print \"pandas: v%s\" % pd.__version__\n",
    "print \"NumPy: v%s\" % np.version.version\n",
    "print \"Matplotlib: v%s\" % matplotlib.__version__\n",
    "print \"JSON: v%s\" % json.__version__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Find results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "dataset = 'SGEMM_NT'\n",
    "data_uoa =  dataset + '-explore-f-n'\n",
    "module_uoa = 'experiment'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "r=ck.access({'action':'list_points', 'module_uoa':module_uoa, 'data_uoa':data_uoa})\n",
    "if r['return']>0:\n",
    "  print (\"Error: %s\" % r['error'])\n",
    "  exit(1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Show results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "data_list  = []\n",
    "index_list = []\n",
    "\n",
    "for point in r['points']:\n",
    "    with open(os.path.join(r['path'], 'ckp-%s.flat.json' % point)) as point_file:\n",
    "        point_data = json.load(point_file)\n",
    "    # Data.    \n",
    "    GFLOPS = point_data.get(\"##characteristics#run#run_time_state#EXECUTION#Gflops/s#all\") # GFLOPS ~ Gflops/s\n",
    "    GPU    = point_data.get(\"##characteristics#run#run_time_state#file_2_energy#all\") # Mali-T628 GPU\n",
    "    memory = point_data.get(\"##characteristics#run#run_time_state#file_1_energy#all\") # memory\n",
    "    big    = point_data.get(\"##characteristics#run#run_time_state#file_0_energy#all\") # Cortex-A15 CPU\n",
    "    LITTLE = point_data.get(\"##characteristics#run#run_time_state#file_3_energy#all\") # Cortex-A7 CPU\n",
    "    data_list.append(GFLOPS + GPU + memory + big  + LITTLE)\n",
    "    \n",
    "    # Row index.\n",
    "    cl_file = point_data.get(\"##characteristics#run#run_time_state#METADATA#file#all_unique\")[0]\n",
    "    lws_j = point_data.get(\"##characteristics#run#run_time_state#EXECUTION#lws_j#all_unique\")[0]\n",
    "    lws_i = point_data.get(\"##characteristics#run#run_time_state#EXECUTION#lws_i#all_unique\")[0]\n",
    "    local_work_size = ('(%s, %s)' % (lws_j, lws_i))\n",
    "    matrix_order = point_data.get(\"##characteristics#run#run_time_state#CMD_LINE_ARGS#matrix_order#all_unique\")[0]\n",
    "    index_list.append((cl_file, local_work_size, matrix_order))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "num_repetitions = 4 # TODO: get from points\n",
    "metrics = ['GPU, Gflops/s', 'GPU, Joules', 'Memory, Joules', 'big CPU, Joules', 'LITTLE CPU, Joules']\n",
    "repetitions_array = range(num_repetitions) * len(metrics)\n",
    "metrics_array = [metric for metric in metrics for n in range(num_repetitions)]\n",
    "cols_mi = pd.MultiIndex.from_arrays(arrays=[metrics_array, repetitions_array], names=['Metric', 'Repetition'])\n",
    "rows_mi = pd.MultiIndex.from_tuples(names=['OpenCL program', 'Local work size', 'Matrix order'], tuples=index_list)\n",
    "df = pd.DataFrame(data=data_list, index=rows_mi, columns=cols_mi).sortlevel()\n",
    "df.index = df.index.droplevel('Local work size') # not interested in here as it's fixed"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "df \\\n",
    "    .unstack('OpenCL program') \\\n",
    "    .stack(level='Repetition')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Performance with errors"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# mean = df['GPU, Gflops/s'].mean(axis=1)\n",
    "# std = df['GPU, Gflops/s'].std(axis=1)\n",
    "# ymax = np.int64(mean.max() + std.max())\n",
    "\n",
    "# plot = mean \\\n",
    "#     .unstack(level='OpenCL program') \\\n",
    "#     .plot(yerr=std.unstack(level='OpenCL program'),\n",
    "#         title='Gflops/s vs Matrix order',\n",
    "#         kind='bar', figsize=(12,8))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### All performance and energy means"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "df_mean_all = df \\\n",
    "    .unstack('OpenCL program') \\\n",
    "    .stack(level='Repetition') \\\n",
    "    .groupby(level='Matrix order') \\\n",
    "    .mean()\n",
    "df_mean_all"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### All energy means"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "df_mean_energy_all = df[['GPU, Joules', 'Memory, Joules', 'big CPU, Joules', 'LITTLE CPU, Joules']] \\\n",
    "    .unstack('OpenCL program') \\\n",
    "    .stack(level='Repetition') \\\n",
    "    .groupby(level='Matrix order') \\\n",
    "    .mean()\n",
    "df_mean_energy_all"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "df_mean_energy_all_plot = df_mean_energy_all \\\n",
    "    .stack('OpenCL program') \\\n",
    "    .loc[[896, 1024]] \\\n",
    "    .plot(kind='bar', figsize=(12,8), colormap=matplotlib.cm.autumn, stacked=True)\n",
    "df_mean_energy_all_plot"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### GPU and memory energy means"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "df_mean_energy_gpu_mem = df[['GPU, Joules', 'Memory, Joules']] \\\n",
    "    .unstack('OpenCL program') \\\n",
    "    .stack(level='Repetition') \\\n",
    "    .groupby(level='Matrix order') \\\n",
    "    .mean()\n",
    "df_mean_energy_gpu_mem"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "df_mean_energy_gpu_mem = df_mean_energy_gpu_mem \\\n",
    "    .swaplevel(0, 1, axis=1) \\\n",
    "    .sortlevel(0, axis=1)\n",
    "df_mean_energy_gpu_mem"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "min_order_index=5\n",
    "order_list = list(df_mean_energy_gpu_mem.index[min_order_index:].values)\n",
    "program_list = ['1x1', '4x1', '4x1_brr']\n",
    "order_program_ticks = [ '%s,%s' % (order, program) for order in order_list for program in program_list ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "df_mean_energy_gpu_mem_plot = df_mean_energy_gpu_mem \\\n",
    "    .stack('OpenCL program') \\\n",
    "    .loc[order_list] \\\n",
    "    .plot(kind='bar', figsize=(12,8), colormap=matplotlib.cm.autumn, stacked=True)\n",
    "df_mean_energy_gpu_mem_plot.set_xticklabels(order_program_ticks)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Dump to files"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "with open('%s-energy-gpu-mem_tmp.tex' % data_uoa, 'w') as tex_file:\n",
    "    tex_file.write(df_mean_energy_gpu_mem.to_latex())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "df_mean_energy_gpu_mem_plot.get_figure().savefig('%s-energy-gpu-mem_tmp.pdf' % data_uoa)\n",
    "df_mean_energy_gpu_mem_plot.get_figure().savefig('%s-energy-gpu-mem_tmp.png' % data_uoa)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "df_mean_energy_all_plot.get_figure().savefig('%s-energy-all_tmp.png' % data_uoa)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
