{
 "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.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",
    "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": [
    "### Data frame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "data_list  = []\n",
    "index_list = []\n",
    "num_repetitions = 4\n",
    "\n",
    "# If the 'data' list has fewer than 'num_repetition' elements, \n",
    "# extend to have exactly 'num_repetitions' elements with 'ext'.\n",
    "def extend_to_num_repetitions(data, ext=None, num_repetitions=num_repetitions):\n",
    "    return data + (num_repetitions - len(data)) * [None]\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",
    "    num_data = 0\n",
    "    \n",
    "    # Data.\n",
    "    Gflops_per_s = point_data.get(\"##characteristics#run#run_time_state#EXECUTION#Gflops/s#all\")\n",
    "    Gflops_per_s = extend_to_num_repetitions(Gflops_per_s)\n",
    "    Gflops_per_s_index = num_data\n",
    "    num_data += 1\n",
    "    \n",
    "    max_lws = point_data.get(\"##characteristics#run#run_time_state#CL_KERNEL_WORK_GROUP_SIZE#all\")\n",
    "    max_lws = extend_to_num_repetitions(max_lws)\n",
    "    max_lws_index = num_data\n",
    "    num_data += 1\n",
    "    \n",
    "    diff = point_data.get(\"##characteristics#run#run_time_state#RESULTS#max_abs_diff#all\")\n",
    "    diff = extend_to_num_repetitions(diff)\n",
    "    diff_index = num_data\n",
    "    num_data += 1\n",
    "    \n",
    "    match = point_data.get(\"##characteristics#run#run_time_state#RESULTS#match#all\")\n",
    "    match = extend_to_num_repetitions(match)\n",
    "    match_index = num_data\n",
    "    num_data += 1\n",
    "        \n",
    "    total_num_data = num_data * num_repetitions\n",
    "    data_entry = [0] * total_num_data\n",
    "    data_entry[Gflops_per_s_index:total_num_data:num_data] = Gflops_per_s\n",
    "    data_entry[max_lws_index:total_num_data:num_data] = max_lws\n",
    "    data_entry[match_index:total_num_data:num_data] = match\n",
    "    data_entry[diff_index:total_num_data:num_data] = diff\n",
    "    data_list.append(data_entry)\n",
    "    \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",
    "    lws = ('(%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, lws, matrix_order))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "ci = pd.MultiIndex.from_arrays(names=['Repetition', 'Metric'], arrays=[\n",
    "        [s for s in range(num_repetitions) for t in range(num_data)], # 0,...,0,1,...,1,2,...,2,...\n",
    "        ['Gflops/s', 'Max LWS', 'Diff', 'Match?'] * num_repetitions])\n",
    "mi = pd.MultiIndex.from_tuples(names=['OpenCL program', 'LWS', 'Matrix order'] , tuples=index_list)\n",
    "df = pd.DataFrame(data=data_list, index=mi, columns=ci).sortlevel(level='OpenCL program')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "df = df.swaplevel('Repetition', 'Metric', axis=1)['Gflops/s']\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "mean = df.mean(axis=1)\n",
    "std = df.std(axis=1)\n",
    "ymax = np.int64(mean.max() + std.max() + 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "plot = mean \\\n",
    "    .unstack(level='LWS').unstack(level='OpenCL program') \\\n",
    "    .plot(yerr=std.unstack(level='LWS').unstack(level='OpenCL program'), \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": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "plot.get_figure().savefig('%s-match_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.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
