Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce kudo writer. #2559

Merged
merged 10 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
/**
* This class contains utility methods for checking preconditions.
*/
class Preconditions {
public class Preconditions {
/**
* Check if the condition is true, otherwise throw an IllegalStateException with the given message.
*/
Expand Down
liurenjie1024 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright (c) 2024, NVIDIA CORPORATION.
*
* Licensed 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 com.nvidia.spark.rapids.jni.kudo;

import ai.rapids.cudf.HostMemoryBuffer;

import java.io.DataOutputStream;
import java.io.IOException;

/**
* Visible for testing
*/
class DataOutputStreamWriter extends DataWriter {
private final byte[] arrayBuffer = new byte[1024 * 128];
private final DataOutputStream dout;

public DataOutputStreamWriter(DataOutputStream dout) {
this.dout = dout;
}

@Override
public void writeByte(byte b) throws IOException {
dout.writeByte(b);
}

@Override
public void writeShort(short s) throws IOException {
dout.writeShort(s);
}

@Override
public void writeInt(int i) throws IOException {
dout.writeInt(i);
}

@Override
public void writeIntNativeOrder(int i) throws IOException {
// TODO this only works on Little Endian Architectures, x86. If we need
// to support others we need to detect the endianness and switch on the right implementation.
writeInt(Integer.reverseBytes(i));
}

@Override
public void writeLong(long val) throws IOException {
dout.writeLong(val);
}

@Override
public void copyDataFrom(HostMemoryBuffer src, long srcOffset, long len) throws IOException {
long dataLeft = len;
while (dataLeft > 0) {
int amountToCopy = (int) Math.min(arrayBuffer.length, dataLeft);
src.getBytes(arrayBuffer, 0, srcOffset, amountToCopy);
dout.write(arrayBuffer, 0, amountToCopy);
srcOffset += amountToCopy;
dataLeft -= amountToCopy;
}
}

@Override
public void flush() throws IOException {
dout.flush();
}

@Override
public void write(byte[] arr, int offset, int length) throws IOException {
dout.write(arr, offset, length);
}
}
52 changes: 52 additions & 0 deletions src/main/java/com/nvidia/spark/rapids/jni/kudo/DataWriter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2024, NVIDIA CORPORATION.
*
* Licensed 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 com.nvidia.spark.rapids.jni.kudo;

import ai.rapids.cudf.HostMemoryBuffer;

import java.io.IOException;

/**
* Visible for testing
*/
abstract class DataWriter {

public abstract void writeByte(byte b) throws IOException;

public abstract void writeShort(short s) throws IOException;

public abstract void writeInt(int i) throws IOException;

public abstract void writeIntNativeOrder(int i) throws IOException;
liurenjie1024 marked this conversation as resolved.
Show resolved Hide resolved

public abstract void writeLong(long val) throws IOException;

/**
* Copy data from src starting at srcOffset and going for len bytes.
*
* @param src where to copy from.
* @param srcOffset offset to start at.
* @param len amount to copy.
*/
public abstract void copyDataFrom(HostMemoryBuffer src, long srcOffset, long len) throws IOException;

public void flush() throws IOException {
// NOOP by default
}

public abstract void write(byte[] arr, int offset, int length) throws IOException;
}
Loading
Loading