Legate.jl
Loading...
Searching...
No Matches
wrapper.inl
Go to the documentation of this file.
1/* Copyright 2025 Northwestern University,
2 * Carnegie Mellon University University
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * Author(s): David Krasowska <krasow@u.northwestern.edu>
17 * Ethan Meitz <emeitz@andrew.cmu.edu>
18 */
19
20#include "legate.h"
21#include "legate/mapping/machine.h"
22#include "legate/runtime/runtime.h"
23#include "legate/timing/timing.h"
24#include "legion.h"
25#include "legion/legion_config.h"
26
27using namespace legate;
34namespace legate_wrapper {
35namespace runtime {
36
41inline void start_legate() { legate::start(); }
42
47inline int32_t legate_finish() { return legate::finish(); }
48
53inline Runtime* get_runtime() { return Runtime::get_runtime(); }
54
59inline bool has_started() { return legate::has_started(); }
60
65inline bool has_finished() { return legate::has_finished(); }
66} // namespace runtime
67
68namespace tasking {
75inline Constraint align(const Variable& a, const Variable& b) {
76 return legate::align(a, b);
77}
78
88inline AutoTask create_auto_task(Runtime* rt, Library lib, LocalTaskID id) {
89 return rt->create_task(lib, id);
90}
91
102inline ManualTask create_manual_task(Runtime* rt, Library lib, LocalTaskID id,
103 const Domain& domain) {
104 return rt->create_task(lib, id, domain);
105}
106
114inline Domain domain_from_shape(const Shape& shape) {
115 if (shape.volume() == 0) return Domain::NO_DOMAIN;
116 switch (shape.ndim()) {
117 case 1: {
118 Legion::Point<1> lo = Legion::Point<1>::ZEROES();
119 Legion::Point<1> hi;
120 hi[0] = shape[0] - 1;
121 return Domain(Legion::Rect<1>(lo, hi));
122 }
123 case 2: {
124 Legion::Point<2> lo = Legion::Point<2>::ZEROES();
125 Legion::Point<2> hi;
126 hi[0] = shape[0] - 1;
127 hi[1] = shape[1] - 1;
128 return Domain(Legion::Rect<2>(lo, hi));
129 }
130 case 3: {
131 Legion::Point<3> lo = Legion::Point<3>::ZEROES();
132 Legion::Point<3> hi;
133 hi[0] = shape[0] - 1;
134 hi[1] = shape[1] - 1;
135 hi[2] = shape[2] - 1;
136 return Domain(Legion::Rect<3>(lo, hi));
137 }
138 default:
139 return Domain::NO_DOMAIN;
140 }
141}
142
150inline auto submit_auto_task(Runtime* rt, AutoTask& task) {
151 return rt->submit(std::move(task));
152}
153
161inline auto submit_manual_task(Runtime* rt, ManualTask& task) {
162 return rt->submit(std::move(task));
163}
164
165} // namespace tasking
166
167namespace data {
174inline Scalar string_to_scalar(std::string str) { return Scalar(str); }
175
184inline LogicalArray create_unbound_array(const Type& ty, std::uint32_t dim = 1,
185 bool nullable = false) {
186 return Runtime::get_runtime()->create_array(ty, dim, nullable);
187}
188
198inline LogicalArray create_array(const Shape& shape, const Type& ty,
199 bool nullable = false,
200 bool optimize_scalar = false) {
201 return Runtime::get_runtime()->create_array(shape, ty, nullable,
202 optimize_scalar);
203}
204
212inline LogicalStore create_unbound_store(const Type& ty,
213 std::uint32_t dim = 1) {
214 return Runtime::get_runtime()->create_store(ty, dim);
215}
216
225inline LogicalStore create_store(const Shape& shape, const Type& ty,
226 bool optimize_scalar = false) {
227 return Runtime::get_runtime()->create_store(shape, ty, optimize_scalar);
228}
229
237inline LogicalStore store_from_scalar(const Scalar& scalar,
238 const Shape& shape = Shape{1}) {
239 return Runtime::get_runtime()->create_store(scalar, shape);
240}
241
250inline LogicalStore attach_external_store_sysmem(void* ptr, const Shape& shape,
251 const Type& ty) {
252 legate::ExternalAllocation alloc = legate::ExternalAllocation::create_sysmem(
253 ptr, shape.volume() * ty.size());
254 legate::mapping::DimOrdering ordering =
255 legate::mapping::DimOrdering::fortran_order();
256
257 auto store =
258 legate::Runtime::get_runtime()->create_store(shape, ty, alloc, ordering);
259 return store;
260}
261
272inline LogicalStore attach_external_store_fbmem(int device_id, void* ptr,
273 const Shape& shape,
274 const Type& ty, bool readonly) {
275 legate::ExternalAllocation alloc = legate::ExternalAllocation::create_fbmem(
276 device_id, ptr, shape.volume() * ty.size(), readonly);
277 legate::mapping::DimOrdering ordering =
278 legate::mapping::DimOrdering::fortran_order();
279
280 auto store =
281 legate::Runtime::get_runtime()->create_store(shape, ty, alloc, ordering);
282 return store;
283}
284
286 template <legate::Type::Code CODE, int DIM>
287 void* operator()(legate::PhysicalStore* store) {
288#if !LEGATE_DEFINED(LEGATE_USE_CUDA)
289 // Check if FLOAT16 is being used without CUDA support
290 if (CODE == legate::Type::Code::FLOAT16) {
291 throw std::runtime_error(
292 "FLOAT16 type is not supported when building without CUDA");
293 }
294#endif
295 using CppT = typename legate_util::code_to_cxx<CODE>::type;
296 auto shp = store->shape<DIM>();
297 return store->write_accessor<CppT, DIM>().ptr(Realm::Point<DIM>(shp.lo));
298 }
299};
300
307inline void* get_ptr(legate::PhysicalStore* store) {
308 int dim = store->dim();
309 legate::Type::Code code = store->type().code();
310 return legate::double_dispatch(dim, code, GetPtrFunctor{}, store);
311}
312
313} // namespace data
314
315namespace time {
316
321inline uint64_t time_microseconds() {
322 return legate::timing::measure_microseconds().value();
323}
324
329inline uint64_t time_nanoseconds() {
330 return legate::timing::measure_nanoseconds().value();
331}
332} // namespace time
333} // namespace legate_wrapper
Definition wrapper.inl:167
Scalar string_to_scalar(std::string str)
Convert a string to a Scalar.
Definition wrapper.inl:174
LogicalArray create_unbound_array(const Type &ty, std::uint32_t dim=1, bool nullable=false)
Create an unbound array.
Definition wrapper.inl:184
LogicalStore store_from_scalar(const Scalar &scalar, const Shape &shape=Shape{1})
Create a store from a scalar value.
Definition wrapper.inl:237
LogicalArray create_array(const Shape &shape, const Type &ty, bool nullable=false, bool optimize_scalar=false)
Create an array with a specified shape.
Definition wrapper.inl:198
LogicalStore create_store(const Shape &shape, const Type &ty, bool optimize_scalar=false)
Create a store with a specified shape.
Definition wrapper.inl:225
LogicalStore attach_external_store_fbmem(int device_id, void *ptr, const Shape &shape, const Type &ty, bool readonly)
Attach an external store in frame buffer memory.
Definition wrapper.inl:272
void * get_ptr(legate::PhysicalStore *store)
Get a pointer to the data in a PhysicalStore.
Definition wrapper.inl:307
LogicalStore attach_external_store_sysmem(void *ptr, const Shape &shape, const Type &ty)
Attach an external store in system memory.
Definition wrapper.inl:250
LogicalStore create_unbound_store(const Type &ty, std::uint32_t dim=1)
Create an unbound store.
Definition wrapper.inl:212
Definition wrapper.inl:35
int32_t legate_finish()
Finalize the Legate runtime.
Definition wrapper.inl:47
bool has_started()
Check whether the Legate runtime has started.
Definition wrapper.inl:59
void start_legate()
Start the Legate runtime.
Definition wrapper.inl:41
bool has_finished()
Check whether the Legate runtime has finished.
Definition wrapper.inl:65
Runtime * get_runtime()
Return the current Legate runtime instance.
Definition wrapper.inl:53
Definition wrapper.inl:68
auto submit_manual_task(Runtime *rt, ManualTask &task)
Submit a manual task to the runtime.
Definition wrapper.inl:161
Domain domain_from_shape(const Shape &shape)
Create a Domain from a Shape.
Definition wrapper.inl:114
auto submit_auto_task(Runtime *rt, AutoTask &task)
Submit an auto task to the runtime.
Definition wrapper.inl:150
Constraint align(const Variable &a, const Variable &b)
Align two variables.
Definition wrapper.inl:75
ManualTask create_manual_task(Runtime *rt, Library lib, LocalTaskID id, const Domain &domain)
Create an manual task in the runtime.
Definition wrapper.inl:102
AutoTask create_auto_task(Runtime *rt, Library lib, LocalTaskID id)
Create an auto task in the runtime.
Definition wrapper.inl:88
Definition wrapper.inl:315
uint64_t time_nanoseconds()
Measure time in nanoseconds.
Definition wrapper.inl:329
uint64_t time_microseconds()
Measure time in microseconds.
Definition wrapper.inl:321
These functions can be invoked from Julia.
Definition wrapper.inl:34
Definition types.h:8
Definition wrapper.inl:285
void * operator()(legate::PhysicalStore *store)
Definition wrapper.inl:287
std::int32_t code(legate::Type ty)
Definition types.cpp:26