Skip to content

Commit

Permalink
Add jupiter notebook
Browse files Browse the repository at this point in the history
  • Loading branch information
vadyushkins committed Jul 30, 2024
1 parent ed2ac7a commit 3819a61
Show file tree
Hide file tree
Showing 4 changed files with 269 additions and 0 deletions.
Binary file added tests/opencl/j_stat/graphics/graph.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions tests/opencl/j_stat/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
CONFIGS=-DNUM_CLUSTERS=1 -DNUM_CORES=4 -DNUM_WARPS=4 -DNUM_THREADS=4
running: CONFIGS=-DNUM_CLUSTERS=1 -DNUM_CORES=4 -DNUM_WARPS=4 -DNUM_THREADS=4 make -C ./ci/../runtime/simx
running: OPTS=-N16 -M16 -K16 make -C ./ci/../tests/opencl/kernel4 run-simx
make: Entering directory '/home/jblab/ivan_khromov/release/vortex/build/tests/opencl/kernel4'
cp /home/jblab/ivan_khromov/release/vortex/tests/opencl/kernel4/common.h common.h
LD_LIBRARY_PATH=/home/jblab/tools/pocl/lib:/home/jblab/ivan_khromov/release/vortex/build/runtime:/home/jblab/tools/llvm-vortex/lib::/usr/local/lib POCL_VORTEX_XLEN=32 LLVM_PREFIX=/home/jblab/tools/llvm-vortex POCL_VORTEX_BINTOOL="OBJCOPY=/home/jblab/tools/llvm-vortex/bin/llvm-objcopy /home/jblab/ivan_khromov/release/vortex/kernel/scripts/vxbin.py" POCL_VORTEX_CFLAGS="-march=rv32imaf -mabi=ilp32f -O3 -mcmodel=medany --sysroot=/home/jblab/tools/riscv32-gnu-toolchain/riscv32-unknown-elf --gcc-toolchain=/home/jblab/tools/riscv32-gnu-toolchain -fno-rtti -fno-exceptions -nostartfiles -nostdlib -fdata-sections -ffunction-sections -I/home/jblab/ivan_khromov/release/vortex/build/hw -I/home/jblab/ivan_khromov/release/vortex/kernel/include -DXLEN_32 -DNDEBUG -Xclang -target-feature -Xclang +vortex -Xclang -target-feature -Xclang +zicond -mllvm -disable-loop-idiom-all" POCL_VORTEX_LDFLAGS="-Wl,-Bstatic,--gc-sections,-T/home/jblab/ivan_khromov/release/vortex/kernel/scripts/link32.ld,--defsym=STARTUP_ADDR=0x80000000 /home/jblab/ivan_khromov/release/vortex/build/kernel/libvortex.a -L/home/jblab/tools/libc32/lib -lm -lc /home/jblab/tools/libcrt32/lib/baremetal/libclang_rt.builtins-riscv32.a" VORTEX_DRIVER=simx ./kernel4 -N16 -M16 -K16
Using device: Vortex OpenGPU
Execute the kernel
Elapsed time: 235 ms
Verify result
PASSED!
PERF: core0: instrs=11548, cycles=40923, IPC=0.282189
PERF: core1: instrs=11548, cycles=41559, IPC=0.277870
PERF: core2: instrs=11548, cycles=41327, IPC=0.279430
PERF: core3: instrs=11548, cycles=41707, IPC=0.276884
PERF: instrs=46192, cycles=41707, IPC=1.107536
make: Leaving directory '/home/jblab/ivan_khromov/release/vortex/build/tests/opencl/kernel4'
3 changes: 3 additions & 0 deletions tests/opencl/j_stat/perf.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
kernel1
--warps=8 --cores=8 --threads=8
cycles=57927
249 changes: 249 additions & 0 deletions tests/opencl/j_stat/statistic.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import subprocess\n",
"import matplotlib.pyplot as plt\n",
"from dataclasses import dataclass, field\n",
"import pandas as pd\n",
"from tqdm import tqdm"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# architecture parameters\n",
"@dataclass\n",
"class arch:\n",
" warps: int = 1\n",
" cores: int = 1\n",
" threads: int = 1\n",
"# running parameters \n",
"@dataclass\n",
"class run:\n",
" arch: arch\n",
" kernel: str\n",
" driver: str = \"simx\"\n",
" args: dict = field(default_factory=dict)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"path_to_vortex = \"/home/jblab/ivan_khromov/release/vortex\"\n",
"tile_size = 'TS'\n",
"work_per_thread = 'WPT'\n",
"width = 'WIDTH'"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"def error_running (run_params: run, error_text: str) -> str:\n",
" return f\"error in running in {run_params.kernel} : warps={run_params.arch.warps} cores={run_params.arch.cores} threads={run_params.arch.threads}\" \\\n",
" f\" driver={run_params.driver} args=-N{run_params.args['N']} -M{run_params.args['M']} -K{run_params.args['K']} error message - {error_text}/n\""
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"def error_verification (run_params: run, number_of_errors: str) -> str:\n",
" return f\"error in verifing results {run_params.kernel} : warps={run_params.arch.warps} cores={run_params.arch.cores} threads={run_params.arch.threads}\" \\\n",
" f\" driver={run_params.driver} args=-N{run_params.args['N']} -M{run_params.args['M']} -K{run_params.args['K']} Number of errors : {number_of_errors}'\\n'\" "
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"def create_common_h (params: dict, kernel_name: str):\n",
" file_name = f\"{path_to_vortex}/tests/opencl/{kernel_name}/common.h\"\n",
" with open(file_name, 'w') as file:\n",
" text = \"#ifndef COMMON_H\\n\" + \"#define COMMON_H\\n\" + \"\\n\" \n",
" if tile_size in params:\n",
" text += f\"#define TS {params[tile_size]}\\n\"\n",
" if work_per_thread in params:\n",
" text += f\"#define WPT {params[work_per_thread]}\\n\"\n",
" text += \"#define RTS (TS/WPT)\\n\"\n",
" if width in params:\n",
" text += f\"#define WIDTH {params[width]}\\n\"\n",
" text += '\\n' + \"#endif // COMMON_H\"\n",
" file.write(text)\n",
" # open main.cc file to recompile before run with new common.h\n",
" with open(f\"{path_to_vortex}/tests/opencl/{kernel_name}/main.cc\", 'a') as main:\n",
" main.write('')"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"def perf (run_params: run, path_to_output_file: str) -> pd.DataFrame:\n",
" # run kernel\n",
" vortex = f\"--warps={run_params.arch.warps} --cores={run_params.arch.cores} --threads={run_params.arch.threads}\"\n",
" run_args = f\"-N{run_params.args['N']} -M{run_params.args['M']} -K{run_params.args['K']}\"\n",
" command = f\"cd {path_to_vortex}/build && ./ci/blackbox.sh {vortex} --driver={run_params.driver} --app={run_params.kernel} --args=\\\"{run_args}\\\"\"\n",
" print(command)\n",
" subprocess.call(f\"{command} > {path_to_output_file}\", shell=True)\n",
"\n",
" # collect statistic \n",
" with open(path_to_output_file, 'r') as file:\n",
" lines = file.readlines()\n",
" error_message = \"\"\n",
" general_perf_stat = \"\"\n",
" for line in lines:\n",
" if \"PERF:\" in line:\n",
" general_perf_stat = line\n",
" # check for errors\n",
" if \"FAILED\" in line: \n",
" error_message = error_verification(run_params, line[line.find(\"FAILED! - \"):])\n",
" if \"Error\" in line:\n",
" error_message = error_running(run_params, line[line.find(\"Error:\"):])\n",
" # pars string with general perf statistic of running kernel\n",
" pairs = general_perf_stat.replace(\"PERF: \", \"\").split(\", \")\n",
" perf_dict = {key_value.split(\"=\")[0]: float(key_value.split(\"=\")[1]) for key_value in pairs}\n",
" if perf_dict[\"cycles\"] <= 0:\n",
" error_message = error_running(run_params, \"Invalid number of cycles\")\n",
" # write result to data frame\n",
" run_result = pd.DataFrame([{\"kernel\": run_params.kernel[-1], \"driver\": run_params.driver, \"cores\": run_params.arch.cores, \n",
" \"warps\": run_params.arch.warps, \"threads\": run_params.arch.threads, \"M\": run_params.args[\"M\"], \n",
" \"N\": run_params.args[\"N\"], \"K\": run_params.args[\"K\"], \"instrs\": perf_dict[\"instrs\"], \"cycles\": perf_dict[\"cycles\"],\n",
" \"IPC\": perf_dict[\"IPC\"], \"error\": error_message}])\n",
" return run_result"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"def draw (data_frame: pd.DataFrame, x_label: str, y_label: str, title: str, path: str):\n",
" data_frame.plot(kind = \"bar\", x = x_label, y = y_label)\n",
" plt.title(title)\n",
" plt.xlabel(x_label)\n",
" plt.ylabel(y_label)\n",
" plt.savefig(path)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
" 0%| | 0/4 [00:00<?, ?it/s]/bin/sh: 1: cannot create /home/jblab/ivan_khromov/release/vortex/tests/opencl/j_stat: Is a directory\n",
" 0%| | 0/4 [00:00<?, ?it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"cd /home/jblab/ivan_khromov/release/vortex/build && ./ci/blackbox.sh --warps=4 --cores=4 --threads=4 --driver=simx --app=kernel1 --args=\"-N16 -M16 -K16\"\n"
]
},
{
"ename": "IsADirectoryError",
"evalue": "[Errno 21] Is a directory: '/home/jblab/ivan_khromov/release/vortex/tests/opencl/j_stat'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mIsADirectoryError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[9], line 37\u001b[0m\n\u001b[1;32m 35\u001b[0m output_file \u001b[38;5;241m=\u001b[39m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mpath_to_vortex\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m/tests/opencl/j_stat\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 36\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m params \u001b[38;5;129;01min\u001b[39;00m tqdm(run_p):\n\u001b[0;32m---> 37\u001b[0m data_frames\u001b[38;5;241m.\u001b[39mappend(\u001b[43mperf\u001b[49m\u001b[43m(\u001b[49m\u001b[43mparams\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43moutput_file\u001b[49m\u001b[43m)\u001b[49m)\n\u001b[1;32m 38\u001b[0m data_frame \u001b[38;5;241m=\u001b[39m pd\u001b[38;5;241m.\u001b[39mconcat(data_frames, ignore_index\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m)\n\u001b[1;32m 40\u001b[0m \u001b[38;5;66;03m# draw graph based on the recived statistic\u001b[39;00m\n",
"Cell \u001b[0;32mIn[7], line 10\u001b[0m, in \u001b[0;36mperf\u001b[0;34m(run_params, path_to_output_file)\u001b[0m\n\u001b[1;32m 7\u001b[0m subprocess\u001b[38;5;241m.\u001b[39mcall(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mcommand\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m > \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mpath_to_output_file\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m, shell\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m)\n\u001b[1;32m 9\u001b[0m \u001b[38;5;66;03m# collect statistic \u001b[39;00m\n\u001b[0;32m---> 10\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m \u001b[38;5;28;43mopen\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mpath_to_output_file\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mr\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m \u001b[38;5;28;01mas\u001b[39;00m file:\n\u001b[1;32m 11\u001b[0m lines \u001b[38;5;241m=\u001b[39m file\u001b[38;5;241m.\u001b[39mreadlines()\n\u001b[1;32m 12\u001b[0m error_message \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m\"\u001b[39m\n",
"File \u001b[0;32m~/.pyenv/versions/3.11.9/lib/python3.11/site-packages/IPython/core/interactiveshell.py:324\u001b[0m, in \u001b[0;36m_modified_open\u001b[0;34m(file, *args, **kwargs)\u001b[0m\n\u001b[1;32m 317\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m file \u001b[38;5;129;01min\u001b[39;00m {\u001b[38;5;241m0\u001b[39m, \u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m2\u001b[39m}:\n\u001b[1;32m 318\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[1;32m 319\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mIPython won\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mt let you open fd=\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mfile\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m by default \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 320\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mas it is likely to crash IPython. If you know what you are doing, \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 321\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124myou can use builtins\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m open.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 322\u001b[0m )\n\u001b[0;32m--> 324\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mio_open\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfile\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n",
"\u001b[0;31mIsADirectoryError\u001b[0m: [Errno 21] Is a directory: '/home/jblab/ivan_khromov/release/vortex/tests/opencl/j_stat'"
]
}
],
"source": [
"# create common.h files for each kernel\n",
"params1 = {\n",
" tile_size: 4\n",
"}\n",
"create_common_h(params1, \"kernel1\")\n",
"create_common_h(params1, \"kernel2\")\n",
"\n",
"params3 = {\n",
" tile_size: 4,\n",
" work_per_thread: 4\n",
"}\n",
"create_common_h(params3, \"kernel3\")\n",
"\n",
"params4 = {\n",
" tile_size: 8,\n",
" width: 4\n",
"}\n",
"create_common_h(params4, \"kernel4\")\n",
"\n",
"# fill running params data class for each kernel\n",
"run_p = []\n",
"arg = {\n",
" \"M\": 16,\n",
" \"N\": 16,\n",
" \"K\": 16\n",
"}\n",
"arch_p = arch(threads=4, cores=4, warps=4)\n",
"run_p.append(run(arch_p, kernel=\"kernel1\", driver=\"simx\", args=arg))\n",
"run_p.append(run(arch_p, kernel=\"kernel2\", driver=\"simx\", args=arg))\n",
"run_p.append(run(arch_p, kernel=\"kernel3\", driver=\"simx\", args=arg))\n",
"run_p.append(run(arch_p, kernel=\"kernel4\", driver=\"simx\", args=arg))\n",
"\n",
"# run all kernels and collect statistic in data frame\n",
"data_frames = []\n",
"output_file = f\"{path_to_vortex}/tests/opencl/j_stat/output.txt\"\n",
"for params in tqdm(run_p):\n",
" data_frames.append(perf(params, output_file))\n",
"data_frame = pd.concat(data_frames, ignore_index=True)\n",
"\n",
"# draw graph based on the recived statistic\n",
"draw(data_frame, \"kernel\", \"cycles\", \"number of cycles per kernel\", \"graphics/graph.png\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.9"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit 3819a61

Please sign in to comment.