From b5b6d0d1378b118d72b133038be16a35f92cd02e Mon Sep 17 00:00:00 2001 From: James Osborn Date: Thu, 21 Nov 2024 14:01:02 -0600 Subject: [PATCH] fix echo in profile --- src/base/profile.nim | 2 +- src/comms/comms.nim | 10 +---- src/comms/commsEcho.nim | 86 ++++++++++++++++++++++++++++++++++++++++ src/comms/commsTypes.nim | 8 ++++ src/comms/commsUtils.nim | 83 -------------------------------------- 5 files changed, 97 insertions(+), 92 deletions(-) create mode 100644 src/comms/commsEcho.nim diff --git a/src/base/profile.nim b/src/base/profile.nim index 6a861f6c..de7ca62e 100644 --- a/src/base/profile.nim +++ b/src/base/profile.nim @@ -1,6 +1,6 @@ import threading export threading -import comms/comms, stdUtils, base/[basicOps,params] +import comms/commsEcho, stdUtils, base/[basicOps,params] import os, strutils, sequtils, std/monotimes, std/tables, std/algorithm, strformat export monotimes getOptimPragmas() diff --git a/src/comms/comms.nim b/src/comms/comms.nim index 9e4be9a8..28e3be03 100644 --- a/src/comms/comms.nim +++ b/src/comms/comms.nim @@ -1,14 +1,6 @@ import commsTypes export commsTypes -# globals - -var defaultComm*: Comm -template getDefaultComm*(): Comm = defaultComm -template getComm*(): Comm = getDefaultComm() # temporary alias -var myRank* = 0 -var nRanks* = 1 - # base methods method name*(c: Comm): string {.base.} = discard @@ -157,6 +149,8 @@ commsNames.add "QMP" commsInits.add getQmpComm commsFinis.add commsFinalizeQmp +import commsEcho +export commsEcho import commsUtils export commsUtils diff --git a/src/comms/commsEcho.nim b/src/comms/commsEcho.nim new file mode 100644 index 00000000..b06c68ef --- /dev/null +++ b/src/comms/commsEcho.nim @@ -0,0 +1,86 @@ +import macros +import base/[threading,metaUtils] +import commsTypes + +proc evalArgs*(call:var NimNode; args:NimNode):NimNode = + result = newStmtList() + for i in 0..".} +#proc printfOrdered( +macro printf*(fmt:string; args:varargs[untyped]):auto = + var call = newCall(ident("cprintf"), fmt) + result = evalArgs(call, args) + result.add(quote do: + if myRank==0 and threadNum==0: + `call` + ) +proc echoRaw*(x: varargs[typed, `$`]) {.magic: "Echo".} +macro echoAll*(args:varargs[untyped]):auto = + var call = newCall(bindSym"echoRaw") + result = evalArgs(call, args) + result.add(quote do: + `call` + ) +macro echoRank*(args:varargs[untyped]):auto = + var call = newCall(bindSym"echoRaw") + call.add ident"myRank" + call.add newLit"/" + call.add ident"nRanks" + call.add newLit": " + result = evalArgs(call, args) + template f(x:untyped):untyped = + if threadNum==0: x + result.add getAst(f(call)) +macro echo0*(args: varargs[untyped]): untyped = + var call = newCall(bindSym"echoRaw") + result = evalArgs(call, args) + result.add(quote do: + bind myRank + if myRank==0 and threadNum==0: + `call` + ) + #echo result.repr +macro makeEchos(n:static[int]): untyped = + template ech(x,y: untyped) = + template echo* = + when nimvm: + x + else: + y + result = newStmtList() + for i in 1..n: + var er = newCall(bindSym"echoRaw") + var e0 = newCall(bindSym"echo0") + var ea = newSeq[NimNode](0) + for j in 1..i: + let ai = ident("a" & $j) + er.add ai + e0.add ai + ea.add newNimNode(nnkIdentDefs).add(ai).add(ident"untyped").add(newEmptyNode()) + var t = getAst(ech(er,e0)).peelStmt + #echo t.treerepr + for j in 0..".} -#proc printfOrdered( -macro printf*(fmt:string; args:varargs[untyped]):auto = - var call = newCall(ident("cprintf"), fmt) - result = evalArgs(call, args) - result.add(quote do: - if myRank==0 and threadNum==0: - `call` - ) -proc echoRaw*(x: varargs[typed, `$`]) {.magic: "Echo".} -macro echoAll*(args:varargs[untyped]):auto = - var call = newCall(bindSym"echoRaw") - result = evalArgs(call, args) - result.add(quote do: - `call` - ) -macro echoRank*(args:varargs[untyped]):auto = - var call = newCall(bindSym"echoRaw") - call.add ident"myRank" - call.add newLit"/" - call.add ident"nRanks" - call.add newLit": " - result = evalArgs(call, args) - template f(x:untyped):untyped = - if threadNum==0: x - result.add getAst(f(call)) -macro echo0*(args: varargs[untyped]): untyped = - var call = newCall(bindSym"echoRaw") - result = evalArgs(call, args) - result.add(quote do: - bind myRank - if myRank==0 and threadNum==0: - `call` - ) - #echo result.repr -macro makeEchos(n:static[int]): untyped = - template ech(x,y: untyped) = - template echo* = - when nimvm: - x - else: - y - result = newStmtList() - for i in 1..n: - var er = newCall(bindSym"echoRaw") - var e0 = newCall(bindSym"echo0") - var ea = newSeq[NimNode](0) - for j in 1..i: - let ai = ident("a" & $j) - er.add ai - e0.add ai - ea.add newNimNode(nnkIdentDefs).add(ai).add(ident"untyped").add(newEmptyNode()) - var t = getAst(ech(er,e0)).peelStmt - #echo t.treerepr - for j in 0..