-
Notifications
You must be signed in to change notification settings - Fork 464
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SYSTEMDS-2699] CLA IO Compressed Matrix
This commit adds the basic blocks for writing a compressed matrix to disk, and adds a basic test for the case of writing a matrix and read it back from disk. Further testing and full integration into DML is needed, and a mechanism to detect if the format of the compression groups have changed.
- Loading branch information
1 parent
37520e8
commit 9241488
Showing
8 changed files
with
365 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
src/main/java/org/apache/sysds/runtime/compress/io/ReaderCompressed.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* 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.compress.io; | ||
|
||
import java.io.DataInput; | ||
import java.io.DataInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
import org.apache.commons.lang.NotImplementedException; | ||
import org.apache.hadoop.fs.FileSystem; | ||
import org.apache.hadoop.fs.Path; | ||
import org.apache.hadoop.mapred.JobConf; | ||
import org.apache.sysds.conf.ConfigurationManager; | ||
import org.apache.sysds.runtime.DMLRuntimeException; | ||
import org.apache.sysds.runtime.compress.CompressedMatrixBlock; | ||
import org.apache.sysds.runtime.io.IOUtilFunctions; | ||
import org.apache.sysds.runtime.io.MatrixReader; | ||
import org.apache.sysds.runtime.matrix.data.MatrixBlock; | ||
|
||
public class ReaderCompressed extends MatrixReader { | ||
|
||
public static ReaderCompressed create() { | ||
return new ReaderCompressed(); | ||
} | ||
|
||
public static MatrixBlock readCompressedMatrixFromHDFS(String fname) throws IOException { | ||
return create().readMatrixFromHDFS(fname, 0, 0, 0, 0); | ||
} | ||
|
||
@Override | ||
public MatrixBlock readMatrixFromHDFS(String fname, long rlen, long clen, int blen, long estnnz) | ||
throws IOException, DMLRuntimeException { | ||
|
||
JobConf job = new JobConf(ConfigurationManager.getCachedJobConf()); | ||
Path path = new Path(fname); | ||
FileSystem fs = IOUtilFunctions.getFileSystem(path, job); | ||
|
||
checkValidInputFile(fs, path); | ||
|
||
MatrixBlock cmb = readCompressedMatrix(path, job, fs); | ||
|
||
if(cmb.getNumRows() != rlen) | ||
LOG.warn("Metadata file does not correlate with compressed file, NRows : " + cmb.getNumRows() + " vs " + rlen); | ||
if(cmb.getNumColumns() != clen) | ||
LOG.warn( | ||
"Metadata file does not correlate with compressed file, NCols : " + cmb.getNumColumns() + " vs " + clen); | ||
|
||
return cmb; | ||
} | ||
|
||
@Override | ||
public MatrixBlock readMatrixFromInputStream(InputStream is, long rlen, long clen, int blen, long estnnz) | ||
throws IOException, DMLRuntimeException { | ||
throw new NotImplementedException("Not implemented reading compressedMatrix from input stream"); | ||
} | ||
|
||
private static MatrixBlock readCompressedMatrix(Path path, JobConf job, FileSystem fs) throws IOException { | ||
if(fs.getFileStatus(path).isDirectory()) | ||
return readCompressedMatrixFolder(path, job, fs); | ||
else | ||
return readCompressedMatrixSingleFile(path, job, fs); | ||
} | ||
|
||
private static MatrixBlock readCompressedMatrixFolder(Path path, JobConf job, FileSystem fs) { | ||
throw new NotImplementedException(); | ||
} | ||
|
||
private static MatrixBlock readCompressedMatrixSingleFile(Path path, JobConf job, FileSystem fs) throws IOException { | ||
final InputStream is = fs.open(path); | ||
final DataInput in = new DataInputStream(is); | ||
MatrixBlock ret; | ||
try { | ||
ret = CompressedMatrixBlock.read(in); | ||
} | ||
finally { | ||
is.close(); | ||
} | ||
return ret; | ||
} | ||
|
||
} |
95 changes: 95 additions & 0 deletions
95
src/main/java/org/apache/sysds/runtime/compress/io/WriterCompressed.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* 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.compress.io; | ||
|
||
import java.io.DataOutput; | ||
import java.io.DataOutputStream; | ||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
|
||
import org.apache.commons.lang.NotImplementedException; | ||
import org.apache.hadoop.fs.FileSystem; | ||
import org.apache.hadoop.fs.Path; | ||
import org.apache.hadoop.mapred.JobConf; | ||
import org.apache.sysds.conf.ConfigurationManager; | ||
import org.apache.sysds.runtime.compress.CompressedMatrixBlock; | ||
import org.apache.sysds.runtime.compress.CompressedMatrixBlockFactory; | ||
import org.apache.sysds.runtime.compress.DMLCompressionException; | ||
import org.apache.sysds.runtime.io.FileFormatProperties; | ||
import org.apache.sysds.runtime.io.IOUtilFunctions; | ||
import org.apache.sysds.runtime.io.MatrixWriter; | ||
import org.apache.sysds.runtime.matrix.data.MatrixBlock; | ||
import org.apache.sysds.runtime.util.HDFSTool; | ||
|
||
public class WriterCompressed extends MatrixWriter { | ||
|
||
public static WriterCompressed create(FileFormatProperties props) { | ||
return new WriterCompressed(); | ||
} | ||
|
||
public static void writeCompressedMatrixToHDFS(MatrixBlock src, String fname) throws IOException { | ||
create(null).writeMatrixToHDFS(src, fname, 0, 0, 0, 0, false); | ||
} | ||
|
||
@Override | ||
public void writeMatrixToHDFS(MatrixBlock src, String fname, long rlen, long clen, int blen, long nnz, boolean diag) | ||
throws IOException { | ||
JobConf job = new JobConf(ConfigurationManager.getCachedJobConf()); | ||
Path path = new Path(fname); | ||
FileSystem fs = IOUtilFunctions.getFileSystem(path, job); | ||
|
||
HDFSTool.deleteFileIfExistOnHDFS(fname); | ||
try { | ||
writeCompressedMatrixToHDFS(path, job, fs, src); | ||
} | ||
catch(DMLCompressionException ce) { | ||
fs.delete(path, true); | ||
throw ce; | ||
} | ||
finally { | ||
IOUtilFunctions.deleteCrcFilesFromLocalFileSystem(fs, path); | ||
} | ||
} | ||
|
||
@Override | ||
public void writeEmptyMatrixToHDFS(String fname, long rlen, long clen, int blen) throws IOException { | ||
throw new NotImplementedException(); | ||
} | ||
|
||
private void writeCompressedMatrixToHDFS(Path path, JobConf conf, FileSystem fs, MatrixBlock src) | ||
throws IOException { | ||
final OutputStream os = fs.create(path, true); | ||
final DataOutput out = new DataOutputStream(os); | ||
try { | ||
final MatrixBlock mb = src instanceof CompressedMatrixBlock ? // If compressed | ||
src : // Do not compress | ||
CompressedMatrixBlockFactory.compress(src).getLeft(); // otherwise compress | ||
|
||
if(!(mb instanceof CompressedMatrixBlock)) | ||
throw new DMLCompressionException("Input was not compressed, therefore the file was not saved to disk"); | ||
|
||
CompressedMatrixBlock cmb = (CompressedMatrixBlock) mb; | ||
cmb.write(out); | ||
} | ||
finally { | ||
os.close(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.