{"id":2343,"date":"2019-11-24T21:39:25","date_gmt":"2019-11-24T12:39:25","guid":{"rendered":"https:\/\/julialang.kr\/?p=2343"},"modified":"2019-11-24T21:39:28","modified_gmt":"2019-11-24T12:39:28","slug":"julia-function%eb%82%b4%ec%97%90%ec%84%9c-%eb%8f%99%ec%a0%81-module-%eb%a1%9c%eb%94%a9-%eb%b0%8f-%eb%b6%84%ec%82%b0%ec%b2%98%eb%a6%ac","status":"publish","type":"post","link":"https:\/\/julialang.kr\/?p=2343","title":{"rendered":"[Julia] function\ub0b4\uc5d0\uc11c \ub3d9\uc801 Module \ub85c\ub529 \ubc0f \ubd84\uc0b0\ucc98\ub9ac"},"content":{"rendered":"\n<p>function \ub0b4\uc5d0\uc11c using ModuleXX \ub4f1\uc73c\ub85c \ub3d9\uc801\uc73c\ub85c \ubaa8\ub4c8\uc744 \ub85c\ub529\ud558\ub294 \uac83\uc740 \uc27d\uc9c0 \uc54a\ub2e4. <br>function\ub0b4\uc5d0\uc11c using ModuleXX\ub85c \ubaa8\ub4c8\uc744 \ub85c\ub529\ud558\ub294 \uacbd\uc6b0 <br>&#8220;syntax: &#8220;using&#8221; expression not at top level &#8220;<br>\uc5d0\ub7ec\uac00 \ubc1c\uc0dd\ud55c\ub2e4.<\/p>\n\n\n\n<p>\uc774\ub7f0 \uacbd\uc6b0 \uc5d0\ub7ec\uc5c6\uc774 \ub3d9\uc801\uc73c\ub85c \ubaa8\ub4c8\uc744 \ub85c\ub529\ud558\uae30 \uc704\ud574\uc11c eval \uc0ac\uc6a9\ub4f1\uc73c\ub85c \uba54\ud0c0\ud504\ub85c\uadf8\ub7a8\uc744 \ud574\uc57c \ub41c\ub2e4.<\/p>\n\n\n\n<p>\uc544\ub798 \uc608\uc81c\uc5d0\uc11c\ub294 \ubd84\uc0b0\ucc98\ub9ac(Parallel Processing)\uc608\ub97c \ub4e4\uc5b4 \uc124\uba85\ud55c\ub2e4.<\/p>\n\n\n\n<p>\ud2b9\uc815 \ud3f4\ub354 \uc608\ub97c \ub4e4\uc5b4 &#8220;\/home\/shpark\/julia_test\/exproptimizatoin\/examples&#8221; \uc5d0 TestM01.jl\uc744 \uc0dd\uc131\ud55c\ub2e4.<br>\ud30c\uc77c\uba85\uacfc \ubaa8\ub4c8\uba85\uc744 \uac19\uc774 \ud574\uc57c \ud55c\ub2e4. using ModuleXX \uc2dc \ubaa8\ub4c8\uacbd\ub85c\ub85c \ub4f1\ub85d\ub41c \uacf3\uc5d0\uc11c ModuleXX.jl \ud30c\uc77c\uc744 \uc77d\uc5b4 \uc624\uae30 \ub54c\ubb38\uc784<\/p>\n\n\n\n<p>\ucc38\uace0\ub85c julia \uae30\ubcf8 \ubaa8\ub4c8 \ud3f4\ub354\uc678 \ucd94\uac00\uc801\uc73c\ub85c \ubaa8\ub4c8 \ud3f4\ub354\ub97c \ub4f1\ub85d\uc2dc <strong>&#8220;push!(LOAD_PATH, $module_dir)&#8221;<\/strong> \ub97c \uc0ac\uc6a9\ud558\uc5ec \ub4f1\ub85d\ud55c\ub2e4.<br>\uc774\ub807\uac8c \ub4f1\ub85d\ub41c \ud3f4\ub354 \uc544\ub798 \uc788\ub294 \ubaa8\ub4c8\ub4e4\uc740 using\uc744 \ud1b5\ud574 \uc0ac\uc6a9 \ud560 \uc218 \uc788\ub2e4. <\/p>\n\n\n\n<p>TestM01.jl \ubaa8\ub4c8\uc774  \/home\/shpark\/julia_test\/exproptimizatoin\/examples \ud3f4\ub354\uc5d0 \uc0dd\uc131\ub418\uc5c8\ub2e4\uace0 \uac00\uc815\ud55c\ub2e4.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>module TestM01\nusing Distributed\nfunction f(x)\n    println(\"MyID=$(myid())\")\n    x\nend\nend  # module TestM01<\/code><\/pre>\n\n\n\n<p>TestM01 \ubaa8\ub4c8\uc5d0 \uc788\ub294 f(x)\ub97c \ubcd1\ub82c\ucc98\ub9ac \ud558\uae30\uc704\ud574 jupyter notebook\uc774\ub098 atom\uc5d0\uc11c \uc544\ub798 \ucf54\ub4dc\ub97c \uc791\uc131\ud55c\ub2e4. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using Distributed\n#=\n\uc77c\ubc18\uc801\uc73c\ub85c Module\uc740 function\ub0b4\uc5d0\uc11c using\uc744 \uc0ac\uc6a9\ud558\uc5ec \uc120\uc5b8 \ub420 \uc218 \uc5c6\uae30 \ub54c\ubb38\uc5d0\nfunction\uc5d0\uc11c \ub3d9\uc801\uc73c\ub85c Module\uc744 worker\ub4e4\uc774 \ub85c\ub4dc \ud560 \uc218 \uc788\uac8c \ud55c\ub2e4.\naddprocs \uc774 \ud6c4\uc5d0 \uc0dd\uc131\ub41c worker \ud504\ub85c\uc138\uc2a4\uac00 \uc54c \uc218 \uc788\ub3c4\ub85d @everywhere\ub97c \uc0ac\uc6a9\ud574\uc11c \uc54c\ub824 \uc900\ub2e4.\n=#\nfunction parallel_procs()  \n  # worker \ud504\ub85c\uc138\uc2a4 \uc0dd\uc131\n  pids = addprocs(10)\n  \n  # TestM01.jl \ubaa8\ub4c8\uc774 \uc788\ub294 \uc704\uce58\n  module_dir = \"\/home\/shpark\/julia_test\/exproptimizatoin\/examples\"\n  \n  # worker \ud504\ub85c\uc138\uc2a4\uc5d0 TestM01 \ubaa8\ub4c8\uc774 \uc788\ub294 \uc704\uce58\ub97c \uc54c\ub824 \uc900\ub2e4\n  @everywhere push!(LOAD_PATH, $module_dir) \n  \n  #= @everywhere\ub97c \uc0ac\uc6a9\ud558\uae30 \uc704\ud574 Distributed\ub97c \uc0ac\uc6a9\ud558\uace0 \n  worker\ub4e4\uc5d0\uac8c TestM01 \ubaa8\ub4c8\uc744 \uc0ac\uc6a9\ud560 \uc218 \uc788\uac8c \ud55c\ub2e4\n  =#\n  eval(macroexpand(Distributed,quote @everywhere using TestM01 end))\n  \n  # \uc0dd\uc131\ub41c worker\ub4e4\uc5d0\uac8c \uc791\uc5c5\uc744 \ubc30\uc815\ud55c\ub2e4.\n  p = &#91;@spawn TestM01.f(i) for i in 1:length(pids)]        \n  \n  # fetch\ub294 worker\ub4e4\uc774 \uc791\uc5c5\uc744 \ub05d\ub0b4\uace0 \uacb0\uacfc\ub97c \ub3cc\ub824 \uc904 \ub54c \uae4c\uc9c0 \uae30\ub2e4\ub9b0\ub2e4\n  r = map(x->fetch(p&#91;x]), 1:length(pids))\n  \n  # \uc0dd\uc131\ud55c worker\ub4e4\uc744 \uc885\ub8cc \ud55c\ub2e4.\n  rmprocs(pids)\n  \n  return r\nend<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>r = parallel_procs()<\/code><\/pre>\n\n\n\n<p>parallel_procs() \ub97c \uc2e4\ud589\ud558\uba74 worker process\uac00 10\uac1c \uc0dd\uae30\uace0 \ubcd1\ub82c\ucc98\ub9ac \ub418\uc5c8\uc74c\uc744 \uc544\ub798\uc5d0\uc11c \uc54c \uc218 \uc788\ub2e4.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>From worker 18:\tMyID=18\nFrom worker 17:\tMyID=17\nFrom worker 14:\tMyID=14\nFrom worker 12:\tMyID=12\nFrom worker 19:\tMyID=19\nFrom worker 20:\tMyID=20\nFrom worker 15:\tMyID=15\nFrom worker 16:\tMyID=16\nFrom worker 21:\tMyID=21\nFrom worker 13:\tMyID=13\n10-element Array{Int64,1}:\n  1\n  2\n  3\n  4\n  5\n  6\n  7\n  8\n  9\n 10<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>function \ub0b4\uc5d0\uc11c using ModuleXX \ub4f1\uc73c\ub85c \ub3d9\uc801\uc73c\ub85c \ubaa8\ub4c8\uc744 \ub85c\ub529\ud558\ub294 \uac83\uc740 \uc27d\uc9c0 \uc54a\ub2e4. function\ub0b4\uc5d0\uc11c using ModuleXX\ub85c \ubaa8\ub4c8\uc744 \ub85c\ub529\ud558\ub294 \uacbd\uc6b0 &#8220;syntax: &#8220;using&#8221; expression not at top level &#8220;\uc5d0\ub7ec\uac00 \ubc1c\uc0dd\ud55c\ub2e4. \uc774\ub7f0 \uacbd\uc6b0 \uc5d0\ub7ec\uc5c6\uc774 \ub3d9\uc801\uc73c\ub85c \ubaa8\ub4c8\uc744 \ub85c\ub529\ud558\uae30 \uc704\ud574\uc11c eval \uc0ac\uc6a9\ub4f1\uc73c\ub85c \uba54\ud0c0\ud504\ub85c\uadf8\ub7a8\uc744 \ud574\uc57c \ub41c\ub2e4. \uc544\ub798 \uc608\uc81c\uc5d0\uc11c\ub294 \ubd84\uc0b0\ucc98\ub9ac(Parallel Processing)\uc608\ub97c \ub4e4\uc5b4 \uc124\uba85\ud55c\ub2e4. \ud2b9\uc815 \ud3f4\ub354 \uc608\ub97c \ub4e4\uc5b4 &#8220;\/home\/shpark\/julia_test\/exproptimizatoin\/examples&#8221; \uc5d0 TestM01.jl\uc744 \uc0dd\uc131\ud55c\ub2e4.\ud30c\uc77c\uba85\uacfc \ubaa8\ub4c8\uba85\uc744 \uac19\uc774 \ud574\uc57c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-site-content-layout":"","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[29,24],"tags":[],"_links":{"self":[{"href":"https:\/\/julialang.kr\/index.php?rest_route=\/wp\/v2\/posts\/2343"}],"collection":[{"href":"https:\/\/julialang.kr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/julialang.kr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/julialang.kr\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/julialang.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2343"}],"version-history":[{"count":2,"href":"https:\/\/julialang.kr\/index.php?rest_route=\/wp\/v2\/posts\/2343\/revisions"}],"predecessor-version":[{"id":2345,"href":"https:\/\/julialang.kr\/index.php?rest_route=\/wp\/v2\/posts\/2343\/revisions\/2345"}],"wp:attachment":[{"href":"https:\/\/julialang.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2343"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/julialang.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2343"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/julialang.kr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2343"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}