-
Notifications
You must be signed in to change notification settings - Fork 457
/
RDDConverterUtils.java
1500 lines (1283 loc) · 52.9 KB
/
RDDConverterUtils.java
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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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.sysds.runtime.instructions.spark.utils;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.SequenceFileOutputFormat;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.FlatMapFunction;
import org.apache.spark.api.java.function.Function;
import org.apache.spark.api.java.function.PairFlatMapFunction;
import org.apache.spark.api.java.function.PairFunction;
import org.apache.spark.ml.feature.LabeledPoint;
import org.apache.spark.ml.linalg.DenseVector;
import org.apache.spark.ml.linalg.SparseVector;
import org.apache.spark.ml.linalg.Vector;
import org.apache.spark.ml.linalg.VectorUDT;
import org.apache.spark.ml.linalg.Vectors;
import org.apache.spark.mllib.util.MLUtils;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.RowFactory;
import org.apache.spark.sql.SQLContext;
import org.apache.spark.sql.SparkSession;
import org.apache.spark.sql.types.DataTypes;
import org.apache.spark.sql.types.StructField;
import org.apache.spark.storage.StorageLevel;
import org.apache.spark.util.LongAccumulator;
import org.apache.sysds.common.Types.FileFormat;
import org.apache.sysds.common.Types.ValueType;
import org.apache.sysds.conf.ConfigurationManager;
import org.apache.sysds.hops.OptimizerUtils;
import org.apache.sysds.runtime.DMLRuntimeException;
import org.apache.sysds.runtime.controlprogram.caching.MatrixObject;
import org.apache.sysds.runtime.data.DenseBlockFP64DEDUP;
import org.apache.sysds.runtime.data.SparseBlock;
import org.apache.sysds.runtime.instructions.spark.data.ReblockBuffer;
import org.apache.sysds.runtime.instructions.spark.data.SerLongWritable;
import org.apache.sysds.runtime.instructions.spark.data.SerText;
import org.apache.sysds.runtime.instructions.spark.functions.ConvertMatrixBlockToIJVLines;
import org.apache.sysds.runtime.instructions.spark.functions.ExtractBlockForBinaryReblock;
import org.apache.sysds.runtime.io.FileFormatPropertiesCSV;
import org.apache.sysds.runtime.io.FileFormatPropertiesLIBSVM;
import org.apache.sysds.runtime.io.FileFormatPropertiesMM;
import org.apache.sysds.runtime.io.IOUtilFunctions;
import org.apache.sysds.runtime.matrix.data.MatrixBlock;
import org.apache.sysds.runtime.matrix.data.MatrixCell;
import org.apache.sysds.runtime.matrix.data.MatrixIndexes;
import org.apache.sysds.runtime.meta.DataCharacteristics;
import org.apache.sysds.runtime.meta.MatrixCharacteristics;
import org.apache.sysds.runtime.util.DataConverter;
import org.apache.sysds.runtime.util.FastStringTokenizer;
import org.apache.sysds.runtime.util.HDFSTool;
import org.apache.sysds.runtime.util.UtilFunctions;
import scala.Tuple2;
public class RDDConverterUtils {
public static final String DF_ID_COLUMN = "__INDEX";
public static JavaPairRDD<MatrixIndexes, MatrixBlock> textCellToBinaryBlock(JavaSparkContext sc,
JavaPairRDD<LongWritable, Text> input, DataCharacteristics mcOut, boolean outputEmptyBlocks, FileFormatPropertiesMM mmProps)
{
//convert textcell rdd to binary block rdd (w/ partial blocks)
JavaPairRDD<MatrixIndexes, MatrixBlock> out = input.values()
.mapPartitionsToPair(new TextToBinaryBlockFunction(mcOut, mmProps));
//inject empty blocks (if necessary)
if( outputEmptyBlocks && mcOut.mightHaveEmptyBlocks() ) {
out = out.union(
SparkUtils.getEmptyBlockRDD(sc, mcOut) );
}
//aggregate partial matrix blocks
out = RDDAggregateUtils.mergeByKey(out, false);
return out;
}
public static JavaPairRDD<MatrixIndexes, MatrixBlock> binaryCellToBinaryBlock(JavaSparkContext sc,
JavaPairRDD<MatrixIndexes, MatrixCell> input, DataCharacteristics mcOut, boolean outputEmptyBlocks)
{
//convert binarycell rdd to binary block rdd (w/ partial blocks)
JavaPairRDD<MatrixIndexes, MatrixBlock> out = input
.mapPartitionsToPair(new BinaryCellToBinaryBlockFunction(mcOut));
//inject empty blocks (if necessary)
if( outputEmptyBlocks && mcOut.mightHaveEmptyBlocks() ) {
out = out.union(
SparkUtils.getEmptyBlockRDD(sc, mcOut) );
}
//aggregate partial matrix blocks
out = RDDAggregateUtils.mergeByKey(out, false);
return out;
}
/**
* Converter from binary block rdd to rdd of labeled points. Note that the input needs to be
* reblocked to satisfy the 'clen <= blen' constraint.
*
* @param in matrix as {@code JavaPairRDD<MatrixIndexes, MatrixBlock>}
* @return JavaRDD of labeled points
*/
public static JavaRDD<LabeledPoint> binaryBlockToLabeledPoints(JavaPairRDD<MatrixIndexes, MatrixBlock> in)
{
//convert indexed binary block input to collection of labeled points
JavaRDD<LabeledPoint> pointrdd = in
.values()
.flatMap(new PrepareBinaryBlockFunction());
return pointrdd;
}
public static JavaRDD<String> binaryBlockToTextCell(JavaPairRDD<MatrixIndexes, MatrixBlock> in, DataCharacteristics mc) {
return in.flatMap(new ConvertMatrixBlockToIJVLines(mc.getBlocksize()));
}
public static JavaRDD<String> binaryBlockToCsv(JavaPairRDD<MatrixIndexes,MatrixBlock> in, DataCharacteristics mcIn, FileFormatPropertiesCSV props, boolean strict)
{
JavaPairRDD<MatrixIndexes,MatrixBlock> input = in;
//fast path without, general case with shuffle
if( mcIn.getCols()>mcIn.getBlocksize() ) {
//create row partitioned matrix
input = input
.flatMapToPair(new SliceBinaryBlockToRowsFunction(mcIn.getBlocksize()))
.groupByKey()
.mapToPair(new ConcatenateBlocksFunction(mcIn.getCols(), mcIn.getBlocksize()));
}
//sort if required (on blocks/rows)
if( strict ) {
input = input.sortByKey(true);
}
//convert binary block to csv (from blocks/rows)
JavaRDD<String> out = input.flatMap(new BinaryBlockToCSVFunction(props));
return out;
}
public static JavaRDD<String> binaryBlockToLibsvm(JavaPairRDD<MatrixIndexes, MatrixBlock> in,
DataCharacteristics mcIn, FileFormatPropertiesLIBSVM props, boolean strict) {
JavaPairRDD<MatrixIndexes, MatrixBlock> input = in;
//fast path without, general case with shuffle
if(mcIn.getCols() > mcIn.getBlocksize()) {
//create row partitioned matrix
input = input.flatMapToPair(new SliceBinaryBlockToRowsFunction(mcIn.getBlocksize())).groupByKey()
.mapToPair(new ConcatenateBlocksFunction(mcIn.getCols(), mcIn.getBlocksize()));
}
//sort if required (on blocks/rows)
if(strict) {
input = input.sortByKey(true);
}
//convert binary block to libsvm (from blocks/rows)
JavaRDD<String> out = input.flatMap(new BinaryBlockToLIBSVMFunction(props));
return out;
}
public static JavaPairRDD<MatrixIndexes, MatrixBlock> binaryBlockToBinaryBlock(
JavaPairRDD<MatrixIndexes, MatrixBlock> in, DataCharacteristics mcIn, DataCharacteristics mcOut)
{
boolean shuffleFreeReblock = mcIn.dimsKnown() && mcOut.dimsKnown()
&& (mcIn.getRows() < mcIn.getBlocksize() || mcIn.getBlocksize()%mcOut.getBlocksize() == 0)
&& (mcIn.getCols() < mcIn.getBlocksize() || mcIn.getBlocksize()%mcOut.getBlocksize() == 0);
JavaPairRDD<MatrixIndexes, MatrixBlock> out = in
.flatMapToPair(new ExtractBlockForBinaryReblock(mcIn, mcOut));
if( !shuffleFreeReblock )
out = RDDAggregateUtils.mergeByKey(out, false);
return out;
}
public static JavaPairRDD<MatrixIndexes, MatrixBlock> csvToBinaryBlock(JavaSparkContext sc,
JavaPairRDD<LongWritable, Text> input, DataCharacteristics mc,
boolean hasHeader, String delim, boolean fill, double fillValue, Set<String> naStrings) {
//determine unknown dimensions and sparsity if required
//(w/ robustness for mistakenly counted header in nnz)
if( !mc.dimsKnown(true) ) {
LongAccumulator aNnz = sc.sc().longAccumulator("nnz");
CSVAnalysisFunction csvAF = new CSVAnalysisFunction(aNnz, delim);
JavaRDD<String> tmp = input.values().map(csvAF);
long rlen = tmp.count() - (hasHeader ? 1 : 0);
long clen = IOUtilFunctions.split(tmp.first(), delim).length;
long nnz = Math.min(rlen*clen, UtilFunctions.toLong(aNnz.value()));
mc.set(rlen, clen, mc.getBlocksize(), nnz);
}
//prepare csv w/ row indexes (sorted by filenames)
JavaPairRDD<Text,Long> prepinput = input.values()
.zipWithIndex(); //zip row index
//convert csv rdd to binary block rdd (w/ partial blocks)
boolean sparse = requiresSparseAllocation(prepinput, mc);
JavaPairRDD<MatrixIndexes, MatrixBlock> out =
prepinput.mapPartitionsToPair(new CSVToBinaryBlockFunction(
mc, sparse, hasHeader, delim, fill, fillValue, naStrings));
//aggregate partial matrix blocks (w/ preferred number of output
//partitions as the data is likely smaller in binary block format,
//but also to bound the size of partitions for compressed inputs)
int parts = SparkUtils.getNumPreferredPartitions(mc, out);
return RDDAggregateUtils.mergeByKey(out, parts, false);
}
public static JavaPairRDD<MatrixIndexes, MatrixBlock> csvToBinaryBlock(JavaSparkContext sc,
JavaRDD<String> input, DataCharacteristics mcOut,
boolean hasHeader, String delim, boolean fill, double fillValue, Set<String> naStrings)
{
//convert string rdd to serializable longwritable/text
JavaPairRDD<LongWritable, Text> prepinput =
input.mapToPair(new StringToSerTextFunction());
//convert to binary block
return csvToBinaryBlock(sc, prepinput, mcOut, hasHeader, delim, fill, fillValue, naStrings);
}
public static JavaPairRDD<MatrixIndexes, MatrixBlock> dataFrameToBinaryBlock(JavaSparkContext sc,
Dataset<Row> df, DataCharacteristics mc, boolean containsID, boolean isVector)
{
//determine unknown dimensions and sparsity if required
if( !mc.dimsKnown(true) ) {
LongAccumulator aNnz = sc.sc().longAccumulator("nnz");
JavaRDD<Row> tmp = df.javaRDD().map(new DataFrameAnalysisFunction(aNnz, containsID, isVector));
long rlen = tmp.count();
long clen = !isVector ? df.columns().length - (containsID?1:0) :
((Vector) tmp.first().get(containsID?1:0)).size();
long nnz = UtilFunctions.toLong(aNnz.value());
mc.set(rlen, clen, mc.getBlocksize(), nnz);
}
//ensure valid blocksizes
if( mc.getBlocksize()<=1 )
mc.setBlocksize(ConfigurationManager.getBlocksize());
//construct or reuse row ids
JavaPairRDD<Row, Long> prepinput = containsID ?
df.javaRDD().mapToPair(new DataFrameExtractIDFunction(
df.schema().fieldIndex(DF_ID_COLUMN))) :
df.javaRDD().zipWithIndex(); //zip row index
//convert csv rdd to binary block rdd (w/ partial blocks)
boolean sparse = requiresSparseAllocation(prepinput, mc);
JavaPairRDD<MatrixIndexes, MatrixBlock> out =
prepinput.mapPartitionsToPair(
new DataFrameToBinaryBlockFunction(mc, sparse, containsID, isVector));
//aggregate partial matrix blocks (w/ preferred number of output
//partitions as the data is likely smaller in binary block format,
//but also to bound the size of partitions for compressed inputs)
int parts = SparkUtils.getNumPreferredPartitions(mc, out);
return RDDAggregateUtils.mergeByKey(out, parts, false);
}
public static Dataset<Row> binaryBlockToDataFrame(SparkSession sparkSession,
JavaPairRDD<MatrixIndexes, MatrixBlock> in, DataCharacteristics mc, boolean toVector)
{
if( !mc.colsKnown() )
throw new RuntimeException("Number of columns needed to convert binary block to data frame.");
//slice blocks into rows, align and convert into data frame rows
JavaRDD<Row> rowsRDD = in
.flatMapToPair(new SliceBinaryBlockToRowsFunction(mc.getBlocksize()))
.groupByKey().map(new ConvertRowBlocksToRows((int)mc.getCols(), mc.getBlocksize(), toVector));
//create data frame schema
List<StructField> fields = new ArrayList<>();
fields.add(DataTypes.createStructField(DF_ID_COLUMN, DataTypes.DoubleType, false));
if( toVector )
fields.add(DataTypes.createStructField("C1", new VectorUDT(), false));
else { // row
for(int i = 1; i <= mc.getCols(); i++)
fields.add(DataTypes.createStructField("C"+i, DataTypes.DoubleType, false));
}
//rdd to data frame conversion
return sparkSession.createDataFrame(rowsRDD.rdd(), DataTypes.createStructType(fields));
}
@Deprecated
public static Dataset<Row> binaryBlockToDataFrame(SQLContext sqlContext,
JavaPairRDD<MatrixIndexes, MatrixBlock> in, DataCharacteristics mc, boolean toVector)
{
return binaryBlockToDataFrame(sqlContext.sparkSession(), in, mc, toVector);
}
/**
* Converts a libsvm text input file into two binary block matrices for features
* and labels, and saves these to the specified output files. This call also deletes
* existing files at the specified output locations, as well as determines and
* writes the meta data files of both output matrices.
* <p>
* Note: We use {@code org.apache.spark.mllib.util.MLUtils.loadLibSVMFile} for parsing
* the libsvm input files in order to ensure consistency with Spark.
*
* @param sc java spark context
* @param pathIn path to libsvm input file
* @param pathX path to binary block output file of features
* @param pathY path to binary block output file of labels
* @param mcOutX matrix characteristics of output matrix X
*/
public static void libsvmToBinaryBlock(JavaSparkContext sc, String pathIn,
String pathX, String pathY, DataCharacteristics mcOutX)
{
if( !mcOutX.dimsKnown() )
throw new DMLRuntimeException("Matrix characteristics "
+ "required to convert sparse input representation.");
try {
//cleanup existing output files
HDFSTool.deleteFileIfExistOnHDFS(pathX);
HDFSTool.deleteFileIfExistOnHDFS(pathY);
//convert libsvm to labeled points
int numFeatures = (int) mcOutX.getCols();
int numPartitions = SparkUtils.getNumPreferredPartitions(mcOutX, null);
JavaRDD<org.apache.spark.mllib.regression.LabeledPoint> lpoints =
MLUtils.loadLibSVMFile(sc.sc(), pathIn, numFeatures, numPartitions).toJavaRDD();
//append row index and best-effort caching to avoid repeated text parsing
JavaPairRDD<org.apache.spark.mllib.regression.LabeledPoint,Long> ilpoints =
lpoints.zipWithIndex().persist(StorageLevel.MEMORY_AND_DISK());
//extract labels and convert to binary block
DataCharacteristics mc1 = new MatrixCharacteristics(mcOutX.getRows(), 1, mcOutX.getBlocksize(), -1);
LongAccumulator aNnz1 = sc.sc().longAccumulator("nnz");
JavaPairRDD<MatrixIndexes,MatrixBlock> out1 = ilpoints
.mapPartitionsToPair(new LabeledPointToBinaryBlockFunction(mc1, true, aNnz1));
int numPartitions2 = SparkUtils.getNumPreferredPartitions(mc1, null);
out1 = RDDAggregateUtils.mergeByKey(out1, numPartitions2, false);
out1.saveAsHadoopFile(pathY, MatrixIndexes.class, MatrixBlock.class, SequenceFileOutputFormat.class);
mc1.setNonZeros(aNnz1.value()); //update nnz after triggered save
HDFSTool.writeMetaDataFile(pathY+".mtd", ValueType.FP64, mc1, FileFormat.BINARY);
//extract data and convert to binary block
DataCharacteristics mc2 = new MatrixCharacteristics(mcOutX.getRows(), mcOutX.getCols(), mcOutX.getBlocksize(), -1);
LongAccumulator aNnz2 = sc.sc().longAccumulator("nnz");
JavaPairRDD<MatrixIndexes,MatrixBlock> out2 = ilpoints
.mapPartitionsToPair(new LabeledPointToBinaryBlockFunction(mc2, false, aNnz2));
out2 = RDDAggregateUtils.mergeByKey(out2, numPartitions, false);
out2.saveAsHadoopFile(pathX, MatrixIndexes.class, MatrixBlock.class, SequenceFileOutputFormat.class);
mc2.setNonZeros(aNnz2.value()); //update nnz after triggered save
HDFSTool.writeMetaDataFile(pathX+".mtd", ValueType.FP64, mc2, FileFormat.BINARY);
//asynchronous cleanup of cached intermediates
ilpoints.unpersist(false);
}
catch(IOException ex) {
throw new DMLRuntimeException(ex);
}
}
//can be removed if not necessary, it's basically the Frame-Matrix reblock but with matrix
public static JavaPairRDD<MatrixIndexes, MatrixBlock> matrixBlockToAlignedMatrixBlock(JavaPairRDD<Long,
MatrixBlock> input, DataCharacteristics mcIn, DataCharacteristics mcOut)
{
//align matrix blocks
JavaPairRDD<MatrixIndexes, MatrixBlock> out = input
.flatMapToPair(new RDDConverterUtils.MatrixBlockToAlignedMatrixBlockFunction(mcIn, mcOut));
//aggregate partial matrix blocks
return RDDAggregateUtils.mergeByKey(out, false);
}
public static JavaPairRDD<LongWritable, Text> stringToSerializableText(JavaPairRDD<Long,String> in)
{
return in.mapToPair(new TextToSerTextFunction());
}
private static boolean requiresSparseAllocation(JavaPairRDD<?,?> in, DataCharacteristics mc) {
//if nnz unknown or sparse, pick the robust sparse representation
if( !mc.nnzKnown() || (mc.nnzKnown() && MatrixBlock.evalSparseFormatInMemory(
mc.getRows(), mc.getCols(), mc.getNonZeros())) ) {
return true;
}
//if dense evaluate expected rows per partition to handle wide matrices
//(pick sparse representation if fraction of rows per block less than sparse theshold)
double datasize = OptimizerUtils.estimatePartitionedSizeExactSparsity(mc);
double rowsize = OptimizerUtils.estimatePartitionedSizeExactSparsity(1, mc.getCols(),
mc.getNumRowBlocks(), Math.ceil((double)mc.getNonZeros()/mc.getRows()));
double partsize = Math.ceil(datasize/in.getNumPartitions());
double blksz = Math.min(mc.getRows(), mc.getBlocksize());
return partsize/rowsize/blksz < MatrixBlock.SPARSITY_TURN_POINT;
}
private static int countNnz(Object vect, boolean isVector, int off) {
if( isVector ) //note: numNonzeros scans entries but handles sparse/dense
return ((Vector) vect).numNonzeros();
else
return countNnz(vect, isVector, off, ((Row)vect).length());
}
/**
* Count the number of non-zeros for a subrange of the given row.
*
* @param vect row object (row of basic types or row including a vector)
* @param isVector if the row includes a vector
* @param pos physical position
* @param cu logical upper column index (exclusive)
* @return number of non-zeros.
*/
private static int countNnz(Object vect, boolean isVector, int pos, int cu ) {
int lnnz = 0;
if( isVector ) {
if( vect instanceof DenseVector ) {
DenseVector vec = (DenseVector) vect;
for( int i=pos; i<cu; i++ )
lnnz += (vec.apply(i) != 0) ? 1 : 0;
}
else if( vect instanceof SparseVector ) {
SparseVector vec = (SparseVector) vect;
int alen = vec.numActives();
int[] aix = vec.indices();
double[] avals = vec.values();
for( int i=pos; i<alen && aix[i]<cu; i++ )
lnnz += (avals[i] != 0) ? 1 : 0;
}
}
else { //row
Row row = (Row) vect;
for( int i=pos; i<cu; i++ )
lnnz += UtilFunctions.isNonZero(row.get(i)) ? 1 : 0;
}
return lnnz;
}
private static Vector createVector(MatrixBlock row) {
if( row.isEmptyBlock(false) ) //EMPTY SPARSE ROW
return Vectors.sparse(row.getNumColumns(), new int[0], new double[0]);
else if( row.isInSparseFormat() ) //SPARSE ROW
return Vectors.sparse(row.getNumColumns(),
row.getSparseBlock().indexes(0), row.getSparseBlock().values(0));
else // DENSE ROW
return Vectors.dense(row.getDenseBlockValues());
}
/////////////////////////////////
// BINARYBLOCK-SPECIFIC FUNCTIONS
/**
* This function converts a binary block input (<X,y>) into mllib's labeled points. Note that
* this function requires prior reblocking if the number of columns is larger than the column
* block size.
*/
private static class PrepareBinaryBlockFunction implements FlatMapFunction<MatrixBlock, LabeledPoint>
{
private static final long serialVersionUID = -6590259914203201585L;
@Override
public Iterator<LabeledPoint> call(MatrixBlock arg0)
throws Exception
{
ArrayList<LabeledPoint> ret = new ArrayList<>();
for( int i=0; i<arg0.getNumRows(); i++ ) {
MatrixBlock tmp = arg0.slice(i, i, 0, arg0.getNumColumns()-2, new MatrixBlock());
ret.add(new LabeledPoint(arg0.getValue(i, arg0.getNumColumns()-1), createVector(tmp)));
}
return ret.iterator();
}
}
/////////////////////////////////
// TEXTCELL-SPECIFIC FUNCTIONS
private static abstract class CellToBinaryBlockFunction implements Serializable
{
private static final long serialVersionUID = 4205331295408335933L;
//internal buffer size (aligned w/ default matrix block size)
protected static final int BUFFER_SIZE = 4 * 1000 * 1000; //4M elements (32MB)
protected int _bufflen = -1;
protected long _rlen = -1;
protected long _clen = -1;
protected int _blen = -1;
protected CellToBinaryBlockFunction(DataCharacteristics mc)
{
_rlen = mc.getRows();
_clen = mc.getCols();
_blen = mc.getBlocksize();
//determine upper bounded buffer len
_bufflen = (int) Math.min(_rlen*_clen, BUFFER_SIZE);
}
protected void flushBufferToList( ReblockBuffer rbuff, ArrayList<Tuple2<MatrixIndexes,MatrixBlock>> ret )
throws DMLRuntimeException
{
rbuff.flushBufferToBinaryBlocks().stream() // prevent library dependencies
.map(b -> SparkUtils.fromIndexedMatrixBlock(b)).forEach(b -> ret.add(b));
}
}
private static class TextToBinaryBlockFunction extends CellToBinaryBlockFunction implements PairFlatMapFunction<Iterator<Text>,MatrixIndexes,MatrixBlock>
{
private static final long serialVersionUID = 4907483236186747224L;
private final FileFormatPropertiesMM _mmProps;
protected TextToBinaryBlockFunction(DataCharacteristics mc, FileFormatPropertiesMM mmProps) {
super(mc);
_mmProps = mmProps;
}
@Override
public Iterator<Tuple2<MatrixIndexes, MatrixBlock>> call(Iterator<Text> arg0)
throws Exception
{
ArrayList<Tuple2<MatrixIndexes,MatrixBlock>> ret = new ArrayList<>();
ReblockBuffer rbuff = new ReblockBuffer(_bufflen, _rlen, _clen, _blen);
FastStringTokenizer st = new FastStringTokenizer(' ');
boolean first = false;
while( arg0.hasNext() ) {
//get input string (ignore matrix market comments as well as
//first row which indicates meta data, i.e., <nrow> <ncol> <nnz>)
String strVal = arg0.next().toString();
if( strVal.startsWith("%") ) {
first = true;
continue;
}
else if (first) {
first = false;
continue;
}
//parse input ijv triple
st.reset( strVal.toString() ); //reinit tokenizer
long row = st.nextLong();
long col = st.nextLong();
double val = (_mmProps == null) ? st.nextDouble() :
_mmProps.isPatternField() ? 1 : _mmProps.isIntField() ? st.nextLong() : st.nextDouble();
//flush buffer if necessary
if( rbuff.getSize() >= rbuff.getCapacity() )
flushBufferToList(rbuff, ret);
//add value to reblock buffer
rbuff.appendCell(row, col, val);
if( _mmProps != null && _mmProps.isSymmetric() && row!=col )
rbuff.appendCell(col, row, val);
}
//final flush buffer
flushBufferToList(rbuff, ret);
return ret.iterator();
}
}
private static class TextToSerTextFunction implements PairFunction<Tuple2<Long,String>,LongWritable,Text>
{
private static final long serialVersionUID = 2286037080400222528L;
@Override
public Tuple2<LongWritable, Text> call(Tuple2<Long, String> arg0)
throws Exception
{
SerLongWritable slarg = new SerLongWritable(arg0._1());
SerText starg = new SerText(arg0._2());
return new Tuple2<>(slarg, starg);
}
}
private static class StringToSerTextFunction implements PairFunction<String, LongWritable, Text>
{
private static final long serialVersionUID = 2286037080400222528L;
@Override
public Tuple2<LongWritable, Text> call(String arg0)
throws Exception
{
SerLongWritable slarg = new SerLongWritable(1L);
SerText starg = new SerText(arg0);
return new Tuple2<>(slarg, starg);
}
}
/////////////////////////////////
// BINARYCELL-SPECIFIC FUNCTIONS
public static class BinaryCellToBinaryBlockFunction extends CellToBinaryBlockFunction implements PairFlatMapFunction<Iterator<Tuple2<MatrixIndexes,MatrixCell>>,MatrixIndexes,MatrixBlock>
{
private static final long serialVersionUID = 3928810989462198243L;
public BinaryCellToBinaryBlockFunction(DataCharacteristics mc) {
super(mc);
}
@Override
public Iterator<Tuple2<MatrixIndexes, MatrixBlock>> call(Iterator<Tuple2<MatrixIndexes,MatrixCell>> arg0)
throws Exception
{
ArrayList<Tuple2<MatrixIndexes,MatrixBlock>> ret = new ArrayList<>();
ReblockBuffer rbuff = new ReblockBuffer(_bufflen, _rlen, _clen, _blen);
while( arg0.hasNext() )
{
//unpack the binary cell input
Tuple2<MatrixIndexes,MatrixCell> tmp = arg0.next();
//parse input ijv triple
long row = tmp._1().getRowIndex();
long col = tmp._1().getColumnIndex();
double val = tmp._2().getValue();
//flush buffer if necessary
if( rbuff.getSize() >= rbuff.getCapacity() )
flushBufferToList(rbuff, ret);
//add value to reblock buffer
rbuff.appendCell(row, col, val);
}
//final flush buffer
flushBufferToList(rbuff, ret);
return ret.iterator();
}
}
/////////////////////////////////
// CSV-SPECIFIC FUNCTIONS
private static class CSVAnalysisFunction implements Function<Text,String>
{
private static final long serialVersionUID = 2310303223289674477L;
private final LongAccumulator _aNnz;
private final String _delim;
public CSVAnalysisFunction( LongAccumulator aNnz, String delim )
{
_aNnz = aNnz;
_delim = delim;
}
@Override
public String call(Text v1)
throws Exception
{
//parse input line
String line = v1.toString();
String[] cols = IOUtilFunctions.split(line, _delim);
//determine number of non-zeros of row (w/o string parsing)
int lnnz = IOUtilFunctions.countNnz(cols);
//update counters
_aNnz.add( lnnz );
return line;
}
}
/**
* This functions allows to map rdd partitions of csv rows into a set of partial binary blocks.
*
* NOTE: For this csv to binary block function, we need to hold all output blocks per partition
* in-memory. Hence, we keep state of all column blocks and aggregate row segments into these blocks.
* In terms of memory consumption this is better than creating partial blocks of row segments.
*
*/
private static class CSVToBinaryBlockFunction implements PairFlatMapFunction<Iterator<Tuple2<Text,Long>>,MatrixIndexes,MatrixBlock>
{
private static final long serialVersionUID = -4948430402942717043L;
private long _rlen = -1;
private long _clen = -1;
private int _blen = -1;
private double _sparsity = 1.0;
private boolean _sparse = false;
private boolean _header = false;
private String _delim = null;
private boolean _fill = false;
private double _fillValue = 0;
private Set<String> _naStrings;
public CSVToBinaryBlockFunction(DataCharacteristics mc, boolean sparse, boolean hasHeader, String delim, boolean fill, double fillValue, Set<String> naStrings)
{
_rlen = mc.getRows();
_clen = mc.getCols();
_blen = mc.getBlocksize();
_sparsity = OptimizerUtils.getSparsity(mc);
_sparse = sparse && (!fill || fillValue==0);
_header = hasHeader;
_delim = delim;
_fill = fill;
_fillValue = fillValue;
_naStrings = naStrings;
}
@Override
public Iterator<Tuple2<MatrixIndexes, MatrixBlock>> call(Iterator<Tuple2<Text,Long>> arg0)
throws Exception {
ArrayList<Tuple2<MatrixIndexes,MatrixBlock>> ret = new ArrayList<>();
int ncblks = (int)Math.ceil((double)_clen/_blen);
MatrixIndexes[] ix = new MatrixIndexes[ncblks];
MatrixBlock[] mb = new MatrixBlock[ncblks];
while(arg0.hasNext()){
Tuple2<Text,Long> tmp = arg0.next();
String row = tmp._1().toString();
long rowix = tmp._2() + (_header ? 0 : 1);
//skip existing header
if( _header && rowix == 0 )
continue;
long rix = UtilFunctions.computeBlockIndex(rowix, _blen);
int pos = UtilFunctions.computeCellInBlock(rowix, _blen);
//create new blocks for entire row
if( ix[0] == null || ix[0].getRowIndex() != rix ) {
if( ix[0] !=null )
flushBlocksToList(ix, mb, ret);
long len = UtilFunctions.computeBlockSize(_rlen, rix, _blen);
createBlocks(rowix, (int)len, ix, mb);
}
//process row data
String[] parts = IOUtilFunctions.split(row, _delim);
boolean emptyFound = false;
for( int cix=1, pix=0; cix<=ncblks; cix++ )
{
final MatrixBlock mbc = mb[cix-1];
int lclen = UtilFunctions.computeBlockSize(_clen, cix, _blen);
if( mbc.isInSparseFormat() ) {
//allocate row once (avoid re-allocations)
int lnnz = IOUtilFunctions.countNnz(parts, pix, lclen);
mbc.getSparseBlock().allocate(pos, lnnz);
}
for( int j=0; j<lclen; j++ ) {
String part = parts[pix++].trim();
emptyFound |= part.isEmpty() && !_fill;
double val = (part.isEmpty() && _fill) ?
_fillValue : UtilFunctions.parseToDouble(part, _naStrings);
mbc.appendValue(pos, j, val);
}
}
//sanity check empty cells filled w/ values
IOUtilFunctions.checkAndRaiseErrorCSVEmptyField(row, _fill, emptyFound);
}
//flush last blocks
flushBlocksToList(ix, mb, ret);
return ret.iterator();
}
// Creates new state of empty column blocks for current global row index.
private void createBlocks(long rowix, int lrlen, MatrixIndexes[] ix, MatrixBlock[] mb)
{
//compute row block index and number of column blocks
long rix = UtilFunctions.computeBlockIndex(rowix, _blen);
int ncblks = (int)Math.ceil((double)_clen/_blen);
//create all column blocks (assume dense since csv is dense text format)
for( int cix=1; cix<=ncblks; cix++ ) {
int lclen = UtilFunctions.computeBlockSize(_clen, cix, _blen);
ix[cix-1] = new MatrixIndexes(rix, cix);
mb[cix-1] = new MatrixBlock(lrlen, lclen, _sparse, (int)(lrlen*lclen*_sparsity));
mb[cix-1].allocateBlock();
}
}
// Flushes current state of filled column blocks to output list.
private static void flushBlocksToList( MatrixIndexes[] ix, MatrixBlock[] mb, ArrayList<Tuple2<MatrixIndexes,MatrixBlock>> ret ) {
int len = ix.length;
for( int i=0; i<len; i++ )
if( mb[i] != null ) {
ret.add(new Tuple2<>(ix[i],mb[i]));
mb[i].examSparsity(); //ensure right representation
}
}
}
private static class LabeledPointToBinaryBlockFunction implements PairFlatMapFunction<Iterator<Tuple2<org.apache.spark.mllib.regression.LabeledPoint,Long>>,MatrixIndexes,MatrixBlock>
{
private static final long serialVersionUID = 2290124693964816276L;
private final long _rlen;
private final long _clen;
private final int _blen;
private final boolean _sparseX;
private final boolean _labels;
private final LongAccumulator _aNnz;
public LabeledPointToBinaryBlockFunction(DataCharacteristics mc, boolean labels, LongAccumulator aNnz) {
_rlen = mc.getRows();
_clen = mc.getCols();
_blen = mc.getBlocksize();
_sparseX = MatrixBlock.evalSparseFormatInMemory(
mc.getRows(), mc.getCols(), mc.getNonZeros());
_labels = labels;
_aNnz = aNnz;
}
@Override
public Iterator<Tuple2<MatrixIndexes, MatrixBlock>> call(Iterator<Tuple2<org.apache.spark.mllib.regression.LabeledPoint,Long>> arg0)
throws Exception
{
ArrayList<Tuple2<MatrixIndexes,MatrixBlock>> ret = new ArrayList<>();
int ncblks = (int)Math.ceil((double)_clen/_blen);
MatrixIndexes[] ix = new MatrixIndexes[ncblks];
MatrixBlock[] mb = new MatrixBlock[ncblks];
while( arg0.hasNext() )
{
Tuple2<org.apache.spark.mllib.regression.LabeledPoint,Long> tmp = arg0.next();
org.apache.spark.mllib.regression.LabeledPoint row = tmp._1();
boolean lsparse = _sparseX || (!_labels &&
row.features() instanceof org.apache.spark.mllib.linalg.SparseVector);
long rowix = tmp._2() + 1;
long rix = UtilFunctions.computeBlockIndex(rowix, _blen);
int pos = UtilFunctions.computeCellInBlock(rowix, _blen);
//create new blocks for entire row
if( ix[0] == null || ix[0].getRowIndex() != rix ) {
if( ix[0] !=null )
flushBlocksToList(ix, mb, ret);
long len = UtilFunctions.computeBlockSize(_rlen, rix, _blen);
createBlocks(rowix, (int)len, ix, mb, lsparse);
}
//process row data
if( _labels ) {
double val = row.label();
mb[0].appendValue(pos, 0, val);
_aNnz.add((val != 0) ? 1 : 0);
}
else { //features
int lnnz = row.features().numNonzeros();
if( row.features() instanceof org.apache.spark.mllib.linalg.SparseVector )
{
org.apache.spark.mllib.linalg.SparseVector srow =
(org.apache.spark.mllib.linalg.SparseVector) row.features();
for( int k=0; k<lnnz; k++ ) {
int gix = srow.indices()[k]+1;
int cix = (int)UtilFunctions.computeBlockIndex(gix, _blen);
int j = UtilFunctions.computeCellInBlock(gix, _blen);
mb[cix-1].appendValue(pos, j, srow.values()[k]);
}
}
else { //dense
for( int cix=1, pix=0; cix<=ncblks; cix++ ) {
int lclen = UtilFunctions.computeBlockSize(_clen, cix, _blen);
for( int j=0; j<lclen; j++ )
mb[cix-1].appendValue(pos, j, row.features().apply(pix++));
}
}
_aNnz.add(lnnz);
}
}
//flush last blocks
flushBlocksToList(ix, mb, ret);
return ret.iterator();
}
// Creates new state of empty column blocks for current global row index.
private void createBlocks(long rowix, int lrlen, MatrixIndexes[] ix, MatrixBlock[] mb, boolean lsparse)
{
//compute row block index and number of column blocks
long rix = UtilFunctions.computeBlockIndex(rowix, _blen);
int ncblks = (int)Math.ceil((double)_clen/_blen);
//create all column blocks (assume dense since csv is dense text format)
for( int cix=1; cix<=ncblks; cix++ ) {
int lclen = UtilFunctions.computeBlockSize(_clen, cix, _blen);
ix[cix-1] = new MatrixIndexes(rix, cix);
mb[cix-1] = new MatrixBlock(lrlen, lclen, lsparse);
mb[cix-1].allocateBlock();
}
}
// Flushes current state of filled column blocks to output list.
private static void flushBlocksToList( MatrixIndexes[] ix, MatrixBlock[] mb, ArrayList<Tuple2<MatrixIndexes,MatrixBlock>> ret ) {
int len = ix.length;
for( int i=0; i<len; i++ )
if( mb[i] != null ) {
ret.add(new Tuple2<>(ix[i],mb[i]));
mb[i].examSparsity(); //ensure right representation
}
}
}
private static class BinaryBlockToCSVFunction implements FlatMapFunction<Tuple2<MatrixIndexes,MatrixBlock>,String>
{
private static final long serialVersionUID = 1891768410987528573L;
private FileFormatPropertiesCSV _props = null;
public BinaryBlockToCSVFunction(FileFormatPropertiesCSV props) {
_props = props;
}
@Override
public Iterator<String> call(Tuple2<MatrixIndexes, MatrixBlock> arg0)
throws Exception
{
MatrixIndexes ix = arg0._1();
MatrixBlock blk = arg0._2();
ArrayList<String> ret = new ArrayList<>();
//handle header information
if(_props.hasHeader() && ix.getRowIndex()==1 ) {
StringBuilder sb = new StringBuilder();
for(int j = 1; j < blk.getNumColumns(); j++) {
if(j != 1)
sb.append(_props.getDelim());
sb.append("C" + j);
}
ret.add(sb.toString());
}
//handle matrix block data
StringBuilder sb = new StringBuilder();
for(int i=0; i<blk.getNumRows(); i++) {
for(int j=0; j<blk.getNumColumns(); j++) {
if(j != 0)
sb.append(_props.getDelim());
double val = blk.quickGetValue(i, j);
if(!(_props.isSparse() && val == 0))
sb.append(val);
}
ret.add(sb.toString());
sb.setLength(0); //reset
}
return ret.iterator();
}
}
private static class BinaryBlockToLIBSVMFunction
implements FlatMapFunction<Tuple2<MatrixIndexes, MatrixBlock>, String> {
private static final long serialVersionUID = 1891768410987528573L;
private FileFormatPropertiesLIBSVM _props = null;
public BinaryBlockToLIBSVMFunction(FileFormatPropertiesLIBSVM props) {
_props = props;
}
// Return string in libsvm format (<index#>:<value#>)
protected void appendIndexValLibsvm(StringBuilder sb, int index, double value) {
sb.append(index + 1); // convert 0 based matrix index to 1 base libsvm index
sb.append(_props.getIndexDelim());
sb.append(value);
}
@Override
public Iterator<String> call(Tuple2<MatrixIndexes, MatrixBlock> arg0) throws Exception {
MatrixBlock blk = arg0._2();
ArrayList<String> ret = new ArrayList<>();
StringBuilder sb = new StringBuilder();
boolean sparse = blk.isInSparseFormat();