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

replace toml to yaml #276

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 14 commits
Commits
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
14 changes: 5 additions & 9 deletions app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import NITTA.Synthesis (TargetSynthesis (..), mlScoreKeyPrefix, noSynthesis, sta
import NITTA.Synthesis.MlBackend.ServerInstance
import NITTA.UIBackend
import NITTA.UIBackend.Types (BackendCtx, mlBackendGetter, nodeScores, outputPath, receivedValues, root)
import NITTA.Utils
import Paths_nitta
import System.Console.CmdArgs hiding (def)
import System.Exit
Expand Down Expand Up @@ -199,8 +198,6 @@ nittaArgs =
getNittaArgs :: IO Nitta
getNittaArgs = cmdArgs nittaArgs

fromConf toml s = getFromTomlSection s =<< toml

main = do
( Nitta
filename
Expand Down Expand Up @@ -229,10 +226,9 @@ main = do
-- force line buffering (always, not just when stdout is connected to a tty),
-- it's critical for successful parsing of NITTA's stdout in python scripts
hSetBuffering stdout LineBuffering

toml <- case uarch of
conf <- case uarch of
Nothing -> return Nothing
Just path -> Just . getToml <$> T.readFile path
Just path -> Just <$> parseConfig path
Copy link
Owner

Choose a reason for hiding this comment

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

Something like parseConfig <$> path should work?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

parseConfig is a FilePath -> IO MicroarchitectureConf type
path is a FilePath type
<$> is a (a -> b) -> f a -> f b type

The result must be type of IO Maybe MicroarchitectureConf, so Just <$> parseConfig pathdoesn't seem to be able to be simplified.


let exactFrontendType = identifyFrontendType filename frontend_language

Expand All @@ -241,8 +237,8 @@ main = do
let frontendResult@FrontendResult{frDataFlow, frTrace, frPrettyLog} =
translate exactFrontendType src
received = [("u#0", map (\i -> read $ show $ sin ((2 :: Double) * 3.14 * 50 * 0.001 * i)) [0 .. toEnum n])]
ioSync = fromJust $ io_sync <|> fromConf toml "ioSync" <|> Just Sync
confMa = toml >>= Just . mkMicroarchitecture ioSync
ioSync = fromJust $ io_sync <|> valueIoSync <$> conf <|> Just Sync
confMa = mkMicroarchitecture <$> conf
ma :: BusNetwork T.Text T.Text (Attr (FX m b)) Int
ma
| auto_uarch && isJust confMa =
Expand Down Expand Up @@ -302,7 +298,7 @@ main = do
exitSuccess
)
$ parseFX . fromJust
$ type_ <|> fromConf toml "type" <|> Just "fx32.32"
$ type_ <|> T.unpack . valueType <$> conf <|> Just "fx32.32"

parseFX input =
let typePattern = mkRegex "fx([0-9]+).([0-9]+)"
Expand Down
46 changes: 0 additions & 46 deletions examples/microarch.toml

This file was deleted.

32 changes: 32 additions & 0 deletions examples/microarch.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
mock: true
type: fx32.32
ioSync: Sync
puLibrary:
Copy link
Owner

Choose a reason for hiding this comment

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

Hmm. Here we have misunderstanding. The idea behind this: describe library of process unit to be allocated in separated place, with name "library" intead of "proto"

isSlave: true
bufferSize: 6
bounceFilter: 0
networks:
net1:
pus:
spi:
type: SPI
mosi: mosi
miso: miso
sclk: sclk
cs: cs
protos:
Copy link
Owner

Choose a reason for hiding this comment

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

Let move it to the top level as puLibrary.

Copy link
Owner

Choose a reason for hiding this comment

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

Is is about all PUs, which nitta can allocate automatically. Current -- in network. Should be -- on the top level & network.

fram{x}: # If you want a PU can be allocated only once, remove {x} from the PU name.
type: Fram
size: 32
shift{x}:
type: Shift
sRight: true
mul{x}:
type: Multiplier
accum{x}:
type: Accum
isInt: true
div{x}:
type: Divider
pipeline: 4

8 changes: 4 additions & 4 deletions nitta.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ library
, hashable
, heap
, hslogger
, htoml
, http-api-data
, http-conduit
, hxt
Expand Down Expand Up @@ -178,6 +177,7 @@ library
, wai-app-static
, wai-cors
, warp
, yaml
default-language: Haskell2010

executable nitta
Expand Down Expand Up @@ -211,7 +211,6 @@ executable nitta
, ginger
, heap
, hslogger
, htoml
, intervals
, mtl
, nitta
Expand All @@ -223,6 +222,7 @@ executable nitta
, tostring
, unordered-containers
, wai-app-static
, yaml
default-language: Haskell2010

executable nitta-api-gen
Expand Down Expand Up @@ -259,7 +259,6 @@ executable nitta-api-gen
, ginger
, heap
, hslogger
, htoml
, intervals
, mtl
, nitta
Expand All @@ -271,6 +270,7 @@ executable nitta-api-gen
, tostring
, unordered-containers
, wai-app-static
, yaml
default-language: Haskell2010

test-suite nitta-test
Expand Down Expand Up @@ -337,7 +337,6 @@ test-suite nitta-test
, genvalidity-property
, ginger
, heap
, htoml
, hxt
, intervals
, language-lua
Expand All @@ -359,4 +358,5 @@ test-suite nitta-test
, tostring
, unordered-containers
, wai-app-static
, yaml
default-language: Haskell2010
2 changes: 1 addition & 1 deletion package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ dependencies:
- data-default
- filepath
- ginger
- htoml
- yaml
- intervals
- mtl
- prettyprinter
Expand Down
111 changes: 68 additions & 43 deletions src/NITTA/Model/Microarchitecture/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,32 @@
{-# LANGUAGE PartialTypeSignatures #-}

module NITTA.Model.Microarchitecture.Config (
MicroarchitectureConf (valueType, valueIoSync),
parseConfig,
mkMicroarchitecture,
) where

import Data.Aeson (
FromJSON (parseJSON),
Options (sumEncoding),
SumEncoding (TaggedObject, contentsFieldName, tagFieldName),
ToJSON (toJSON),
defaultOptions,
genericParseJSON,
genericToJSON,
)
import Data.Default (Default (def))
import Data.HashMap.Internal.Strict (HashMap)
import Data.Map as M (
Map,
toList,
)
import Data.Maybe (fromMaybe)
import Data.Text qualified as T
import Data.Yaml (
FromJSON (parseJSON),
ToJSON (toJSON),
Value (Object),
decodeFileThrow,
(.:),
)
import GHC.Generics (Generic)
import NITTA.Intermediate.Value (Val)
import NITTA.Intermediate.Variable (Var)
Expand All @@ -30,38 +41,34 @@ import NITTA.Model.Networks.Bus (
)
import NITTA.Model.Networks.Types (IOSynchronization)
import NITTA.Model.ProcessorUnits qualified as PU
import NITTA.Utils (getFromToml)

data PULibrary = PULibrary
{ isSlave :: Bool
, bufferSize :: Maybe Int
, bounceFilter :: Int
}
deriving (Generic, Show)

instance FromJSON PULibrary
instance ToJSON PULibrary

data PUConf
= Accum
{ name :: T.Text
}
| Divider
{ name :: T.Text
, pipeline :: Int
, mock :: Bool
{ pipeline :: Int
}
| Multiplier
{ name :: T.Text
, mock :: Bool
}
| Fram
{ name :: T.Text
, size :: Int
{ size :: Int
}
| SPI
{ name :: T.Text
, mosi :: T.Text
{ mosi :: T.Text
, miso :: T.Text
, sclk :: T.Text
, cs :: T.Text
, isSlave :: Bool
, bufferSize :: Maybe Int
, bounceFilter :: Int
}
| Shift
{ name :: T.Text
, sRight :: Maybe Bool
{ sRight :: Maybe Bool
}
deriving (Generic, Show)

Expand All @@ -77,40 +84,59 @@ instance FromJSON PUConf where
parseJSON = genericParseJSON puConfJsonOptions

data NetworkConf = NetworkConf
{ name :: T.Text
, pus :: [PUConf]
, protos :: [PUConf]
{ pus :: Maybe (Map T.Text PUConf)
, protos :: Maybe (Map T.Text PUConf)
}
deriving (Generic, Show)

instance FromJSON NetworkConf
instance ToJSON NetworkConf

newtype MicroarchitectureConf = MicroarchitectureConf
{ networks :: [NetworkConf]
data MicroarchitectureConf = MicroarchitectureConf
{ mock :: Bool
, valueType :: T.Text
, valueIoSync :: IOSynchronization
, puLibrary :: PULibrary
, networks :: Map T.Text NetworkConf
}
deriving (Generic, Show)

instance FromJSON MicroarchitectureConf
instance FromJSON MicroarchitectureConf where
Copy link
Owner

Choose a reason for hiding this comment

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

Is it possible to do it via Generic?

Copy link
Collaborator Author

@dokerplp dokerplp Apr 1, 2024

Choose a reason for hiding this comment

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

Didn't work out ¯\_(ツ)_/¯

Copy link
Owner

Choose a reason for hiding this comment

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

Strange. Maybe because you need to use HashMap instead of Map.

parseJSON (Object v) = do
mock <- v .: "mock"
valueType <- v .: "type"
valueIoSync <- v .: "ioSync"
puLibrary <- v .: "puLibrary"
networks <- v .: "networks"
return MicroarchitectureConf{mock, valueType, valueIoSync, puLibrary, networks}
parseJSON v = fail $ show v
instance ToJSON MicroarchitectureConf

mkMicroarchitecture :: (Val v, Var x, ToJSON a, ToJSON x) => IOSynchronization -> HashMap T.Text a -> BusNetwork T.Text x v Int
mkMicroarchitecture ioSync toml =
parseConfig :: FilePath -> IO MicroarchitectureConf
parseConfig path = do
decodeFileThrow path :: IO MicroarchitectureConf

mkMicroarchitecture :: (Val v, Var x, ToJSON x) => MicroarchitectureConf -> BusNetwork T.Text x v Int
mkMicroarchitecture MicroarchitectureConf{mock, valueIoSync, puLibrary, networks} =
let addPU proto
| proto = addCustomPrototype
| otherwise = addCustom
isSlave_ = isSlave puLibrary
bufferSize_ = bufferSize puLibrary
bounceFilter_ = bounceFilter puLibrary
build NetworkConf{pus, protos} = do
mapM_ (configure False) pus
mapM_ (configure True) protos
mapM_ (configure_ False) $ M.toList $ fromMaybe def pus
mapM_ (configure_ True) $ M.toList $ fromMaybe def protos
where
configure proto Accum{name} = addPU proto name def PU.AccumIO
configure proto Divider{name, pipeline, mock} = addPU proto name (PU.divider pipeline mock) PU.DividerIO
configure proto Multiplier{name, mock} = addPU proto name (PU.multiplier mock) PU.MultiplierIO
configure proto Fram{name, size} = addPU proto name (PU.framWithSize size) PU.FramIO
configure proto Shift{name, sRight} = addPU proto name (PU.shift $ Just False /= sRight) PU.ShiftIO
configure proto SPI{name, mosi, miso, sclk, cs, isSlave, bounceFilter, bufferSize} =
addPU proto name (PU.anySPI bounceFilter bufferSize) $
if isSlave
configure_ proto (name, pu) = configure proto name pu
configure proto name Accum = addPU proto name def PU.AccumIO
configure proto name Divider{pipeline} = addPU proto name (PU.divider pipeline mock) PU.DividerIO
configure proto name Multiplier = addPU proto name (PU.multiplier mock) PU.MultiplierIO
configure proto name Fram{size} = addPU proto name (PU.framWithSize size) PU.FramIO
configure proto name Shift{sRight} = addPU proto name (PU.shift $ Just False /= sRight) PU.ShiftIO
configure proto name SPI{mosi, miso, sclk, cs} =
addPU proto name (PU.anySPI bounceFilter_ bufferSize_) $
if isSlave_
then
PU.SPISlave
{ slave_mosi = PU.InputPortTag mosi
Expand All @@ -125,8 +151,7 @@ mkMicroarchitecture ioSync toml =
, master_sclk = PU.OutputPortTag sclk
, master_cs = PU.OutputPortTag cs
}
nets = networks (getFromToml toml :: MicroarchitectureConf)
mkNetwork net@NetworkConf{name} = modifyNetwork (busNetwork name ioSync) (build net)
in case nets of
[n] -> mkNetwork n
mkNetwork name net = modifyNetwork (busNetwork name valueIoSync) (build net)
in case M.toList networks of
[(name, net)] -> mkNetwork name net
_ -> error "multi-networks are not currently supported"
Loading
Loading