Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "cells": [
- {
- "cell_type": "code",
- "execution_count": 1,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "using Base.Threads, BenchmarkTools, Distances"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 2,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "4"
- ]
- },
- "execution_count": 2,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "nthreads()"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 3,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "4-element Array{Int64,1}:\n",
- " 2\n",
- " 3\n",
- " 4\n",
- " 5"
- ]
- },
- "execution_count": 3,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "addprocs()"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 4,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "pdist (generic function with 1 method)"
- ]
- },
- "execution_count": 4,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "function pdist(x, y)\n",
- " n = length(x)\n",
- " dist = zeros(n, n)\n",
- " for j in 1:n\n",
- " for i in 1:n\n",
- " dist[i,j] = (x[i] - x[j])^2 + (y[i] - y[j])^2 |> sqrt\n",
- " end\n",
- " end\n",
- " return dist\n",
- "end"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 46,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "pdist_inbounds (generic function with 1 method)"
- ]
- },
- "execution_count": 46,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "function pdist_inbounds(x, y)\n",
- " n = length(x)\n",
- " dist = zeros(n, n)\n",
- " for j in 1:n\n",
- " for i in 1:n\n",
- " @inbounds dist[i,j] = (x[i] - x[j])^2 + (y[i] - y[j])^2 |> sqrt\n",
- " end\n",
- " end\n",
- " return dist\n",
- "end"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 5,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "pdist_hypot (generic function with 1 method)"
- ]
- },
- "execution_count": 5,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "function pdist_hypot(x, y)\n",
- " n = length(x)\n",
- " dist = zeros(n, n)\n",
- " for j in 1:n\n",
- " for i in 1:n\n",
- " dist[i,j] = hypot(x[i] - x[j], y[i] - y[j])\n",
- " end\n",
- " end\n",
- " return dist\n",
- "end"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 6,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "pdist_thread (generic function with 1 method)"
- ]
- },
- "execution_count": 6,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "function pdist_thread(x, y)\n",
- " n = length(x)\n",
- " dist = zeros(n, n)\n",
- " @threads for j in 1:n\n",
- " for i in 1:n\n",
- " dist[i,j] = (x[i] - x[j])^2 + (y[i] - y[j])^2 |> sqrt\n",
- " end\n",
- " end\n",
- " return dist\n",
- "end"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 35,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "pdist_proc (generic function with 1 method)"
- ]
- },
- "execution_count": 35,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "function pdist_proc(x, y)\n",
- " n = length(x)\n",
- " dist = SharedMatrix{Float64}(n,n,init=0.0)\n",
- " @parallel for j in 1:n\n",
- " for i in 1:n\n",
- " dist[i,j] = (x[i] - x[j])^2 + (y[i] - y[j])^2 |> sqrt\n",
- " end\n",
- " end\n",
- " return dist\n",
- "end"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 8,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "pdist_thread_inbounds (generic function with 1 method)"
- ]
- },
- "execution_count": 8,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "function pdist_thread_inbounds(x, y)\n",
- " n = length(x)\n",
- " dist = zeros(n, n)\n",
- " @threads for j in 1:n\n",
- " for i in 1:n\n",
- " @inbounds dist[i,j] = (x[i] - x[j])^2 + (y[i] - y[j])^2 |> sqrt\n",
- " end\n",
- " end\n",
- " return dist\n",
- "end"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 9,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "pdist_thread_inbounds_sp (generic function with 1 method)"
- ]
- },
- "execution_count": 9,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "function pdist_thread_inbounds_sp(x::Vector{Float64}, y::Vector{Float64})\n",
- " n = length(x)\n",
- " dist = zeros(n, n)\n",
- " @threads for j in 1:n\n",
- " for i in 1:n\n",
- " @inbounds dist[i,j] = (x[i] - x[j])^2 + (y[i] - y[j])^2 |> sqrt\n",
- " end\n",
- " end\n",
- " return dist\n",
- "end"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 11,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "pdist_thread_hypot (generic function with 1 method)"
- ]
- },
- "execution_count": 11,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "function pdist_thread_hypot(x, y)\n",
- " n = length(x)\n",
- " dist = zeros(n, n)\n",
- " @threads for j in 1:n\n",
- " for i in 1:n\n",
- " dist[i,j] = hypot(x[i] - x[j], y[i] - y[j])\n",
- " end\n",
- " end\n",
- " return dist\n",
- "end"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 12,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "n = 100\n",
- "x, y = rand(n), rand(n);"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 13,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "BenchmarkTools.Trial: \n",
- " memory estimate: 78.20 KiB\n",
- " allocs estimate: 2\n",
- " --------------\n",
- " minimum time: 62.063 μs (0.00% GC)\n",
- " median time: 67.215 μs (0.00% GC)\n",
- " mean time: 74.850 μs (3.95% GC)\n",
- " maximum time: 2.004 ms (90.67% GC)\n",
- " --------------\n",
- " samples: 10000\n",
- " evals/sample: 1"
- ]
- },
- "execution_count": 13,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "@benchmark pdist(x,y)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 47,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "BenchmarkTools.Trial: \n",
- " memory estimate: 78.20 KiB\n",
- " allocs estimate: 2\n",
- " --------------\n",
- " minimum time: 36.741 μs (0.00% GC)\n",
- " median time: 39.696 μs (0.00% GC)\n",
- " mean time: 41.591 μs (0.00% GC)\n",
- " maximum time: 780.681 μs (0.00% GC)\n",
- " --------------\n",
- " samples: 10000\n",
- " evals/sample: 1"
- ]
- },
- "execution_count": 47,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "@benchmark pdist_inbounds(x,y)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 14,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "BenchmarkTools.Trial: \n",
- " memory estimate: 78.27 KiB\n",
- " allocs estimate: 3\n",
- " --------------\n",
- " minimum time: 25.954 μs (0.00% GC)\n",
- " median time: 29.194 μs (0.00% GC)\n",
- " mean time: 58.706 μs (5.58% GC)\n",
- " maximum time: 17.391 ms (0.00% GC)\n",
- " --------------\n",
- " samples: 10000\n",
- " evals/sample: 1"
- ]
- },
- "execution_count": 14,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "@benchmark pdist_thread(x,y)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 16,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "BenchmarkTools.Trial: \n",
- " memory estimate: 78.27 KiB\n",
- " allocs estimate: 3\n",
- " --------------\n",
- " minimum time: 13.056 μs (0.00% GC)\n",
- " median time: 21.805 μs (0.00% GC)\n",
- " mean time: 82.652 μs (4.41% GC)\n",
- " maximum time: 33.319 ms (0.00% GC)\n",
- " --------------\n",
- " samples: 10000\n",
- " evals/sample: 1"
- ]
- },
- "execution_count": 16,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "@benchmark pdist_thread_inbounds(x,y)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 17,
- "metadata": {
- "scrolled": true
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "BenchmarkTools.Trial: \n",
- " memory estimate: 78.27 KiB\n",
- " allocs estimate: 3\n",
- " --------------\n",
- " minimum time: 13.045 μs (0.00% GC)\n",
- " median time: 21.896 μs (0.00% GC)\n",
- " mean time: 30.416 μs (11.45% GC)\n",
- " maximum time: 7.469 ms (0.00% GC)\n",
- " --------------\n",
- " samples: 10000\n",
- " evals/sample: 1"
- ]
- },
- "execution_count": 17,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "@benchmark pdist_thread_inbounds_sp(x,y)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 44,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- " 0.002618 seconds (1.06 k allocations: 49.031 KiB)\n"
- ]
- }
- ],
- "source": [
- "@time pdist_proc(x,y);"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 19,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "BenchmarkTools.Trial: \n",
- " memory estimate: 78.20 KiB\n",
- " allocs estimate: 2\n",
- " --------------\n",
- " minimum time: 120.076 μs (0.00% GC)\n",
- " median time: 137.538 μs (0.00% GC)\n",
- " mean time: 140.645 μs (0.00% GC)\n",
- " maximum time: 234.016 μs (0.00% GC)\n",
- " --------------\n",
- " samples: 10000\n",
- " evals/sample: 1"
- ]
- },
- "execution_count": 19,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "@benchmark pdist_hypot(x,y)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 20,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "BenchmarkTools.Trial: \n",
- " memory estimate: 78.27 KiB\n",
- " allocs estimate: 3\n",
- " --------------\n",
- " minimum time: 43.035 μs (0.00% GC)\n",
- " median time: 45.851 μs (0.00% GC)\n",
- " mean time: 49.708 μs (0.00% GC)\n",
- " maximum time: 11.202 ms (0.00% GC)\n",
- " --------------\n",
- " samples: 10000\n",
- " evals/sample: 1"
- ]
- },
- "execution_count": 20,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "@benchmark pdist_thread_hypot(x,y)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 21,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "BenchmarkTools.Trial: \n",
- " memory estimate: 89.34 KiB\n",
- " allocs estimate: 205\n",
- " --------------\n",
- " minimum time: 92.835 μs (0.00% GC)\n",
- " median time: 98.546 μs (0.00% GC)\n",
- " mean time: 101.390 μs (0.00% GC)\n",
- " maximum time: 1.467 ms (0.00% GC)\n",
- " --------------\n",
- " samples: 10000\n",
- " evals/sample: 1"
- ]
- },
- "execution_count": 21,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "a = [x'; y']\n",
- "@benchmark pairwise(Euclidean(), a, a)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 22,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "true"
- ]
- },
- "execution_count": 22,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "pdist(x,y) ≈ pdist_hypot(x,y) ≈ pdist_thread(x,y) ≈ pdist_thread_hypot(x,y) ≈ pairwise(Euclidean(), [x'; y'], [x'; y'])"
- ]
- }
- ],
- "metadata": {
- "kernelspec": {
- "display_name": "Julia 0.6.1",
- "language": "julia",
- "name": "julia-0.6"
- },
- "language_info": {
- "file_extension": ".jl",
- "mimetype": "application/julia",
- "name": "julia",
- "version": "0.6.2"
- },
- "toc": {
- "nav_menu": {},
- "number_sections": true,
- "sideBar": true,
- "skip_h1_title": false,
- "toc_cell": false,
- "toc_position": {},
- "toc_section_display": "block",
- "toc_window_display": false
- }
- },
- "nbformat": 4,
- "nbformat_minor": 2
- }
Add Comment
Please, Sign In to add comment