Repository navigation
Expand file tree
/
Copy pathTransform.java
More file actions
275 lines (228 loc) · 7.64 KB
/
Copy pathTransform.java
File metadata and controls
275 lines (228 loc) · 7.64 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
/*
* 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 org.apache.sysml.lops.LopProperties.ExecLocation;
import org.apache.sysml.lops.LopProperties.ExecType;
import org.apache.sysml.lops.compile.JobType;
import org.apache.sysml.parser.Expression.*;
/*
* Lop to perform transpose/vector to diag operations
* This lop can change the keys and hence break alignment.
*/
public class Transform extends Lop
{
public enum OperationTypes {
Transpose,
Diag,
Reshape,
Sort,
Rev
};
private OperationTypes operation = null;
private boolean _bSortIndInMem = false;
private int _numThreads = 1;
/**
* Constructor when we have one input.
*
* @param input low-level operator
* @param op transform operation type
* @param dt data type
* @param vt value type
* @param et execution type
*/
public Transform(Lop input, Transform.OperationTypes op, DataType dt, ValueType vt, ExecType et) {
this(input, op, dt, vt, et, 1);
}
public Transform(Lop input, Transform.OperationTypes op, DataType dt, ValueType vt, ExecType et, int k) {
super(Lop.Type.Transform, dt, vt);
init(input, op, dt, vt, et);
_numThreads = k;
}
public Transform(Lop input, Transform.OperationTypes op, DataType dt, ValueType vt) {
super(Lop.Type.Transform, dt, vt);
init(input, op, dt, vt, ExecType.MR);
}
public Transform(Lop input, Transform.OperationTypes op, DataType dt, ValueType vt, ExecType et, boolean bSortIndInMem) {
super(Lop.Type.Transform, dt, vt);
_bSortIndInMem = bSortIndInMem;
init(input, op, dt, vt, et);
}
private void init (Lop input, Transform.OperationTypes op, DataType dt, ValueType vt, ExecType et)
{
operation = op;
this.addInput(input);
input.addOutput(this);
boolean breaksAlignment = true;
boolean aligner = false;
boolean definesMRJob = false;
if ( et == ExecType.MR ) {
/*
* This lop CAN NOT be executed in PARTITION, SORT, STANDALONE
* MMCJ: only in mapper.
*/
lps.addCompatibility(JobType.GMR);
lps.addCompatibility(JobType.DATAGEN);
lps.addCompatibility(JobType.REBLOCK);
lps.addCompatibility(JobType.CSV_REBLOCK);
lps.addCompatibility(JobType.MMCJ);
lps.addCompatibility(JobType.MMRJ);
if( op == OperationTypes.Reshape )
//reshape should be executed in map because we have potentially large intermediate data and want to exploit the combiner.
this.lps.setProperties( inputs, et, ExecLocation.Map, breaksAlignment, aligner, definesMRJob );
else
this.lps.setProperties( inputs, et, ExecLocation.MapOrReduce, breaksAlignment, aligner, definesMRJob );
}
else //CP/SPARK
{
// <code>breaksAlignment</code> is not meaningful when <code>Transform</code> executes in CP.
breaksAlignment = false;
lps.addCompatibility(JobType.INVALID);
lps.setProperties( inputs, et, ExecLocation.ControlProgram, breaksAlignment, aligner, definesMRJob );
}
}
@Override
public String toString() {
return " Operation: " + operation;
}
/**
* method to get operation type
* @return operaton type
*/
public OperationTypes getOperationType()
{
return operation;
}
private String getOpcode() {
switch(operation) {
case Transpose:
// Transpose a matrix
return "r'";
case Rev:
// Transpose a matrix
return "rev";
case Diag:
// Transform a vector into a diagonal matrix
return "rdiag";
case Reshape:
// Transform a vector into a diagonal matrix
return "rshape";
case Sort:
// Transform a matrix into a sorted matrix
return "rsort";
default:
throw new UnsupportedOperationException(this.printErrorLocation() + "Instruction is not defined for Transform operation " + operation);
}
}
//CP instructions
@Override
public String getInstructions(String input1, String output)
throws LopsException
{
StringBuilder sb = new StringBuilder();
sb.append( getExecType() );
sb.append( OPERAND_DELIMITOR );
sb.append( getOpcode() );
sb.append( OPERAND_DELIMITOR );
sb.append( getInputs().get(0).prepInputOperand(input1));
sb.append( OPERAND_DELIMITOR );
sb.append( this.prepOutputOperand(output));
if( getExecType()==ExecType.CP && operation == OperationTypes.Transpose ) {
sb.append( OPERAND_DELIMITOR );
sb.append( _numThreads );
}
return sb.toString();
}
@Override
public String getInstructions(String input1, String input2, String input3, String input4, String output)
throws LopsException
{
//only used for reshape
StringBuilder sb = new StringBuilder();
sb.append( getExecType() );
sb.append( OPERAND_DELIMITOR );
sb.append( getOpcode() );
sb.append( OPERAND_DELIMITOR );
sb.append( getInputs().get(0).prepInputOperand(input1));
//rows, cols, byrow
String[] inputX = new String[]{input2,input3,input4};
for( int i=1; i<=(inputX.length); i++ ) {
Lop ltmp = getInputs().get(i);
sb.append( OPERAND_DELIMITOR );
sb.append( ltmp.prepScalarInputOperand(getExecType()));
}
//output
sb.append( OPERAND_DELIMITOR );
sb.append( this.prepOutputOperand(output));
if( getExecType()==ExecType.SPARK && operation == OperationTypes.Sort ){
sb.append( OPERAND_DELIMITOR );
sb.append( _bSortIndInMem );
}
return sb.toString();
}
//MR instructions
@Override
public String getInstructions(int input_index, int output_index)
throws LopsException
{
StringBuilder sb = new StringBuilder();
sb.append( getExecType() );
sb.append( OPERAND_DELIMITOR );
sb.append( getOpcode() );
sb.append( OPERAND_DELIMITOR );
sb.append( getInputs().get(0).prepInputOperand(input_index));
sb.append( OPERAND_DELIMITOR );
sb.append( this.prepOutputOperand(output_index));
return sb.toString();
}
@Override
public String getInstructions(int input_index1, int input_index2, int input_index3, int input_index4, int output_index)
throws LopsException
{
//only used for reshape
StringBuilder sb = new StringBuilder();
sb.append( getExecType() );
sb.append( OPERAND_DELIMITOR );
sb.append( getOpcode() );
sb.append( OPERAND_DELIMITOR );
sb.append( getInputs().get(0).prepInputOperand(input_index1));
//rows
Lop input2 = getInputs().get(1);
String rowsString = input2.prepScalarLabel();
sb.append( OPERAND_DELIMITOR );
sb.append( rowsString );
//cols
Lop input3 = getInputs().get(2);
String colsString = input3.prepScalarLabel();
sb.append( OPERAND_DELIMITOR );
sb.append( colsString );
//byrow
Lop input4 = getInputs().get(3);
String byrowString = input4.prepScalarLabel();
if ( input4.getExecLocation() == ExecLocation.Data
&& !((Data)input4).isLiteral() || !(input4.getExecLocation() == ExecLocation.Data ) ){
throw new LopsException(this.printErrorLocation() + "Parameter 'byRow' must be a literal for a matrix operation.");
}
sb.append( OPERAND_DELIMITOR );
sb.append( byrowString );
//output
sb.append( OPERAND_DELIMITOR );
sb.append( this.prepOutputOperand(output_index));
return sb.toString();
}
}