I would like to interface with the Grappa framework, which is C++ based. I have never tried to use the C++ support in Nim before, so I am struggling a little.
Their hello world example is heavily based on lambdas. What is the support for C++ lambdas in Nim? Is it enough to write an anonymous proc?
My tentative translation goes like this (with an empty init):
type
Grappa {.final, header: "Grappa.hpp", importcpp: "Grappa".} = object
proc init(argc: ptr cint, argv: ptr ptr ptr char) {. header: "Grappa.hpp", importcpp: "Grappa::init(@)".}
proc finalize() {. header: "Grappa.hpp", importcpp: "Grappa::finalize()".}
proc run(p: proc()) {. header: "Grappa.hpp", importcpp: "Grappa::run(@)".}
proc onAllCores(p: proc()) {. header: "Grappa.hpp", importcpp: "Grappa::on_all_cores(@)".}
proc noInit() =
var x = 0.cint
var y: char
init(addr(x), cast[ptr[ptr[ptr[char]]]](addr(y)))
when isMainModule:
noInit()
run(proc() =
onAllCores(proc() =
echo "Hello"
# Grappa.LOG(Grappa.INFO) << "Hello world from locale " << Grappa.mylocale() << " core " << Grappa.mycore()
)
)
finalize()
but I am getting an error at the gcc level which I do not understand
In file included from /home/papillon/tools/grappa/system/GlobalCompletionEvent.hpp:31:0,
from /home/papillon/tools/grappa/system/Delegate.hpp:31,
from /home/papillon/tools/grappa/system/Grappa.hpp:45,
from /home/papillon/esperimenti/gnappa/nimcache/hello.cpp:10:
/home/papillon/tools/grappa/system/Collective.hpp: In instantiation of ‘Grappa::on_all_cores(F)::<lambda()>::<lambda()> [with F = TY92034]’:
/home/papillon/tools/grappa/system/Collective.hpp:184:25: required from ‘struct Grappa::on_all_cores(F)::<lambda()> [with F = TY92034]::<lambda()>’
/home/papillon/tools/grappa/system/Collective.hpp:187:10: required from ‘Grappa::on_all_cores(F)::<lambda()> [with F = TY92034]’
/home/papillon/tools/grappa/system/Collective.hpp:183:38: required from ‘struct Grappa::on_all_cores(F) [with F = TY92034]::<lambda()>’
/home/papillon/tools/grappa/system/Collective.hpp:188:8: required from ‘void Grappa::on_all_cores(F) [with F = TY92034]’
/home/papillon/esperimenti/gnappa/nimcache/hello.cpp:94:27: required from here
/home/papillon/tools/grappa/system/Collective.hpp:185:16: error: no match for call to ‘(const TY92034) ()’
work();
^
In file included from /home/papillon/tools/grappa/system/Grappa.hpp:40:0,
from /home/papillon/esperimenti/gnappa/nimcache/hello.cpp:10:
/home/papillon/tools/grappa/system/Tasking.hpp: In instantiation of ‘Grappa::run(FP)::<lambda()> [with FP = TY92034]’:
/home/papillon/tools/grappa/system/Tasking.hpp:201:22: required from ‘struct Grappa::run(FP) [with FP = TY92034]::<lambda()>’
/home/papillon/tools/grappa/system/Tasking.hpp:206:9: required from ‘void Grappa::run(FP) [with FP = TY92034]’
/home/papillon/esperimenti/gnappa/nimcache/hello.cpp:145:18: required from here
/home/papillon/tools/grappa/system/Tasking.hpp:203:12: error: no match for call to ‘(TY92034) ()’
fp();
Nim doesn't generate C++ lambdas at all and you need to get the calling convention right, for example:
proc onAllCores(p: proc() {.cdecl, gcsafe.}) {.cdecl.}
Maybe you also need to use emit, something like this:
proc onAllCores(p: proc() {.cdecl, gcsafe.}) =
{.emit: """Grappa::on_all_cores([] () { `p`() });""".}