Skip to content

Commit

Permalink
* Add presets for MKL
Browse files Browse the repository at this point in the history
  • Loading branch information
saudet committed Apr 5, 2017
1 parent e77593c commit 190fcfc
Show file tree
Hide file tree
Showing 10 changed files with 52,948 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Add missing call to `Loader.load()` in helper class for `opencv_ml` ([issue bytedeco/javacv#638](https://github.com/bytedeco/javacv/issues/638))
* Work around issues with TensorFlow on some versions of Mac OS X ([issue #335](https://github.com/bytedeco/javacpp-presets/issues/335))
* Upgrade presets for OpenCV 3.2.0, FlyCapture 2.10.3.266 ([pull #400](https://github.com/bytedeco/javacpp-presets/pull/400)), libdc1394 2.2.5, LLVM 4.0.0 ([pull #404](https://github.com/bytedeco/javacpp-presets/pull/404)), cuDNN 6.0, TensorFlow 1.0.1
* Add presets for libfreenect2 ([pull #340](https://github.com/bytedeco/javacpp-presets/pull/340)) and LiquidFun ([pull #356](https://github.com/bytedeco/javacpp-presets/pull/356))
* Add presets for MKL, libfreenect2 ([pull #340](https://github.com/bytedeco/javacpp-presets/pull/340)) and LiquidFun ([pull #356](https://github.com/bytedeco/javacpp-presets/pull/356))
* Fix the `FlyCapture2` module for some versions on Windows ([issue #337](https://github.com/bytedeco/javacpp-presets/issues/337))
* Add functions missing from the presets of MXNet ([issue #332](https://github.com/bytedeco/javacpp-presets/issues/332))
* Add presets for the `text` module of OpenCV 3.x ([pull #333](https://github.com/bytedeco/javacpp-presets/pull/333))
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ Each child module in turn relies on its corresponding native libraries being alr
* Chilitags https://github.com/chili-epfl/chilitags
* flandmark 1.07 http://cmp.felk.cvut.cz/~uricamic/flandmark/#download
* HDF5 1.10.0 https://support.hdfgroup.org/HDF5/
* MKL 2017.2 https://software.intel.com/intel-mkl
* OpenBLAS 0.2.19 http://www.openblas.net/
* FFTW 3.3.5 http://www.fftw.org/download.html
* GSL 2.2.1 http://www.gnu.org/software/gsl/#downloading
Expand Down
2 changes: 1 addition & 1 deletion cppbuild.sh
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ function download {
}

if [[ -z ${PROJECTS:-} ]]; then
PROJECTS=(opencv ffmpeg flycapture libdc1394 libfreenect libfreenect2 librealsense videoinput artoolkitplus chilitags flandmark hdf5 openblas fftw gsl llvm leptonica tesseract caffe cuda mxnet tensorflow liquidfun)
PROJECTS=(opencv ffmpeg flycapture libdc1394 libfreenect libfreenect2 librealsense videoinput artoolkitplus chilitags flandmark hdf5 mkl openblas fftw gsl llvm leptonica tesseract caffe cuda mxnet tensorflow liquidfun)
fi

for PROJECT in ${PROJECTS[@]}; do
Expand Down
160 changes: 160 additions & 0 deletions mkl/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
JavaCPP Presets for MKL
=======================

Introduction
------------
This directory contains the JavaCPP Presets module for:

* MKL 2017.2 https://software.intel.com/intel-mkl

Please refer to the parent README.md file for more detailed information about the JavaCPP Presets.


Documentation
-------------
Java API documentation is available here:

* http://bytedeco.org/javacpp-presets/mkl/apidocs/

∗ MKL also gets used by the [JavaCPP Presets for OpenBLAS](https://github.com/bytedeco/javacpp-presets/tree/master/openblas).


Sample Usage
------------
Here is a simple example of MKL ported to Java from the `dgemm_example.c` sample file included in `mkl_c_samples_022017_0.zip` available at:

* https://software.intel.com/product-code-samples

We can use [Maven 3](http://maven.apache.org/) to download and install automatically all the class files as well as the native binaries. To run this sample code, after creating the `pom.xml` and `src/main/java/DGEMMExample.java` source files below, simply execute on the command line:
```bash
$ mvn compile exec:java
```

### The `pom.xml` build file
```xml
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.bytedeco.javacpp-presets.mkl</groupId>
<artifactId>mkl</artifactId>
<version>1.3.3-SNAPSHOT</version>
<properties>
<exec.mainClass>DGEMMExample</exec.mainClass>
</properties>
<dependencies>
<dependency>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>mkl-platform</artifactId>
<version>2017.2-1.3.3-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
```

### The `src/main/java/DGEMMExample.java` source file
```java
//==============================================================
//
// SAMPLE SOURCE CODE - SUBJECT TO THE TERMS OF SAMPLE CODE LICENSE AGREEMENT,
// http://software.intel.com/en-us/articles/intel-sample-source-code-license-agreement/
//
// Copyright 2016-2017 Intel Corporation
//
// THIS FILE IS PROVIDED "AS IS" WITH NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT
// NOT LIMITED TO ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE, NON-INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS.
//
// =============================================================
/*******************************************************************************
* This example computes real matrix C=alpha*A*B+beta*C using Intel(R) MKL
* function dgemm, where A, B, and C are matrices and alpha and beta are
* scalars in double precision.
*
* In this simple example, practices such as memory management, data alignment,
* and I/O that are necessary for good programming style and high MKL
* performance are omitted to improve readability.
********************************************************************************/

import org.bytedeco.javacpp.*;
import org.bytedeco.javacpp.indexer.*;

import static org.bytedeco.javacpp.mkl_rt.*;

public class DGEMMExample {
public static void main(String[] args) throws Exception {
System.out.println("\n This example computes real matrix C=alpha*A*B+beta*C using \n"
+ " Intel(R) MKL function dgemm, where A, B, and C are matrices and \n"
+ " alpha and beta are double precision scalars\n");

int m = 2000, p = 200, n = 1000;
System.out.printf(" Initializing data for matrix multiplication C=A*B for matrix \n"
+ " A(%dx%d) and matrix B(%dx%d)\n\n", m, p, p, n);
double alpha = 1.0, beta = 0.0;

System.out.println(" Allocating memory for matrices aligned on 64-byte boundary for better \n"
+ " performance \n");
DoublePointer A = new DoublePointer(MKL_malloc(m * p * Double.BYTES, 64));
DoublePointer B = new DoublePointer(MKL_malloc(p * n * Double.BYTES, 64));
DoublePointer C = new DoublePointer(MKL_malloc(m * n * Double.BYTES, 64));
if (A.isNull() || B.isNull() || C.isNull()) {
System.out.println( "\n ERROR: Can't allocate memory for matrices. Aborting... \n");
MKL_free(A);
MKL_free(B);
MKL_free(C);
System.exit(1);
}

System.out.println(" Intializing matrix data \n");
DoubleIndexer Aidx = DoubleIndexer.create(A.capacity(m * p));
for (int i = 0; i < m * p; i++) {
A.put(i, (double)(i + 1));
}

DoubleIndexer Bidx = DoubleIndexer.create(B.capacity(p * n));
for (int i = 0; i < p * n; i++) {
B.put(i, (double)(-i - 1));
}

DoubleIndexer Cidx = DoubleIndexer.create(C.capacity(m * n));
for (int i = 0; i < m * n; i++) {
C.put(i, 0.0);
}

System.out.println(" Computing matrix product using Intel(R) MKL dgemm function via CBLAS interface \n");
cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans,
m, n, p, alpha, A, p, B, n, beta, C, n);
System.out.println("\n Computations completed.\n");

System.out.println(" Top left corner of matrix A: ");
for (int i = 0; i < Math.min(m, 6); i++) {
for (int j = 0; j< Math.min(p, 6); j++) {
System.out.printf("%12.0f", Aidx.get(j + i * p));
}
System.out.println();
}

System.out.println("\n Top left corner of matrix B: ");
for (int i = 0; i < Math.min(p, 6); i++) {
for (int j = 0; j < Math.min(n, 6); j++) {
System.out.printf("%12.0f", Bidx.get(j + i * n));
}
System.out.println();
}

System.out.println("\n Top left corner of matrix C: ");
for (int i = 0; i < Math.min(m, 6); i++) {
for (int j = 0; j < Math.min(n, 6); j++) {
System.out.printf("%12.5G", Cidx.get(j + i * n));
}
System.out.println();
}

System.out.println("\n Deallocating memory \n");
MKL_free(A);
MKL_free(B);
MKL_free(C);

System.out.println(" Example completed. \n");
System.exit(0);
}
}
```
32 changes: 32 additions & 0 deletions mkl/cppbuild.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/bash
# This file is meant to be included by the parent cppbuild.sh script
if [[ -z "$PLATFORM" ]]; then
pushd ..
bash cppbuild.sh "$@" mkl
popd
exit
fi

case $PLATFORM in
linux-*)
if [[ ! -d "/opt/intel/mkl/" ]]; then
echo "Please install MKL under the default installation directory"
exit 1
fi
;;
macosx-*)
if [[ ! -d "/opt/intel/mkl/" ]]; then
echo "Please install MKL under the default installation directory"
exit 1
fi
;;
windows-*)
if [[ ! -d "/C/Program Files (x86)/IntelSWTools/compilers_and_libraries/windows/mkl/" ]]; then
echo "Please install MKL under the default installation directory"
exit 1
fi
;;
*)
echo "Error: Platform \"$PLATFORM\" is not supported"
;;
esac
80 changes: 80 additions & 0 deletions mkl/platform/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.bytedeco</groupId>
<artifactId>javacpp-presets</artifactId>
<version>1.3.3-SNAPSHOT</version>
<relativePath>../../</relativePath>
</parent>

<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>mkl-platform</artifactId>
<version>2017.2-${project.parent.version}</version>
<name>JavaCPP Presets Platform for MKL</name>

<properties>
<javacpp.moduleId>mkl</javacpp.moduleId>
</properties>

<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>${javacpp.moduleId}</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>${javacpp.moduleId}</artifactId>
<version>${project.version}</version>
<classifier>${javacpp.platform.linux-x86}</classifier>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>${javacpp.moduleId}</artifactId>
<version>${project.version}</version>
<classifier>${javacpp.platform.linux-x86_64}</classifier>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>${javacpp.moduleId}</artifactId>
<version>${project.version}</version>
<classifier>${javacpp.platform.macosx-x86_64}</classifier>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>${javacpp.moduleId}</artifactId>
<version>${project.version}</version>
<classifier>${javacpp.platform.windows-x86}</classifier>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>${javacpp.moduleId}</artifactId>
<version>${project.version}</version>
<classifier>${javacpp.platform.windows-x86_64}</classifier>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>default-jar</id>
<configuration>
<archive>
<manifestEntries>
<Class-Path>${javacpp.moduleId}.jar ${javacpp.moduleId}-linux-x86.jar ${javacpp.moduleId}-linux-x86_64.jar ${javacpp.moduleId}-macosx-x86_64.jar ${javacpp.moduleId}-windows-x86.jar ${javacpp.moduleId}-windows-x86_64.jar</Class-Path>
</manifestEntries>
</archive>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
Loading

0 comments on commit 190fcfc

Please sign in to comment.