<commit_before>{-# LANGUAGE DeriveDataTypeable, TypeFamilies, TypeOperators, 
             FlexibleContexts #-}

module Main where

import Control.Monad

import Flow
import Flow.Kernel ( IsKernelDef )

import Kernel.Binning
import Kernel.Data
import Kernel.FFT
import Kernel.Gridder
import Kernel.IO
import Kernel.Scheduling

-- ----------------------------------------------------------------------------
-- ---                             Functional                               ---
-- ----------------------------------------------------------------------------

-- Abstract kernel signatures.
createGrid :: Flow UVGrid
createGrid = flow "create grid"
grid :: Flow Vis -> Flow GCFs -> Flow UVGrid -> Flow UVGrid
grid = flow "grid"
idft :: Flow UVGrid -> Flow Image
idft = flow "idft"
gcf :: Flow Vis -> Flow GCFs
gcf = flow "gcf"

-- | Compound gridder actor
gridder :: Flow Vis -> Flow GCFs -> Flow Image
gridder vis gcfs = idft (grid vis gcfs createGrid)

-- ----------------------------------------------------------------------------
-- ---                               Strategy                               ---
-- ----------------------------------------------------------------------------

gridderStrat :: Config -> Strategy ()
gridderStrat cfg = do

  -- Make point domain for visibilities
  (ddom, ixs, _) <- makeOskarDomain cfg 1
  let dkern :: IsKernelDef kf => kf -> kf
      dkern = regionKernel ddom
  tdom <- makeRangeDomain 0 (cfgPoints cfg)

  -- Create data flow for tag, bind it to FFT plans
  let gpar = cfgGrid cfg
      gcfpar = cfgGCF cfg

  -- Data flows we want to calculate
  vis <- uniq $ flow "vis" ixs
  let gcfs = gcf vis
      gridded = grid vis gcfs createGrid
      result = gridder vis gcfs

  -- Create ranged domains for grid coordinates
  udoms <- makeRangeDomain 0 (gridWidth gpar)
  vdoms <- makeRangeDomain 0 (gridHeight gpar)
  let uvdoms = (udoms, vdoms)

  -- Split and distribute over coordinate domain
  vdom <- split vdoms (gridTiles gpar)
  udom <- split udoms (gridTiles gpar)
  let uvdom = (udom, vdom)
  distribute vdom ParSchedule $ distribute udom ParSchedule $ do

    -- Read visibilities
    bind vis $ oskarReader ddom tdom (cfgInput cfg) 0 0 ixs

    -- Create w-binned domain, split
    wdoms <- makeBinDomain $ binSizer gpar tdom uvdom vis
    wdom <- split wdoms (gridBins gpar)

    -- Load GCFs
    distribute wdom SeqSchedule $
      bind gcfs (gcfKernel gcfpar wdom)

    -- Bin visibilities (could distribute, but there's no benefit)
    rebind vis (binner gpar tdom uvdom wdom)

    -- Bind kernel rules
    bindRule createGrid (gridInit gcfpar uvdom)
    bindRule grid (gridKernel gpar gcfpar uvdoms wdom uvdom)

    -- Run gridding distributed
    calculate gridded

  -- Compute the result by detiling & iFFT on result tiles
  bind createGrid (gridInitDetile uvdoms)
  bind gridded (gridDetiling gcfpar uvdom uvdoms gridded createGrid)
  bindRule idft (ifftKern gpar uvdoms)
  calculate result

  -- Write out
  void $ bindNew $ imageWriter gpar (cfgOutput cfg) result

main :: IO ()
main = do

  let gpar = GridPar { gridWidth = 2048
                     , gridHeight = 2048
                     , gridPitch = 2048
                     , gridTheta = 0.10
                     , gridFacets = 1
                     , gridTiles = 2
                     , gridBins = 10
                     }
      gcfpar = GCFPar { gcfSize = 16
                      , gcfOver = 8
                      , gcfFile = "gcf16.dat"
                      }
      config = Config<commit_msg>Missing regionKernel changes for "gridding"<commit_after>{-# LANGUAGE DeriveDataTypeable, TypeFamilies, TypeOperators, 
             FlexibleContexts #-}

module Main where

import Control.Monad

import Flow
import Flow.Kernel ( IsKernelDef )

import Kernel.Binning
import Kernel.Data
import Kernel.FFT
import Kernel.Gridder
import Kernel.IO
import Kernel.Scheduling

-- ----------------------------------------------------------------------------
-- ---                             Functional                               ---
-- ----------------------------------------------------------------------------

-- Abstract kernel signatures.
createGrid :: Flow UVGrid
createGrid = flow "create grid"
grid :: Flow Vis -> Flow GCFs -> Flow UVGrid -> Flow UVGrid
grid = flow "grid"
idft :: Flow UVGrid -> Flow Image
idft = flow "idft"
gcf :: Flow Vis -> Flow GCFs
gcf = flow "gcf"

-- | Compound gridder actor
gridder :: Flow Vis -> Flow GCFs -> Flow Image
gridder vis gcfs = idft (grid vis gcfs createGrid)

-- ----------------------------------------------------------------------------
-- ---                               Strategy                               ---
-- ----------------------------------------------------------------------------

gridderStrat :: Config -> Strategy ()
gridderStrat cfg = do

  -- Make point domain for visibilities
  (ddom, ixs, _) <- makeOskarDomain cfg 1
  let dkern :: IsKernelDef kf => kf -> kf
      dkern = regionKernel ddom
  tdom <- makeRangeDomain 0 (cfgPoints cfg)

  -- Create data flow for tag, bind it to FFT plans
  let gpar = cfgGrid cfg
      gcfpar = cfgGCF cfg

  -- Data flows we want to calculate
  vis <- uniq $ flow "vis" ixs
  let gcfs = gcf vis
      gridded = grid vis gcfs createGrid
      result = gridder vis gcfs

  -- Create ranged domains for grid coordinates
  udoms <- makeRangeDomain 0 (gridWidth gpar)
  vdoms <- makeRangeDomain 0 (gridHeight gpar)
  let uvdoms = (udoms, vdoms)

  -- Split and distribute over coordinate domain
  vdom <- split vdoms (gridTiles gpar)
  udom <- split udoms (gridTiles gpar)
  let uvdom = (udom, vdom)
  distribute vdom ParSchedule $ distribute udom ParSchedule $ do

    -- Read visibilities
    bind vis $ oskarReader ddom tdom (cfgInput cfg) 0 0 ixs

    -- Create w-binned domain, split
    wdoms <- makeBinDomain $ dkern $ binSizer gpar tdom uvdom vis
    wdom <- split wdoms (gridBins gpar)

    -- Load GCFs
    distribute wdom SeqSchedule $
      bind gcfs (dkern $ gcfKernel gcfpar wdom)

    -- Bin visibilities (could distribute, but there's no benefit)
    rebind vis (dkern $ binner gpar tdom uvdom wdom)

    -- Bind kernel rules
    bindRule createGrid (dkern $ gridInit gcfpar uvdom)
    bindRule grid (dkern $ gridKernel gpar gcfpar uvdoms wdom uvdom)

    -- Run gridding distributed
    calculate gridded

  -- Compute the result by detiling & iFFT on result tiles
  bind createGrid (dkern $ gridInitDetile uvdoms)
  bind gridded (dkern $ gridDetiling gcfpar uvdom uvdoms gridded createGrid)
  bindRule idft (dkern $ ifftKern gpar uvdoms)
  calculate result

  -- Write out
  void $ bindNew $ dkern $ imageWriter gpar (cfgOutput cfg) result

main :: IO ()
main = do

  let gpar = GridPar { gridWidth = 2048
                     , gridHeight = 2048
                     , gridPitch = 2048
                     , gridTheta = 0.10
                     , gridFacets = 1
                     , gridTiles = 2
                     , gridBins = 10
                     }
      gcfpar = GCFPar { gcfSize = 16
                      , gcfOver = 8
                      , gcfFile = "gcf16.dat"
                      }
      config = Config
{'fraction_non_alphanumeric': 0.15516281658780964, 'fraction_numerical': 0.008349568605622042, 'mean_word_length': 1.6087114337568058, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5796887', 'n_tokens_mistral': 2158, 'n_tokens_neox': 1991, 'n_words': 704}