Save 37% off PRO during our Black Friday Sale! »

Region based memory management for mini ML

Region based memory management for mini ML

region推論の実装をMLのサブセットで行った際の発表資料
リポジトリはこちら:https://github.com/Everysick/region_based_mini_ml

E9ccecdb6018022c5fbed245311747d1?s=128

TAKAMASA Saichi

January 12, 2017
Tweet

Transcript

  1. Regionਪ࿦ΞϧΰϦζϜͷ࣮૷ͱಈ޲ Takamasa Sachi, a.k.a @everysick

  2. 1. ֓ཁ 2. Region ਪ࿦ʹ͍ͭͯ • Region ͷΞΠσΞ • Region

    ਪ࿦ͷ໨త • ਪ࿦ΞϧΰϦζϜ 3. ࣮༻ྫ 4. ·ͱΊ, ՝୊ 5. ࢀߟจݙ
  3. 1. ֓ཁ 2. Region ਪ࿦ʹ͍ͭͯ • Region ͷΞΠσΞ • Region

    ਪ࿦ͷ໨త • ਪ࿦ΞϧΰϦζϜ 3. ࣮༻ྫ 4. ·ͱΊ, ՝୊ 5. ࢀߟจݙ
  4. ֓ཁ • Regionਪ࿦ • Regionʹؔ͢ΔจݙΛಡΉ • Mads Tofte, Jean-Pierre Talpin

    (1996)
 “Region-Based Memory Management” • Regionਪ࿦ΞϧΰϦζϜͷ࣮૷ • ࣮૷ͷΞΠσΞ΍ൃݟͨ͠఺Λ·ͱΊΔ • ಈ޲ͷௐࠪ • ML kit Ͱͷ࣮༻ྫ • ͦͷ΄͔طଘݴޠ΁ͷԠ༻ྫ
  5. 1. ֓ཁ 2. Region ਪ࿦ʹ͍ͭͯ • Region ͷΞΠσΞ • Region

    ਪ࿦ͷ໨త • ਪ࿦ΞϧΰϦζϜ 3. ࣮༻ྫ 4. ·ͱΊ, ՝୊ 5. ࢀߟจݙ
  6. Regionਪ࿦ʹ͍ͭͯ - RegionͷΞΠσΞ • ಈతʹϝϞϦ֬อ, ղ์͞ΕΔ"σʔλ"ʹ͍ͭͯɺ
 ͦͷ"σʔλ"͕ࢀর͞ΕΔྖҬΛRegionͱఆٛ • είʔϓ୯ҐͰRegionͷൣғ͸ܾఆ͞ΕΔ {

    int y; { int x = 5; print (x); } print (y); } xͷείʔϓ xͷRegion yͷείʔϓ yͷRegion
  7. Regionਪ࿦ʹ͍ͭͯ - RegionͷΞΠσΞ • ಈతʹϝϞϦ֬อ, ղ์͞ΕΔ"σʔλ"ʹ͍ͭͯɺ
 ͦͷ"σʔλ"͕ࢀর͞ΕΔྖҬΛRegionͱఆٛ • είʔϓ୯ҐͰRegionͷൣғ͸ܾఆ͞ΕΔ {

    int *ptr; { int x = 5; ptr = &x; } print (*ptr); } xͷείʔϓ(ෆਖ਼) είʔϓ୯Ґͷ؅ཧͰ͸ ͜ͷΑ͏ͳྫʹରͯ͠ରԠͰ͖ͳ͍
  8. Regionਪ࿦ʹ͍ͭͯ - RegionͷΞΠσΞ • ಈతʹϝϞϦ֬อ, ղ์͞ΕΔ"σʔλ"ʹ͍ͭͯɺ
 ͦͷ"σʔλ"͕ࢀর͞ΕΔྖҬΛRegionͱఆٛ • είʔϓ୯ҐͰRegionͷൣғ͸ܾఆ͞ΕΔ {

    int *ptr; { int x = 5; ptr = &x; } print (*ptr); } xͷRegion ɾptrͷείʔϓ͕xͷRegion ɾࢀরΛฦؔ͢਺΋ಉ༷
  9. Regionਪ࿦ʹ͍ͭͯ - Regionਪ࿦ͷ໨త • શͯͷRegionΛ࠷దʹਪ࿦Ͱ͖Ε͹, είʔϓΛ·͙ͨ "σʔλ"ͷ؅ཧΛશͯ੩తʹߦ͏͜ͱ͕Մೳ 㱺 Garbage Collection

    ͕ෆඞཁʹͳΔ 㱺 Stop the world ʹΑΔ࣮ߦ࣌ͷఀࢭ࣌ؒ࡟ݮͳͲ 㱺 ࣮ߦ࣌εϧʔϓοτͷ޲্ʹܨ͕Δ • ͨͩ͠Regionͷਪ࿦݁Ռʹ͸ྑ͠ѱ͕͋͠Δ • શͯάϩʔόϧͳRegionʹׂΓ౰ͯΔ͜ͱ΋Մೳ
  10. Regionਪ࿦ʹ͍ͭͯ - ਪ࿦ΞϧΰϦζϜ • "Regionਪ࿦" ͸ "Region஫ऍ෇͖ܕͷܕਪ࿦" • ਪ࿦ʹΑͬͯRegionͷಋೖ, ඥ෇͚Λܾఆ͢Δ͜ͱ͕Մೳ

    • Regionਪ࿦͸ϓϩάϥϛϯάݴޠML΁ͷΞΠσΞ • આ໌ɺ࣮૷ͷλʔήοτʹMLΛ࠾༻ • ҎԼͷΑ͏ʹͯ͠MLͷߏจ΁RegionΛఆٛ letregion r in … end 1 @ r (λ x. x) @ r ɾRegion "r" ͷಋೖ "r" ʹඥ෇͘ "σʔλ" ͸ ͜ͷείʔϓ಺ͰͷΈ࢖༻͞ΕΔ ɾRegion "r" ΁ͷඥ෇͚ "σʔλ"ΛͲͷRegion΁ ඥ෇͚Δ͔Λ໌ࣔ
  11. Regionਪ࿦ʹ͍ͭͯ - ਪ࿦ΞϧΰϦζϜ • Regionͷ৘ใ͕ແ͍ݴޠ͔ΒRegion෇͖ͷݴޠ΁ • ݩʹͳΔݴޠ͸७ਮͳMLͰྑ͍ • نଇʹैͬͯܕਪ࿦ͱஔ͖׵͑Λద༻͢Δ let

    x = (2, 3) in (λ y. (#1 x, y) end 5 letregion r4, r5 in letregion r6 in let x = (2 @ r2, 3 @ r6) @ r4 in (λ y. (#1 x, y) @ r1) @ r5 end end 5 @ r3 end
  12. Regionਪ࿦ʹ͍ͭͯ - ਪ࿦ΞϧΰϦζϜ • MLͷܕΛ֦ு • Regionͷࣝผࢠ, Region΁ͷ࡞༻ͳͲͷ஫ऍΛ௥Ճ • e.g.

    (int) ! ((int, r1), {put(r1), get(r2)}) • ֦ு͞Εͨܕʹରͯ͠ܕਪ࿦Λߦ͏ • Damas and Milner (1982) ͷ MLtype, ML type scheme Λ֦ு • Regionਪ࿦ͷੑೳ͸ܕͷ୯ҰԽʹؼண • ୯ҰԽͷΞϧΰϦζϜʹΑͬͯ͸
 ඇޮ཰తͳRegionʹ"σʔλ"͕഑ஔ͞Εͯ͠·͏
  13. 1. ֓ཁ 2. Region ਪ࿦ʹ͍ͭͯ • Region ͷΞΠσΞ • Region

    ਪ࿦ͷ໨త • ਪ࿦ΞϧΰϦζϜ 3. ࣮༻ྫ 4. ·ͱΊ, ՝୊ 5. ࢀߟจݙ
  14. ࣮༻ྫ • ML kit • Standard MLॲཧܥ • Regionਪ࿦Λಋೖ͠ϝϞϦ؅ཧΛ΄΅RegionͰߦ͑Δ •

    ׬શͳGC less ͸೉͍͠ • ୯ҰԽΞϧΰϦζϜ͸Regionͷಉఆ͕Մೳͳ͜ͱ͕ূ໌͞Ε͍ͯΔ let fib n in if n < 2 then 1 else fib(n-1) + fib(n-2) end 10000…000 ɾ࠶ؼਂ౓͕ਂ͍ ɾRegionͷղ์ʹͨͲΓண͚ͳ͍ ɾෆཁͳϝϞϦ͕࠶ؼதʹ࢒Δ ɾղফͷͨΊGCΛಋೖ ɾRegion಺ͷϝϞϦΛ࠶׆༻͢Δ
  15. ࣮༻ྫ • Rust (Cyclone) • Cyclone͸CݴޠͷγϯλοΫεʹRegionͷ֓೦Λಋೖ • ಈతͳRegion, ੩తͳRegion, ώʔϓΛ࢖ͬͨRegion

    • Regionਪ࿦ͱ͸ҟͳΔ΋ͷ • Cycloneͱͯ͠ͷ։ൃ͸ऴྃ • Cyclone͕Rust΁ٵऩ͞Ε৽نݴޠͱͯ͠։ൃ͕ਐߦ • Ownership΍Lifetime(≒ Region)ͳͲͷ֓೦Λಋೖ • Regionਪ࿦Ͱ͸ͳ͘RegionΛಋೖͨ͠GC lessͷݴޠ
  16. 1. ֓ཁ 2. Region ਪ࿦ʹ͍ͭͯ • Region ͷΞΠσΞ • Region

    ਪ࿦ͷ໨త • ਪ࿦ΞϧΰϦζϜ 3. ࣮༻ྫ 4. ·ͱΊ, ՝୊ 5. ࢀߟจݙ
  17. ·ͱΊ, ՝୊ • ·ͱΊ • RegionɺRegionਪ࿦ʹֶ͍ͭͯΜͩ • Regionਪ࿦ ≒ ୯ҰԽʹґଘ͢Δ͜ͱΛཧղͨ͠

    • Regionਪ࿦ͷ࣮૷Λߦͬͨ • ਪ࿦෦෼500ߦఔ౓, MLͷαϒηοτͰಈ࡞ • Region׆༻ྫʹ͍ͭͯͷαʔϕΠΛߦͬͨ • ՝୊ • ࣮૷ͨ͠ϓϩάϥϜͷվળ • ਪ࿦Ҏ֎ͷRegionͷѻ͍ʹ͍ͭͯͷௐࠪ
  18. 1. ֓ཁ 2. Region ਪ࿦ʹ͍ͭͯ • Region ͷΞΠσΞ • Region

    ਪ࿦ͷ໨త • ਪ࿦ΞϧΰϦζϜ 3. ࣮༻ྫ 4. ·ͱΊ, ՝୊ 5. ࢀߟจݙ
  19. ࢀߟจݙ • Mads Tofte, Jean-Pierre Talpin (1996)
 “Region-Based Memory Management”

    • Mads Tofte, Lars Birkedal (1998)
 “A region inference algorithm” • Dan Grossman, Greg Morrisett, Trevor Jim, 
 Michael Hicks, Yanling Wang, James Cheney(2002) 
 “Region-based memory management in cyclone” • ถాڡ࢙, ӏ઒࢝ཅ, ՖҪ ྄, ീਿণ޺, ౬ᕆଠҰ(2005)
 ”Ϧʔδϣϯม਺ͷಈతͳΤΠϦΞε൑ఆʹΑΔϝϞϦޮ཰޲্”