a4c978b94f2258e2b6ca759772ea94a39ee6b05a
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;
39 bool ReadFrame(ExecutionObject* eo, int frame_idx,
40 const Configuration& configuration,
41 std::istream& input_file)
42 {
43 if (frame_idx >= configuration.numFrames)
44 return false;
46 assert (eo->GetInputBufferPtr() != nullptr);
47 assert (input_file.good());
49 input_file.read(eo->GetInputBufferPtr(),
50 eo->GetInputBufferSizeInBytes());
51 assert (input_file.good());
53 if (input_file.eof())
54 return false;
56 // Note: Frame index is used by the EO for debug messages only
57 eo->SetFrameIndex(frame_idx);
59 // Wrap-around : if EOF is reached, start reading from the beginning.
60 if (input_file.peek() == EOF)
61 input_file.seekg(0, input_file.beg);
63 if (input_file.good())
64 return true;
66 return false;
67 }
69 bool WriteFrame(const ExecutionObject* eo, std::ostream& output_file)
70 {
71 output_file.write(eo->GetOutputBufferPtr(),
72 eo->GetOutputBufferSizeInBytes());
73 assert(output_file.good() == true);
75 if (output_file.good())
76 return true;
78 return false;
79 }
81 void ReportTime(const ExecutionObject* eo)
82 {
83 double elapsed_host = eo->GetHostProcessTimeInMilliSeconds();
84 double elapsed_device = eo->GetProcessTimeInMilliSeconds();
85 double overhead = 100 - (elapsed_device/elapsed_host*100);
87 std::cout << format("frame[%3d]: Time on %s: %4.2f ms, host: %4.2f ms"
88 " API overhead: %2.2f %%\n")
89 % eo->GetFrameIndex() % eo->GetDeviceName()
90 % elapsed_device % elapsed_host % overhead;
91 }
93 // Compare output against reference output
94 bool CheckFrame(const ExecutionObject *eo, const char *ref_output)
95 {
96 if (std::memcmp(static_cast<const void*>(ref_output),
97 static_cast<const void*>(eo->GetOutputBufferPtr()),
98 eo->GetOutputBufferSizeInBytes()) == 0)
99 return true;
101 return false;
102 }
105 namespace tidl {
106 std::size_t GetBinaryFileSize (const std::string &F);
107 bool ReadBinary (const std::string &F, char* buffer, int size);
108 }
110 // Read file into a buffer.
111 const char* ReadReferenceOutput(const string& name)
112 {
113 size_t size = GetBinaryFileSize(name);
115 if (size == 0)
116 return nullptr;
118 char* buffer = new char[size];
120 if (!buffer)
121 return nullptr;
123 if (!ReadBinary(name, buffer, size))
124 {
125 delete [] buffer;
126 return nullptr;
127 }
129 return buffer;
130 }