Skip to content

Commit

Permalink
Use timestats, make ijcs a Set
Browse files Browse the repository at this point in the history
  • Loading branch information
DaveBarton committed Oct 31, 2023
1 parent be53b96 commit 4c4d405
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 77 deletions.
11 changes: 7 additions & 4 deletions calculi.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ common deps
default-extensions: DuplicateRecordFields LambdaCase MonoLocalBinds NoFieldSelectors
OverloadedRecordDot PatternSynonyms RecordWildCards
other-extensions: CPP DataKinds FunctionalDependencies Strict ViewPatterns
ghc-options: -Wall -Wcompat -feager-blackholing
ghc-options: -Wall -Wcompat -feager-blackholing -threaded
if impl(ghc >= 9.8.1)
ghc-options: -Wno-x-partial
build-depends: base >= 4.16 && < 5, containers >= 0.6 && < 0.8, extra >= 1.6 && < 1.8

library
Expand Down Expand Up @@ -62,6 +64,7 @@ library
strict-list,
-- text,
time >= 1.9.3 && < 2,
timestats,
vector

hs-source-dirs: src
Expand All @@ -77,7 +80,7 @@ executable bp-demo
build-depends: calculi

hs-source-dirs: bp-demo
ghc-options: -threaded -rtsopts "-with-rtsopts=-N -A64m -t"
ghc-options: -rtsopts "-with-rtsopts=-N -A64m -t"

test-suite calculi-test
import: deps
Expand All @@ -99,7 +102,7 @@ test-suite calculi-test
build-depends: calculi, hedgehog == 1.*, mod, rrb-vector, strict-list

hs-source-dirs: test
ghc-options: -threaded -rtsopts "-with-rtsopts=-N -A64m"
ghc-options: -rtsopts "-with-rtsopts=-N -A64m"

benchmark time-gb
import: deps
Expand All @@ -113,7 +116,7 @@ benchmark time-gb
build-depends: calculi, process >= 1.2 && < 2, time >= 1.9.3 && < 2

hs-source-dirs: timings
ghc-options: -threaded -rtsopts "-with-rtsopts=-N -A64m -s"
ghc-options: -rtsopts "-with-rtsopts=-N -A64m -s"

benchmark bench
import: deps
Expand Down
22 changes: 11 additions & 11 deletions src/Control/Parallel/Cooperative.hs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ module Control.Parallel.Cooperative (
) where

import Control.Monad (when)
import Control.Monad.Extra (whileM)
import Control.Monad.Extra (ifM, unlessM, whileM)
import Data.Bits (Bits, FiniteBits, (.&.), bit, countLeadingZeros, finiteBitSize, xor)
import Data.Foldable (toList)
import qualified Data.IntMap.Strict as IMS
Expand All @@ -67,13 +67,14 @@ import qualified Data.Vector as V
import GHC.Stack (HasCallStack)

import Control.Concurrent (getNumCapabilities, myThreadId, threadCapability)
import Control.Concurrent.Async (Async, waitAny, withAsync, withAsyncOn)
import Control.Concurrent.Async (Async, wait, withAsync, withAsyncOn)
import Control.Concurrent.STM.TVar (TVar, modifyTVar', newTVarIO, readTVar, readTVarIO,
stateTVar, writeTVar)
import Control.Monad.STM (STM, atomically, retry)
import System.IO.Unsafe (unsafePerformIO)

import Data.Time.Clock.System (SystemTime(MkSystemTime), getSystemTime)
import qualified Debug.TimeStats as TS
-- import Numeric (showFFloat)


Expand Down Expand Up @@ -137,23 +138,22 @@ parWithAsyncs wakeAllThreads numSleepingVar allPairs = do
_ -> go [] allPairs
where
numThreads = length allPairs
go asyncs [] = snd <$> waitAny (reverse asyncs)
go asyncs [] = mapM_ wait (reverse asyncs)
go asyncs ((waf, task) : ps) = waf threadF (\aa -> go (aa : asyncs) ps)
where
done = (== numThreads) <$> readTVarIO numSleepingVar
threadF = do
wake0 <- readTVarIO wakeAllThreads
q <- task
if q then threadF else do
ok <- atomically $ do
n <- readTVar numSleepingVar
writeTVar numSleepingVar $! n + 1
pure $ n /= numThreads - 1
when ok $ do
atomically $ do
inc1TVar numSleepingVar
ifM done (inc1TVar wakeAllThreads) $ do
TS.measureM "sleep" $ atomically $ do
wake1 <- readTVar wakeAllThreads
when (wake1 == wake0) retry
atomically $ modifyTVar' numSleepingVar (subtract 1)
threadF
unlessM done $ do
atomically $ modifyTVar' numSleepingVar (subtract 1)
threadF

