{
 "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": false
   },
   "outputs": [],
   "source": [
    "dataset = 'SGEMM_NT'\n",
    "data_uoa =  dataset + '-explore-n-lws'\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": "markdown",
   "metadata": {},
   "source": [
    "### Table"
   ]
  },
  {
   "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_per_s = point_data.get(\"##characteristics#run#run_time_state#EXECUTION#Gflops/s#all\")\n",
    "    #match = point_data.get(\"##characteristics#run#run_time_state#RESULTS#match#all\")\n",
    "    data_list.append(Gflops_per_s) # + match)\n",
    "    # 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",
    "    matrix_order = point_data.get(\"##characteristics#run#run_time_state#CMD_LINE_ARGS#matrix_order#all_unique\")[0]\n",
    "    index_list.append((cl_file, lws_j * lws_i, lws_j, lws_i, matrix_order))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "num_repetitions = 4\n",
    "#ci = pd.MultiIndex.from_arrays([range(num_repetitions)*2, ['Gflops/s']*num_repetitions + ['Match?']*num_repetitions])\n",
    "ci = pd.MultiIndex.from_arrays([range(num_repetitions)])\n",
    "mi = pd.MultiIndex.from_tuples(names=['OpenCL program', 'LWS_jxi', 'LWS_j', 'LWS_i', 'Matrix order'], tuples=index_list)\n",
    "df = pd.DataFrame(data=data_list, index=mi, columns=ci)\n",
    "df.index = df.index.droplevel('OpenCL program') # not interested in as it's fixed here\n",
    "df['mean'] = df[range(num_repetitions)].mean(axis=1)\n",
    "df['std']  = df[range(num_repetitions)].std(axis=1)\n",
    "df.sortlevel('LWS_jxi')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# import re\n",
    "# # left paren, whitespace, number, whitespace, comma, whitespace, number, whitespace, right paren\n",
    "# r = '\\(\\s*(?P<lws_j>\\d+)\\s*,\\s*(?P<lws_i>\\d+)\\s*\\)'\n",
    "# f = df.index.to_series().values\n",
    "# [ m.groups() for t in f for m in [re.match(r, t[0])] ]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "ymax = np.int64(df['mean'].max() + df['std'].max())\n",
    "plot = df['mean'] \\\n",
    "    .unstack(level='Matrix order') \\\n",
    "    .plot(yerr=df['std'].unstack(level='Matrix order'),\n",
    "        title='Gflops/s vs Local work size',\n",
    "        kind='bar', figsize=(12,10), colormap=matplotlib.cm.autumn,\n",
    "        ylim=(0, ymax), yticks=range(ymax))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# plot = mean \\\n",
    "#     .unstack(level='Local work size') \\\n",
    "#     .plot(yerr=std.unstack(level='Local work size'),\n",
    "#         title='Gflops/s vs Matrix order',\n",
    "#         kind='bar', figsize=(12,8), colormap=matplotlib.cm.autumn,\n",
    "#         ylim=(0, ymax), yticks=range(ymax))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Dump results for paper"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Table"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "num_repetitions = 4\n",
    "mi_tex = pd.MultiIndex.from_tuples(tuples=index_list)\n",
    "df_tex = pd.DataFrame(data=data_list, index=mi_tex)\n",
    "df_tex.index = df_tex.index.droplevel(0) # not interested in as it's fixed here\n",
    "df_tex = df_tex.sortlevel(0) # 'LWS_jxi'\n",
    "df_tex['mean'] = df_tex[range(num_repetitions)].mean(axis=1)\n",
    "df_tex['std']  = df_tex[range(num_repetitions)].std(axis=1)\n",
    "df_tex = df_tex.loc[[16,32,64]]\n",
    "df_tex"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "with open('%s_tmp.tex' % data_uoa, 'w') as tex_file:\n",
    "     tex_file.write(df_tex.to_latex())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "plot.get_figure().savefig('%s_tmp.pdf' % data_uoa)\n",
    "plot.get_figure().savefig('%s_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
}
