]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - tidl/tidl-api.git/blob - examples/two_eo_per_frame/main.cpp
Video input option and document update
[tidl/tidl-api.git] / examples / two_eo_per_frame / main.cpp
1 /******************************************************************************
2  * Copyright (c) 2017-2018  Texas Instruments Incorporated - http://www.ti.com/
3  *  All rights reserved.
4  *
5  *  Redistribution and use in source and binary forms, with or without
6  *  modification, are permitted provided that the following conditions are met:
7  *      * Redistributions of source code must retain the above copyright
8  *        notice, this list of conditions and the following disclaimer.
9  *      * Redistributions in binary form must reproduce the above copyright
10  *        notice, this list of conditions and the following disclaimer in the
11  *        documentation and/or other materials provided with the distribution.
12  *      * Neither the name of Texas Instruments Incorporated nor the
13  *        names of its contributors may be used to endorse or promote products
14  *        derived from this software without specific prior written permission.
15  *
16  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26  *  THE POSSIBILITY OF SUCH DAMAGE.
27  *****************************************************************************/
29 //
30 // This example illustrates using multiple EOs to process a single frame
31 // For details, refer http://downloads.ti.com/mctools/esd/docs/tidl-api/
32 //
33 #include <signal.h>
34 #include <iostream>
35 #include <fstream>
36 #include <cassert>
37 #include <string>
39 #include "executor.h"
40 #include "execution_object.h"
41 #include "execution_object_pipeline.h"
42 #include "configuration.h"
43 #include "utils.h"
45 using namespace tidl;
46 using std::string;
47 using std::unique_ptr;
48 using std::vector;
50 using EOP = tidl::ExecutionObjectPipeline;
52 bool Run(int num_eve,int num_dsp, const char* ref_output);
54 Executor* CreateExecutor(DeviceType dt, int num, const Configuration& c,
55                          int layer_group_id);
58 int main(int argc, char *argv[])
59 {
60     // Catch ctrl-c to ensure a clean exit
61     signal(SIGABRT, exit);
62     signal(SIGTERM, exit);
64     // This example requires both EVE and C66x
65     uint32_t num_eve = Executor::GetNumDevices(DeviceType::EVE);
66     uint32_t num_dsp = Executor::GetNumDevices(DeviceType::DSP);
67     if (num_eve == 0 || num_dsp == 0)
68     {
69         std::cout << "TI DL not supported on this SoC." << std::endl;
70         return EXIT_SUCCESS;
71     }
73     string ref_file ="../test/testvecs/reference/j11_v2_ref.bin";
74     unique_ptr<const char> reference_output(ReadReferenceOutput(ref_file));
76     bool status = Run(num_eve, num_dsp, reference_output.get());
78     if (!status)
79     {
80         std::cout << "FAILED" << std::endl;
81         return EXIT_FAILURE;
82     }
84     std::cout << "PASSED" << std::endl;
85     return EXIT_SUCCESS;
86 }
88 bool Run(int num_eve, int num_dsp, const char* ref_output)
89 {
90     string config_file ="../test/testvecs/config/infer/tidl_config_j11_v2.txt";
92     Configuration c;
93     if (!c.ReadFromFile(config_file))
94         return false;
96     // Heap sizes for this network determined using Configuration::showHeapStats
97     c.PARAM_HEAP_SIZE   = (3 << 20); // 3MB
98     c.NETWORK_HEAP_SIZE = (20 << 20); // 20MB
100     c.numFrames = 16;
102     // Assign layers 12, 13 and 14 to layer group 2
103     c.layerIndex2LayerGroupId = { {12, 2}, {13, 2}, {14, 2} };
105     // Open input file for reading
106     std::ifstream input(c.inData, std::ios::binary);
108     bool status = true;
109     try
110     {
111         // Create Executors - use all the DSP and EVE cores available
112         // Layer group 1 will be executed on EVE, 2 on DSP
113         unique_ptr<Executor> eve(CreateExecutor(DeviceType::EVE,num_eve,c,1));
114         unique_ptr<Executor> dsp(CreateExecutor(DeviceType::DSP,num_dsp,c,2));
116         // Create pipelines. Each pipeline has 1 EVE and 1 DSP. If there are
117         // more EVEs than DSPs, the DSPs are shared across multiple
118         // pipelines. E.g.
119         // 2 EVE, 2 DSP: EVE1 -> DSP1, EVE2 -> DSP2
120         // 4 EVE, 2 DSP: EVE1 -> DSP1, EVE2 -> DSP2, EVE3 -> DSP1, EVE4 ->DSP2
121         std::vector<EOP *> EOPs;
122         uint32_t num_pipe = std::max(num_eve, num_dsp);
123         for (uint32_t i = 0; i < num_pipe; i++)
124               EOPs.push_back(new EOP( { (*eve)[i % num_eve],
125                                         (*dsp)[i % num_dsp] } ));
127         AllocateMemory(EOPs);
129         // Process frames with EOs in a pipelined manner
130         // additional num_eos iterations to flush the pipeline (epilogue)
131         int num_eops = EOPs.size();
132         for (int frame_idx = 0; frame_idx < c.numFrames + num_eops; frame_idx++)
133         {
134             EOP* eop = EOPs[frame_idx % num_eops];
136             // Wait for previous frame on the same eo to finish processing
137             if (eop->ProcessFrameWait())
138             {
139                 ReportTime(eop);
141                 // The reference output is valid only for the first frame
142                 // processed on each EOP
143                 if (frame_idx < num_eops && !CheckFrame(eop, ref_output))
144                     status = false;
145             }
147             // Read a frame and start processing it with current eo
148             if (ReadFrame(eop, frame_idx, c, input))
149                 eop->ProcessFrameStartAsync();
150         }
152         FreeMemory(EOPs);
154     }
155     catch (tidl::Exception &e)
156     {
157         std::cerr << e.what() << std::endl;
158         status = false;
159     }
161     input.close();
163     return status;
166 // Create an Executor with the specified type and number of EOs
167 Executor* CreateExecutor(DeviceType dt, int num, const Configuration& c,
168                          int layer_group_id)
170     if (num == 0) return nullptr;
172     DeviceIds ids;
173     for (int i = 0; i < num; i++)
174         ids.insert(static_cast<DeviceId>(i));
176     return new Executor(dt, ids, c, layer_group_id);