Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8009355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:25:32+00:00 2026-06-04T18:25:32+00:00

To be more specific, I have the following innocuous-looking little Repa 3 program: {-#

  • 0

To be more specific, I have the following innocuous-looking little Repa 3 program:

{-# LANGUAGE QuasiQuotes #-}

import Prelude hiding (map, zipWith)
import System.Environment (getArgs)
import Data.Word (Word8)
import Data.Array.Repa
import Data.Array.Repa.IO.DevIL
import Data.Array.Repa.Stencil
import Data.Array.Repa.Stencil.Dim2

main = do
  [s] <- getArgs
  img <- runIL $ readImage s

  let out = output x where RGB x = img
  runIL . writeImage "out.bmp" . Grey =<< computeP out

output img = map cast . blur . blur $ blur grey
  where
    grey              = traverse img to2D luminance
    cast n            = floor n :: Word8
    to2D (Z:.i:.j:._) = Z:.i:.j

---------------------------------------------------------------

luminance f (Z:.i:.j)   = 0.21*r + 0.71*g + 0.07*b :: Float
  where
    (r,g,b) = rgb (fromIntegral . f) i j

blur = map (/ 9) . convolve kernel
  where
    kernel = [stencil2| 1 1 1
                        1 1 1
                        1 1 1 |]

convolve = mapStencil2 BoundClamp

rgb f i j = (r,g,b)
  where
    r = f $ Z:.i:.j:.0
    g = f $ Z:.i:.j:.1
    b = f $ Z:.i:.j:.2

Which takes this much time to process a 640×420 image on my 2Ghz core 2 duo laptop:

real    2m32.572s
user    4m57.324s
sys     0m1.870s

I know something must be quite wrong, because I have gotten much better performance on much more complex algorithms using Repa 2. Under that API, the big improvement I found came from adding a call to ‘force’ before every array transform (which I understand to mean every call to map, convolve, traverse etc). I cannot quite make out the analogous thing to do in Repa 3 – in fact I thought the new manifestation type parameters are supposed to ensure there is no ambiguity about when an array needs to be forced? And how does the new monadic interface fit into this scheme? I have read the nice tutorial by Don S, but there are some key gaps between the Repa 2 and 3 APIs that are little discussed online AFAIK.

More simply, is there a minimally impactful way to fix the above program’s efficiency?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T18:25:34+00:00Added an answer on June 4, 2026 at 6:25 pm

    The new representation type parameters don’t automagically force when needed (it’s probably a hard problem to do that well) – you still have to force manually. In Repa 3 this is done with the computeP function:

    computeP
      :: (Monad m, Repr r2 e, Fill r1 r2 sh e)
      => Array r1 sh e -> m (Array r2 sh e)
    

    I personally really don’t understand why it’s monadic, because you can just as well use Monad Identity:

    import Control.Monad.Identity (runIdentity)
    force
      :: (Repr r2 e, Fill r1 r2 sh e)
      => Array r1 sh e -> Array r2 sh e
    force = runIdentity . computeP
    

    So, now your output function can be rewritten with appropriate forcing:

    output img = map cast . f . blur . f . blur . f . blur . f $ grey
      where ...
    

    with an abbreviation f using a helper function u to aid type inference:

    u :: Array U sh e -> Array U sh e
    u = id
    f = u . force
    

    With these changes, the speedup is quite dramatic – which is to be expected, as without intermediate forcing each output pixel ends up evaluating much more than is necessary (the intermediate values aren’t shared).

    Your original code:

    real    0m25.339s
    user    1m35.354s
    sys     0m1.760s
    

    With forcing:

    real    0m0.130s
    user    0m0.320s
    sys     0m0.028s
    

    Tested with a 600×400 png, the output files were identical.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically I have a Base class called Program. I then have more specific program
I do have something more specific in mind, however: Each web service method needs
I have more than one Event Listener that calls a specific function, and I
To be more specific, let's say we have a collection sorted by properties P1
I have a very specific deserialization need, see example below: say I have following
I have following 2 activities(plus many more not important for this question): <activity android:name=.activities.HomeActivity
I am looking for a more specific version of the :sort u command that
To be more specific I do not know what the exact values will be.
Exists a way to call .net assemblies more specific .dll files in java? I
How to find out the more specific error situations like whether the error is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.