Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/devel' into mcmc
Browse files Browse the repository at this point in the history
  • Loading branch information
ctpeterson committed Dec 7, 2024
2 parents 2728ad9 + ea83f2e commit 05ec081
Show file tree
Hide file tree
Showing 71 changed files with 4,442 additions and 1,739 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ name: test
on:
pull_request:
push:
branches:
- 'devel'
- 'master'
# branches:
# - 'devel'
# - 'master'

jobs:
build:
strategy:
matrix:
mpi-impl: [openmpi, mpich]
nim-branch: [version-1-6, version-2-0, devel]
nim-branch: [version-2-0, version-2-2, devel]
fuel-compat: [0, 1]
fail-fast: false
name: nim-${{ matrix.nim-branch }}-${{ matrix.mpi-impl }}-FUELCompat:${{ matrix.fuel-compat }}
Expand Down
3 changes: 2 additions & 1 deletion configure
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ if [ "X$NIM" = "X" ]; then
fi

echo "Using Nim compiler: $NIM"
echo "Runing: $NIM $dir/build/configure.nims ${@@Q}"
echo "Runing: $NIM $dir/build/configure.nims $@"
#echo "Runing: $NIM $dir/build/configure.nims ${@@Q}"
$NIM $dir/build/configure.nims "$@"
3 changes: 2 additions & 1 deletion src/base/backtrace.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ proc backtrace_symbols(buffer: ptr pointer, size: cint): ptr UncheckedArray[cstr

proc print_trace =
#void *array[10];
const nmax = 10
const nmax = 20
var arr: array[nmax, pointer]
#char **strings;

Expand All @@ -22,6 +22,7 @@ proc print_trace =

proc sigtrace(sig: cint) {.noconv.} =
print_trace()
quit()

proc setTrace* =
c_signal(SIGSEGV, sigtrace)
Expand Down
1 change: 1 addition & 0 deletions src/base/basicOps.nim
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ template setUnopT*(op,fun,t1,t2: untyped) {.dirty.} =
rSetUnopT

template setBinopP*(op,fun,t1,t2,t3: untyped) {.dirty.} =
getOptimPragmas()
#template op*(x: typedesc[t1]; y: typedesc[t2]): typedesc = t3
proc op*(x: t1; y: t2): auto {.alwaysInline,noInit.} =
var r{.noInit.}: t3
Expand Down
10 changes: 10 additions & 0 deletions src/base/globals.nim
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,13 @@ macro setDefaultNc*(n: static[int]): untyped =
result = newEmptyNode()
macro getDefaultNc*(): untyped =
return newLit(defaultNc)

template getDefPrecStr:string =
const defPrec {.strdefine.} = "D"
defPrec

var defPrec* {.compiletime.} = getDefPrecStr()
macro setDefaultSingle* =
defPrec = "S"
echo "Default precision: ", defPrec
static: echo "Default precision: ", defPrec
11 changes: 11 additions & 0 deletions src/base/omp.nim
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ else:
#forceOmpOn()
#{. emit:["#pragma omp ", p] .}
{. emit:["_Pragma(\"omp ", p, "\")"] .}
template ompPragma(p:string,body:typed) =
{. push stackTrace:off, lineTrace:off, line_dir:off .}
{. emit:["_Pragma(\"omp ", p, "\")"] .}
body
{. pop .}
template ompBlock*(p:string; body:untyped) =
#{. emit:"#pragma omp " & p .}
#{. emit:"{ /* Inserted by ompBlock " & p & " */".}
Expand All @@ -38,6 +43,12 @@ else:
#{. emit:"} /* End ompBlock " & p & " */".}

template ompBarrier* = ompPragma("barrier")
template ompFlush* = ompPragma("flush")
template ompFlushAcquire* = ompPragma("flush acquire")
template ompFlushRelease* = ompPragma("flush release")
template ompFlushSeqCst* = ompPragma("flush seq_cst")
template ompAtomicRead*(body) = ompPragma("atomic read acquire", body)
template ompAtomicWrite*(body) = ompPragma("atomic write release", body)

template ompParallel*(body:untyped) =
ompBlock("parallel"):
Expand Down
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()
75 changes: 30 additions & 45 deletions src/base/profile.nim
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
import threading
export threading
import comms/comms, stdUtils, base/basicOps
import comms/commsEcho, stdUtils, base/[basicOps,params]
import os, strutils, sequtils, std/monotimes, std/tables, std/algorithm, strformat
export monotimes
getOptimPragmas()

const noTicToc {.boolDefine.} = false

type PerfInfo* = object
count*: int
flops*: float
secs*: float
template clear*(pi: var PerfInfo) =
pi.count = 0
pi.flops = 0.0
pi.secs = 0.0
template `+=`*(r: var PerfInfo, x: PerfInfo) =
r.count += x.count
r.flops += x.flops
r.secs += x.secs
proc `$`*(pi: PerfInfo): string =
result = system.`$`(pi)
result &= &" {1e-9*pi.flops/pi.secs:.2f} Gflops"

type TicType* = distinct int64
template getTics*: TicType = TicType(getMonoTime().ticks)
template nsec*(t:TicType):int64 = int64(t)
Expand All @@ -15,12 +31,13 @@ template ticDiffSecs*(x,y: TicType): float = 1e-9 * float(x.int64 - y.int64)
template `-`*(x,y: TicType): TicType = TicType(x.int64 - y.int64)

var
DropWasteTimerRatio* = 0.05 ## Drop children timers if the proportion of their overhead is larger than this.
## Drop children timers if the proportion of their overhead is larger than this.
DropWasteTimerRatio* = floatParam("dropRatio", 0.05)
VerboseTimer* = false ## If true print out all the timers during execution.

##[
Each Tic starts a local timer. Each Toc records the time difference
Each tic() starts a local timer. Each toc() records the time difference
of the current time with the one in the local timer visible in the
scope, and then update the timer with the current time.
Expand Down Expand Up @@ -516,49 +533,10 @@ template tocI(f: SomeNumber; s:SString = ""; n = -1) =
localCode {.global.} = newList[CodePoint]()
thisCode = CodePoint(-1)
if threadNum==0:
when false:
#echo "==== begin toc ",s," ",ii
#echo " rtiStack: ",indent($rtiStack,5)
#echo " cpHeap: ",indent($cpHeap,5)
if unlikely VerboseTimer: echoToc(s,ii)
if prevRTI.int32 >= 0:
if restartTimer:
thawTimers()
restartTimer = false
if not timersFrozen():
let theTime = getTics()
when not cname:
for c in items(localCode):
if cpHeap[c.int].name.equal(s):
thisCode = c
break
if thisCode.isNil:
thisCode = newCodePoint(ii.addr, s)
when not cname:
localCode.add thisCode
let
ns = theTime-localTimer
thisRTI = record(localTic, prevRTI, thisCode, ns, float(f))
var oh = rtiStack[thisRTI.int].childrenOverhead
let c = rtiStack[thisRTI.int].children
for i in 0..<c.len:
if toDropTimer(c[i].prev):
oh -= c[i].childrenOverhead
if oh.float / ns.float > DropWasteTimerRatio:
# Signal stop if the overhead is too large.
dropTimer(prevRTI)
if toDropTimer(thisCode):
freezeTimers()
restartTimer = true
localTimer = getTics()
rtiStack[thisRTI.int].overhead = nsec(localTimer-theTime)
prevRTI = thisRTI
#echo "==== end toc ",s," ",ii
when cname:
tocSet(localTimer,prevRTI,restartTimer,thisCode,f,s,ii.addr,localTic,false)
else:
when cname:
tocSet(localTimer,prevRTI,restartTimer,thisCode,f,s,ii.addr,localTic,false)
else:
tocSet(localTimer,prevRTI,restartTimer,thisCode,f,s,ii.addr,localTic,addr localCode)
tocSet(localTimer,prevRTI,restartTimer,thisCode,f,s,ii.addr,localTic,addr localCode)

when noTicToc:
template toc*() = discard
Expand Down Expand Up @@ -826,6 +804,7 @@ proc makeHotspotTable(lrti: List[RTInfoObj]): tuple[ns:int64,oh:int64] =
return (nstot, ohtot)

proc echoHotspots* =
hs.clear
let tot = makeHotspotTable(rtiStack)
#let nstot = tot.ns
let ohtot = tot.oh
Expand Down Expand Up @@ -879,6 +858,12 @@ proc echoHotspots* =
let tsnspct = 100.0 * tsns / nstot
echo &"{pct:6.3f} {tsnspct:7.3f} {count} {mf} {nc} S {lc} {nm}"

proc echoProf*(def = 0) =
case intParam("prof",def)
of 1: echoHotspots()
of 2: echoTimers()
else: discard

when isMainModule:
import os
proc test =
Expand Down
20 changes: 14 additions & 6 deletions src/base/stdUtils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ type
#template `[]`*(x: cArray): untyped = addr x[0]
#template `&`*(x: ptr cArray): untyped = addr x[0]

template ptrInt*(x:untyped):untyped = cast[ByteAddress](x)
template addrInt*(x:untyped):untyped = cast[ByteAddress](addr(x))
template unsafeAddrInt*(x:untyped):untyped = cast[ByteAddress](addr(x))
template toHex*(x: ptr typed): untyped = toHex(cast[ByteAddress](x))
template ptrInt*(x: auto): auto = cast[uint](x)
template addrInt*(x: auto): auto = cast[uint](addr(x))
template unsafeAddrInt*(x: auto): auto = cast[uint](addr(x))
template toHex*(x: ptr auto): auto = toHex(cast[uint](x))
template toHex*(x: pointer): auto = toHex(cast[uint](x))

type
ConstInt* {.importc:"const int".} = object
Expand All @@ -31,7 +32,7 @@ proc isInteger*(s: string):bool =
var t:int
parseInt(s, t) == s.len

template `$&`*(x: untyped): string =
template `$&`*(x: auto): string =
toHex(unsafeAddrInt(x))

proc `|`*(s: string, d: tuple[w:int,c:char]): string =
Expand All @@ -52,9 +53,16 @@ proc `|`*(f: SomeFloat, d: tuple[w,p: int]): string =
formatFloat(f, ffDefault, d.p) | d.w
proc `|`*(f: float, d: int): string =
f | (d,d)
template `|-`*(x:SomeNumber, y: int): untyped =
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
Loading

0 comments on commit 05ec081

Please sign in to comment.