Skip to content

Commit cb59d61

Browse files
committed
Prepare file structure for stats.
refs #6.
1 parent 7a15e03 commit cb59d61

File tree

3 files changed

+74
-39
lines changed

3 files changed

+74
-39
lines changed

src/AI/AlphaBeta/Cache.hs

+4-11
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ lookupAiCache params board depth side handle = do
202202
AICache _ _ cache <- liftIO $ atomically $ readTVar (aichData handle)
203203
case lookupBoardMap (bc,bk) cache of
204204
Nothing -> return Nothing
205-
Just byDepth -> do
205+
Just (PerBoardData {boardScores = byDepth}) -> do
206206
let ds = [dpTarget depth .. dpTarget depth + aiUseCacheMaxDepthPlus cfg] ++
207207
[dpTarget depth - aiUseCacheMaxDepthMinus cfg .. dpTarget depth-1]
208208
depths = [depth {dpTarget = d} | d <- ds]
@@ -230,20 +230,13 @@ putAiCache' params board depth side sideItem handle = do
230230
store <- asks (aiStoreCache . gcAiConfig . csConfig)
231231
liftIO $ atomically $ do
232232
aic <- readTVar (aichData handle)
233-
let updateItem item1 item2 =
234-
case side of
235-
First -> item1 {ciFirst = ciFirst item2 `mplus` ciFirst item1}
236-
Second -> item2 {ciSecond = ciSecond item2 `mplus` ciSecond item1}
237-
238-
updateDepthMap m1 m2 = M.unionWith updateItem m1 m2
239-
240-
item = case side of
233+
let item = case side of
241234
First -> CacheItem {ciFirst = Just sideItem, ciSecond = Nothing}
242235
Second -> CacheItem {ciFirst = Nothing, ciSecond = Just sideItem}
243236

244-
init = M.singleton depth item
237+
init = PerBoardData (M.singleton depth item) Nothing
245238

246-
newAicData = putBoardMapWith updateDepthMap (bc,bk) init (aicData aic)
239+
newAicData = putBoardMapWith (<>) (bc,bk) init (aicData aic)
247240
aic' = aic {aicDirty = True, aicData = newAicData}
248241

249242
Just perBoard = lookupBoardMap (bc,bk) newAicData

src/AI/AlphaBeta/Persistent.hs

+21-25
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ instance Store IndexRecord where
274274
dataBlock <- peek
275275
return $ IndexRecord idxBlock dataBlock
276276

277-
lookupFileB :: B.ByteString -> Storage (Maybe (M.Map DepthParams CacheItem))
277+
lookupFileB :: B.ByteString -> Storage (Maybe PerBoardData)
278278
lookupFileB bstr = do
279279
st <- get
280280
case ssData st of
@@ -311,14 +311,19 @@ lookupFile board depth side = Metrics.timed "cache.lookup.file" $ do
311311
mbItem <- lookupFileB (encodeBoard board)
312312
case mbItem of
313313
Nothing -> return Nothing
314-
Just item -> case M.lookup depth item of
314+
Just item -> case M.lookup depth (boardScores item) of
315315
Nothing -> return Nothing
316316
Just ci -> case side of
317317
First -> return $ ciFirst ci
318318
Second -> return $ ciSecond ci
319319

320-
putRecordFileB :: B.ByteString -> DepthParams -> Side -> StorageValue -> Storage ()
321-
putRecordFileB bstr depth side value = do
320+
lookupStatsFile :: Board -> Storage (Maybe Stats)
321+
lookupStatsFile board = Metrics.timed "stats.lookup.file" $ do
322+
mbItem <- lookupFileB (encodeBoard board)
323+
return $ join $ boardStats `fmap` mbItem
324+
325+
putRecordFileB :: B.ByteString -> PerBoardData -> Storage ()
326+
putRecordFileB bstr newData = do
322327
st <- get
323328
case ssData st of
324329
Nothing -> return ()
@@ -338,17 +343,16 @@ putRecordFileB bstr depth side value = do
338343
seek IndexFile idxOffset
339344
writeData IndexFile record'
340345
seek DataFile $ calcDataBlockOffset newDataBlock
341-
let newData = M.singleton depth newItem
342346
writeDataSized DataFile newData
343347
return ()
344348
else do
345-
let dataOffset = calcDataBlockOffset dataBlockNumber
346-
seek DataFile dataOffset
347-
oldData <- readDataSized DataFile
348-
let newData = updateData oldData
349-
seek DataFile dataOffset
350-
writeDataSized DataFile newData
351-
return ()
349+
let dataOffset = calcDataBlockOffset dataBlockNumber
350+
seek DataFile dataOffset
351+
oldData <- readDataSized DataFile
352+
let newData' = oldData <> newData
353+
seek DataFile dataOffset
354+
writeDataSized DataFile newData'
355+
return ()
352356
| otherwise = do
353357
bsize <- gets ssBoardSize
354358
let idxOffset = calcIndexOffset bsize blockNumber (B.head bstr)
@@ -389,22 +393,14 @@ putRecordFileB bstr depth side value = do
389393
writeBytes DataFile empty
390394
return newBlockNumber
391395

