MAGMA  magma-1.4.0
Matrix Algebra on GPU and Multicore Architectures
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
codegen.Conversion Class Reference

Public Member Functions

def __init__
 
def run
 
def convert_names
 
def export_data
 
def convert_data
 
def substitute
 
def convert
 

Data Fields

 content
 
 file
 
 date
 
 types
 
 precision
 
 precisions
 
 precstmp
 
 names
 
 dates
 
 converted
 

Static Public Attributes

 debug = False;
 
 test = False;
 
 make = False;
 
 prefix = None;
 
list required_precisions = []
 
list files_in = []
 
list files_out = []
 

Detailed Description

This class works on a single file to create generations
Static. Is the conversion in debug mode? More verbose.

Definition at line 66 of file codegen.py.

Constructor & Destructor Documentation

def codegen.Conversion.__init__ (   self,
  file = None,
  match = None,
  content = None 
)
Constructor that takes a file, match, and content.
@param file The file name of the input.
@param match The regular expression matches
@param content The ASCII content of the file.

Definition at line 85 of file codegen.py.

85 
86  def __init__(self, file = None, match = None, content = None):
87  """Constructor that takes a file, match, and content.
88  @param file The file name of the input.
89  @param match The regular expression matches
90  @param content The ASCII content of the file.
91  """
92  if file is None: return;
93  self.content = content;
94  file = path.realpath(file);
95  rel = relpath(file);
96  self.file = list(path.split(file));
97  self.date = path.getmtime(file);
98  if sys.platform!="win32" and path.samefile(path.join(self.file[0],self.file[1]),sys.argv[0]):
99  raise ValueError('Let\'s just forget codegen.py');
100  try:
101  """['normal','all','mixed'] for example. This(ese) are the replacement types to be used."""
102  self.types = match[0].split(',');
103  """'z' for example. This is the current file's `type`."""
104  self.precision = match[2].lower();
105  """['c','d','s'] for example. This is the current file's destination `types`."""
106  self.precisions = match[3].lower().split();
107  if len(self.required_precisions):
108  self.precstmp = [];
109  for prec in self.required_precisions:
110  if prec in self.precisions:
111  self.precstmp.append(prec);
112  self.precisions = self.precstmp;
113  except:
114  raise ValueError(path.join(self.file[0],self.file[1])+' : Invalid conversion string');
115  self.files_in.append(rel);
116 
def relpath
Definition: codegen.py:61
list required_precisions
Definition: codegen.py:79

Member Function Documentation

def codegen.Conversion.convert (   self,
  data,
  precision 
)
Select appropriate replacements for the current file.
@param data The content subject for the replacements.
@param precision The target precision for generation.

Definition at line 237 of file codegen.py.

References codegen.Conversion.substitute(), and codegen.Conversion.types.

238  def convert(self, data, precision):
239  """Select appropriate replacements for the current file.
240  @param data The content subject for the replacements.
241  @param precision The target precision for generation.
242  """
243  global KEYWORD, DONE_KEYWORD;
244  try:
245  """All files undergo the "all" replacements."""
246  data = self.substitute('all', data, precision);
247  except: pass;
248  for sub_type in self.types:
249  """For all the other types of conversion for the current file,
250  make the correct replacements."""
251  if sub_type == 'all': continue;
252  try:
253  data = self.substitute(sub_type, data, precision);
254  except Exception, e:
255  raise ValueError('I encountered an unrecoverable error while working in subtype:',sub_type+'.');
256  """Replace the replacement keywork with one that signifies this is an output file,
257  to prevent multiple replacement issues if run again."""
258  data = re.sub(KEYWORD+' '+','.join(self.types)+'.*', DONE_KEYWORD+' '+precision+' '+datetime.now().ctime(), data);
259  return data;

Here is the call graph for this function:

Here is the caller graph for this function:

def codegen.Conversion.convert_data (   self)
Convert the data in the files by making the 
appropriate replacements for each destination precision.

Definition at line 194 of file codegen.py.

References codegen.Conversion.content, codegen.Conversion.convert(), codegen.Conversion.dates, codegen.Conversion.names, and codegen.Conversion.precisions.

195  def convert_data(self):
196  """Convert the data in the files by making the
197  appropriate replacements for each destination precision."""
198  for i in range(len(self.precisions)):
199  precision = self.precisions[i];
200  name = self.names[i];
201  date = self.dates[i];
202  if name is not None and (date is None or date > 0):
203  self.converted.append(self.convert(self.content, precision));
204  else: self.converted.append(None);

Here is the call graph for this function:

Here is the caller graph for this function:

def codegen.Conversion.convert_names (   self)
Investigate file name and make appropriate changes.

Definition at line 124 of file codegen.py.

