Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core rewrite #21

Open
wants to merge 54 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
3a3bcf1
Add Node and Port types with related functions and data
KubEF Sep 13, 2024
5ae3549
Add Reducer
KubEF Sep 13, 2024
03490a2
pin fourmolu version in CI
KubEF Sep 13, 2024
f22318b
Delete Example.Project and add simpliest test for Core.Node
KubEF Sep 14, 2024
142fb67
Delete usless comments and complement useful ones
KubEF Sep 15, 2024
076670f
Add loader
KubEF Sep 15, 2024
e8c6d4e
Fix fourmolu to process $ correctly
KubEF Sep 15, 2024
396c345
Add lenses and make some style changes
KubEF Sep 15, 2024
f20525b
Add tests for isActive and isPortToLoad functions
KubEF Sep 16, 2024
47cdb69
Replace elemIndex to elem in markAllInnerEdges
KubEF Sep 17, 2024
c42efc4
Add tests for selectAddresToLoad function
KubEF Sep 17, 2024
957dd8b
Replace repeat to def in handle function
KubEF Sep 17, 2024
69b3439
Add tests for markAllInnerEdges function
KubEF Sep 17, 2024
77a1365
[WIP]Add layout of memory manager
KubEF Sep 18, 2024
beb03b7
Redesign Address and add LocalNode
KubEF Oct 1, 2024
cb4f812
Add updateRam function
KubEF Oct 1, 2024
9204022
Add IdOfPort
KubEF Oct 21, 2024
28948dc
Add more types and getter port function
KubEF Oct 28, 2024
fcc12c1
Add memory manager
KubEF Oct 28, 2024
ee84822
Fully redesign reducer
KubEF Oct 28, 2024
dfd0921
Comment irrelevant (all) tests
KubEF Oct 28, 2024
503c9a1
Change default json formatter
KubEF Oct 28, 2024
6c44b6a
Add simple vector-based key-value storage
KubEF Nov 7, 2024
8c684c2
Replace Address to AddressNum in Loader and unify LoadedNode with Loc…
KubEF Nov 7, 2024
f1d32ac
Rework memory manager update to minimize usage of RAM
KubEF Nov 7, 2024
833d147
Rename Map functions
KubEF Nov 8, 2024
9e0c858
Merge foldls
KubEF Nov 8, 2024
98f2046
Delete updateRam
KubEF Nov 15, 2024
d4f2574
Refactor and some doc changes
KubEF Nov 15, 2024
6db019e
Minor refactoring
KubEF Nov 15, 2024
27f82a3
Simplified reducer
KubEF Nov 15, 2024
52030f6
Add handmade agents
KubEF Nov 15, 2024
1918777
Write concrete handmade reduction rule
KubEF Nov 15, 2024
fa9b564
Add some doctests in memory manager
KubEF Nov 18, 2024
4afd346
Weakened Index to Unsigned constraint
KubEF Nov 18, 2024
fa5f3be
Update simple lambda reduction rule
KubEF Nov 18, 2024
ed75dd2
Rewrite indexToUnsigned via resize and bitCoerce
KubEF Nov 18, 2024
c8d17af
Add skipping svg files in pre-commit
KubEF Nov 19, 2024
2564570
Fix doctests
KubEF Nov 25, 2024
9fce015
Rename agent constructors
KubEF Nov 28, 2024
3547c90
Rework Port and Node, add Connection type
KubEF Dec 8, 2024
3893e18
Split MemoryManager functionality
KubEF Dec 8, 2024
3262a04
Replace Edge, ActivePair and Delta type
KubEF Dec 8, 2024
7ee225d
Replace types in MemoryManager modules
KubEF Dec 8, 2024
be705c9
Rewrite addresses allocated to Vec of Maybe
KubEF Dec 8, 2024
164643d
Add giveActiveAddressNumber function
KubEF Dec 9, 2024
43c3e6f
Fix the case when necessary amount of memory is zero
KubEF Dec 9, 2024
01db43e
Add necessary loading functions
KubEF Dec 9, 2024
9e46b32
Rewrite Reducer
KubEF Dec 9, 2024
b685955
[WIP] Add core function
KubEF Dec 9, 2024
2ca782d
Fix pedantic build
KubEF Dec 9, 2024
1240556
Add specific reduce rules for simple lambdas
KubEF Dec 9, 2024
63aa4ef
Add example of initial net
KubEF Dec 9, 2024
462d8d5
Add functions to export list for doctests
KubEF Dec 9, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lamagraph-core/lamagraph-core.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,12 @@ library
import: common-options
hs-source-dirs: src
exposed-modules:
Core.Core
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is Core' a good name for top-level ? May we use something like ReductionMachine` for top-level namespace?

