Skip to content

Commit

Permalink
Use Intel hardware-accelerated Deflater for writing BAM files where a…
Browse files Browse the repository at this point in the history
…ppropriate. This is only supported on unix systems. net.sf.samtools.util.zip.IntelDeflater is used instead of java.util.zip.Deflater.

In order to use IntelDeflater, a shared library libIntelDeflater.so must be dynamically loaded.  This library is included in the picard-tools zipfile.  The shared library is found via one of the following methods:
  
  If -Dsamjdk.intel_deflater_so_path is set and points to the shared library, it is loaded from that location.
  Else if the shared library is in the same directory as the Picard jar that is executing, it is loaded from that location.  We presume that this will be the typical way in which the shared library will be found.
  Else if the shared library is found on LD_LIBRARY_PATH, it is loaded from there.
  Else java.util.zip.Deflater is used.
  
  Use of IntelDeflater may be suppressed with -Dsamjdk.try_use_intel_deflater=false.
  
  In the header line written by Picard command-line programs, at the end of the line will appear either IntelDeflater or JdkDeflater to indicate which has been loaded.
  
  We have seen compression time reduced by 13% to 33% depending on the hardware.
  • Loading branch information
alecw committed Nov 20, 2013
1 parent c51de97 commit bf03b25
Show file tree
Hide file tree
Showing 10 changed files with 1,026 additions and 3 deletions.
4 changes: 4 additions & 0 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,8 @@
<zip zipfile="${dist}/picard-tools-${picard-version}.zip">
<zipfileset dir="${dist}" includes="*.jar" prefix="picard-tools-${picard-version}"/>
<zipfileset dir="${lib}" includes="snappy*.jar"/>
<!-- distribute libJniDeflater.so in same directory as jarfiles-->
<zipfileset dir="${lib}/jni" includes="*" prefix="picard-tools-${picard-version}"/>
</zip>
</target>

Expand All @@ -372,6 +374,8 @@
<fileset dir="${command_tmp}" includes="org/xerial/snappy/native/Linux/**/*"/>
<fileset dir="${command_tmp}" includes="org/xerial/snappy/VERSION"/>
<fileset dir="${command_tmp}" includes="META-INF/maven/org.xerial.snappy/snappy-java/pom.properties"/>
<!-- Force inclusion of IntelDeflater, which is dynamically linked. -->
<fileset dir="${command_tmp}" includes="net/sf/samtools/util/zip/*"/>
<filesets/>
<manifest>
<attribute name="Implementation-Title" value="@{title}"/>
Expand Down
Binary file added lib/jni/libIntelDeflater.so
Binary file not shown.
247 changes: 247 additions & 0 deletions src/c/inteldeflater/IntelDeflater.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
/*
* Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

/*
* Native method support for net.sf.samtools.util.zip.IntelDeflater.
* This is copied from OpenJDK native support for java.util.zip.Deflater, with only package and class name changed.
*/

#include <stdio.h>
#include <stdlib.h>
#include "jlong.h"
#include "jni.h"
#include "jni_util.h"
#include "zlib.h"

#include "net_sf_samtools_util_zip_IntelDeflater.h"

#define DEF_MEM_LEVEL 8

static jfieldID levelID;
static jfieldID strategyID;
static jfieldID setParamsID;
static jfieldID finishID;
static jfieldID finishedID;
static jfieldID bufID, offID, lenID;

JNIEXPORT void JNICALL
Java_net_sf_samtools_util_zip_IntelDeflater_initIDs(JNIEnv *env, jclass cls)
{
levelID = (*env)->GetFieldID(env, cls, "level", "I");
strategyID = (*env)->GetFieldID(env, cls, "strategy", "I");
setParamsID = (*env)->GetFieldID(env, cls, "setParams", "Z");
finishID = (*env)->GetFieldID(env, cls, "finish", "Z");
finishedID = (*env)->GetFieldID(env, cls, "finished", "Z");
bufID = (*env)->GetFieldID(env, cls, "buf", "[B");
offID = (*env)->GetFieldID(env, cls, "off", "I");
lenID = (*env)->GetFieldID(env, cls, "len", "I");
}

JNIEXPORT jlong JNICALL
Java_net_sf_samtools_util_zip_IntelDeflater_init(JNIEnv *env, jclass cls, jint level,
jint strategy, jboolean nowrap)
{
z_stream *strm = calloc(1, sizeof(z_stream));

if (strm == 0) {
JNU_ThrowOutOfMemoryError(env, 0);
return jlong_zero;
} else {
char *msg;
switch (deflateInit2(strm, level, Z_DEFLATED,
nowrap ? -MAX_WBITS : MAX_WBITS,
DEF_MEM_LEVEL, strategy)) {
case Z_OK:
return ptr_to_jlong(strm);
case Z_MEM_ERROR:
free(strm);
JNU_ThrowOutOfMemoryError(env, 0);
return jlong_zero;
case Z_STREAM_ERROR:
free(strm);
JNU_ThrowIllegalArgumentException(env, 0);
return jlong_zero;
default:
msg = strm->msg;
free(strm);
JNU_ThrowInternalError(env, msg);
return jlong_zero;
}
}
}