parThreads :: TVar Int -> TVar Int -> [IO Bool] -> IO ()
{- ^ @parThreads wakeAllThreads numSleepingVar tasks@ runs the @tasks@ in separate threads. When
Expand Down
7 changes: 4 additions & 3 deletions src/Math/Algebra/Commutative/BinPoly.hs
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,10 @@ bp58Ops evCmp isGraded descVarSs useSugar = assert (nVars <= 58)
descVarPs = map (SL.singleton . evBit) varBitJsDesc

varBitJsAsc = reverse varBitJsDesc
extraSPairs v j h = [SPair (i - nVars) j (h + 1) v | i <- varBitJsAsc, testBit v.w64 i]
sPoly _f g (SPair i _j _h _m) | i < 0 = g `bpTimesEv` fromBits58 (bit (i + nVars))
sPoly f g (SPair _i _j _h m) = mult1 f `bpPlus` mult1 g
extraSPairs v j h = [SPair (i - nVars) j (h + 1) v (spCmp evCmp useSugar)
| i <- varBitJsAsc, testBit v.w64 i]
sPoly _f g (SPair { i }) | i < 0 = g `bpTimesEv` fromBits58 (bit (i + nVars))
sPoly f g (SPair { m }) = mult1 f `bpPlus` mult1 g
where
mult1 (v :! t) = bpTimesEv t (fromJust (evMaybeQuo m v))
mult1 _ = undefined
Expand Down
10 changes: 6 additions & 4 deletions src/Math/Algebra/Commutative/EPoly.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import qualified StrictList2 as SL

import Control.Parallel.Cooperative

import qualified Debug.TimeStats as TS


zipWithExact :: (a -> b -> c) -> [a] -> [b] -> [c]
-- ^ or use library @safe@
Expand Down Expand Up @@ -140,12 +142,12 @@ evDividesF nVars ev@(ExponVec d es) ev'@(ExponVec d' es') = expsDivs es es'
| d' < 256 || d >= 256 = U.ifoldr (\i e ~b -> bytesDivs e (a' U.! i) && b) True a
expsDivs (ExponsN a) (ExponsN a')
= U.ifoldr (\i e ~b -> e <= a' U.! i && b) True a
expsDivs _ _ =
expsDivs _ _ = TS.measurePure "evDividesF slow" $
and (zipWithExact (<=) (exponsL nVars ev) (exponsL nVars ev'))
perW = perWord64 nVars d
mask = if perW == 8 then 0x0101_0101_0101_0100 else 0x0001_0001_0001_0000
bytesDivs w w' = w <= w' && (w' - w) .&. mask `xor` w .&. mask `xor` w' .&. mask == 0
where -- check if any bytes subtracted in (w' - w) cause borrowing from any mask bits
perW = perWord64 nVars d
mask = if perW == 8 then 0x0101_0101_0101_0100 else 0x0001_0001_0001_0000
-- check if any bytes subtracted in (w' - w) cause borrowing from any mask bits
{-# INLINABLE evDividesF #-}

evLCMF :: Int -> Op2 ExponVec -- really Least Common Multiple of vars^ev1 and vars^ev2
Expand Down
13 changes: 10 additions & 3 deletions src/Math/Algebra/Commutative/GBDemo.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import Math.Algebra.Commutative.EPoly
-- import Math.Algebra.Commutative.VMPoly
import Math.Algebra.Commutative.Field.ZModPW

import Control.Monad (when)
import Data.List (find)
import GHC.TypeNats (KnownNat)

import System.Environment (setEnv)
-- import Test.Inspection (inspect, hasNoTypeClassesExcept)


Expand All @@ -31,7 +33,8 @@ data GBExOpts = GBExOpts {
useVMPoly :: Bool,
sec :: StdEvCmp,
noSugar :: Bool,
gbTrace :: Int
gbTrace :: Int,
showTimes :: Bool
}

epGbpA :: forall p. KnownNat p => StdEvCmp -> UseSugar -> [String] ->
Expand Down Expand Up @@ -91,6 +94,7 @@ showUsage = mapM_ putStrLn [
" --tr show the final result (generators)",
" --tq show characters with info about threads and queues (\"dprRsS\", \"rg\")",
" --ts show details relating to selection strategy",
" --tt show total times of some algorithm parts",
"",
"examples: simpleDemo buchberger87 gerdt93 katsura5 katsura6 katsura7 katsura8 katsura10",
" hCyclic4 cyclic4 hCyclic5 cyclic5 hCyclic6 cyclic6 hCyclic7 cyclic7 hCyclic8 cyclic8",
Expand All @@ -116,13 +120,14 @@ parseOpt s opts = case s of
"--tr" -> Right $ opts { gbTrace = opts.gbTrace .|. gbTResults }
"--tq" -> Right $ opts { gbTrace = opts.gbTrace .|. gbTQueues }
"--ts" -> Right $ opts { gbTrace = opts.gbTrace .|. gbTProgressDetails }
"--tt" -> Right $ opts { showTimes = True }
_ -> Left $ "Unknown option: " ++ s

parseArgs :: [String] -> Either String (GBExOpts, [GBEx])
parseArgs args = goOpts args opts0
where
opts0 = GBExOpts { showHelp = False, useVMPoly = False, sec = GrRevLexCmp,
noSugar = False, gbTrace = gbTSummary }
noSugar = False, gbTrace = gbTSummary, showTimes = False }
goOpts :: [String] -> GBExOpts -> Either String (GBExOpts, [GBEx])
goOpts ("--" : t) opts = (opts, ) <$> goNames t -- unnec. here
goOpts (h@('-':_) : t) opts = goOpts t =<< parseOpt h opts
Expand All @@ -144,7 +149,9 @@ usageErr s = do
gbDemo :: [String] -> IO ()
gbDemo args = either usageErr run (parseArgs args)
where
run (opts, exs) = if opts.showHelp then showUsage else mapM_ (gbDemo1 opts) exs
run (opts, exs) = if opts.showHelp then showUsage else do
when opts.showTimes $ setEnv "DEBUG_TIMESTATS_ENABLE" "1"
mapM_ (gbDemo1 opts) exs


-- See http://www.math.usm.edu/perry/Research/f5ex.lib as in
Expand Down
Loading

0 comments on commit 4c4d405

Please sign in to comment.