3

I am wondering if it is possible to create a finalise method for OCaml records? I am creating a GPU database and I am using OCaml for building the DSL to query and manipulate GPU data, and I need a way to free GPU memory when they are eligible for GC (not referenced anymore).

I have creating a binding to my C-based GPU api to allow to malloc, free and manipulate GPU data but I am looking at solutions to free GPU memory when it is not referenced by variables anymore in the toplevel.

let bids = (**a 1-dim GPU array*) and asks = (**another 1-dim GPU array*) in
let spread = asks - bids 

In this example, I would need to free up the GPU memory for the bids and asks vector residing in GPU memory. I guess I need to write my own language?

2 Answers 2

3

What you need is probably custom blocks, since your data is not pure Caml:

http://caml.inria.fr/pub/docs/manual-ocaml-4.00/manual033.html

19.9 Advanced topic: custom blocks

Blocks with tag Custom_tag contain both arbitrary user data and a pointer to a C struct, with type struct custom_operations, that associates user-provided finalization, comparison, hashing, serialization and deserialization functions to this block.

You can attach your C finalization functions to custom blocks. They are called when the block is GCed in OCaml world.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use Gc.finalise to do this. Using GC finalization (American spelling) to control things is quite perilous (or so I've read). For example, finalization functions are not called at program exit. So be careful :-)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.