| 1 | # Licensed to the Apache Software Foundation (ASF) under one | ||
| 2 | # or more contributor license agreements. See the NOTICE file | ||
| 3 | # distributed with this work for additional information | ||
| 4 | # regarding copyright ownership. The ASF licenses this file | ||
| 5 | # to you under the Apache License, Version 2.0 (the | ||
| 6 | # "License"); you may not use this file except in compliance | ||
| 7 | # with the License. You may obtain a copy of the License at | ||
| 8 | # | ||
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 | ||
| 10 | # | ||
| 11 | # Unless required by applicable law or agreed to in writing, | ||
| 12 | # software distributed under the License is distributed on an | ||
| 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| 14 | # KIND, either express or implied. See the License for the | ||
| 15 | # specific language governing permissions and limitations | ||
| 16 | # under the License. | ||
| 17 | |||
| 18 | package AI::MXNet::Engine; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use AI::MXNet::Function::Parameters; | ||
| 22 | use AI::MXNet::Base; | ||
| 23 | use AI::MXNet::NS; | ||
| 24 | |||
| 25 | =head1 NAME | ||
| 26 | |||
| 27 | AI::MXNet::Engine - Allows management of properties of the MXNet's engine. | ||
| 28 | =cut | ||
| 29 | |||
| 30 | =head1 SYNOPSIS | ||
| 31 | |||
| 32 | my $x; | ||
| 33 | mx->engine->bulk(10, sub { | ||
| 34 | $x = mx->nd->ones([10]); | ||
| 35 | $x *= 2; | ||
| 36 | $x += 1; | ||
| 37 | $x->wait_to_read(); | ||
| 38 | $x += 1; | ||
| 39 | ok(($x->aspdl == 4)->all); | ||
| 40 | for my $i (1..100) | ||
| 41 | { | ||
| 42 | $x += 1; | ||
| 43 | } | ||
| 44 | }); | ||
| 45 | ok(($x->aspdl == 104)->all); | ||
| 46 | =cut | ||
| 47 | |||
| 48 | =head2 set_bulk_size | ||
| 49 | |||
| 50 | Set size limit on bulk execution. | ||
| 51 | |||
| 52 | Bulk execution bundles many operators to run together. | ||
| 53 | This can improve performance when running a lot of small | ||
| 54 | operators sequentially. | ||
| 55 | |||
| 56 | Parameters | ||
| 57 | ---------- | ||
| 58 | $size : int | ||
| 59 | Maximum number of operators that can be bundled in a bulk. | ||
| 60 | |||
| 61 | Returns | ||
| 62 | ------- | ||
| 63 | int | ||
| 64 | Previous bulk size. | ||
| 65 | =cut | ||
| 66 | |||
| 67 | method set_bulk_size(Int $size) | ||
| 68 | { | ||
| 69 | return scalar(check_call(AI::MXNetCAPI::EngineSetBulkSize($size))); | ||
| 70 | } | ||
| 71 | |||
| 72 | |||
| 73 | =head2 bulk | ||
| 74 | |||
| 75 | Bulk execution bundles many operators to run together. | ||
| 76 | This can improve performance when running a lot of small | ||
| 77 | operators sequentially. | ||
| 78 | |||
| 79 | Parameters | ||
| 80 | ---------- | ||
| 81 | $size : int | ||
| 82 | Maximum number of operators that can be bundled in a bulk. | ||
| 83 | $sub: CodeRef to execute | ||
| 84 | |||
| 85 | my $x; | ||
| 86 | mx->engine->bulk(10, sub { | ||
| 87 | $x = mx->nd->zeros([1]); | ||
| 88 | for my $i (1..100) | ||
| 89 | { | ||
| 90 | $x += 1; | ||
| 91 | } | ||
| 92 | }); | ||
| 93 | =cut | ||
| 94 | |||
| 95 | method bulk(Int $size, CodeRef $sub) | ||
| 96 | { | ||
| 97 | my $prev = __PACKAGE__->set_bulk_size($size); | ||
| 98 | eval { $sub->() }; | ||
| 99 | my $err = $@; | ||
| 100 | __PACKAGE__->set_bulk_size($prev) unless $prev == $size; | ||
| 101 | Carp::confess($err) if $err; | ||
| 102 | } | ||
| 103 | |||
| 104 | 1; |