Core.Node
Core.Reducer
Core.MemoryManager
Core.MemoryManager.MemoryManager
Core.MemoryManager.ChangesAccumulator
Core.MemoryManager.NodeChanges
Core.Map
Core.Concrete.ReduceRulesLambda
INet.Net
Expand Down
10 changes: 5 additions & 5 deletions lamagraph-core/src/Core/Concrete/ReduceRulesLambda.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ import INet.Net
Node 2 ->
ReduceRuleResult 2 2 2
lNode |><| rNode = case (lNode ^. nodeType, rNode ^. nodeType) of
(Apply, Abs) -> applyToLambdaRule lNode rNode
(Abs, Apply) -> applyToLambdaRule lNode rNode
(Eps, _) -> epsToAnyRule lNode rNode
(_, Eps) -> epsToAnyRule rNode lNode
(Apply, Abstract) -> applyToLambdaRule lNode rNode
(Abstract, Apply) -> applyToLambdaRule lNode rNode
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be left and right node be switched?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is symmetrical

(Erase, _) -> epsToAnyRule lNode rNode
(_, Erase) -> epsToAnyRule rNode lNode
_ -> error "There is no rule for this active pair in the reduction rules"

{- | Reduce rule for `Apply` and `Abs`
Expand Down Expand Up @@ -60,7 +60,7 @@ epsToAnyRule ::
ReduceRuleResult portsNumber edgesNumber portsNumber
epsToAnyRule _ nSome =
let arisingEdges = def
genNewEpsNode port = Node port def Eps
genNewEpsNode port = Node port def Erase
arisingNodes =
imap
(\i maybePort -> flip LocalNode (indexToUnsigned i) . genNewEpsNode <$> maybePort)
Expand Down
77 changes: 77 additions & 0 deletions lamagraph-core/src/Core/Core.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}
{-# HLINT ignore "Eta reduce" #-}
{-# OPTIONS_GHC -Wno-unused-local-binds #-}

module Core.Core where

import Clash.Prelude
import Control.Lens hiding ((:>))
import Core.Loader (Ram, loadActivePair, loadInterface)
import Core.MemoryManager.ChangesAccumulator (getAllChangesByDelta)
import Core.MemoryManager.MemoryManager (
ActivePair,
MemoryManager,
giveActiveAddressNumber,
leftNode,
removeActivePair,
rightNode,
)
import Core.MemoryManager.NodeChanges
import Core.Node
import Core.Reducer (ChooseReductionRule, getInterface, reducer)

core ::
forall portsNumber nodesNumber edgesNumber cellsNumber dom.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can core be parametrized with single dom? What about different domains for memory, reducer, other parts?

( KnownNat portsNumber
, KnownNat nodesNumber
, KnownNat edgesNumber
, KnownNat cellsNumber
, 1 <= cellsNumber
, CLog 2 cellsNumber <= BitSize AddressNumber
, nodesNumber <= cellsNumber
, KnownDomain dom
, HiddenClockResetEnable dom
, Enum AddressNumber
) =>
Vec cellsNumber (Maybe (Node portsNumber)) -> -- Initial network
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a network, but active pairs. We need (at least) two layers: program loader and processor itself.

MemoryManager cellsNumber -> -- Initial information about busy addresses and active pairs
ChooseReductionRule cellsNumber nodesNumber edgesNumber portsNumber ->
Signal dom (Vec cellsNumber (Maybe (Node portsNumber)))
core initialNetwork initialMemoryManager chooseReductionRule = bundle $ map (`ram` def) (unbundle allAddresses)
where
memoryManager = register @dom initialMemoryManager allocatedAddressesMemoryManager
ram = blockRam initialNetwork :: Ram dom portsNumber
activeAddress = case sequenceA $ giveActiveAddressNumber memoryManager of
Just x -> x
Nothing -> error "" -- end of program. TODO: add handling of this
acPair = loadActivePair ram activeAddress
removedActivePairMemoryManager = removeActivePair acPair memoryManager
_ = removeActivePairFromRam ram acPair
(delta, allocatedAddressesMemoryManager) = reducer chooseReductionRule removedActivePairMemoryManager acPair
-- instead of "@portsNumber @nodesNumber" it possible to write ":: Signal dom (Interface nodesNumber)"
interface = getInterface @portsNumber @nodesNumber <$> acPair
externalNodes = loadInterface ram interface
changes = updateLoadedNodesByChanges <$> externalNodes <*> getAllChangesByDelta delta interface
_ = writeChanges ram changes
allAddresses = pure (generateI (+ 1) (0 :: AddressNumber))

writeChanges ::
(KnownNat maxNumOfChangedNodes, KnownNat portsNumber, KnownDomain dom, HiddenClockResetEnable dom) =>
Ram dom portsNumber ->
Signal dom (Vec maxNumOfChangedNodes (Maybe (LoadedNode portsNumber))) ->
Vec maxNumOfChangedNodes (Signal dom (Maybe (Node portsNumber)))
writeChanges ram changes = map writeByLoadedNode (unbundle changes)
where
writeByLoadedNode signalMaybeLoadedNode = case sequenceA signalMaybeLoadedNode of
Nothing -> ram (pure 0) def
Just signalLoadedNode ->
let f = Just (view originalAddress <$> signalLoadedNode, Just . view containedNode <$> signalLoadedNode)
in ram (pure 0) (traverse bundle f)

removeActivePairFromRam ::
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it be here, not in Ram module?

Ram dom portsNumber -> Signal dom (ActivePair portsNumber) -> Vec 2 (Signal dom (Maybe (Node portsNumber)))
removeActivePairFromRam ram acPair = map (\address -> partRam (Just <$> bundle (address, def))) (leftAddress :> rightAddress :> Nil)
where
partRam = ram def
leftAddress = view (leftNode . originalAddress) <$> acPair
rightAddress = view (rightNode . originalAddress) <$> acPair
60 changes: 48 additions & 12 deletions lamagraph-core/src/Core/Loader.hs
Original file line number Diff line number Diff line change
@@ -1,21 +1,57 @@
module Core.Loader where

import Clash.Prelude
import Control.Lens (view)
import Core.MemoryManager.MemoryManager (ActivePair (ActivePair))
import Core.MemoryManager.NodeChanges
import Core.Node

-- | Get `Node` by his `AddressNumber` from RAM. Actually, preparing to reducer work.
loader ::
-- | Type alias for partial applied `blockRam`
type Ram dom portsNumber =
( Signal dom AddressNumber ->
Signal dom (Maybe (AddressNumber, Maybe (Node portsNumber))) ->
Signal dom (Maybe (Node portsNumber))
)

-- | Read external `Node`s from ram
loadInterface ::
( KnownDomain dom
, HiddenClockResetEnable dom
, KnownNat numberOfPorts
, KnownNat portsNumber
, KnownNat externalNodesNumber
) =>
(Signal dom AddressNumber -> Signal dom (Node numberOfPorts)) ->
Signal dom (Maybe AddressNumber) ->
Signal dom (Maybe (LoadedNode numberOfPorts))
loader ram mbAddressNumberToLoad =
mkLoadedNode <$> mbNode <*> mbAddressNumberToLoad
Ram dom portsNumber ->
Signal dom (Interface externalNodesNumber) ->
Signal dom (Vec externalNodesNumber (Maybe (LoadedNode portsNumber)))
loadInterface ram interface =
bundle $
map
(traverse readFromRam . sequenceA)
(unbundle interface)
where
partRam address = ram address def
readFromRam address = case sequenceA (partRam address) of
Just node -> LoadedNode <$> node <*> address
Nothing -> error "An attempt to read at a free address"

-- | Load `ActivePair` by `AddressNumber`. It is assumed that `AddressNumber` is actually active
loadActivePair ::
(KnownDomain dom, HiddenClockResetEnable dom, KnownNat portsNumber) =>
Ram dom portsNumber ->
Signal dom AddressNumber ->
Signal dom (ActivePair portsNumber)
loadActivePair ram leftActiveNodeAddress =
ActivePair
<$> (LoadedNode <$> leftActiveNode <*> leftActiveNodeAddress)
<*> (LoadedNode <$> rightActiveNode <*> rightActiveNodeAddress)
where
mkLoadedNode node address = LoadedNode <$> node <*> address
mbNode = case sequenceA mbAddressNumberToLoad of
Nothing -> pure Nothing
Just n -> sequenceA $ Just (ram n)
partRam address = ram address def
getNodeByAddress address = case sequenceA $ partRam address of
Just node -> node
Nothing -> error "An attempt to read at a free address"
getRightActiveNodeAddress node = case view primaryPort node of
Connected port -> view nodeAddress port
NotConnected -> error "Wrong definition of active pair"
leftActiveNode = getNodeByAddress leftActiveNodeAddress
rightActiveNodeAddress = getRightActiveNodeAddress <$> leftActiveNode
rightActiveNode = getNodeByAddress rightActiveNodeAddress
Loading
Loading