clDNN
chapter_1.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 #include <../api/CPP/cldnn_defs.h>
17 #include <../api/CPP/engine.hpp>
18 #include <../api/CPP/memory.hpp>
19 #include <../api/CPP/tensor.hpp>
20 #include <../api/CPP/input_layout.hpp>
21 #include <../api/CPP/data.hpp>
22 #include <iostream>
32 using namespace cldnn;
33 
34 engine chapter_1()
35 {
36 
37  std::cout << std::endl << "-- Chapter 1 --" << std::endl;
38  // To create memory we have to create engine first. Engine is responsible for memory and kernel handling (creation, compilation, allocation).
39  // Currently OCL backend implementation only is available.
40 
41  // Add profiling information
42  const bool profiling = true;
43 
44  // Create an engine
45  engine engine(profiling);
46  // We have to choose data type (f32 or f16):
47  data_types data_type = data_types::f32;
48  // Format (order of dimensions in memory), bfyx is the most optimal and common:
50 
51  // Before memory allocation we have to create tensor that describes memory size. We can do it in serveral ways:
52  tensor tensor1(
53  4, // batches
54  1, // features
55  32, // width (spatial x)
56  32); // height (spatial y)
57 
58  tensor tensor2(spatial(32, 32), batch(4), feature(1));
59  tensor tensor3(spatial(32, 32), batch(4)); // default value for non-initialized dimension is 1
60 
61  std::cout << "Is tensor1 == tensor2 == tensor3?:" <<
62  (((tensor1 == tensor2) && (tensor2 == tensor3)) ? "yes" : "no") << std::endl;
63  std::cout << "print tensor:" << tensor1 << std::endl;
64 
65  // Now we are ready to create layout:
66  layout layout1(data_type, format, tensor1);
67  // which can be used to allocate memory for given engine:
68  memory memory1 = memory::allocate(engine, layout1);
69 
70  // Special type of layout is input layout. It is named layout. Name is a string with identifier of layout.
71  input_layout in_layout("input", layout1);
72 
73  // You can also give name to memory to create a data.
74  data data("named_memory", memory1);
75 
76  return engine;
77 }
N-dimensional vector. Mostly used to represent memory size.
Definition: tensor.hpp:256
Provides input data to topology.
Definition: data.hpp:36
data_types
Possible data types could be stored in memory.
Definition: layout.hpp:32
Provides input layout for a data to be passed later to network.
static memory allocate(const engine &engine, const layout &layout)
Allocate memory on engine using specified layout.
Definition: memory.hpp:50
Represents memory formats (orders). In CNN most of data is described as 4 dimensional blocks...
Definition: tensor.hpp:75
used in bitmaps, input from user i.e b images of RGB format
Definition: tensor.hpp:80
Represents clDNN engine object.
Definition: engine.hpp:110
Represents buffer with particular layout.
Definition: memory.hpp:42
Describes memory layout.
Definition: layout.hpp:223