Repository navigation
Expand file tree
/
Copy pathDataGen.java
More file actions
551 lines (459 loc) · 18.1 KB
/
Copy pathDataGen.java
File metadata and controls
551 lines (459 loc) · 18.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.sysml.lops;
import java.util.HashMap;
import org.apache.sysml.hops.Hop.DataGenMethod;
import org.apache.sysml.lops.LopProperties.ExecLocation;
import org.apache.sysml.lops.LopProperties.ExecType;
import org.apache.sysml.lops.OutputParameters.Format;
import org.apache.sysml.lops.compile.JobType;
import org.apache.sysml.parser.DataIdentifier;
import org.apache.sysml.parser.DataExpression;
import org.apache.sysml.parser.Statement;
import org.apache.sysml.parser.Expression.*;
/**
* <p>Defines a LOP that generates data.</p>
*/
public class DataGen extends Lop
{
public static final String RAND_OPCODE = "rand"; //rand
public static final String SEQ_OPCODE = "seq"; //sequence
public static final String SINIT_OPCODE = "sinit"; //string initialize
public static final String SAMPLE_OPCODE = "sample"; //sample.int
private int _numThreads = 1;
/** base dir for rand input */
private String baseDir;
private HashMap<String, Lop> _inputParams;
DataGenMethod method;
/**
* <p>Creates a new Rand-LOP. The target identifier has to hold the dimensions of the new random object.</p>
*
* @param mthd data gen method
* @param id target identifier
* @param inputParametersLops Lops of input parameters
* @param baseDir base dir for runtime
* @param dt Data type
* @param vt Value type
* @param et Execution type
* @throws LopsException if LopsException occurs
*/
public DataGen(DataGenMethod mthd, DataIdentifier id, HashMap<String, Lop>
inputParametersLops, String baseDir, DataType dt, ValueType vt, ExecType et) throws LopsException
{
super(Type.DataGen, dt, vt);
method = mthd;
for (Lop lop : inputParametersLops.values()) {
this.addInput(lop);
lop.addOutput(this);
}
_inputParams = inputParametersLops;
init(id, baseDir, et);
}
public DataGenMethod getDataGenMethod() {
return method;
}
public void init(DataIdentifier id, String baseDir, ExecType et)
{
this.getOutputParameters().setFormat(Format.BINARY);
this.getOutputParameters().setBlocked(true);
this.getOutputParameters().setNumRows(id.getDim1());
this.getOutputParameters().setNumCols(id.getDim2());
this.getOutputParameters().setNnz(-1);
this.getOutputParameters().setRowsInBlock(id.getRowsInBlock());
this.getOutputParameters().setColsInBlock(id.getColumnsInBlock());
this.baseDir = baseDir;
boolean breaksAlignment = false;
boolean aligner = false;
boolean definesMRJob;
if ( et == ExecType.MR ) {
definesMRJob = true;
lps.addCompatibility(JobType.DATAGEN);
this.lps.setProperties( inputs, et, ExecLocation.Map, breaksAlignment, aligner, definesMRJob);
}
else {
definesMRJob = false;
lps.addCompatibility(JobType.INVALID);
this.lps.setProperties( inputs, et, ExecLocation.ControlProgram, breaksAlignment, aligner, definesMRJob);
}
}
/**
* Function to generate CP/SP instructions for data generation via Rand and Seq.
* Since DataGen Lop can have many inputs, ONLY the output variable name is
* passed from piggybacking as the function argument <code>output</code>.
*/
@Override
public String getInstructions(String output)
throws LopsException
{
switch( method )
{
case RAND:
return getCPInstruction_Rand(output);
case SINIT:
return getCPInstruction_SInit(output);
case SEQ:
return getCPInstruction_Seq(output);
case SAMPLE:
return getCPInstruction_Sample(output);
default:
throw new LopsException("Unknown data generation method: " + method);
}
}
@Override
public String getInstructions(int inputIndex, int outputIndex)
throws LopsException
{
switch(method) {
case RAND:
return getMRInstruction_Rand(inputIndex, outputIndex);
case SEQ:
return getMRInstruction_Seq(inputIndex, outputIndex);
default:
throw new LopsException("Unknown data generation method: " + method);
}
}
/**
* Private method that generates CP Instruction for Rand.
*
* @param output output operand
* @return cp instruction for rand
* @throws LopsException if LopsException occurs
*/
private String getCPInstruction_Rand(String output)
throws LopsException
{
//sanity checks
if ( method != DataGenMethod.RAND )
throw new LopsException("Invalid instruction generation for data generation method " + method);
if( getInputs().size() != DataExpression.RAND_VALID_PARAM_NAMES.length ) {
throw new LopsException(printErrorLocation() + "Invalid number of operands ("
+ getInputs().size() + ") for a Rand operation");
}
StringBuilder sb = new StringBuilder( );
sb.append( getExecType() );
sb.append( Lop.OPERAND_DELIMITOR );
sb.append(RAND_OPCODE);
sb.append(OPERAND_DELIMITOR);
Lop iLop = _inputParams.get(DataExpression.RAND_ROWS.toString());
sb.append(iLop.prepScalarLabel());
sb.append(OPERAND_DELIMITOR);
iLop = _inputParams.get(DataExpression.RAND_COLS.toString());
sb.append(iLop.prepScalarLabel());
sb.append(OPERAND_DELIMITOR);
sb.append(String.valueOf(getOutputParameters().getRowsInBlock()));
sb.append(OPERAND_DELIMITOR);
sb.append(String.valueOf(getOutputParameters().getColsInBlock()));
sb.append(OPERAND_DELIMITOR);
iLop = _inputParams.get(DataExpression.RAND_MIN.toString());
sb.append(iLop.prepScalarLabel());
sb.append(OPERAND_DELIMITOR);
iLop = _inputParams.get(DataExpression.RAND_MAX.toString());
sb.append(iLop.prepScalarLabel());
sb.append(OPERAND_DELIMITOR);
iLop = _inputParams.get(DataExpression.RAND_SPARSITY.toString());
if (iLop.isVariable())
sb.append(iLop.prepScalarLabel());
else
sb.append(iLop.getOutputParameters().getLabel());
sb.append(OPERAND_DELIMITOR);
iLop = _inputParams.get(DataExpression.RAND_SEED.toString());
sb.append(iLop.prepScalarLabel());
sb.append(OPERAND_DELIMITOR);
if ( getExecType() == ExecType.MR || getExecType() == ExecType.SPARK ) {
sb.append(baseDir);
sb.append(OPERAND_DELIMITOR);
}
iLop = _inputParams.get(DataExpression.RAND_PDF.toString()); //no variable support
if (iLop.isVariable())
throw new LopsException(printErrorLocation()
+ "Parameter " + DataExpression.RAND_PDF
+ " must be a literal for a Rand operation.");
sb.append(iLop.getOutputParameters().getLabel());
sb.append(OPERAND_DELIMITOR);
iLop = _inputParams.get(DataExpression.RAND_LAMBDA.toString()); //no variable support
sb.append(iLop == null ? "" : iLop.prepScalarLabel());
sb.append(OPERAND_DELIMITOR);
if( getExecType() == ExecType.CP ) {
//append degree of parallelism
sb.append( _numThreads );
sb.append( OPERAND_DELIMITOR );
}
sb.append( prepOutputOperand(output));
return sb.toString();
}
private String getCPInstruction_SInit(String output)
throws LopsException
{
if ( method != DataGenMethod.SINIT )
throw new LopsException("Invalid instruction generation for data generation method " + method);
//prepare instruction parameters
Lop iLop = _inputParams.get(DataExpression.RAND_ROWS.toString());
String rowsString = iLop.prepScalarLabel();
iLop = _inputParams.get(DataExpression.RAND_COLS.toString());
String colsString = iLop.prepScalarLabel();
String rowsInBlockString = String.valueOf(this
.getOutputParameters().getRowsInBlock());
String colsInBlockString = String.valueOf(this
.getOutputParameters().getColsInBlock());
iLop = _inputParams.get(DataExpression.RAND_MIN.toString());
String minString = iLop.getOutputParameters().getLabel();
if (iLop.isVariable())
throw new LopsException(this.printErrorLocation()
+ "Parameter " + DataExpression.RAND_MIN
+ " must be a literal for a Rand operation.");
//generate instruction
StringBuilder sb = new StringBuilder( );
ExecType et = getExecType();
sb.append( et );
sb.append( Lop.OPERAND_DELIMITOR );
sb.append(SINIT_OPCODE);
sb.append(OPERAND_DELIMITOR);
sb.append(rowsString);
sb.append(OPERAND_DELIMITOR);
sb.append(colsString);
sb.append(OPERAND_DELIMITOR);
sb.append(rowsInBlockString);
sb.append(OPERAND_DELIMITOR);
sb.append(colsInBlockString);
sb.append(OPERAND_DELIMITOR);
sb.append(minString);
sb.append(OPERAND_DELIMITOR);
sb.append( this.prepOutputOperand(output));
return sb.toString();
}
private String getCPInstruction_Sample(String output)
throws LopsException
{
if ( method != DataGenMethod.SAMPLE )
throw new LopsException("Invalid instruction generation for data generation method " + method);
//prepare instruction parameters
Lop lsize = _inputParams.get(DataExpression.RAND_ROWS.toString());
String size = lsize.prepScalarLabel();
Lop lrange = _inputParams.get(DataExpression.RAND_MAX.toString());
String range = lrange.prepScalarLabel();
Lop lreplace = _inputParams.get(DataExpression.RAND_PDF.toString());
String replace = lreplace.prepScalarLabel();
Lop lseed = _inputParams.get(DataExpression.RAND_SEED.toString());
String seed = lseed.prepScalarLabel();
String rowsInBlockString = String.valueOf(this.getOutputParameters().getRowsInBlock());
String colsInBlockString = String.valueOf(this.getOutputParameters().getColsInBlock());
StringBuilder sb = new StringBuilder();
sb.append( getExecType() );
sb.append( Lop.OPERAND_DELIMITOR );
sb.append( "sample" );
sb.append( Lop.OPERAND_DELIMITOR );
sb.append( range );
sb.append( Lop.OPERAND_DELIMITOR );
sb.append( size );
sb.append( Lop.OPERAND_DELIMITOR );
sb.append( replace );
sb.append( Lop.OPERAND_DELIMITOR );
sb.append( seed );
sb.append( Lop.OPERAND_DELIMITOR );
sb.append( rowsInBlockString );
sb.append( Lop.OPERAND_DELIMITOR );
sb.append( colsInBlockString );
sb.append( Lop.OPERAND_DELIMITOR );
sb.append( prepOutputOperand(output) );
return sb.toString();
}
/**
* Private method that generates CP Instruction for Seq.
*
* @param output output operand
* @return cp instruction for seq
* @throws LopsException if LopsException occurs
*/
private String getCPInstruction_Seq(String output) throws LopsException {
if ( method != DataGenMethod.SEQ )
throw new LopsException("Invalid instruction generation for data generation method " + method);
StringBuilder sb = new StringBuilder( );
ExecType et = getExecType();
sb.append( et );
sb.append( Lop.OPERAND_DELIMITOR );
Lop iLop = null;
iLop = _inputParams.get(Statement.SEQ_FROM.toString());
String fromString = iLop.prepScalarLabel();
iLop = _inputParams.get(Statement.SEQ_TO.toString());
String toString = iLop.prepScalarLabel();
iLop = _inputParams.get(Statement.SEQ_INCR.toString());
String incrString = iLop.prepScalarLabel();
String rowsString = String.valueOf(this.getOutputParameters().getNumRows());
String colsString = String.valueOf(this.getOutputParameters().getNumCols());
String rowsInBlockString = String.valueOf(this.getOutputParameters().getRowsInBlock());
String colsInBlockString = String.valueOf(this.getOutputParameters().getColsInBlock());
sb.append( DataGen.SEQ_OPCODE );
sb.append( OPERAND_DELIMITOR );
sb.append( rowsString );
sb.append( OPERAND_DELIMITOR );
sb.append( colsString );
sb.append( OPERAND_DELIMITOR );
sb.append( rowsInBlockString );
sb.append( OPERAND_DELIMITOR );
sb.append( colsInBlockString );
sb.append( OPERAND_DELIMITOR );
sb.append( fromString );
sb.append( OPERAND_DELIMITOR );
sb.append( toString );
sb.append( OPERAND_DELIMITOR );
sb.append( incrString );
sb.append( OPERAND_DELIMITOR );
if ( et == ExecType.MR ) {
sb.append( baseDir );
sb.append( OPERAND_DELIMITOR );
}
sb.append( this.prepOutputOperand(output));
return sb.toString();
}
/**
* Private method to generate MR instruction for Rand.
*
* @param inputIndex input index
* @param outputIndex output index
* @return mr instruction for rand
* @throws LopsException if LopsException occurs
*/
private String getMRInstruction_Rand(int inputIndex, int outputIndex)
throws LopsException
{
//sanity checks
if( getInputs().size() != DataExpression.RAND_VALID_PARAM_NAMES.length) {
throw new LopsException(printErrorLocation() + "Invalid number of operands ("
+ getInputs().size() + ") for a Rand operation");
}
StringBuilder sb = new StringBuilder();
sb.append( getExecType() );
sb.append( Lop.OPERAND_DELIMITOR );
sb.append( RAND_OPCODE );
sb.append( OPERAND_DELIMITOR );
sb.append( inputIndex );
sb.append( OPERAND_DELIMITOR );
sb.append( outputIndex );
sb.append( OPERAND_DELIMITOR );
Lop iLop = _inputParams.get(DataExpression.RAND_ROWS.toString());
sb.append( iLop.prepScalarInputOperand(getExecType()) );
sb.append( OPERAND_DELIMITOR );
iLop = _inputParams.get(DataExpression.RAND_COLS.toString());
sb.append( iLop.prepScalarInputOperand(getExecType()) );
sb.append( OPERAND_DELIMITOR );
sb.append( String.valueOf(getOutputParameters().getRowsInBlock()) );
sb.append( OPERAND_DELIMITOR );
sb.append( String.valueOf(getOutputParameters().getColsInBlock()) );
sb.append( OPERAND_DELIMITOR );
iLop = _inputParams.get(DataExpression.RAND_MIN.toString());
sb.append( iLop.prepScalarInputOperand(getExecType()) );
sb.append( OPERAND_DELIMITOR );
iLop = _inputParams.get(DataExpression.RAND_MAX.toString());
sb.append( iLop.prepScalarInputOperand(getExecType()) );
sb.append( OPERAND_DELIMITOR );
iLop = _inputParams.get(DataExpression.RAND_SPARSITY.toString()); //no variable support
if (iLop.isVariable())
sb.append(iLop.prepScalarLabel());
else
sb.append( iLop.getOutputParameters().getLabel() );
sb.append( OPERAND_DELIMITOR );
iLop = _inputParams.get(DataExpression.RAND_SEED.toString());
sb.append( iLop.prepScalarLabel() );
sb.append( OPERAND_DELIMITOR );
sb.append( baseDir );
sb.append( OPERAND_DELIMITOR );
iLop = _inputParams.get(DataExpression.RAND_PDF.toString()); //no variable support
if (iLop.isVariable())
throw new LopsException(this.printErrorLocation() + "Parameter "
+ DataExpression.RAND_PDF + " must be a literal for a Rand operation.");
sb.append( iLop.getOutputParameters().getLabel() );
sb.append( OPERAND_DELIMITOR );
iLop = _inputParams.get(DataExpression.RAND_LAMBDA.toString()); //no variable support
sb.append( iLop == null ? "" : iLop.prepScalarLabel() );
return sb.toString();
}
/**
* Private method to generate MR instruction for Seq.
*
* @param inputIndex input index
* @param outputIndex output index
* @return mr instruction for seq
* @throws LopsException if LopsException occurs
*/
private String getMRInstruction_Seq(int inputIndex, int outputIndex) throws LopsException {
StringBuilder sb = new StringBuilder();
sb.append( getExecType() );
sb.append( Lop.OPERAND_DELIMITOR );
Lop iLop = null;
iLop = _inputParams.get(Statement.SEQ_FROM.toString());
String fromString = iLop.getOutputParameters().getLabel();
if ( (iLop.getExecLocation() == ExecLocation.Data &&
!((Data)iLop).isLiteral()) || !(iLop.getExecLocation() == ExecLocation.Data ))
fromString = Lop.VARIABLE_NAME_PLACEHOLDER + fromString + Lop.VARIABLE_NAME_PLACEHOLDER;
iLop = _inputParams.get(Statement.SEQ_TO.toString());
String toString = iLop.getOutputParameters().getLabel();
if ( iLop.getExecLocation() == ExecLocation.Data
&& !((Data)iLop).isLiteral() || !(iLop.getExecLocation() == ExecLocation.Data ) )
toString = Lop.VARIABLE_NAME_PLACEHOLDER + toString + Lop.VARIABLE_NAME_PLACEHOLDER;
iLop = _inputParams.get(Statement.SEQ_INCR.toString());
String incrString = iLop.getOutputParameters().getLabel();
if ( iLop.getExecLocation() == ExecLocation.Data
&& !((Data)iLop).isLiteral() || !(iLop.getExecLocation() == ExecLocation.Data ) )
incrString = Lop.VARIABLE_NAME_PLACEHOLDER + incrString + Lop.VARIABLE_NAME_PLACEHOLDER;
String rowsString = String.valueOf(this.getOutputParameters().getNumRows());
String colsString = String.valueOf(this.getOutputParameters().getNumCols());
String rowsInBlockString = String.valueOf(this.getOutputParameters().getRowsInBlock());
String colsInBlockString = String.valueOf(this.getOutputParameters().getColsInBlock());
sb.append( DataGen.SEQ_OPCODE );
sb.append( OPERAND_DELIMITOR );
sb.append( inputIndex );
sb.append( OPERAND_DELIMITOR );
sb.append( outputIndex );
sb.append( OPERAND_DELIMITOR );
sb.append( rowsString );
sb.append( OPERAND_DELIMITOR );
sb.append( colsString );
sb.append( OPERAND_DELIMITOR );
sb.append( rowsInBlockString );
sb.append( OPERAND_DELIMITOR );
sb.append( colsInBlockString );
sb.append( OPERAND_DELIMITOR );
sb.append( fromString );
sb.append( OPERAND_DELIMITOR );
sb.append( toString );
sb.append( OPERAND_DELIMITOR );
sb.append( incrString );
sb.append( OPERAND_DELIMITOR );
sb.append( baseDir );
return sb.toString();
}
@Override
public String toString()
{
StringBuilder sb = new StringBuilder();
sb.append(method.toString());
sb.append(" ; num_rows=" + this.getOutputParameters().getNumRows());
sb.append(" ; num_cols=" + this.getOutputParameters().getNumCols());
sb.append(" ; nnz=" + this.getOutputParameters().getNnz());
sb.append(" ; num_rows_per_block=" + this.getOutputParameters().getRowsInBlock());
sb.append(" ; num_cols_per_block=" + this.getOutputParameters().getColsInBlock());
sb.append(" ; format=" + this.getOutputParameters().getFormat());
sb.append(" ; blocked=" + this.getOutputParameters().isBlocked());
sb.append(" ; dir=" + this.baseDir);
return sb.toString();
}
public void setNumThreads(int k) {
_numThreads = k;
}
}