55open Base
66
77type 'buffer_ptr buffer = { ptr : 'buffer_ptr ; size_in_bytes : int } [@@ deriving sexp_of ]
8+ type 'buffer_ptr ctx_arrays = 'buffer_ptr Map .M (Tnode ).t [@@ deriving sexp_of ]
89
910module Buffer_types (Buffer_ptr : sig
1011 type buffer_ptr [@@deriving sexp_of]
1112end ) =
1213struct
1314 type nonrec buffer = Buffer_ptr .buffer_ptr buffer [@@ deriving sexp_of ]
14- type ctx_arrays = Buffer_ptr .buffer_ptr Map .M ( Tnode ).t [@@ deriving sexp_of ]
15+ type nonrec ctx_arrays = Buffer_ptr .buffer_ptr ctx_arrays [@@ deriving sexp_of ]
1516end
1617
1718module type Buffer = sig
@@ -105,11 +106,22 @@ type ('buffer_ptr, 'dev, 'runner, 'event) stream = {
105106}
106107[@@ deriving sexp_of ]
107108
109+ type ('buffer_ptr, 'stream) context = {
110+ stream : 'stream ;
111+ parent : ('buffer_ptr , 'stream ) context option ;
112+ ctx_arrays : 'buffer_ptr ctx_arrays ;
113+ (* * This map contains arrays used in this context or an ancestor context (they might be unique
114+ but might also be cross-stream shared. *)
115+ finalized : Utils .atomic_bool ;
116+ }
117+ [@@ deriving sexp_of ]
118+
108119module type Device_types = sig
109120 include Device_config
110121
111122 type nonrec device = (buffer_ptr , dev , event ) device [@@ deriving sexp_of ]
112123 type nonrec stream = (buffer_ptr , dev , runner , event ) stream [@@ deriving sexp_of ]
124+ type nonrec context = (buffer_ptr , stream ) context [@@ deriving sexp_of ]
113125end
114126
115127module type Device = sig
@@ -118,32 +130,28 @@ module type Device = sig
118130
119131 val make_device : dev -> ordinal :int -> device
120132 val make_stream : device -> runner -> stream_id :int -> stream
133+
134+ val make_context : ?ctx_arrays : ctx_arrays -> stream -> context
135+ (* * Returns a context without a parent. *)
136+
137+ val make_child : ?ctx_arrays : ctx_arrays -> context -> context
138+ (* * Returns a context with the same {!field-stream}, and {!field-ctx_arrays} if omitted, as the
139+ given context's, which is also the {!field-parent}. *)
140+
121141 val get_name : stream -> string
122142end
123143
124144(* * Parts shared by both assignments-level and lowered-level backend interfaces. *)
125145module type Backend_any_common = sig
126146 include Buffer
127147
128- type context [@@deriving sexp_of]
129- type stream
130-
131- type init_info
132- (* * For backends derived via {!No_device_backend}, this is usually the backend name concatenated
133- with the device or stream number. For {!Backend}, [init_info = stream]. *)
134-
135148 val initialize : config -> unit
136149 (* * Initializes a backend before first use. Typically does nothing if the backend is already
137150 initialized, but some backends can do some safe cleanups. *)
138151
139152 val is_initialized : unit -> bool
140153 (* * Returns false if there was no previous {!initialize} call. If it returns false, one must call
141154 {!initialize} before using the backend. *)
142-
143- val init : init_info -> context
144-
145- val finalize : context -> unit
146- (* * Finalizes (just) the context. *)
147155end
148156
149157(* * Parts shared by assignments-level backend interfaces. *)
175183 and devices. *)
176184module type Backend_device_common = sig
177185 include Device
178-
179- include
180- Backend_any_common
181- with type buffer_ptr := buffer_ptr
182- and type init_info := stream
183- and type stream := stream
186+ include Backend_any_common with type buffer_ptr := buffer_ptr
184187
185188 val sync : event -> unit
186189 (* * Blocks till the event completes, if it's not done already. *)
@@ -216,7 +219,6 @@ module type Backend_device_common = sig
216219 {!No_device_backend.initialize}. *)
217220
218221 val new_stream : device -> stream
219- val get_ctx_stream : context -> stream
220222end
221223
222224module type With_buffer_retrieval_and_syncing = sig
262264
263265module type Backend = sig
264266 include Backend_device_common
265-
266- include
267- Backend_common
268- with type buffer_ptr := buffer_ptr
269- and type context := context
270- and type init_info := stream
271- and type stream := stream
267+ include Backend_common with type buffer_ptr := buffer_ptr
272268
273269 val link : context -> code -> context routine
274270 (* * Returns the routine for the code's procedure, in a new context derived from the given context. *)
0 commit comments