clDNN
chapter_4.cpp
1 /*
2 // Copyright (c) 2017 Intel Corporation
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 
17 #include <../api/CPP/cldnn_defs.h>
18 #include <../api/CPP/engine.hpp>
19 #include <../api/CPP/input_layout.hpp>
20 #include <../api/CPP/memory.hpp>
21 #include <../api/CPP/data.hpp>
22 #include <../api/CPP/topology.hpp>
23 #include <../api/CPP/network.hpp>
24 #include <iostream>
25 
26 #include "helper_functions.h"
27 
38 using namespace cldnn;
39 
40 
41 void chapter_4(engine& engine, topology& topology)
42 {
43 
44  std::cout << std::endl << "-- Chapter 4 --" << std::endl;
45 
46  // To get access to intermediate results of our network. To get special features we need to set custom building options:
47  build_options build_opt;
48  // Prepare vector of primitives that we want to have as an output:
49  std::vector<cldnn::primitive_id> outputs_list(0);
50  // Put every primitive from topology into this container:
51  for (auto prim_id : topology.get_primitive_ids())
52  outputs_list.push_back(prim_id);
53  // Note: output from get_primitive_ids() can be used directly as a parameter in building option.
54  // Set option.
55  build_opt.set_option(build_option::outputs(outputs_list));
56  // Add build options to network build.
57  network network(engine, topology, build_opt);
58  // We are almost ready to go. Need to create and set input for network:
59  memory input_prim = memory::allocate(engine, { data_types::f32, format::bfyx,{ 1, 1, 3, 1 } });
60  set_values(input_prim, { -3.0f, -2.0f, 2.5f });
61  // Set input.
62  network.set_input_data("input", input_prim);
63  // Ready to go:
64  auto outputs = network.execute();
65 
66  for (auto& it : outputs)
67  {
68  // Print id and output values.
69  std::cout << it.first << std::endl;
70  auto mem_pointer = it.second.get_memory().pointer<float>();
71  for (auto i : mem_pointer)
72  {
73  std::cout << i << " ";
74  }
75  std::cout << std::endl;
76  }
77 }
Represents program build options list.
Definition: program.hpp:399
std::map< primitive_id, network_output > execute(const std::vector< event > &dependencies={}) const
Executes network and returns the list of network_output.
Definition: network.hpp:246
void set_input_data(const primitive_id &id, const memory &mem) const
Provides memory for input_layout primitives defined by user in source topology.
Definition: network.hpp:122
void set_option(std::shared_ptr< const build_option > opt)
Adds or replace option to the options list.
Definition: program.hpp:403
User selected list of program outputs.
Network topology to be defined by user.
Definition: topology.hpp:33
static memory allocate(const engine &engine, const layout &layout)
Allocate memory on engine using specified layout.
Definition: memory.hpp:50
the most common format for activations in clDNN.
Definition: tensor.hpp:81
static std::shared_ptr< const build_option > outputs(const std::vector< primitive_id > &outs)
User selected list of program outputs.
Executable network allocated from program.
Definition: network.hpp:59
Represents clDNN engine object.
Definition: engine.hpp:110
Represents buffer with particular layout.
Definition: memory.hpp:42