125  def convert_names(self):
126  """Investigate file name and make appropriate changes."""
127  self.names = [];
128  self.dates = [];
129  self.converted = [];
130  load = False;
131  if self.debug: print '|'.join(self.types), self.precision, relpath(path.join(self.file[0],self.file[1]));
132  for precision in self.precisions:
133  """For each destination precision, make the appropriate changes to the file name/data."""
134  new_file = self.convert(self.file[1], precision);
135  if self.debug: print precision,':',
136  if new_file <> self.file[1]:
137  """A filename change is required for the conversion to continue."""
138  if self.prefix is None:
139  """If no prefix is specified, use the file's current folder."""
140  prefix = self.file[0]
141  makeprefix = '';
142  else:
143  """If a prefix is specified, set it up."""
144  prefix = self.prefix;
145  makeprefix = '--prefix '+prefix;
146  """Where the destination file will reside."""
147  conversion = path.join(prefix, new_file);
148  file_out = relpath(conversion);
149  if self.make:
150  """If in GNU Make mode, write the rule to create the file."""
151  file_in = relpath(path.join(self.file[0],self.file[1]));
152  print file_out+':',file_in;
153  print "\t$(PYTHON)",path.realpath(sys.argv[0]),makeprefix,'-p',precision,"--file",file_in;
154  self.names.append(new_file);
155  self.files_out.append(file_out);
156  if self.debug: print relpath(conversion), ':',
157  try:
158  """Try to emulate Make like time based dependencies."""
159  date = path.getmtime(conversion);
160  diff = self.date - date;
161  self.dates.append(diff);
162  if self.debug:
163  if diff > 0: print 'Old',
164  else: print 'Current',
165  print diff;
166  if diff > 0: load = True;
167  except:
168  if self.debug: print 'Missing';
169  self.dates.append(None);
170  load = True;
171  else:
172  """There was no change in the file's name, thus,
173  no work can be done without overwriting the original."""
174  if self.debug: print '<No Change>',':';
175  else: print >> sys.stderr, new_file, 'had no change for', precision;
176  self.names.append(None);
177  self.dates.append(None);
178  return load;
def relpath
Definition: codegen.py:61

Here is the caller graph for this function:

def codegen.Conversion.export_data (   self)
After all of the conversions are complete, 
this will write the output file contents to the disk.

Definition at line 179 of file codegen.py.

References codegen.Conversion.converted, codegen.Conversion.file, codegen.Conversion.names, and codegen.Conversion.prefix.

180  def export_data(self):
181  """After all of the conversions are complete,
182  this will write the output file contents to the disk."""
183  for i in range(len(self.names)):
184  name = self.names[i];
185  data = self.converted[i];
186  if data is None or name is None: continue;
187  if self.prefix is None:
188  fd = open(path.join(self.file[0],name), 'w');
189  else:
190  fd = open(path.join(self.prefix,name), 'w');
191  fd.write(data);
192  fd.close();
193 

Here is the caller graph for this function:

def codegen.Conversion.run (   self)
Does the appropriate work, if in test mode, this is limited to only converting names.

Definition at line 117 of file codegen.py.

References codegen.Conversion.convert_data(), codegen.Conversion.convert_names(), codegen.Conversion.export_data(), and codegen.Conversion.test.

118  def run(self):
119  """Does the appropriate work, if in test mode, this is limited to only converting names."""
120  if self.convert_names() and not self.test:
121  """If not in test mode, actually make changes and export to disk."""
122  self.convert_data();
123  self.export_data();

Here is the call graph for this function:

def codegen.Conversion.substitute (   self,
  sub_type,
  data,
  precision 
)
This operates on a single replacement type.
@param sub_type The name of the replacement set.
@param data The content subject for replacments.
@param precision The target precision for replacements.

Definition at line 205 of file codegen.py.

References codegen.Conversion.precision.

206  def substitute(self, sub_type, data, precision):
207  """This operates on a single replacement type.
208  @param sub_type The name of the replacement set.
209  @param data The content subject for replacments.
210  @param precision The target precision for replacements.
211  """
212  try:
213  """Try to select the requested replacements."""
214  work = subs[sub_type];
215  prec_to = work[0].index(precision);
216  prec_from = work[0].index(self.precision);
217  except:
218  """If requested replacement type does not exist,
219  return unaltered contents."""
220  return data;
221  for i in range(1,len(work)):
222  """Requested replacements were found,
223  execute replacements for each entry."""
224  try:
225  search = work[i][prec_from];
226  replace = work[i][prec_to];
227  if not search: continue;
228  replace = replace.replace('\*','*');
229  if sub_type != 'tracing' :
230  replace = replace.replace('\(','(');
231  replace = replace.replace('\)',')');
232  data = re.sub(search, replace, data);
233  except:
234  print 'Bad replacement pair ',i,'in',sub_type;
235  continue;
236  return data;

Here is the caller graph for this function:

Field Documentation

codegen.Conversion.content

Definition at line 92 of file codegen.py.

codegen.Conversion.converted

Definition at line 128 of file codegen.py.

codegen.Conversion.date

Definition at line 96 of file codegen.py.

codegen.Conversion.dates

Definition at line 127 of file codegen.py.

codegen.Conversion.debug = False;
static

Definition at line 71 of file codegen.py.

codegen.Conversion.file

Definition at line 95 of file codegen.py.

list codegen.Conversion.files_in = []
static

Definition at line 81 of file codegen.py.

list codegen.Conversion.files_out = []
static

Definition at line 83 of file codegen.py.

codegen.Conversion.make = False;
static

Definition at line 75 of file codegen.py.

codegen.Conversion.names

Definition at line 126 of file codegen.py.

codegen.Conversion.precision

Definition at line 103 of file codegen.py.

codegen.Conversion.precisions

Definition at line 105 of file codegen.py.

codegen.Conversion.precstmp

Definition at line 107 of file codegen.py.

codegen.Conversion.prefix = None;
static

Definition at line 78 of file codegen.py.

list codegen.Conversion.required_precisions = []
static

Definition at line 79 of file codegen.py.

codegen.Conversion.test = False;
static

Definition at line 73 of file codegen.py.

codegen.Conversion.types

Definition at line 101 of file codegen.py.


The documentation for this class was generated from the following file: