Skip to content

Commit

Permalink
include compile-time parameters in help
Browse files Browse the repository at this point in the history
small update to random generators
  • Loading branch information
jcosborn committed Dec 3, 2024
1 parent e5ed8b0 commit 01128e2
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 30 deletions.
57 changes: 41 additions & 16 deletions src/base/params.nim
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import macros, os, strUtils, tables, algorithm, seqUtils
# Note: may get imported before 'echo' is redefined
# uses of 'echo' should be in a template with 'mixin echo'

# TODO
# include CT in saveParams RT
# include CT in help
# check used
# only write from rank 0
# only read from rank 0
# add way to represent empty seq/string (versus unset/unknown)
# sort paramHelp output by file and option name

type
ParamListT = object # used to store set parameters from cmd line or file
Expand All @@ -28,7 +26,8 @@ var loadParamsCommand {.compileTime.} = "loadParams" # normally not changed
var params: Table[string,ParamListT] # store params during setup
# param info generated at compile time, as setParam commands are compiled
var paramInfoCT {.compileTime.} = newSeq[ParamObj](0)
var paramInfoCTRT = newSeq[ParamObj](0) # conversion of paramInfoCT to RT variable
#var paramInfoCTRT = newSeq[ParamObj](0) # conversion of paramInfoCT to RT variable
var paramInfoCTRT: seq[ParamObj] # conversion of paramInfoCT to RT variable
# param info generated at run time, as setParam commands executed (using set values)
var paramInfo = newSeq[ParamObj](0)

Expand Down Expand Up @@ -218,13 +217,16 @@ proc saveParams(x: seq[ParamObj], fn: string, loc: string, thisfile: string) =
proc addParam(s,r: string, c: string = "", ii: IiType) =
paramInfo.set(s, r, c, ii.filename, ii.line)

proc addComment(s,c:string):string =
let spcName = " "
let spcValue = " #"
let spcLoc = " ## "
proc addComment(s,c,spc:string):string =
result = s
if c.len>0:
let
spc = " ## "
m = min(s.len, spc.len-8)
result &= spc[m..^1] & c
proc addComment(s,c:string):string = addComment(s,c,spcValue)

proc echoParamsX*(warnUnknown=false):string =
result = "Params:\n"
Expand All @@ -243,6 +245,8 @@ template echoParams*(warnUnknown=false) =
echo echoParamsX(warnUnknown)

proc paramHelp*(p:string = ""):string =
#echo paramInfo
#echo paramInfoCTRT
result = "Usage:\n " & getAppFileName()
var t: ParamObj
var i = paramInfo.find p
Expand All @@ -256,10 +260,21 @@ proc paramHelp*(p:string = ""):string =
result &= addComment(" -" & p & ":" & t.value & " (current value)", t.comment)
else:
result &= " -OPTION:VALUE ...\nAvailable OPTIONs and current VALUEs:"
let spc = " "
for t in paramInfo:
template p(t) =
let nm = t.name
result &= "\n " & (nm & spc[min(spc.len-1,nm.len)..^1] & " : " & t.value).addComment(t.comment)
var s = nm & spcName[min(spcName.len-1,nm.len)..^1] & " : " & t.value
let f = t.file.splitfile[1]
let c = f & "," & $t.line
s = s.addComment(c,spcValue)
s = s.addComment(t.comment,spcLoc)
result &= "\n " & s
var paramnames = newSeq[string](0)
for t in paramInfo:
paramnames.add t.name
p(t)
for t in paramInfoCTRT:
if t.name notin paramnames: # FIXME: should check location too
p(t)

template cnvnone(x:typed):untyped = x
template makeTypeParam(name,typ,deflt,cnvrt: untyped): untyped {.dirty.} =
Expand Down Expand Up @@ -375,15 +390,15 @@ template processSaveParams*(index = -1) =
let ii = instantiationInfo(index, fullPaths=true)
saveParams(paramInfo, saveValue, ii.filename & ":" & $ii.line, ii.filename)

var makeParamInfoCTRT: proc()
template processMakeParamInfoCTRT* =
proc makeParamInfoCTRT() {.inject.}
makeParamInfoCTRT()
if not isNil makeParamInfoCTRT: makeParamInfoCTRT()

template installStandardParams*(idx= -3) =
installSaveParams(index=idx)
installLoadParams(index=idx)
installHelpParam(index=idx)
processMakeParamInfoCTRT()
#processMakeParamInfoCTRT()

# write param file at compile time
macro writeParamFileX*(filename: static string, ii: static IiType) =
Expand All @@ -405,10 +420,14 @@ macro makeParamInfoCTRTX*(): auto =
let l = newLit t.line
result.add quote do:
paramInfoCTRT.add ParamObj(name:`n`,value:`v`,comment:`c`,file:`f`,line:`l`)
template finalizeParams* =
proc makeParamInfoCTRTImpl(): bool =
makeParamInfoCTRTX()
true
#makeParamInfoCTRT = makeParamInfoCTRTImpl
let dum {.global.} = makeParamInfoCTRTImpl()
template writeParamFile*(filename: static string = "") =
writeParamFileX(filename, instantiationInfo(fullPaths=true))
proc makeParamInfoCTRT() {.inject.} =
makeParamInfoCTRTX()

template assertParam*(p:auto, f:auto) =
if not f p:
Expand Down Expand Up @@ -439,8 +458,10 @@ template CLIset*(p:typed, n:untyped, prefix = "") =
discard

when isMainModule:
import qex, paramtest
import qex
qexInit()
installHelpParam("h")
processHelpParam()

letParam:
bf = false
Expand All @@ -462,11 +483,15 @@ when isMainModule:
fa1 = @[1.0,1,1,1]
fax = if true: @[2.0,2,2,2] else: @[3.0,3,3,3]

installHelpParam("h")
echoParams()

defaultSetup()
proc paramTest* =
var i = intParam("i", 0, "Test intParam")
var f = floatParam("f", 0.0, "Test floatParam")
echo i, f
paramTest()

writeParamFile("")
qexFinalize()
finalizeParams()
7 changes: 7 additions & 0 deletions src/base/stdUtils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ proc `|`*(f: float, d: int): string =
template `|-`*(x:SomeNumber, y: int): auto =
x | -y

proc values*(e: typedesc[enum]): string =
result = ""
var sep = ""
for v in e:
result &= sep & $v
sep = " "

proc indexOf*[T](x: openArray[T], y: auto): int =
let n = x.len
while result<n and x[result]!=y: inc result
Expand Down
2 changes: 1 addition & 1 deletion src/base/threading.nim
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ template threadSum01A*[T](a: T) =
template threadSum01B*[T](a: T) =
## sum value with result on thread 0, barrier version
block:
tic("threadSum01")
tic("threadSum01B")
if threadNum!=0:
threadLocals.share[threadNum].p = a.addr
threadBarrier()
Expand Down
3 changes: 3 additions & 0 deletions src/bench/benchStagProp.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import os
import rng

qexInit()
installHelpParam()
processHelpParam()
#var defaultLat = [4,4,4,4]
#var defaultLat = [8,8,8,8]
var defaultLat = @[8,8,8,8]
Expand Down Expand Up @@ -81,3 +83,4 @@ s3.solve(v2, v1, mass, sp)
echoProf()

qexFinalize()
finalizeParams()
13 changes: 10 additions & 3 deletions src/layout/layoutX.nim
Original file line number Diff line number Diff line change
Expand Up @@ -242,14 +242,21 @@ proc coord*(l: Layout, coord: ptr cint, ri: tuple[rank,index:cint]) =
#layoutCoordQ(l.lq.addr, cast[ptr cArray[cint]](coord), li.addr)
layoutCoordQ(l.lq.addr, toOpenArray(ca,0,l.nDim-1), li.addr)

# from physical index to local rank and index
proc rankIndex*(lo: Layout, lex: int): tuple[rank,index:int] =
let n = lo.nDim
var c = newSeq[cint](n)
var k = lex
for i in 0..<n:
c[i] = (k mod lo.physGeom[i]).cint
lo.temp[i] = (k mod lo.physGeom[i]).cint
k = k div lo.physGeom[i]
rankIndex(lo, c)
rankIndex(lo, lo.temp)

proc physIndex*(lo: Layout, rank,index: int): int =
lo.coord(lo.temp, rank, index)
let n = lo.nDim
result = 0
for i in countdown(n-1,0):
result = (result*lo.physGeom[i]) + lo.temp[i]

proc rankFromRankCoords*(l: Layout, coords: ptr cArray): int =
for i in 0..<l.nDim:
Expand Down
2 changes: 1 addition & 1 deletion src/rng/distribution.nim
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func generate*[T:RandomGenerator](d: UniformRealOpenOpen, g: var T): float =
########################## normal #####################################

type
NormalAlgorithm = enum
NormalAlgorithm* = enum
naBoxMuller, naPolar, naCDF
NormalDistribution* = object
mean: float
Expand Down
6 changes: 6 additions & 0 deletions src/rng/generator.nim
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import field

type
RandomGeneratorBase* {.inheritable,pure.} = object
count: int
Expand Down Expand Up @@ -50,6 +52,10 @@ func skip*(e: var Milc6Generator, c = 1) =
e.incCount(c)
e.state.skip(c)

func seed*(x: Field[1, Milc6Generator]; sed: auto) =
for e in x:
x[e].seed(sed)

########################## MRG32k3a #############################
import mrg32k3a

Expand Down
18 changes: 12 additions & 6 deletions src/rng/milcrng.nim
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,11 @@ const
SCALE = 1.0'f32 / 0x01000000.float32
#SCALE1 = 1.0'f32 / MASK.float32

#template maxInt*(x: RngMilc6): int = int(MASK)
template maxInt*(x: RngMilc6): int = int(MASK)
template maxInt*(x: typedesc[RngMilc6]): int = int(MASK)
#template numInts*(x: RngMilc6): int = int(NUMINTS)
template high*(x: RngMilc6): uint = uint(MASK)
template high*(x: typedesc[RngMilc6]): uint = uint(MASK)
template numInts*(x: RngMilc6): int = int(NUMINTS)
template numInts*(x: typedesc[RngMilc6]): int = int(NUMINTS)

when defined(FUELCompat):
Expand Down Expand Up @@ -115,7 +117,7 @@ proc seed*(prn: var RngMilc6; sed,index: auto) =
QMP_broadcast(ss.addr, sizeof(ss).csize_t)
seedIndep(prn, ss, index)

proc next(prn: var RngMilc6): uint32 {.inline.} =
proc nextI(prn: var RngMilc6): uint32 {.inline.} =
## internal routine to return next value
let t = (((prn.r5 shr 7) or (prn.r6 shl 17)) xor
((prn.r4 shr 1) or (prn.r5 shl 23))) and MASK
Expand All @@ -132,16 +134,20 @@ proc next(prn: var RngMilc6): uint32 {.inline.} =

func skip*(prn: var RngMilc6, c = 1) =
for i in 1..c:
discard prn.next
discard prn.nextI

proc integer*(prn: var RngMilc6): int =
## Return random integer from 0 to maxInt
result = int prn.next
result = int prn.nextI

proc next*(prn: var RngMilc6): uint =
## Return random integer from 0 to maxInt
result = uint prn.nextI

proc uniform*(prn: var RngMilc6): float32 =
## Return random number uniform on [0,1)
## The choice of including endpoints may vary among different RNGs
let i = prn.next
let i = prn.nextI
result = SCALE * float32(i)

#var QLA_use_milc_gaussian* = false
Expand Down
12 changes: 9 additions & 3 deletions src/rng/mrg32k3a.nim
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ when a2sq[76]!=[[1511326704u32, 3759209742u32, 1610795712u32], [4292754251u32, 1

#template maxInt*(x: MRG32k3a): int = int 4294967086
template maxInt*(x: typedesc[MRG32k3a]): int = int 4294967086
template high*(x: typedesc[MRG32k3a]): uint = uint 4294967086
template high*(x: MRG32k3a): uint = uint 4294967086
#template numInts*(x: MRG32k3a): int = 4294967087
template numInts*(x: typedesc[MRG32k3a]): int = int 4294967087

Expand Down Expand Up @@ -152,7 +154,7 @@ proc next0(prn: var MRG32k3a): float {.inline.} =
result = p1 - p2
]#

proc next(prn: var MRG32k3a): int {.inline.} =
proc nextI(prn: var MRG32k3a): int {.inline.} =
## Return random integer uniform on [1,m1]
const
a12i = int a12
Expand Down Expand Up @@ -185,7 +187,11 @@ proc next(prn: var MRG32k3a): int {.inline.} =

proc integer*(prn: var MRG32k3a): int =
## Return random integer from 0 to maxInt
result = int(prn.next) - 1
result = int(prn.nextI) - 1

proc next*(prn: var MRG32k3a): uint =
## Return random integer from 0 to maxInt
result = uint(prn.nextI - 1)

#[
proc uniform*(prn:var MRG32k3a): float =
Expand Down Expand Up @@ -214,7 +220,7 @@ proc uniform*(prn:var MRG32k3a): float =
]#
proc uniform*(prn: var MRG32k3a): float =
## Return random number uniform on (0,1)
result = norm * prn.next.float
result = norm * prn.nextI.float

proc gaussian*(prn: var MRG32k3a): float =
## Gaussian normal deviate
Expand Down

0 comments on commit 01128e2

Please sign in to comment.