ArmNN
 24.08
ClContextSerializer.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2020 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
8 
9 #include <armnn/Exceptions.hpp>
10 #include <armnn/Logging.hpp>
12 
13 #include <fmt/format.h>
14 
15 #include <cstdlib>
16 #include <fstream>
17 #include <iostream>
18 #include <vector>
19 
20 namespace armnn
21 {
22 
23 void ClContextSerializer::Serialize(const arm_compute::CLCompileContext& clCompileContext)
24 {
25  // Get map of built programs from clCompileContext
26  std::map<std::string, cl::Program> builtProgramsMap = clCompileContext.get_built_programs();
27  if (builtProgramsMap.empty())
28  {
29  ARMNN_LOG(warning) << "There are no built programs to be serialised.";
30  return;
31  }
32 
33  // Create Flatbuffer CL Programs
34  std::vector<flatbuffers::Offset<armnn::Program>> clPrograms;
35  for(const auto& program : builtProgramsMap)
36  {
37  std::vector<std::vector<uint8_t>> binaries = program.second.getInfo<CL_PROGRAM_BINARIES>();
38  clPrograms.push_back(CreateProgram(m_FlatBufferBuilder,
39  m_FlatBufferBuilder.CreateString(program.first),
40  m_FlatBufferBuilder.CreateVector(binaries[0])));
41  }
42 
43  // Create Flatbuffer CLContext
44  auto clContext = CreateClContext(m_FlatBufferBuilder, m_FlatBufferBuilder.CreateVector(clPrograms));
45 
46  m_FlatBufferBuilder.Finish(clContext);
47 }
48 
50 {
51  // Write to a stream
52  auto bytesToWrite = armnn::numeric_cast<std::streamsize>(m_FlatBufferBuilder.GetSize());
53  stream.write(reinterpret_cast<const char*>(m_FlatBufferBuilder.GetBufferPointer()), bytesToWrite);
54  return !stream.bad();
55 }
56 
57 } // namespace armnn
ClContextSchema_generated.h
armnn::CreateProgram
flatbuffers::Offset< Program > CreateProgram(flatbuffers::FlatBufferBuilder &_fbb, flatbuffers::Offset< flatbuffers::String > name=0, flatbuffers::Offset< flatbuffers::Vector< uint8_t >> binary=0)
Definition: ClContextSchema_generated.h:118
armnn::CreateClContext
flatbuffers::Offset< ClContext > CreateClContext(flatbuffers::FlatBufferBuilder &_fbb, flatbuffers::Offset< flatbuffers::Vector< flatbuffers::Offset< armnn::Program >>> programs=0)
Definition: ClContextSchema_generated.h:57
NumericCast.hpp
ARMNN_LOG
#define ARMNN_LOG(severity)
Definition: Logging.hpp:212
armnn::ClContextSerializer::Serialize
void Serialize(const arm_compute::CLCompileContext &clCompileContext)
Serializes the CLCompileContext built-in programs.
Definition: ClContextSerializer.cpp:23
Logging.hpp
Exceptions.hpp
armnn
Copyright (c) 2021 ARM Limited and Contributors.
Definition: 01_00_quick_start.dox:6
armnn::BoostLogSeverityMapping::warning
@ warning
armnn::ClContextSerializer::SaveSerializedToStream
bool SaveSerializedToStream(std::ostream &stream)
Serializes the ClContext to the stream.
Definition: ClContextSerializer.cpp:49
ClContextSerializer.hpp