JNIEXPORT void JNICALL
Java_net_sf_samtools_util_zip_IntelDeflater_setDictionary(JNIEnv *env, jclass cls, jlong addr,
jarray b, jint off, jint len)
{
Bytef *buf = (*env)->GetPrimitiveArrayCritical(env, b, 0);
int res;
if (buf == 0) {/* out of memory */
return;
}
res = deflateSetDictionary((z_stream *)jlong_to_ptr(addr), buf + off, len);
(*env)->ReleasePrimitiveArrayCritical(env, b, buf, 0);
switch (res) {
case Z_OK:
break;
case Z_STREAM_ERROR:
JNU_ThrowIllegalArgumentException(env, 0);
break;
default:
JNU_ThrowInternalError(env, ((z_stream *)jlong_to_ptr(addr))->msg);
break;
}
}

JNIEXPORT jint JNICALL
Java_net_sf_samtools_util_zip_IntelDeflater_deflateBytes(JNIEnv *env, jobject this, jlong addr,
jarray b, jint off, jint len, jint flush)
{
z_stream *strm = jlong_to_ptr(addr);

jarray this_buf = (*env)->GetObjectField(env, this, bufID);
jint this_off = (*env)->GetIntField(env, this, offID);
jint this_len = (*env)->GetIntField(env, this, lenID);
jbyte *in_buf;
jbyte *out_buf;
int res;
if ((*env)->GetBooleanField(env, this, setParamsID)) {
int level = (*env)->GetIntField(env, this, levelID);
int strategy = (*env)->GetIntField(env, this, strategyID);
in_buf = (*env)->GetPrimitiveArrayCritical(env, this_buf, 0);
if (in_buf == NULL) {
// Throw OOME only when length is not zero
if (this_len != 0)
JNU_ThrowOutOfMemoryError(env, 0);
return 0;
}
out_buf = (*env)->GetPrimitiveArrayCritical(env, b, 0);
if (out_buf == NULL) {
(*env)->ReleasePrimitiveArrayCritical(env, this_buf, in_buf, 0);
if (len != 0)
JNU_ThrowOutOfMemoryError(env, 0);
return 0;
}

strm->next_in = (Bytef *) (in_buf + this_off);
strm->next_out = (Bytef *) (out_buf + off);
strm->avail_in = this_len;
strm->avail_out = len;
res = deflateParams(strm, level, strategy);
(*env)->ReleasePrimitiveArrayCritical(env, b, out_buf, 0);
(*env)->ReleasePrimitiveArrayCritical(env, this_buf, in_buf, 0);

switch (res) {
case Z_OK:
(*env)->SetBooleanField(env, this, setParamsID, JNI_FALSE);
this_off += this_len - strm->avail_in;
(*env)->SetIntField(env, this, offID, this_off);
(*env)->SetIntField(env, this, lenID, strm->avail_in);
return len - strm->avail_out;
case Z_BUF_ERROR:
(*env)->SetBooleanField(env, this, setParamsID, JNI_FALSE);
return 0;
default:
JNU_ThrowInternalError(env, strm->msg);
return 0;
}
} else {
jboolean finish = (*env)->GetBooleanField(env, this, finishID);
in_buf = (*env)->GetPrimitiveArrayCritical(env, this_buf, 0);
if (in_buf == NULL) {
if (this_len != 0)
JNU_ThrowOutOfMemoryError(env, 0);
return 0;
}
out_buf = (*env)->GetPrimitiveArrayCritical(env, b, 0);
if (out_buf == NULL) {
(*env)->ReleasePrimitiveArrayCritical(env, this_buf, in_buf, 0);
if (len != 0)
JNU_ThrowOutOfMemoryError(env, 0);

return 0;
}

strm->next_in = (Bytef *) (in_buf + this_off);
strm->next_out = (Bytef *) (out_buf + off);
strm->avail_in = this_len;
strm->avail_out = len;
res = deflate(strm, finish ? Z_FINISH : flush);
(*env)->ReleasePrimitiveArrayCritical(env, b, out_buf, 0);
(*env)->ReleasePrimitiveArrayCritical(env, this_buf, in_buf, 0);

switch (res) {
case Z_STREAM_END:
(*env)->SetBooleanField(env, this, finishedID, JNI_TRUE);
/* fall through */
case Z_OK:
this_off += this_len - strm->avail_in;
(*env)->SetIntField(env, this, offID, this_off);
(*env)->SetIntField(env, this, lenID, strm->avail_in);
return len - strm->avail_out;
case Z_BUF_ERROR:
return 0;
default:
JNU_ThrowInternalError(env, strm->msg);
return 0;
}
}
}

JNIEXPORT jint JNICALL
Java_net_sf_samtools_util_zip_IntelDeflater_getAdler(JNIEnv *env, jclass cls, jlong addr)
{
return ((z_stream *)jlong_to_ptr(addr))->adler;
}

