Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "cells": [
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "using BenchmarkTools"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Original code:\n",
- "```C\n",
- "double mat[N][N], s[N][N], val;\n",
- "int i, j, v[N];\n",
- "//\n",
- "// ... v[] and s[][] may be assumed to contain valid data\n",
- "//\n",
- "for(i=0; i<N ; ++i) {\n",
- " for(j=0; j<N; ++j) {\n",
- " val = (double)(v[i] % 256);\n",
- " mat[j][i] = s[j][i]*(sin(val)*sin(val)-cos(val)*cos(val));\n",
- " }\n",
- "}\n",
- "```"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 50,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "work (generic function with 2 methods)"
- ]
- },
- "execution_count": 50,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "function work(mat, s, v, N)\n",
- " val = 0.0\n",
- " @inbounds for i in 1:N\n",
- " for j in 1:N\n",
- " val = mod(v[i],256);\n",
- " mat[i,j] = s[i,j]*(sin(val)*sin(val)-cos(val)*cos(val));\n",
- " end\n",
- " end;\n",
- "end"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 21,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "\u001b[32m\u001b[1mTest Passed\u001b[22m\u001b[39m"
- ]
- },
- "execution_count": 21,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "using Test\n",
- "x = rand()\n",
- "@test 1-2*cos(x)*cos(x) β sin(x)*sin(x)-cos(x)*cos(x)\n",
- "@test -cos(2*x) β sin(x)*sin(x)-cos(x)*cos(x)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 51,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "opt1 (generic function with 2 methods)"
- ]
- },
- "execution_count": 51,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "# pulling out + analytical opt\n",
- "function opt1(mat, s, v, N)\n",
- " val = 0.0\n",
- " @inbounds for i in 1:N\n",
- " val = mod(v[i],256);\n",
- " val = -cos(2*val)\n",
- " for j in 1:N\n",
- " mat[i,j] = s[i,j]*val;\n",
- " end\n",
- " end;\n",
- " mat\n",
- "end"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 52,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "opt12 (generic function with 2 methods)"
- ]
- },
- "execution_count": 52,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "# pulling out + analytical opt + allocate values\n",
- "function opt12(mat, s, v, N)\n",
- " val = Vector{Float64}(undef, length(v))\n",
- " @inbounds for i in eachindex(val)\n",
- " val[i] = -cos(2*mod(v[i],256));\n",
- " end\n",
- " \n",
- " @inbounds for i in 1:N\n",
- " for j in 1:N\n",
- " mat[i,j] = s[i,j]*val[i];\n",
- " end\n",
- " end;\n",
- " mat\n",
- "end"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 55,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "opt2 (generic function with 2 methods)"
- ]
- },
- "execution_count": 55,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "# reordering loops\n",
- "function opt2(mat, s, v, N)\n",
- " val = 0.0\n",
- " @inbounds for j in 1:N\n",
- " for i in 1:N\n",
- " val = mod(v[i],256);\n",
- " val = -cos(2*val)\n",
- " mat[i,j] = s[i,j]*val;\n",
- " end\n",
- " end;\n",
- "end"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 56,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "opt22 (generic function with 1 method)"
- ]
- },
- "execution_count": 56,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "# reordering loops + allocate values\n",
- "function opt22(mat, s, v, N)\n",
- " val = Vector{Float64}(undef, length(v))\n",
- " @inbounds for i in eachindex(val)\n",
- " val[i] = -cos(2*mod(v[i],256));\n",
- " end\n",
- " \n",
- " @inbounds for j in 1:N\n",
- " for i in 1:N\n",
- " mat[i,j] = s[i,j]*val[i];\n",
- " end\n",
- " end;\n",
- "end"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 63,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "8"
- ]
- },
- "execution_count": 63,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "using Hwloc\n",
- "Hwloc.num_physical_cores()"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 68,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "opt22_threaded (generic function with 1 method)"
- ]
- },
- "execution_count": 68,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "function opt22_threaded(mat, s, v, N)\n",
- " val = Vector{Float64}(undef, length(v))\n",
- " @inbounds for i in eachindex(val)\n",
- " val[i] = -cos(2*mod(v[i],256));\n",
- " end\n",
- " \n",
- " @inbounds Threads.@threads for j in 1:N\n",
- " for i in 1:N\n",
- " mat[i,j] = s[i,j]*val[i];\n",
- " end\n",
- " end;\n",
- " mat\n",
- "end"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 88,
- "metadata": {},
- "outputs": [],
- "source": [
- "N = 4000\n",
- "mat = zeros(N,N)\n",
- "s = rand(N,N)\n",
- "v = rand(Int, N);"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 92,
- "metadata": {},
- "outputs": [],
- "source": [
- "opt22_threaded(mat, s, v, N);"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 57,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Performance: 9.096081484617212 MIt/s\n"
- ]
- }
- ],
- "source": [
- "runtime = @belapsed work($mat, $s, $v, $N);\n",
- "perf = N*N*1e-6/runtime # MIt/s\n",
- "println(\"Performance: $perf MIt/s\")"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 58,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Performance: 19.456335886711514 MIt/s\n"
- ]
- }
- ],
- "source": [
- "runtime = @belapsed opt1($mat, $s, $v, $N);\n",
- "perf = N*N*1e-6/runtime # MIt/s\n",
- "println(\"Performance: $perf MIt/s\")"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 59,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Performance: 19.358628902845535 MIt/s\n"
- ]
- }
- ],
- "source": [
- "runtime = @belapsed opt12($mat, $s, $v, $N);\n",
- "perf = N*N*1e-6/runtime # MIt/s\n",
- "println(\"Performance: $perf MIt/s\")"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 60,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Performance: 65.3802668739134 MIt/s\n"
- ]
- }
- ],
- "source": [
- "runtime = @belapsed opt2($mat, $s, $v, $N);\n",
- "perf = N*N*1e-6/runtime # MIt/s\n",
- "println(\"Performance: $perf MIt/s\")"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 61,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Performance: 658.4475132762567 MIt/s\n"
- ]
- }
- ],
- "source": [
- "runtime = @belapsed opt22($mat, $s, $v, $N);\n",
- "perf = N*N*1e-6/runtime # MIt/s\n",
- "println(\"Performance: $perf MIt/s\")"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 91,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "73.11111111111111"
- ]
- },
- "execution_count": 91,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "658/9"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 93,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "86"
- ]
- },
- "execution_count": 93,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "emmy = 86"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "emmy_opt = 600"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "emmy_opt_threaded = 1.8 * 1e9"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "# Multiple `N`"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 18,
- "metadata": {},
- "outputs": [],
- "source": [
- "using Plots\n",
- "\n",
- "runtime = @belapsed work($mat, $s, $v);\n",
- "perf = N*N*1e-6/runtime # MIt/s\n",
- "println(\"Performance: $perf MIt/s\")"
- ]
- }
- ],
- "metadata": {
- "kernelspec": {
- "display_name": "Julia 1.3.1",
- "language": "julia",
- "name": "julia-1.3"
- },
- "language_info": {
- "file_extension": ".jl",
- "mimetype": "application/julia",
- "name": "julia",
- "version": "1.3.1"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 2
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement