On 2011-02-13, at 10:35 AM, Jason E. Aten wrote:
Hi Gambit enthusiasts,
I'm curious if anyone has played with generating CUDA kernels or CUDA from Gambit scheme code.
I've learned some Lisp and some Scheme, but I'm still unclear how the "C embedding" in Gambit works.
I haven't tried to interface CUDA code with Gambit, but I don't see much of a problem. If you can do it from C (or C++) then you can do it from Scheme compiled with Gambit-C. The first thing to do is to figure out how you would do it in plain C/C++. Then you put that code in a C function, and you write in Scheme an interface to that C function. For example, if your function is called foo and it takes two "int" parameters and returns a "double", you would write:
(c-declare #<<EOF
double foo(int x, int y) { return x/(1.0+y*y); }
EOF )
(define foo (c-lambda (int int) double "foo"))
(print (foo 11 22)) ;; test it...
Note that you could have put the definition of foo in a .c file that is compiled separately. In that case you will still need a prototype for the foo function. If the prototype is in the header file foo.h, the you could write:
(c-declare "#include "foo.h"")
(define foo (c-lambda (int int) double "foo"))
The last hurdle is to link your Scheme program with the C code. If you are generating an executable program, and prog.scm is the file with your Scheme code and the c-lambda form, and -lCUDA is required to link with the CUDA libraries, then this should do:
% gsc -exe -ld-options "-lCUDA" prog.scm foo.c % ./prog
or
% gcc -c foo.c % gsc -exe -ld-options "-lCUDA" prog.scm foo.o % ./prog
If you are generating a loadable object file, then use this instead (lib.scm is the file with your Scheme code, and the c-lambda form):
% gcc -c foo.c % gsc -ld-options "-lCUDA foo.o" lib.scm
For example, if one needs to call nVidia's nvcc compiler to handle their dialect of C (CUDA-C == C with GPU extensions) instead of gcc (for example), will the Gambit compiler pass through the embedded C-with-extensions to the C compiler of choice?
It might be simpler to compile Gambit with the nvcc compiler. That way, nvcc will be used for compiling all the code and you will avoid possible linking problems. That can be done with:
% ./configure CC=nvcc
For that matter, the Thrust libraries bring an STL-like interface to creating and using CUDA kernels; is it possible to embed C++ code with templates directly into Gambit Scheme code?
The code generated by the Gambit compiler is compatible with C and C++. For example, if you want to compile Gambit with the g++ compiler, just do:
% ./configure --enable-cplusplus
or
% ./configure CC=g++
I'm not sure about the templates though... what use of templates are you considering?
Marc