source: CIVL/examples/compare/provesa/ADFirstAidKit/profile/reader.cc

main
Last change on this file was ea777aa, checked in by Alex Wilton <awilton@…>, 3 years ago

Moved examples, include, build_default.properties, common.xml, and README out from dev.civl.com into the root of the repo.

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@5704 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 5.0 KB
Line 
1#include "reader.h"
2#include "string-utils.h"
3#include <cstdio>
4#include <stdexcept>
5#include <map>
6
7// Real impl
8using namespace std;
9struct TapenadeLogReaderImpl {
10 FILE *file;
11 int file_length;
12 map<int,string> function_id_2_name;
13 ~TapenadeLogReaderImpl() {
14 if (file)
15 fclose(file);
16 }
17};
18template <typename T> static T read(TapenadeLogReaderImpl *impl) {
19 T data;
20 fread(&data, 1, sizeof(T), impl->file);
21 return data;
22}
23template <> static inline string read(TapenadeLogReaderImpl *impl) {
24 unsigned int len = read<unsigned int>(impl);
25 char *buffer = new char[len];
26 fread(buffer, 1, len, impl->file);
27 string ret(buffer, len);
28 delete[] buffer;
29 return ret;
30}
31template <> static inline TapenadeLogEvent read(TapenadeLogReaderImpl *impl) {
32 unsigned int function_id = read<unsigned int>(impl);
33 string &function_name = impl->function_id_2_name[function_id];
34 unsigned int kind, size;
35 unsigned long long int time;
36 kind = read<unsigned int>(impl);
37 time = read<unsigned long long int>(impl);
38 size = read<unsigned int>(impl);
39 return TapenadeLogEvent(function_name, kind, time, size);
40}
41static inline bool eof(TapenadeLogReaderImpl *impl) {
42 int curPos = ftell(impl->file);
43 return (curPos >= impl->file_length);
44}
45
46static void initialize(TapenadeLogReaderImpl *&impl, const string &filename, bool &ok, bool &init) throw(runtime_error) {
47 if ((! init) && ok) {
48 impl = new TapenadeLogReaderImpl();
49 impl->file = fopen(filename.c_str(), "r");
50 if (! impl->file) {
51 ok = false;
52 throw new runtime_error("Can't open "+
53 filename+
54 "for reading");
55 }
56 fseek(impl->file, 0, SEEK_END);
57 impl->file_length = ftell(impl->file);
58 rewind(impl->file);
59 // Read functions ids
60 unsigned int hashtable_count;
61 hashtable_count = read<unsigned int>(impl);
62 while (hashtable_count--) {
63 unsigned int function_id = read<unsigned int>(impl);
64 impl->function_id_2_name[function_id] =
65 read<string>(impl);
66 }
67 init = true;
68 }
69}
70
71// Interface
72#define TapenadeLogReaderTemplate(type, name) \
73type TapenadeLogReader::name() throw(runtime_error) {\
74 if (! initialized)\
75 initialize(impl, filename, ok, initialized);\
76 if (ok)\
77 return read<type>(impl);\
78 else \
79 throw runtime_error("End of file reached in \""+\
80 filename+"\"");\
81}
82bool TapenadeLogReader::eof() throw(runtime_error) {
83 if (! initialized)
84 initialize(impl, filename, ok, initialized);
85 if (ok)
86 return ::eof(impl);
87 else
88 throw runtime_error("End of file reached in \""+
89 filename+"\"");
90}
91TapenadeLogReader::~TapenadeLogReader() {
92 if (impl) {
93 delete impl;
94 }
95}
96
97TapenadeLogReaderTemplate(TapenadeLogEvent, readEvent);
98
99// Interface for ReadingOnlyInAdjointBranch
100ReadingOnlyInAdjointBranch::ReadingOnlyInAdjointBranch(const std::string &filename)
101{
102 unread = false;
103 done =false;
104 adjoint_branch = false;
105 reader = new TapenadeLogReader(filename);
106}
107ReadingOnlyInAdjointBranch::~ReadingOnlyInAdjointBranch() {
108 if (unreadEvent)
109 delete unreadEvent;
110 if (reader)
111 delete reader;
112}
113
114bool ReadingOnlyInAdjointBranch::eof() throw(runtime_error)
115{
116 if (adjoint_branch)
117 return reader->eof();
118 if (done)
119 return true;
120
121 while (! reader->eof()) {
122 TapenadeLogEvent event(reader->readEvent());
123 string suffix = last_part_after(event.function_name, '_');
124 if (suffix == "b" || suffix == "fwd") {
125 adjoint_branch_first_function_name = event.function_name;
126 adjoint_branch_first_function_recursion = 1;
127 unreadEvent = new TapenadeLogEvent(event);
128 unread = true;
129 adjoint_branch = true;
130 return false;
131 }
132 }
133 return true;
134}
135TapenadeLogEvent ReadingOnlyInAdjointBranch::readEvent() throw(runtime_error)
136{
137 if (! adjoint_branch)
138 throw runtime_error("readEvent called without verifying eof!");
139 if (unread) {
140 TapenadeLogEvent event(*unreadEvent);
141 delete unreadEvent;
142 unreadEvent = 0;
143 unread = false;
144 return event;
145 }
146 TapenadeLogEvent event(reader->readEvent());
147 if (event.function_name == adjoint_branch_first_function_name) {
148 if (event.kind == END)
149 if (adjoint_branch_first_function_recursion == 1) {
150 adjoint_branch = false;
151 done = true;
152 } else
153 adjoint_branch_first_function_recursion--;
154 if (event.kind == BEGIN)
155 adjoint_branch_first_function_recursion++;
156 }
157 return event;
158}
Note: See TracBrowser for help on using the repository browser.