]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - tidl/tidl-api.git/blob - examples/common/utils.cpp
c45acc6b04f3701b0df0ddc92e55b3d86679f800
[tidl/tidl-api.git] / examples / common / utils.cpp
1 /******************************************************************************
2  * Copyright (c) 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 #include <boost/format.hpp>
30 #include <cstring>
32 #include "utils.h"
34 using namespace tidl;
36 using boost::format;
37 using std::string;
38 using std::istream;
39 using std::ostream;
41 static bool read_frame_helper(char* ptr, size_t size, istream& input_file);
43 bool ReadFrame(ExecutionObject*     eo,
44                int                  frame_idx,
45                const Configuration& configuration,
46                std::istream&        input_file)
47 {
48     if (frame_idx >= configuration.numFrames)
49         return false;
51     // Note: Frame index is used by the EO for debug messages only
52     eo->SetFrameIndex(frame_idx);
54     return read_frame_helper(eo->GetInputBufferPtr(),
55                              eo->GetInputBufferSizeInBytes(),
56                              input_file);
57 }
59 bool ReadFrame(ExecutionObjectPipeline* eop,
60                int                      frame_idx,
61                const Configuration&     configuration,
62                std::istream&            input_file)
63 {
64     if (frame_idx >= configuration.numFrames)
65         return false;
67     // Note: Frame index is used by the EOP for debug messages only
68     eop->SetFrameIndex(frame_idx);
70     return read_frame_helper(eop->GetInputBufferPtr(),
71                              eop->GetInputBufferSizeInBytes(),
72                              input_file);
73 }
75 bool read_frame_helper(char* ptr, size_t size, istream& input_file)
76 {
77     assert (ptr != nullptr);
78     assert (input_file.good());
80     input_file.read(ptr, size);
81     assert (input_file.good());
83     if (input_file.eof())
84         return false;
86     // Wrap-around : if EOF is reached, start reading from the beginning.
87     if (input_file.peek() == EOF)
88         input_file.seekg(0, input_file.beg);
90     if (input_file.good())
91         return true;
93     return false;
94 }
97 bool WriteFrame(const ExecutionObject* eo, ostream& output_file)
98 {
99     output_file.write(eo->GetOutputBufferPtr(),
100                       eo->GetOutputBufferSizeInBytes());
101     assert(output_file.good() == true);
103     if (output_file.good())
104         return true;
106     return false;
109 void ReportTime(const ExecutionObject* eo)
111     double elapsed_host   = eo->GetHostProcessTimeInMilliSeconds();
112     double elapsed_device = eo->GetProcessTimeInMilliSeconds();
113     double overhead = 100 - (elapsed_device/elapsed_host*100);
115     std::cout << format("frame[%3d]: Time on %s: %4.2f ms, host: %4.2f ms"
116                         " API overhead: %2.2f %%\n")
117                         % eo->GetFrameIndex() % eo->GetDeviceName()
118                         % elapsed_device % elapsed_host % overhead;
121 void ReportTime(const ExecutionObjectPipeline* eop)
123     double elapsed_host   = eop->GetHostProcessTimeInMilliSeconds();
124     double elapsed_device = eop->GetProcessTimeInMilliSeconds();
125     double overhead = 100 - (elapsed_device/elapsed_host*100);
127     std::cout << format("frame[%3d]: Time on %s: %4.2f ms, host: %4.2f ms"
128                         " API overhead: %2.2f %%\n")
129                         % eop->GetFrameIndex() % eop->GetDeviceName()
130                         % elapsed_device % elapsed_host % overhead;
133 // Compare output against reference output
134 bool CheckFrame(const ExecutionObject *eo, const char *ref_output)
136     if (std::memcmp(static_cast<const void*>(ref_output),
137                static_cast<const void*>(eo->GetOutputBufferPtr()),
138                eo->GetOutputBufferSizeInBytes()) == 0)
139         return true;
141     return false;
144 bool CheckFrame(const ExecutionObjectPipeline *eop, const char *ref_output)
146     if (std::memcmp(static_cast<const void*>(ref_output),
147                static_cast<const void*>(eop->GetOutputBufferPtr()),
148                eop->GetOutputBufferSizeInBytes()) == 0)
149         return true;
151     return false;
155 namespace tidl {
156 std::size_t GetBinaryFileSize (const std::string &F);
157 bool        ReadBinary        (const std::string &F, char* buffer, int size);
160 // Read file into a buffer.
161 const char* ReadReferenceOutput(const string& name)
163     size_t size = GetBinaryFileSize(name);
165     if (size == 0)
166         return nullptr;
168     char* buffer = new char[size];
170     if (!buffer)
171         return nullptr;
173     if (!ReadBinary(name, buffer, size))
174     {
175         delete [] buffer;
176         return nullptr;
177     }
179     return buffer;