microsoft / knossos-ksc

Compiler with automatic differentiation
Other
45 stars 10 forks source link

Make support for allocation optional #1004

Closed dcrc2 closed 2 years ago

dcrc2 commented 2 years ago

This PR modifies knososs.h to support scenarios where we want to prohibit any allocation taking place, e.g. for a basic version of GPU support.

When allocation is disabled you get a runtime assertion failure if tensor::create is called.

I would have much preferred to make this a compile-time failure. However, this would have meant that prelude.ks could not be compiled. (prelude.ks contains several build calls, including some generated by gdefs.)

My initial plan was to provide a separate prelude-cuda.ks, removing the functions that do allocation. But although this works, I found some problems:

An alternative would be for ksc to strip out any unused defs as the final stage of compilation. Then we could get a compile-time error if any remaining defs involved a build (or another allocating function). I could try implementing this if we thought it was a good idea (it might also be good for reducing code size and C++ compile times). But, nearly all of this PR would still be necessary in that case: the difference would be that uses of tensor::create would cause a compile-time error rather than an assertion failure.