Skip to content

Commit

Permalink
fix echo in profile
Browse files Browse the repository at this point in the history
  • Loading branch information
jcosborn committed Nov 21, 2024
1 parent e0b23c4 commit b5b6d0d
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 92 deletions.
2 changes: 1 addition & 1 deletion src/base/profile.nim
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
10 changes: 2 additions & 8 deletions src/comms/comms.nim
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -157,6 +149,8 @@ commsNames.add "QMP"
commsInits.add getQmpComm
commsFinis.add commsFinalizeQmp

import commsEcho
export commsEcho
import commsUtils
export commsUtils

Expand Down
86 changes: 86 additions & 0 deletions src/comms/commsEcho.nim
Original file line number Diff line number Diff line change
@@ -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..<args.len:
let t = genSym()
let a = args[i]
result.add(quote do:
when `a` is openarray:
let `t` = $`a`
else:
let `t` = `a`
)
call.add(t)
proc cprintf*(fmt:cstring){.importc:"printf",varargs,header:"<stdio.h>".}
#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..<i: t[3].add ea[j]
result.add t
#echo result.treerepr
makeEchos(64)
#[
template echo*(a1: untyped) =
when nimvm:
echoRaw(a1)
else:
echo0(a1)
template echo*(a1,a2: untyped) =
when nimvm:
echoRaw(a1,a2)
else:
echo0(a1,a2)
]#
8 changes: 8 additions & 0 deletions src/comms/commsTypes.nim
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
type
Comm* = ref object of RootObj

# globals

var defaultComm*: Comm
template getDefaultComm*(): Comm = defaultComm
template getComm*(): Comm = getDefaultComm() # temporary alias
var myRank* = 0
var nRanks* = 1
83 changes: 0 additions & 83 deletions src/comms/commsUtils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,89 +6,6 @@ import macros
import comms/comms
getOptimPragmas()

proc evalArgs*(call:var NimNode; args:NimNode):NimNode =
result = newStmtList()
for i in 0..<args.len:
let t = genSym()
let a = args[i]
result.add(quote do:
when `a` is openarray:
let `t` = $`a`
else:
let `t` = `a`
)
call.add(t)
proc cprintf*(fmt:cstring){.importc:"printf",varargs,header:"<stdio.h>".}
#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..<i: t[3].add ea[j]
result.add t
#echo result.treerepr
makeEchos(64)
#[
template echo*(a1: untyped) =
when nimvm:
echoRaw(a1)
else:
echo0(a1)
template echo*(a1,a2: untyped) =
when nimvm:
echoRaw(a1,a2)
else:
echo0(a1,a2)
]#

template sum*(c:Comm, v:var SomeNumber) = c.allReduce(v)
template sum*(c:Comm, v:ptr float32, n:int) = c.allReduce(v,n)
template sum*(c:Comm, v:ptr float64, n:int) = c.allReduce(v,n)
Expand Down

0 comments on commit b5b6d0d

Please sign in to comment.