{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "using MetagraphOptimization\n",
    "using BenchmarkTools\n",
    "\n",
    "Threads.nthreads()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Graph:\n",
       "  Nodes: Total: 15866, DataTask: 7937, ComputeTaskQED_S2: 720, \n",
       "         ComputeTaskQED_Sum: 1, ComputeTaskQED_V: 4320, ComputeTaskQED_S1: 2880, \n",
       "         ComputeTaskQED_U: 8\n",
       "  Edges: 21617\n",
       "  Total Compute Effort: 66249.0\n",
       "  Total Data Transfer: 1.314048e6\n",
       "  Total Compute Intensity: 0.050415966540035065\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "machine = get_machine_info()\n",
    "model = QEDModel()\n",
    "process = parse_process(\"ke->kkkkke\", model)\n",
    "\n",
    "inputs = [gen_process_input(process) for _ in 1:1e3];\n",
    "graph = gen_graph(process)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Graph:\n",
       "  Nodes: Total: 2234, DataTask: 1121, ComputeTaskQED_S2: 720, \n",
       "         ComputeTaskQED_Sum: 1, ComputeTaskQED_V: 312, ComputeTaskQED_S1: 72, \n",
       "         ComputeTaskQED_U: 8\n",
       "  Edges: 3977\n",
       "  Total Compute Effort: 11313.0\n",
       "  Total Data Transfer: 659712.0\n",
       "  Total Compute Intensity: 0.017148392025611175\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "optimizer = ReductionOptimizer()\n",
    "\n",
    "compute_compton = get_compute_function(graph, process, machine)\n",
    "optimize_to_fixpoint!(optimizer, graph)\n",
    "graph"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Calculated 133942.0 results/s, 11162.0 results/s per thread for QED Process: 'ke->kkkkke' (12 threads)\n"
     ]
    }
   ],
   "source": [
    "compute_compton_reduced = get_compute_function(graph, process, machine)\n",
    "outputs = [zero(ComplexF64) for _ in 1:1e6]\n",
    "\n",
    "bench_result = @benchmark begin\n",
    "    Threads.@threads :static for i in eachindex(inputs)\n",
    "        outputs[i] = compute_compton_reduced(inputs[i])\n",
    "    end\n",
    "end\n",
    "\n",
    "rate = length(inputs) / (mean(bench_result.times) / 1.0e9)\n",
    "rate_per_thread = rate / Threads.nthreads()\n",
    "println(\"Calculated $(round(rate)) results/s, $(round(rate_per_thread)) results/s per thread for $(process) ($(Threads.nthreads()) threads)\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Calculated 17124.0 results/s, 1427.0 results/s per thread for QED Process: 'ke->kkkkke' (12 threads)\n"
     ]
    }
   ],
   "source": [
    "bench_result = @benchmark begin\n",
    "    Threads.@threads :static for i in eachindex(inputs)\n",
    "        outputs[i] = compute_compton(inputs[i])\n",
    "    end\n",
    "end\n",
    "\n",
    "rate = length(inputs) / (mean(bench_result.times) / 1.0e9)\n",
    "rate_per_thread = rate / Threads.nthreads()\n",
    "println(\"Calculated $(round(rate)) results/s, $(round(rate_per_thread)) results/s per thread for $(process) ($(Threads.nthreads()) threads)\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Julia 1.9.4",
   "language": "julia",
   "name": "julia-1.9"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "1.9.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
