This example is basically a copy of the SimpleSample example.
This example is basically a copy of the SimpleSample example. But it makes use of a CustomAllocator to allocate memory for the inputs, outputs and inter layer memory.
#include <arm_compute/core/CL/CLKernelLibrary.h>
#include <arm_compute/runtime/CL/CLScheduler.h>
#include <iostream>
{
public:
SampleClBackendCustomAllocator() = default;
void*
allocate(
size_t size,
size_t alignment)
override
{
if (alignment == 0)
{
alignment = arm_compute::CLKernelLibrary::get().get_device().getInfo<CL_DEVICE_GLOBAL_MEM_CACHELINE_SIZE>();
}
size_t space = size + alignment + alignment;
auto allocatedMemPtr = std::malloc(space * sizeof(size_t));
if (std::align(alignment, size, allocatedMemPtr, space) == nullptr)
{
}
return allocatedMemPtr;
}
void free(
void* ptr)
override
{
std::free(ptr);
}
{
}
};
int main()
{
float number;
std::cout << "Please enter a number: " << std::endl;
std::cin >> number;
float weightsData[] = {1.0f};
network->AddFullyConnectedLayer(fullyConnectedDesc, "fully connected");
auto customAllocator = std::make_shared<SampleClBackendCustomAllocator>();
size_t totalBytes = numElements * sizeof(float);
Optimize(*network, {
"GpuAcc"}, runtime->GetDeviceSpec(), optOptions);
if (!optNet)
{
std::cerr << "Error: Failed to optimise the input network." << std::endl;
return 1;
}
std::string ignoredErrorMessage;
runtime->LoadNetwork(networkIdentifier, std::move(optNet), ignoredErrorMessage, networkProperties);
const size_t alignment =
arm_compute::CLKernelLibrary::get().get_device().getInfo<CL_DEVICE_GLOBAL_MEM_CACHELINE_SIZE>();
auto* inputPtr = reinterpret_cast<float*>(alignedInputPtr);
std::fill_n(inputPtr, numElements, number);
auto* outputPtr = reinterpret_cast<float*>(alignedOutputPtr);
std::fill_n(outputPtr, numElements, -10.0f);
inputTensorInfo = runtime->GetInputTensorInfo(networkIdentifier, 0);
{
};
{
{0,
Tensor(runtime->GetOutputTensorInfo(networkIdentifier, 0), alignedOutputPtr)}
};
runtime->EnqueueWorkload(networkIdentifier, inputTensors, outputTensors);
arm_compute::CLScheduler::get().sync();
auto* outputResult = reinterpret_cast<float*>(alignedOutputPtr);
std::cout << "Your number was " << outputResult[0] << std::endl;
runtime->UnloadNetwork(networkIdentifier);
return 0;
}
A tensor defined by a TensorInfo (shape and data type) and an immutable backing store.
Base class for all ArmNN exceptions so that users can filter to just those.
Interface for a layer that is connectable to other layers via InputSlots and OutputSlots.
virtual const IInputSlot & GetInputSlot(unsigned int index) const =0
Get a const input slot handle by slot index.
virtual const IOutputSlot & GetOutputSlot(unsigned int index) const =0
Get the const output slot handle by slot index.
Custom Allocator interface.
virtual void * allocate(size_t size, size_t alignment)=0
Interface to be implemented by the child class to allocate bytes.
virtual void free(void *ptr)=0
Interface to be implemented by the child class to free the allocated bytes.
virtual armnn::MemorySource GetMemorySourceType()=0
Used to specify what type of memory is being allocated by this allocator.
static INetworkPtr Create(const NetworkOptions &networkOptions={})
virtual void SetTensorInfo(const TensorInfo &tensorInfo)=0
virtual int Connect(IInputSlot &destination)=0
static IRuntimePtr Create(const CreationOptions &options)
void SetImportEnabled(bool ImportState)
A tensor defined by a TensorInfo (shape and data type) and a mutable backing store.
unsigned int GetNumElements() const
void SetConstant(const bool IsConstant=true)
Marks the data corresponding to this tensor info as constant.
Copyright (c) 2021 ARM Limited and Contributors.
MemorySource
Define the Memory Source to reduce copies.
std::unique_ptr< IRuntime, void(*)(IRuntime *runtime)> IRuntimePtr
std::unique_ptr< IOptimizedNetwork, void(*)(IOptimizedNetwork *network)> IOptimizedNetworkPtr
std::vector< std::pair< LayerBindingId, class ConstTensor > > InputTensors
IOptimizedNetworkPtr Optimize(const INetwork &network, const std::vector< BackendId > &backendPreferences, const IDeviceSpec &deviceSpec, const OptimizerOptionsOpaque &options=OptimizerOptionsOpaque(), Optional< std::vector< std::string > & > messages=EmptyOptional())
Create an optimized version of the network.
void ConfigureLogging(bool printToStandardOutput, bool printToDebugOutput, LogSeverity severity)
Configures the logging behaviour of the ARMNN library.
std::unique_ptr< INetwork, void(*)(INetwork *network)> INetworkPtr
std::vector< std::pair< LayerBindingId, class Tensor > > OutputTensors
A FullyConnectedDescriptor for the FullyConnectedLayer.
std::map< BackendId, std::shared_ptr< ICustomAllocator > > m_CustomAllocatorMap
A map to define a custom memory allocator for specific backend Ids.