Warning, /acts/Traccc/extras/ccl_partitioning/src/Main.hs is written in an unsupported language. File is not indexed.
0001 import System.Exit (exitFailure)
0002 import Data.Maybe (isNothing, fromJust)
0003 import Data.Either (partitionEithers)
0004 import Control.Monad (forM_)
0005 import System.Environment
0006 import CsvIo (getCsvCells)
0007 import Data (Cell(..), Partition(..))
0008
0009 makePartitions :: [Cell] -> [Partition]
0010 makePartitions [] = []
0011 makePartitions acs = go acs 0 0 0 Nothing []
0012 where
0013 go :: [Cell] -> Integer -> Integer -> Integer -> Maybe Cell -> [Partition] -> [Partition]
0014 go [] _ s l _ p = p ++ [Partition { start = s, psize = l }]
0015 go (c:cs) i s l lc p
0016 | isNothing lc = go cs (i + 1) 0 1 (Just c) p
0017 | moduleId c /= moduleId (fromJust lc) = go cs (i + 1) i 1 (Just c) (p ++ [Partition { start = s, psize = l }])
0018 | otherwise = go cs (i + 1) s (l + 1) (Just c) p
0019
0020 packPartitionsTrivial :: [Partition] -> [[Partition]]
0021 packPartitionsTrivial [] = []
0022 packPartitionsTrivial a = [a]
0023
0024 packPartitionsClassic :: Integer -> [Partition] -> [[Partition]]
0025 packPartitionsClassic bs aps = go aps 0 [] []
0026 where
0027 go :: [Partition] -> Integer -> [Partition] -> [[Partition]] -> [[Partition]]
0028 go [] _ c r
0029 | null c = r
0030 | otherwise = r ++ [c]
0031 go (p:ps) n c r
0032 | ((psize p) + n) >= bs = go ps 0 [] (r ++ [(c ++ [p])])
0033 | otherwise = go ps (n + (psize p)) (c ++ [p]) r
0034
0035 printPackingStats :: [[Partition]] -> IO ()
0036 printPackingStats p = do
0037 putStrLn ("Total partitions: " ++ (show . length $ concatPartitions))
0038 putStrLn ("Total cells: " ++ (show numCells))
0039 putStrLn ("Total bins: " ++ (show numBins))
0040 putStrLn ("Smallest bin: " ++ (show smallestBin))
0041 putStrLn ("Largest bin: " ++ (show largestBin))
0042 putStrLn ("Inefficiency: " ++ (show ineffRatio))
0043 putStrLn ("Bins: " ++ (show binCells))
0044 where
0045 numBins = toInteger (length p)
0046 concatPartitions = concat p
0047 numCells = sum . map psize $ concatPartitions
0048 binCells = map (sum . map psize) $ p
0049 smallestBin = minimum binCells
0050 largestBin = maximum binCells
0051 ineffRatio = ((fromInteger (numBins * largestBin)) / (fromInteger (numCells))) :: Float
0052
0053
0054 main :: IO ()
0055 main = do
0056 args <- getArgs;
0057 let fileName = args !! 0
0058 putStrLn ("Reading cells from " ++ fileName)
0059 errorCells <- getCsvCells fileName
0060 let (errors, cells) = partitionEithers errorCells
0061 if not (null errors) then
0062 do
0063 putStrLn "Some cells failed to parse";
0064 exitFailure
0065 else
0066 putStrLn "Successfully read all cells!"
0067 let partitions = makePartitions cells
0068 let packingAlgs =
0069 [ ("Trivial packing", packPartitionsTrivial)
0070 , ("Classic packing (n = 1024)", (packPartitionsClassic 1024))
0071 , ("Classic packing (n = 2048)", (packPartitionsClassic 2048))
0072 ]
0073 forM_ packingAlgs (\(name, fun) -> do
0074 putStrLn ("\n" ++ name ++ ":")
0075 printPackingStats (fun partitions))