JNIEXPORT jlong JNICALL
Java_net_sf_samtools_util_zip_IntelDeflater_getBytesRead(JNIEnv *env, jclass cls, jlong addr)
{
return ((z_stream *)jlong_to_ptr(addr))->total_in;
}

JNIEXPORT jlong JNICALL
Java_net_sf_samtools_util_zip_IntelDeflater_getBytesWritten(JNIEnv *env, jclass cls, jlong addr)
{
return ((z_stream *)jlong_to_ptr(addr))->total_out;
}

JNIEXPORT void JNICALL
Java_net_sf_samtools_util_zip_IntelDeflater_reset(JNIEnv *env, jclass cls, jlong addr)
{
if (deflateReset((z_stream *)jlong_to_ptr(addr)) != Z_OK) {
JNU_ThrowInternalError(env, 0);
}
}

JNIEXPORT void JNICALL
Java_net_sf_samtools_util_zip_IntelDeflater_end(JNIEnv *env, jclass cls, jlong addr)
{
if (deflateEnd((z_stream *)jlong_to_ptr(addr)) == Z_STREAM_ERROR) {
JNU_ThrowInternalError(env, 0);
} else {
free((z_stream *)jlong_to_ptr(addr));
}
}
4 changes: 3 additions & 1 deletion src/java/net/sf/picard/cmdline/CommandLineProgram.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import net.sf.samtools.util.BlockCompressedOutputStream;
import net.sf.samtools.util.BlockCompressedStreamConstants;
import net.sf.samtools.util.IOUtil;
import net.sf.samtools.util.zip.DeflaterFactory;

/**
* Abstract class to facilitate writing command-line programs.
Expand Down Expand Up @@ -167,7 +168,8 @@ public int instanceMain(final String[] argv) {
" on " + System.getProperty("os.name") + " " + System.getProperty("os.version") +
" " + System.getProperty("os.arch") + "; " + System.getProperty("java.vm.name") +
" " + System.getProperty("java.runtime.version") +
"; Picard version: " + commandLineParser.getVersion());
"; Picard version: " + commandLineParser.getVersion() +
" " + (DeflaterFactory.usingIntelDeflater()? "IntelDeflater": "JdkDeflater"));
}
catch (Exception e) { /* Unpossible! */ }
}
Expand Down
9 changes: 9 additions & 0 deletions src/java/net/sf/samtools/Defaults.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,21 @@ public class Defaults {
/** Buffer size, in bytes, used whenever reading/writing files or streams. Default = 128k. */
public static final int BUFFER_SIZE;

/** Should BlockCompressedOutputStream attempt to load libIntelDeflater? */
public static final boolean TRY_USE_INTEL_DEFLATER;

/** Path to libIntelDeflater.so. If this is not set, the library is looked for in the directory
* where the executable jar lives. */
public static final String INTEL_DEFLATER_SHARED_LIBRARY_PATH;

static {
CREATE_INDEX = getBooleanProperty("create_index", false);
CREATE_MD5 = getBooleanProperty("create_md5", false);
USE_ASYNC_IO = getBooleanProperty("use_async_io", false);
COMPRESSION_LEVEL = getIntProperty("compression_level", 5);
BUFFER_SIZE = getIntProperty("buffer_size", 1024 * 128);
TRY_USE_INTEL_DEFLATER = getBooleanProperty(";", true);
INTEL_DEFLATER_SHARED_LIBRARY_PATH = getStringProperty("intel_deflater_so_path", null);
}

/** Gets a string system property, prefixed with "samjdk." using the default if the property does not exist.*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
*/
package net.sf.samtools.util;

import net.sf.samtools.util.zip.DeflaterFactory;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
Expand Down Expand Up @@ -83,6 +85,8 @@ public static int getDefaultCompressionLevel() {
// which would attempt to compress up to 64K bytes, and if the resulting compressed block was too large,
// try compressing fewer input bytes (aka "downshifting'). The problem with downshifting is that
// getFilePointer might return an inaccurate value.
// I assume (AW 29-Oct-2013) that there is no value in using hardware-assisted deflater for no-compression mode,
// so just use JDK standard.
private final Deflater noCompressionDeflater = new Deflater(Deflater.NO_COMPRESSION, true);
private final CRC32 crc32 = new CRC32();
private File file = null;
Expand Down Expand Up @@ -121,7 +125,7 @@ public BlockCompressedOutputStream(final String filename, final int compressionL
public BlockCompressedOutputStream(final File file, final int compressionLevel) {
this.file = file;
codec = new BinaryCodec(file, true);
deflater = new Deflater(compressionLevel, true);
deflater = DeflaterFactory.makeDeflater(compressionLevel, true);
}

/**
Expand All @@ -138,7 +142,7 @@ public BlockCompressedOutputStream(final OutputStream os, final File file, final
if (file != null) {
codec.setOutputFileName(file.getAbsolutePath());
}
deflater = new Deflater(compressionLevel, true);
deflater = DeflaterFactory.makeDeflater(compressionLevel, true);
}

/**
Expand Down
Loading

0 comments on commit bf03b25

Please sign in to comment.