392-
updateData oldMap =
393-
M.insertWith updateItem depth newItem oldMap
394-
395-
newItem = case side of
396-
First -> CacheItem {ciFirst = Just value, ciSecond = Nothing}
397-
Second -> CacheItem {ciFirst = Nothing, ciSecond = Just value}
398-
399-
updateItem item1 item2 =
400-
case side of
401-
First -> item1 {ciFirst = ciFirst item2 `mplus` ciFirst item1}
402-
Second -> item2 {ciSecond = ciSecond item2 `mplus` ciSecond item1}
403-
404396
putRecordFile :: Board -> DepthParams -> Side -> StorageValue -> Storage ()
405397
putRecordFile board depth side value = Metrics.timed "cache.put.file" $ do
406398
let bstr = encodeBoard board
407-
putRecordFileB bstr depth side value
399+
newData = PerBoardData (M.singleton depth item) Nothing
400+
item = case side of
401+
First -> CacheItem {ciFirst = Just value, ciSecond = Nothing}
402+
Second -> CacheItem {ciFirst = Nothing, ciSecond = Just value}
403+
putRecordFileB bstr newData
408404

409405
initFile :: Storage ()
410406
initFile = do

src/AI/AlphaBeta/Types.hs

+49-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import qualified Control.Concurrent.ReadWriteLock as RWL
1515
import Control.Concurrent.STM
1616
import qualified Data.Map as M
1717
import qualified Data.HashPSQ as PQ
18+
import Data.Int
1819
import Data.Word
1920
import Data.Binary
2021
import Data.Store
@@ -58,14 +59,35 @@ data DepthParams = DepthParams {
5859
deriving (Eq, Ord, Show, Typeable, Generic)
5960

6061
instance Store DepthParams
62+
instance Binary DepthParams
63+
64+
data Stats = Stats {
65+
statsCount :: Int16
66+
, statsMaxScore :: Score
67+
, statsMinScore :: Score
68+
, statsSumScore :: Score
69+
}
70+
deriving (Eq, Show, Generic, Typeable)
71+
72+
instance Binary Stats
73+
instance Store Stats
74+
75+
instance Semigroup Stats where
76+
s1 <> s2 =
77+
Stats (statsCount s1 + statsCount s2)
78+
(max (statsMaxScore s1) (statsMaxScore s2))
79+
(min (statsMinScore s1) (statsMinScore s2))
80+
(statsSumScore s1 + statsSumScore s2)
81+
82+
instance Monoid Stats where
83+
mempty = Stats 0 0 0 0
6184

6285
data CacheItemSide = CacheItemSide {
6386
cisScore :: ! Score
6487
}
6588
deriving (Eq, Show, Generic, Typeable)
6689

6790
instance Binary CacheItemSide
68-
6991
instance Store CacheItemSide
7092

7193
data CacheItem = CacheItem {
@@ -75,10 +97,34 @@ data CacheItem = CacheItem {
7597
deriving (Generic, Typeable, Show)
7698

7799
instance Binary CacheItem
78-
79100
instance Store CacheItem
80101

81-
type PerBoardData = M.Map DepthParams CacheItem
102+
instance Semigroup CacheItem where
103+
item1 <> item2 = CacheItem {
104+
ciFirst = ciFirst item2 `mplus` ciFirst item1,
105+
ciSecond = ciSecond item2 `mplus` ciSecond item1
106+
}
107+
108+
instance Monoid CacheItem where
109+
mempty = CacheItem Nothing Nothing
110+
111+
data PerBoardData = PerBoardData {
112+
boardScores :: M.Map DepthParams CacheItem
113+
, boardStats :: Maybe Stats
114+
}
115+
deriving (Generic, Typeable, Show)
116+
117+
instance Semigroup PerBoardData where
118+
d1 <> d2 = PerBoardData {
119+
boardScores = M.unionWith (<>) (boardScores d1) (boardScores d2),
120+
boardStats = liftM2 (<>) (boardStats d1) (boardStats d2)
121+
}
122+
123+
instance Monoid PerBoardData where
124+
mempty = PerBoardData M.empty Nothing
125+
126+
instance Binary PerBoardData
127+
instance Store PerBoardData
82128

83129
type AIData = BoardMap PerBoardData
84130

0 commit comments

Comments
 (0)