| 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::Context; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use Mouse; | ||
| 22 | use AI::MXNet::NS; | ||
| 23 | use AI::MXNet::Base; | ||
| 24 | use AI::MXNet::Types; | ||
| 25 | use AI::MXNet::Function::Parameters; | ||
| 26 | use constant devtype2str => { 1 => 'cpu', 2 => 'gpu', 3 => 'cpu_pinned' }; | ||
| 27 | use constant devstr2type => { cpu => 1, gpu => 2, cpu_pinned => 3 }; | ||
| 28 | around BUILDARGS => sub { | ||
| 29 | my $orig = shift; | ||
| 30 | my $class = shift; | ||
| 31 | return $class->$orig(device_type => $_[0]) | ||
| 32 | if @_ == 1 and $_[0] =~ /^(?:cpu|gpu|cpu_pinned)$/; | ||
| 33 | return $class->$orig( | ||
| 34 | device_type => $_[0]->device_type, | ||
| 35 | device_id => $_[0]->device_id | ||
| 36 | ) if @_ == 1 and blessed $_[0]; | ||
| 37 | return $class->$orig(device_type => $_[0], device_id => $_[0]) | ||
| 38 | if @_ == 2 and $_[0] =~ /^(?:cpu|gpu|cpu_pinned)$/; | ||
| 39 | return $class->$orig(@_); | ||
| 40 | }; | ||
| 41 | |||
| 42 | has 'device_type' => ( | ||
| 43 | is => 'rw', | ||
| 44 | isa => enum([qw[cpu gpu cpu_pinned]]), | ||
| 45 | default => 'cpu' | ||
| 46 | ); | ||
| 47 | |||
| 48 | has 'device_type_id' => ( | ||
| 49 | is => 'rw', | ||
| 50 | isa => enum([1, 2, 3]), | ||
| 51 | default => sub { devstr2type->{ shift->device_type } }, | ||
| 52 | lazy => 1 | ||
| 53 | ); | ||
| 54 | |||
| 55 | has 'device_id' => ( | ||
| 56 | is => 'rw', | ||
| 57 | isa => 'Int', | ||
| 58 | default => 0 | ||
| 59 | ); | ||
| 60 | |||
| 61 | use overload | ||
| 62 | '==' => sub { | ||
| 63 | my ($self, $other) = @_; | ||
| 64 | return 0 unless blessed($other) and $other->isa(__PACKAGE__); | ||
| 65 | return "$self" eq "$other"; | ||
| 66 | }, | ||
| 67 | '""' => sub { | ||
| 68 | my ($self) = @_; | ||
| 69 | return sprintf("%s(%s)", $self->device_type, $self->device_id); | ||
| 70 | }, | ||
| 71 | fallback => 1; | ||
| 72 | =head1 NAME | ||
| 73 | |||
| 74 | AI::MXNet::Context - A device context. | ||
| 75 | =cut | ||
| 76 | |||
| 77 | =head1 DESCRIPTION | ||
| 78 | |||
| 79 | This class governs the device context of AI::MXNet::NDArray objects. | ||
| 80 | =cut | ||
| 81 | |||
| 82 | =head1 SYNOPSIS | ||
| 83 | |||
| 84 | use AI::MXNet qw(mx); | ||
| 85 | print nd->array([[1,2],[3,4]], ctx => mx->cpu)->aspdl; | ||
| 86 | my $arr_gpu = nd->random->uniform(shape => [10, 10], ctx => mx->gpu(0)); | ||
| 87 | =cut | ||
| 88 | |||
| 89 | =head2 | ||
| 90 | |||
| 91 | Constructing a context. | ||
| 92 | |||
| 93 | Parameters | ||
| 94 | ---------- | ||
| 95 | device_type : {'cpu', 'gpu'} or Context. | ||
| 96 | String representing the device type | ||
| 97 | |||
| 98 | device_id : int (default=0) | ||
| 99 | The device id of the device, needed for GPU | ||
| 100 | =cut | ||
| 101 | |||
| 102 | =head2 cpu | ||
| 103 | |||
| 104 | Returns a CPU context. | ||
| 105 | |||
| 106 | Parameters | ||
| 107 | ---------- | ||
| 108 | device_id : int, optional | ||
| 109 | The device id of the device. device_id is not needed for CPU. | ||
| 110 | This is included to make interface compatible with GPU. | ||
| 111 | |||
| 112 | Returns | ||
| 113 | ------- | ||
| 114 | context : AI::MXNet::Context | ||
| 115 | The corresponding CPU context. | ||
| 116 | =cut | ||
| 117 | |||
| 118 | method cpu(Int $device_id=0) | ||
| 119 | { | ||
| 120 | return $self->new(device_type => 'cpu', device_id => $device_id); | ||
| 121 | } | ||
| 122 | |||
| 123 | =head2 cpu_pinned | ||
| 124 | |||
| 125 | Returns a CPU pinned memory context. Copying from CPU pinned memory to GPU | ||
| 126 | is faster than from normal CPU memory. | ||
| 127 | |||
| 128 | Parameters | ||
| 129 | ---------- | ||
| 130 | device_id : int, optional | ||
| 131 | The device id of the device. `device_id` is not needed for CPU. | ||
| 132 | This is included to make interface compatible with GPU. | ||
| 133 | |||
| 134 | Returns | ||
| 135 | ------- | ||
| 136 | context : Context | ||
| 137 | The corresponding CPU pinned memory context. | ||
| 138 | =cut | ||
| 139 | |||
| 140 | method cpu_pinned(Int $device_id=0) | ||
| 141 | { | ||
| 142 | return $self->new(device_type => 'cpu_pinned', device_id => $device_id); | ||
| 143 | } | ||
| 144 | |||
| 145 | =head2 gpu | ||
| 146 | |||
| 147 | Returns a GPU context. | ||
| 148 | |||
| 149 | Parameters | ||
| 150 | ---------- | ||
| 151 | device_id : int, optional | ||
| 152 | |||
| 153 | Returns | ||
| 154 | ------- | ||
| 155 | context : AI::MXNet::Context | ||
| 156 | The corresponding GPU context. | ||
| 157 | =cut | ||
| 158 | |||
| 159 | method gpu(Int $device_id=0) | ||
| 160 | { | ||
| 161 | return $self->new(device_type => 'gpu', device_id => $device_id); | ||
| 162 | } | ||
| 163 | |||
| 164 | =head2 current_context | ||
| 165 | |||
| 166 | Returns the current context. | ||
| 167 | |||
| 168 | Returns | ||
| 169 | ------- | ||
| 170 | $default_ctx : AI::MXNet::Context | ||
| 171 | =cut | ||
| 172 | |||
| 173 | |||
| 174 | =head2 num_gpus | ||
| 175 | |||
| 176 | Query CUDA for the number of GPUs present. | ||
| 177 | |||
| 178 | Raises | ||
| 179 | ------ | ||
| 180 | Will raise an exception on any CUDA error. | ||
| 181 | |||
| 182 | Returns | ||
| 183 | ------- | ||
| 184 | count : int | ||
| 185 | The number of GPUs. | ||
| 186 | |||
| 187 | =cut | ||
| 188 | |||
| 189 | method num_gpus() | ||
| 190 | { | ||
| 191 | return scalar(check_call(AI::MXNetCAPI::GetGPUCount())); | ||
| 192 | } | ||
| 193 | |||
| 194 | =head2 gpu_memory_info | ||
| 195 | |||
| 196 | Query CUDA for the free and total bytes of GPU global memory. | ||
| 197 | |||
| 198 | Parameters | ||
| 199 | ---------- | ||
| 200 | $device_id=0 : int, optional | ||
| 201 | The device id of the GPU device. | ||
| 202 | |||
| 203 | Raises | ||
| 204 | ------ | ||
| 205 | Will raise an exception on any CUDA error. | ||
| 206 | |||
| 207 | Returns | ||
| 208 | ------- | ||
| 209 | ($free, $total) : (int, int) | ||
| 210 | Free and total memory in bytes. | ||
| 211 | =cut | ||
| 212 | |||
| 213 | method gpu_memory_info($device_id=0) | ||
| 214 | { | ||
| 215 | return check_call(AI::MXNetCAPI::GetGPUMemoryInformation64($device_id)); | ||
| 216 | } | ||
| 217 | |||
| 218 | method current_ctx() | ||
| 219 | { | ||
| 220 | return $AI::MXNet::Context; | ||
| 221 | } | ||
| 222 | |||
| 223 | method set_current(AI::MXNet::Context $current) | ||
| 224 | { | ||
| 225 | $AI::MXNet::Context = $current; | ||
| 226 | } | ||
| 227 | |||
| 228 | *current_context = \¤t_ctx; | ||
| 229 | |||
| 230 | method deepcopy() | ||
| 231 | { | ||
| 232 | return __PACKAGE__->new( | ||
| 233 | device_type => $self->device_type, | ||
| 234 | device_id => $self->device_id | ||
| 235 | ); | ||
| 236 | } | ||
| 237 | |||
| 238 | __PACKAGE__->AI::MXNet::NS::register('AI::MXNet'); | ||
| 239 | |||
| 240 | 1; |