transpose :: [[a]] -> [[a]]
transpose [] = []
transpose ([] : xss) = transpose xss
transpose ((x : xs) : xss) = combine x hds xs tls
where
-- We tie the calculations of heads and tails together
-- to prevent heads from leaking into tails and vice versa.
-- unzip makes the selector thunk arrangements we need to
-- ensure everything gets cleaned up properly.
(hds, tls) = unzip [(hd, tl) | hd : tl <- xss]
prefetchTails :: IO ()
prefetchTails = mapM_ (\tl -> prefetchElem3 tl 0) tls
combine y h ys t = (y:h) : transpose (ys:t)
{-# NOINLINE combine #-}
But I am not sure ho to invoke prefetchTails in the actual main.
Jaror thank you for the input. What I am trying to experiment is that if this addition of prefetch reduces the runtime and L2 cache misses in Haskell. This is just an experimental test.
nofib-prefetch.exe: Prelude.!!: index too large
CallStack (from HasCallStack):
error, called at libraries\base\GHC\List.hs:1368:14 in base:GHC.List
tooLarge, called at libraries\base\GHC\List.hs:1378:50 in base:GHC.List
The reason Iâm asking what youâre planning is because it seems youâre prefetching the elements of the double nested linked list, while those are never accessed by the algorithm. So youâre only adding extra work without any benefit.
To understand where prefetching is useful you need to have a firm grasp of the code that GHC generates. Thatâs definitely something I consider out of reach for beginners. I think there are only very few people who can really reliably reason about things like this (sadly).
^ That error message means what it says. Youâre giving it a linked list as input that is shorter than the index that youâre trying to prefetch. In this case youâre prefetching the element with index 0 (the first element of the list), so that must mean youâre giving it an empty list as input.
I have few benchmarks that uses few of the list functions and transpose is one of them. I want to try to use prefetch in transpose and see if it helps in optimising. I would love to take my time in learning the depth and the concept and then do it, unfortunately this is my dissertation and I should complete it by the end of march and hence seeking help. The resources available on the internet is pretty much very less and hence posting questions. I am very grateful to you for helping me out as I am able to understand bits and pieces and most of the things makes sense.
Jaror! Would it be too much to ask if you can provide input on any one of the following function on how to incorporate prefetch so that I can test out my theory.
I donât mind helping you by explaining a few error message here and there, but I think it would also take me days (if not more) to figure out how properly to apply prefetching here. I donât know that much about prefetching. So, I donât think I can help you much further.
No worries Jaror. Thank you so much for helping this far. I really appreciate it⌠I will try understanding and doing some and if I face some issue I will post it in the thread and if it strikes of anything you can help out.