Recording fusible O (1) updates for vector

This is a continuation of the question . Since the vector library does not seem to have O (1) smooth output capability, I wonder if it is possible to write O (1) smooth output function that does not include unsafeFreeze and unsafeThaw . It would use the vector stream view, I think - I am not familiar with how to write one using stream and unstream - hence this question. The reason is that this will give us the opportunity to write a caching-friendly update function on a vector where only a narrow region of the vector changes, and therefore we donโ€™t want to go through the entire vector just to process this narrow region (and this operation can happen billions of times in each function call, so the motivation to keep overhead very low). Conversion functions such as map process the entire vector, so they will be too slow.

I have an example of what I want to do, but the upd function below uses unsafeThaw and unsafeFreeze - it does not seem to be optimized in the kernel, and also breaks the promise without using the buffer further:

 module Main where import Data.Vector.Unboxed as U import Data.Vector.Unboxed.Mutable as MU import Control.Monad.ST upd :: Vector Int -> Int -> Int -> Vector Int upd vix = runST $ do v' <- U.unsafeThaw v MU.write v' ix U.unsafeFreeze v' sum :: Vector Int -> Int sum = U.sum . (\x -> upd x 0 73) . (\x -> upd x 1 61) main = print $ Main.sum $ U.fromList [1..3] 

I know how to implement imperative algorithms using STVector . In case you are wondering why this alternative approach, I want to try this approach of using pure vectors to check how the GHC conversion of a particular algorithm differs when writing using fusible pure vector flows (with monadic operations under the hood, of course),

When an algorithm is written using STVector , it does not look as iterative as I would like. (I think itโ€™s more difficult for the GHC optimizer to define loops when there is a lot of variability.) So, I am exploring this alternative approach to see that I can get a more enjoyable cycle.

+4
source share
2 answers

The upd function that you wrote does not look correct, not to mention melting. Fusion is library level optimization, and you need to write your code from specific primitives. In this case, you want not just merging, but recycling , which can be easily done using bulk update operations such as // and update . These operations will merge and even occur on site most of the time.

If you really want to write your own destructive code based on updates, DO NOT use unsafeThaw - use modify

+4
source

Any function is a smooth update function; you seem to be trying to get away from the programming model the vector package is trying to make you use

 module Main where import Data.Vector.Unboxed as U change :: Int -> Int -> Int change 0 n = 73 change 1 n = 61 change mn = n myfun2 = U.sum . U.imap change . U.enumFromStepN 1 1 main = print $ myfun2 30000000 

- this does not create any vectors, much less โ€œupdateโ€ them, as you will see, study the core.

+3
source

Source: https://habr.com/ru/post/1485183/


All Articles