Publishing R3
[platform/upstream/dldt.git] / inference-engine / thirdparty / clDNN / tutorial / 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>
23 /*! @page c1 Engine, layout, tensor, memory, data and input
24 * @section intro Introduction
25 * In this chapter we will explain how to create engine, define and allocate memory. What is and how to use: tensor, layout, input_layout and data.
26 *
27 * @include chapter_1.cpp
28
29 *
30 */
31
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:
49     format::type format = format::byxf;
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 }