├── .gitignore
├── LICENSE
├── Makefile
├── Makefile.config.template
├── README.md
├── accrdd
├── .gitignore
├── build
│ └── mvn
├── pom.xml
└── src
│ └── main
│ ├── java
│ └── org
│ │ └── apache
│ │ └── spark
│ │ └── blaze
│ │ ├── .gitignore
│ │ └── DataTransmitter.java
│ └── scala
│ └── org
│ └── apache
│ └── spark
│ └── blaze
│ ├── AccMapPartitionsRDD.scala
│ ├── AccRDD.scala
│ ├── Accelerator.scala
│ ├── BlazeBroadcast.scala
│ ├── BlazeMemoryFileHandler.scala
│ ├── BlazeRuntime.scala
│ ├── BlockInfo.scala
│ ├── ShellRDD.scala
│ ├── TestSampler.scala
│ └── Util.scala
├── common
├── Makefile
├── acc_conf.proto
├── msgGamNam.proto
├── python
│ └── .gitignore
└── task.proto
├── examples
├── .gitignore
├── arrayTest
│ ├── app
│ │ ├── pom.xml
│ │ ├── run-local.sh.template
│ │ └── src
│ │ │ └── main
│ │ │ └── scala
│ │ │ └── fcs_runtime
│ │ │ └── examples
│ │ │ └── ArrayTest.scala
│ ├── cpp_app
│ │ ├── .gitignore
│ │ ├── Makefile
│ │ └── main.cpp
│ ├── cpu_task
│ │ ├── ArrayTest.cpp
│ │ └── Makefile
│ └── gpu_task
│ │ ├── .gitignore
│ │ ├── ArrayTest.cl
│ │ ├── ArrayTest.cpp
│ │ └── Makefile
├── loopback
│ ├── aal_task
│ │ ├── conf.prototxt
│ │ └── src
│ │ │ ├── Makefile
│ │ │ ├── NLB.cpp
│ │ │ └── NLBApp.h
│ └── app
│ │ ├── pom.xml
│ │ ├── run-local.sh.template
│ │ └── src
│ │ └── main
│ │ └── scala
│ │ └── LoopBack
│ │ └── LoopBack.scala
└── pi
│ ├── app
│ ├── .gitignore
│ ├── Makefile
│ ├── map.cpp
│ ├── map.sh
│ ├── reduce.cpp
│ ├── reduce.sh
│ └── run.sh
│ └── cpu_task
│ ├── Makefile
│ ├── Pi.cpp
│ └── conf
├── functional-test
├── arraytest
│ ├── acc_task
│ │ ├── ArrayTest.cpp
│ │ └── Makefile
│ ├── app
│ │ ├── .gitignore
│ │ ├── pom.xml
│ │ ├── run.sh.template
│ │ └── src
│ │ │ └── main
│ │ │ └── scala
│ │ │ └── testapp
│ │ │ └── ArrayTest.scala
│ ├── client
│ │ ├── Makefile
│ │ └── main.cpp
│ └── ocl_task
│ │ ├── ArrayTest.cl
│ │ ├── ArrayTest.cpp
│ │ └── Makefile
├── conf.prototxt
├── masktest
│ ├── acc_task
│ │ ├── Makefile
│ │ └── MaskTest.cpp
│ └── app
│ │ ├── pom.xml
│ │ └── src
│ │ └── main
│ │ └── scala
│ │ └── testapp
│ │ └── MaskTest.scala
└── tuple2test
│ ├── acc_task
│ ├── Makefile
│ └── Tuple2Test.cpp
│ └── app
│ ├── pom.xml
│ └── src
│ └── main
│ └── scala
│ └── testapp
│ └── Tuple2Test.scala
├── manager
├── bin
│ └── .gitignore
├── include
│ └── blaze
│ │ ├── AccAgent.h
│ │ ├── Admin.h
│ │ ├── BlazeTest.h
│ │ ├── Block.h
│ │ ├── BlockManager.h
│ │ ├── Client.h
│ │ ├── CommManager.h
│ │ ├── Common.h
│ │ ├── Platform.h
│ │ ├── PlatformManager.h
│ │ ├── QueueManager.h
│ │ ├── Task.h
│ │ ├── TaskEnv.h
│ │ ├── TaskManager.h
│ │ ├── TaskQueue.h
│ │ ├── main.h
│ │ └── proto
│ │ ├── acc_conf.pb.h
│ │ ├── msgGamNam.pb.h
│ │ └── task.pb.h
├── lib
│ └── .gitignore
├── src
│ ├── .gitignore
│ ├── AccAgent.cpp
│ ├── Admin.cpp
│ ├── AppCommManager.cpp
│ ├── Block.cpp
│ ├── BlockManager.cpp
│ ├── Client.cpp
│ ├── CommManager.cpp
│ ├── Common.cpp
│ ├── GAMCommManager.cpp
│ ├── Makefile
│ ├── Platform.cpp
│ ├── PlatformManager.cpp
│ ├── QueueManager.cpp
│ ├── Task.cpp
│ ├── TaskEnv.cpp
│ ├── TaskManager.cpp
│ ├── admin.cpp
│ ├── main.cpp
│ └── proto
│ │ └── .gitignore
└── test
│ ├── .gitignore
│ ├── Makefile
│ ├── bin
│ └── .gitignore
│ ├── common.mk
│ ├── logs
│ └── .gitignore
│ ├── python
│ ├── .gitignore
│ ├── blaze_client.py
│ ├── delete.py
│ ├── register.py
│ └── task_pb2.py
│ ├── src
│ ├── AppClients.cpp
│ ├── BlockTests.cpp
│ ├── ClientTests.cpp
│ ├── ConfigTests.cpp
│ ├── TestCommon.h
│ └── main.cpp
│ └── tasks
│ ├── Makefile
│ └── cpu
│ ├── Makefile
│ ├── arrayTest
│ ├── Makefile
│ └── arrayTest.cpp
│ ├── common.mk
│ ├── delay
│ ├── Makefile
│ └── delay.cpp
│ └── loopBack
│ ├── Makefile
│ └── loopBack.cpp
├── platforms
├── Makefile
├── intel_aal
│ ├── .gitignore
│ ├── AALBlock.cpp
│ ├── AALBlock.h
│ ├── AALEnv.h
│ ├── AALPlatform.cpp
│ ├── AALPlatform.h
│ ├── Makefile
│ ├── RuntimeClient.cpp
│ └── RuntimeClient.h
└── nv_opencl
│ ├── .gitignore
│ ├── Makefile
│ ├── include
│ └── blaze
│ │ └── nv_opencl
│ │ ├── OpenCLBlock.h
│ │ ├── OpenCLCommon.h
│ │ ├── OpenCLEnv.h
│ │ ├── OpenCLPlatform.h
│ │ └── OpenCLQueueManager.h
│ └── src
│ ├── OpenCLBlock.cpp
│ ├── OpenCLPlatform.cpp
│ ├── OpenCLQueueManager.cpp
│ └── OpenCLTaskEnv.cpp
└── setup.sh
/.gitignore:
--------------------------------------------------------------------------------
1 | *.log
2 | *.o
3 | *.so
4 | *.err
5 | target
6 | Makefile.config
7 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | COPYRIGHT
2 |
3 | All contributions by the University of California:
4 | Copyright (c) 2015, The Regents of the University of California (Regents)
5 | All rights reserved.
6 |
7 | All other contributions:
8 | Copyright (c) 2015, the respective contributors
9 | All rights reserved.
10 |
11 | Blaze uses a shared copyright model: each contributor holds copyright over
12 | their contributions to Blaze. The project versioning records all such
13 | contribution and copyright details. If a contributor wants to further mark
14 | their specific copyright on a particular contribution, they should indicate
15 | their copyright solely in the commit message of the change when it is
16 | committed.
17 |
18 | LICENSE
19 |
20 | Redistribution and use in source and binary forms, with or without
21 | modification, are permitted provided that the following conditions are met:
22 |
23 | 1. Redistributions of source code must retain the above copyright notice, this
24 | list of conditions and the following disclaimer.
25 | 2. Redistributions in binary form must reproduce the above copyright notice,
26 | this list of conditions and the following disclaimer in the documentation
27 | and/or other materials provided with the distribution.
28 |
29 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
30 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
31 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
32 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
33 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
35 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
36 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
38 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 |
40 | CONTRIBUTION AGREEMENT
41 |
42 | By contributing to the UCLA-VAST/blaze repository through pull-request, comment,
43 | or otherwise, the contributor releases their content to the
44 | license and copyright terms herein.
45 |
--------------------------------------------------------------------------------
/Makefile:
--------------------------------------------------------------------------------
1 | all: common accrdd nam
2 |
3 | common:
4 | make -C ./common
5 |
6 | accrdd: common
7 | cd accrdd; mvn package; mvn install
8 |
9 | nam: common
10 | make -C ./manager/src
11 | make -C ./platforms
12 |
13 | examples:
14 | make -C examples
15 |
16 | .PHONY: all common accrdd nam examples
17 |
--------------------------------------------------------------------------------
/Makefile.config.template:
--------------------------------------------------------------------------------
1 | BOOST_DIR=
2 | PROTOBUF_DIR=
3 | GLOG_DIR=
4 | GFLAGS_DIR=
5 | GTEST_DIR=
6 | CUDA_DIR=
7 | ALTERA_OPENCL_DIR=
8 | ALTERA_OPENCL_BSP_DIR=
9 |
10 | CC=gcc
11 | PP=g++
12 | AR=ar
13 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Blaze: Deploying Accelerators at Datacenter Scale
2 | ### Description
3 | Blaze is an accelerator-aware programming framework for warehouse-scale accelerator deployment. Blaze provides a programming interface that is compatible to Spark, an in-memory compute engine for large-scale data processing, and a runtime system that provides transparent accelerator management. With Blaze, the deployment effort of accelerator task is reduced by more than 10x compared to traditional frameworks such as OpenCL. Blaze is designed to efficiently manage accelerator platforms with intelligent data caching and task pipelining schemes to minimize the communication and data movement overheads.
4 |
5 | ### License
6 | The Blaze runtime system is released under [BSD 2-Clause license](https://github.com/BVLC/caffe/blob/master/LICENSE).
7 | The Blaze runtime system includes the following components:
8 |
9 | * AccRDD: A Spark extension including interface between Spark program and accelerators
10 | * manager: A accelerator manager that controls all local accelerators including context setup, data transfer and task execution
11 |
12 | In addition, each accelerator task is compiled with Blaze APIs. Some simple examples are provided in the `./examples` folder, which are released without restriction.
13 |
14 | ### Installing Blaze
15 | 0. **Prerequisites**
16 | 0. Boost (tested with 1.55.0)
17 | 0. Google Protobuf (tested with 2.5.0)
18 | 0. Apache Spark (tested with 1.3.1)
19 | 0. Apache Hadoop (tested with 2.5.2)
20 | 0. **Compiling**
21 | 0. Edit Makefile.config
22 | 0. `export BLAZE_HOME=$PWD/accrdd`
23 | 0. run `make`
24 |
25 | ### Running Loopback example
26 | 0. **Prerequisites**
27 | 0. Intel AALSDK 4.1.7
28 | 0. Intel AAL NLB example RTL
29 | 0. **Compiling**
30 | 0. From root directory run `make examples`
31 | 0. **Execution**
32 | 0. Start NLB ASE
33 | 0. On terminal 1 start Manager: `./manager/bin/acc_manager ./examples/loopback/acc_task/conf.prototxt`
34 | 0. On terminal 2 tart Spark program: `./examples/loopback/app/run.sh` or `./examples/loopback/app/run_local.sh`
35 |
36 | ### Contacts
37 | For any question or discussion, please contact the authors:
38 |
39 | * Di Wu: allwu@cs.ucla.edu
40 | * Hao Yu (Cody): hyu@cs.ucla.edu
41 |
--------------------------------------------------------------------------------
/accrdd/.gitignore:
--------------------------------------------------------------------------------
1 | target
2 |
--------------------------------------------------------------------------------
/accrdd/build/mvn:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | # Determine the current working directory
4 | _DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
5 | # Preserve the calling directory
6 | _CALLING_DIR="$(pwd)"
7 | # Options used during compilation
8 | _COMPILE_JVM_OPTS="-Xmx2g -XX:MaxPermSize=512M -XX:ReservedCodeCacheSize=512m"
9 |
10 | # Installs any application tarball given a URL, the expected tarball name,
11 | # and, optionally, a checkable binary path to determine if the binary has
12 | # already been installed
13 | ## Arg1 - URL
14 | ## Arg2 - Tarball Name
15 | ## Arg3 - Checkable Binary
16 | install_app() {
17 | local remote_tarball="$1/$2"
18 | local local_tarball="${_DIR}/$2"
19 | local binary="${_DIR}/$3"
20 |
21 | # setup `curl` and `wget` silent options if we're running on Jenkins
22 | local curl_opts="-L"
23 | local wget_opts=""
24 | if [ -n "$AMPLAB_JENKINS" ]; then
25 | curl_opts="-s ${curl_opts}"
26 | wget_opts="--quiet ${wget_opts}"
27 | else
28 | curl_opts="--progress-bar ${curl_opts}"
29 | wget_opts="--progress=bar:force ${wget_opts}"
30 | fi
31 |
32 | if [ -z "$3" -o ! -f "$binary" ]; then
33 | # check if we already have the tarball
34 | # check if we have curl installed
35 | # download application
36 | [ ! -f "${local_tarball}" ] && [ $(command -v curl) ] && \
37 | echo "exec: curl ${curl_opts} ${remote_tarball}" && \
38 | curl ${curl_opts} "${remote_tarball}" > "${local_tarball}"
39 | # if the file still doesn't exist, lets try `wget` and cross our fingers
40 | [ ! -f "${local_tarball}" ] && [ $(command -v wget) ] && \
41 | echo "exec: wget ${wget_opts} ${remote_tarball}" && \
42 | wget ${wget_opts} -O "${local_tarball}" "${remote_tarball}"
43 | # if both were unsuccessful, exit
44 | [ ! -f "${local_tarball}" ] && \
45 | echo -n "ERROR: Cannot download $2 with cURL or wget; " && \
46 | echo "please install manually and try again." && \
47 | exit 2
48 | cd "${_DIR}" && tar -xzf "$2"
49 | rm -rf "$local_tarball"
50 | fi
51 | }
52 |
53 | # Install maven under the build/ folder
54 | install_mvn() {
55 | install_app \
56 | "http://archive.apache.org/dist/maven/maven-3/3.2.5/binaries" \
57 | "apache-maven-3.2.5-bin.tar.gz" \
58 | "apache-maven-3.2.5/bin/mvn"
59 | MVN_BIN="${_DIR}/apache-maven-3.2.5/bin/mvn"
60 | }
61 |
62 | # Install zinc under the build/ folder
63 | install_zinc() {
64 | local zinc_path="zinc-0.3.5.3/bin/zinc"
65 | [ ! -f "${zinc_path}" ] && ZINC_INSTALL_FLAG=1
66 | install_app \
67 | "http://downloads.typesafe.com/zinc/0.3.5.3" \
68 | "zinc-0.3.5.3.tgz" \
69 | "${zinc_path}"
70 | ZINC_BIN="${_DIR}/${zinc_path}"
71 | }
72 |
73 | # Determine the Scala version from the root pom.xml file, set the Scala URL,
74 | # and, with that, download the specific version of Scala necessary under
75 | # the build/ folder
76 | install_scala() {
77 | # determine the Scala version used in Spark
78 | local scala_version=`grep "scala.version" "${_DIR}/../pom.xml" | \
79 | head -1 | cut -f2 -d'>' | cut -f1 -d'<'`
80 | local scala_bin="${_DIR}/scala-${scala_version}/bin/scala"
81 |
82 | install_app \
83 | "http://downloads.typesafe.com/scala/${scala_version}" \
84 | "scala-${scala_version}.tgz" \
85 | "scala-${scala_version}/bin/scala"
86 |
87 | SCALA_COMPILER="$(cd "$(dirname ${scala_bin})/../lib" && pwd)/scala-compiler.jar"
88 | SCALA_LIBRARY="$(cd "$(dirname ${scala_bin})/../lib" && pwd)/scala-library.jar"
89 | }
90 |
91 | # Determines if a given application is already installed. If not, will attempt
92 | # to install
93 | ## Arg1 - application name
94 | ## Arg2 - Alternate path to local install under build/ dir
95 | check_and_install_app() {
96 | # create the local environment variable in uppercase
97 | local app_bin="`echo $1 | awk '{print toupper(\$0)}'`_BIN"
98 | # some black magic to set the generated app variable (i.e. MVN_BIN) into the
99 | # environment
100 | eval "${app_bin}=`which $1 2>/dev/null`"
101 |
102 | if [ -z "`which $1 2>/dev/null`" ]; then
103 | install_$1
104 | fi
105 | }
106 |
107 | # Setup healthy defaults for the Zinc port if none were provided from
108 | # the environment
109 | ZINC_PORT=${ZINC_PORT:-"3030"}
110 |
111 | # Check and install all applications necessary to build Spark
112 | check_and_install_app "mvn"
113 |
114 | # Install the proper version of Scala and Zinc for the build
115 | install_zinc
116 | install_scala
117 |
118 | # Reset the current working directory
119 | cd "${_CALLING_DIR}"
120 |
121 | # Now that zinc is ensured to be installed, check its status and, if its
122 | # not running or just installed, start it
123 | if [ -n "${ZINC_INSTALL_FLAG}" -o -z "`${ZINC_BIN} -status`" ]; then
124 | export ZINC_OPTS=${ZINC_OPTS:-"$_COMPILE_JVM_OPTS"}
125 | ${ZINC_BIN} -shutdown
126 | ${ZINC_BIN} -start -port ${ZINC_PORT} \
127 | -scala-compiler "${SCALA_COMPILER}" \
128 | -scala-library "${SCALA_LIBRARY}" &>/dev/null
129 | fi
130 |
131 | # Set any `mvn` options if not already present
132 | export MAVEN_OPTS=${MAVEN_OPTS:-"$_COMPILE_JVM_OPTS"}
133 |
134 | # Last, call the `mvn` command as usual
135 | ${MVN_BIN} "$@"
136 |
--------------------------------------------------------------------------------
/accrdd/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 4.0.0
4 |
5 | org.apache.spark
6 | blaze_2.10
7 | 1.0.1
8 | jar
9 |
10 |
11 | UTF-8
12 | 1.7
13 | 2.10.4
14 | 2.10
15 |
16 |
17 |
18 |
19 |
20 | org.scala-tools
21 | maven-scala-plugin
22 |
23 |
24 |
25 | compile
26 |
27 | compile
28 |
29 | compile
30 |
31 |
32 | test-compile
33 |
34 | testCompile
35 |
36 | test-compile
37 |
38 |
39 | process-resources
40 |
41 | compile
42 |
43 |
44 |
45 |
46 |
47 | maven-compiler-plugin
48 |
49 | 1.5
50 | 1.5
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 | org.scala-lang
59 | scala-library
60 | ${scala.version}
61 |
62 |
63 | junit
64 | junit
65 | 4.11
66 | test
67 |
68 |
69 | org.scalacheck
70 | scalacheck_${scala.version.tools}
71 | 1.12.0
72 | test
73 |
74 |
75 | org.scalatest
76 | scalatest_${scala.version.tools}
77 | 2.2.4
78 | test
79 |
80 |
81 | org.apache.spark
82 | spark-core_${scala.version.tools}
83 | 2.1.0
84 |
85 |
86 | com.google.protobuf
87 | protobuf-java
88 | 2.5.0
89 |
90 |
91 |
92 |
--------------------------------------------------------------------------------
/accrdd/src/main/java/org/apache/spark/blaze/.gitignore:
--------------------------------------------------------------------------------
1 | AccMessage.java
2 |
--------------------------------------------------------------------------------
/accrdd/src/main/scala/org/apache/spark/blaze/AccMapPartitionsRDD.scala:
--------------------------------------------------------------------------------
1 | package org.apache.spark.blaze
2 |
3 | import java.io._
4 | import java.util.ArrayList
5 | import java.nio.ByteBuffer
6 | import java.nio.ByteOrder
7 |
8 | import scala.reflect.{ClassTag, classTag}
9 | import scala.reflect.runtime.universe._
10 | import scala.collection.mutable._
11 |
12 | import org.apache.spark._
13 | import org.apache.spark.{Partition, TaskContext}
14 | import org.apache.spark.rdd._
15 | import org.apache.spark.storage._
16 | import org.apache.spark.scheduler._
17 | import org.apache.spark.util.random.RandomSampler
18 | import org.apache.spark.internal.Logging
19 | /**
20 | * A RDD that uses accelerator to accelerate the computation. The behavior of AccMapPartitionsRDD is
21 | * similar to Spark MapPartitionsRDD which performs the computation for a whole partition at a
22 | * time. AccMapPartitionsRDD also processes a partition to reduce the communication overhead.
23 | *
24 | * @param appId The unique application ID of Spark.
25 | * @param prev The original RDD of Spark.
26 | * @param acc The developer extended accelerator class.
27 | */
28 | class AccMapPartitionsRDD[U: ClassTag, T: ClassTag](
29 | appId: String,
30 | prev: RDD[T],
31 | acc: Accelerator[T, U],
32 | port: Int,
33 | sampler: RandomSampler[Int, Int]
34 | ) extends AccRDD[U, T](appId, prev, acc, port, sampler) with Logging {
35 |
36 | /**
37 | * In case of failing to execute the computation on accelerator,
38 | * Blaze will execute the computation on JVM instead to guarantee the
39 | * functionality correctness, even the overhead of failing to execute
40 | * on accelerator is quite large.
41 | *
42 | * @param split The partition to be executed on JVM.
43 | * @param context TaskContext of Spark.
44 | * @return The output array
45 | */
46 | override def computeOnJTP(split: Partition, context: TaskContext, partitionMask: Array[Byte]): Iterator[U] = {
47 | logInfo("Compute partition " + split.index + " using CPU")
48 | val input: Iterator[T] = firstParent[T].iterator(split, context)
49 |
50 | if (partitionMask != null) { // Sampled dataset
51 | val sampledInput = new Iterator[T] {
52 | val inputAry = input.toArray
53 | var sampledList = List[T]()
54 | var idx = 0
55 | while (idx < inputAry.length) {
56 | if (partitionMask(idx) != 0)
57 | sampledList = sampledList :+ inputAry(idx)
58 | idx += 1
59 | }
60 | idx = 0
61 |
62 | def hasNext : Boolean = {
63 | idx < sampledList.length
64 | }
65 |
66 | def next : T = {
67 | idx += 1
68 | sampledList(idx - 1)
69 | }
70 | }
71 | acc.call(sampledInput)
72 | }
73 | else {
74 | logWarning("Partition " + split.index + " has no mask")
75 | acc.call(input)
76 | }
77 | }
78 | }
79 |
80 |
--------------------------------------------------------------------------------
/accrdd/src/main/scala/org/apache/spark/blaze/Accelerator.scala:
--------------------------------------------------------------------------------
1 | package org.apache.spark.blaze
2 |
3 | /**
4 | * An accelerator interface. Accelerator interface allows the developer to
5 | * extend and assign the hardware accelerator to accelerate the function.
6 | * The value `id` is used to assign the accelerator which should be provided
7 | * by accelerator manager. In addition, in case of unavailable accelerators,
8 | * the developer also has to create a software implementation, `call` method,
9 | * with the same functionality as the accelerator.
10 | *
11 | * @tparam T Type of accelerator input. Only allow primitive types and 1-D array.
12 | * @tparam U Type of accelerator output. Only allow primitive types and 1-D array.
13 | */
14 | trait Accelerator[T, U] extends java.io.Serializable {
15 |
16 | /** The corresponding accelerator ID of the hardware accelerator.
17 | * The developer should consult the manager for the correct ID to use it.
18 | **/
19 | val id: String
20 |
21 | /** The developer should specify the reference variables as arguments of extended
22 | * Accelerator class. This method is used for mapping from the class arguments to
23 | * the order in the hardware accelerator. For example:
24 | *
25 | * {{{
26 | * class MyAccelerator(ref1: BlazeBroadcast[Array[Int]], ref2: BlazeBroadcast[Int])
27 | * extends Accelerator[Array[Int], Int] {
28 | * def getArg(idx: Int): Option[BlazeBroadcast[_]] = {
29 | * if (idx == 0) Some(ref1)
30 | * else if (idx == 1) Some(ref2)
31 | * else None
32 | * }
33 | * }
34 | * }}}
35 | *
36 | * In this case, the protocol of hardware accelerator kernel would be:
37 | *
38 | * {{{
39 | * void acc(int *input, int *ref1, int ref2)
40 | * }}}
41 | *
42 | * @param idx The index of each variable.
43 | * @return The corresponding variable within Some. Return None if
44 | * the index is out of range.
45 | **/
46 | def getArg(idx: Int): Option[_]
47 |
48 | /** Report the total number of reference (broadcast) variables.
49 | * @return The total number of reference variables.
50 | **/
51 | def getArgNum(): Int
52 |
53 | /** The content of the function in Scala which has the same functionality as
54 | * the hardware accelerator. This method is used when accelerator is not available.
55 | *
56 | * @param in An element of the input.
57 | * @return A corresponding element of the output.
58 | **/
59 | def call(in: T): U = in.asInstanceOf[U]
60 |
61 | def call(in: Iterator[T]): Iterator[U] = in.asInstanceOf[Iterator[U]]
62 | }
63 |
--------------------------------------------------------------------------------
/accrdd/src/main/scala/org/apache/spark/blaze/BlazeBroadcast.scala:
--------------------------------------------------------------------------------
1 | package org.apache.spark.blaze
2 |
3 | import java.io._
4 |
5 | import org.apache.spark.broadcast.Broadcast
6 | import org.apache.spark.SparkException
7 | import org.apache.spark.internal.Logging
8 | import org.apache.spark.util.Utils
9 |
10 | import scala.reflect.ClassTag
11 |
12 | /**
13 | * A BlazeBroadcast variable. A BlazeBroadcast variable wraps a Spark broadcast variable with
14 | * an unique broadcast ID within the application. BlazeBroadcast variables will be broadcast
15 | * to the Blaze manager lazily and cached until the application has been terminated.
16 | *
17 | * @tparam T Type of broadcast data.
18 | */
19 | class BlazeBroadcast[T: ClassTag](appId: String, bd: Broadcast[T]) extends java.io.Serializable {
20 | def getIntId() = Math
21 | .abs(("""\d+""".r findAllIn appId)
22 | .addString(new StringBuilder).toLong.toInt)
23 |
24 | var brdcst_id: Long = Util.getBlockID(getIntId(), bd.id.asInstanceOf[Int])
25 | lazy val data = bd.value
26 | var length: Int = 0
27 | var size: Int = 0
28 | }
29 |
--------------------------------------------------------------------------------
/accrdd/src/main/scala/org/apache/spark/blaze/BlazeRuntime.scala:
--------------------------------------------------------------------------------
1 | package org.apache.spark.blaze
2 |
3 | import java.io._
4 | import scala.util.matching.Regex
5 | import scala.reflect.ClassTag
6 |
7 | import org.apache.spark._
8 | import org.apache.spark.{Partition, TaskContext}
9 | import org.apache.spark.rdd._
10 | import org.apache.spark.storage._
11 | import org.apache.spark.scheduler._
12 | import org.apache.spark.broadcast._
13 | import org.apache.spark.internal.Logging
14 |
15 | /**
16 | * The entry point of Blaze runtime system. BlazeRuntime is mainly used for
17 | * wrapping RDD and broadcast variables, and maintaining the broadcast data list
18 | * for releasing.
19 | *
20 | * @param sc Spark context.
21 | */
22 | class BlazeRuntime(sc: SparkContext) extends Logging {
23 | // get Logger object
24 |
25 | // The application signature generated based on Spark application ID.
26 | val appSignature: String = sc.applicationId
27 |
28 | var accPort: Int = 1027
29 |
30 | // Configure the port for AccManager
31 | def setAccPort(port: Int) = {
32 | accPort = port
33 | }
34 |
35 | var BroadcastList: List[BlazeBroadcast[_]] = List()
36 |
37 | /**
38 | * This method should be called by the developer at the end of the application
39 | * to release all broadcast blocks from Blaze manager and shutdown the SparkContext.
40 | * Ignore this method causes useless broadcast blocks occupy Blaze manager scratch memory.
41 | */
42 | def stop() = {
43 | if (BroadcastList.length == 0)
44 | logInfo("No broadcast block to be released")
45 | else {
46 | // FIXME: Currently we use the fixed port number (1027) for all managers
47 | val WorkerList: Array[(String, Int)] = (sc.getExecutorStorageStatus)
48 | .map(w => w.blockManagerId.host)
49 | .distinct
50 | .map(w => (w, accPort))
51 |
52 | logInfo("Releasing broadcast blocks from workers (" + WorkerList.length + "): " +
53 | WorkerList.map(w => w._1).mkString(", "))
54 |
55 | val msg = DataTransmitter.buildMessage(appSignature, AccMessage.MsgType.ACCTERM)
56 |
57 | for (e <- BroadcastList) {
58 | DataTransmitter.addBroadcastData(msg, e.brdcst_id)
59 | logInfo("Broadcast block to be released: " + e.brdcst_id)
60 | }
61 |
62 | for (worker <- WorkerList) {
63 | try {
64 | val workerIP = Util.getIPByHostName(worker._1)
65 | if (!workerIP.isDefined)
66 | throw new RuntimeException("Cannot resolve host name " + worker._1)
67 | val transmitter = new DataTransmitter(workerIP.get, worker._2)
68 | transmitter.send(msg)
69 | val revMsg = transmitter.receive()
70 | if (revMsg.getType() == AccMessage.MsgType.ACCFINISH)
71 | logInfo("Successfully release " + BroadcastList.length + " broadcast blocks from Manager " + worker._1)
72 | else
73 | logInfo("Fail to release broadcast blocks from Manager " + worker._1)
74 | }
75 | catch {
76 | case e: Throwable =>
77 | val sw = new StringWriter
78 | logInfo("Fail to release broadcast data from Manager " + worker._1 + ": " + sw.toString)
79 | }
80 | }
81 | }
82 | // do not shutdown SparkContext here since BlazeRuntime does not construct
83 | // a SparkContext
84 | // sc.stop
85 | // logInfo("Application " + appSignature + " shutdown.")
86 | }
87 |
88 | /**
89 | * Wrap a Spark RDD in a ShellRDD of Blaze.
90 | */
91 | def wrap[T: ClassTag](rdd : RDD[T]) : ShellRDD[T] = {
92 | new ShellRDD[T](appSignature, rdd, accPort)
93 | }
94 |
95 | /**
96 | * Wrap a Spark broadcast in a BlazeBroadcast.
97 | */
98 | def wrap[T: ClassTag](bd : Broadcast[T]) : BlazeBroadcast[T] = {
99 | val newBrdcst = new BlazeBroadcast[T](appSignature, bd)
100 | BroadcastList = BroadcastList :+ newBrdcst
101 | newBrdcst
102 | }
103 | }
104 |
105 |
--------------------------------------------------------------------------------
/accrdd/src/main/scala/org/apache/spark/blaze/BlockInfo.scala:
--------------------------------------------------------------------------------
1 | package org.apache.spark.blaze
2 |
3 | import java.io._
4 |
5 | import org.apache.spark.internal.Logging
6 | import org.apache.spark.util.Utils
7 |
8 | import scala.reflect.ClassTag
9 |
10 | /**
11 | *
12 | */
13 | class BlockInfo {
14 | var id: Long = 0
15 | var numElt: Int = -1
16 | var eltLength: Int = -1
17 | var eltSize: Int = -1
18 | var fileName: String = null
19 | var fileSize: Int = 0
20 | var offset: Int = 0
21 | }
22 |
--------------------------------------------------------------------------------
/accrdd/src/main/scala/org/apache/spark/blaze/ShellRDD.scala:
--------------------------------------------------------------------------------
1 | package org.apache.spark.blaze
2 |
3 | import scala.reflect.ClassTag
4 | import scala.reflect.runtime.universe._
5 | import scala.collection.mutable._
6 |
7 | import org.apache.spark._
8 | import org.apache.spark.{Partition, TaskContext}
9 | import org.apache.spark.rdd._
10 | import org.apache.spark.storage._
11 | import org.apache.spark.scheduler._
12 | import org.apache.spark.util.random._
13 |
14 | /**
15 | * ShellRDD is only used for wrapping a Spark RDD. It returns a AccRDD
16 | * if the developer decides to execute the computation on the accelerator
17 | * by calling `map_acc` method.
18 | *
19 | * @param appId The application ID.
20 | * @param prev The original Spark RDD.
21 | */
22 | class ShellRDD[T: ClassTag](
23 | appId: String,
24 | prev: RDD[T],
25 | port: Int,
26 | sampler: RandomSampler[Int, Int]
27 | ) extends RDD[T](prev) {
28 |
29 | def this(id: String, prev: RDD[T], port: Int) = this(id, prev, port, null)
30 |
31 | override def getPartitions: Array[Partition] = firstParent[T].partitions
32 |
33 | // do nothing
34 | override def compute(split: Partition, context: TaskContext) = {
35 | val iter = new Iterator[T] {
36 | val nested = firstParent[T].iterator(split, context)
37 |
38 | def hasNext : Boolean = {
39 | nested.hasNext
40 | }
41 |
42 | def next : T = {
43 | nested.next
44 | }
45 | }
46 | iter
47 | }
48 |
49 | def map_acc[U: ClassTag](clazz: Accelerator[T, U]): AccRDD[U, T] = {
50 | new AccRDD(appId, this, clazz, port, sampler)
51 | }
52 |
53 | def sample_acc (
54 | withReplacement: Boolean,
55 | fraction: Double,
56 | seed: Long = Util.random.nextLong): ShellRDD[T] = {
57 | require(fraction >= 0.0, "Negative fraction value: " + fraction)
58 |
59 | var thisSampler: RandomSampler[Int, Int] = null
60 |
61 | if (withReplacement)
62 | thisSampler = new PoissonSampler[Int](fraction)
63 | else
64 | thisSampler = new BernoulliSampler[Int](fraction)
65 | thisSampler.setSeed(seed)
66 |
67 | //if (seed == 904401792) { // Test mode
68 | // thisSampler = new TestSampler[Int]
69 | //}
70 |
71 | new ShellRDD(appId, this, port, thisSampler)
72 | }
73 |
74 | def mapPartitions_acc[U: ClassTag](clazz: Accelerator[T, U]): AccMapPartitionsRDD[U, T] = {
75 | new AccMapPartitionsRDD(appId, this, clazz, port, sampler)
76 | }
77 | }
78 |
79 |
--------------------------------------------------------------------------------
/accrdd/src/main/scala/org/apache/spark/blaze/TestSampler.scala:
--------------------------------------------------------------------------------
1 | package org.apache.spark.blaze
2 |
3 | import scala.reflect.ClassTag
4 | import scala.reflect.runtime.universe._
5 | import scala.collection.mutable._
6 |
7 | import org.apache.spark._
8 | import org.apache.spark.{Partition, TaskContext}
9 | import org.apache.spark.rdd._
10 | import org.apache.spark.util.random._
11 |
12 | abstract class TestSampler[T] extends RandomSampler[T, T] {
13 |
14 | override def setSeed(r: Long) = {}
15 |
16 | override def sample(items: Iterator[T]): Iterator[T] = {
17 | var out: List[T] = List()
18 | val N: Int = 30
19 | var idx: Int = 0
20 |
21 | while (items.hasNext) {
22 | val v: T = items.next
23 | if (idx < N)
24 | out = out :+ v
25 | idx += 1
26 | }
27 | out.iterator
28 | }
29 |
30 | //override def clone = new TestSampler[T]
31 | }
32 |
--------------------------------------------------------------------------------
/common/Makefile:
--------------------------------------------------------------------------------
1 | include ../Makefile.config
2 |
3 | ifeq ($(PROTOBUF_DIR),)
4 | $(error PROTOBUF_DIR not set properly in Makefile.config)
5 | endif
6 |
7 | JAVA_OUT=../accrdd/src/main/java
8 | CPP_OUT=../manager/src/proto
9 | PYTHON_OUT=./python
10 |
11 | all: task.proto acc_conf.proto msgGamNam.proto
12 | $(PROTOBUF_DIR)/bin/protoc --cpp_out=$(CPP_OUT) --java_out=$(JAVA_OUT) --python_out=$(PYTHON_OUT) task.proto
13 | $(PROTOBUF_DIR)/bin/protoc --cpp_out=$(CPP_OUT) --python_out=$(PYTHON_OUT) acc_conf.proto
14 | $(PROTOBUF_DIR)/bin/protoc --cpp_out=$(CPP_OUT) msgGamNam.proto
15 |
--------------------------------------------------------------------------------
/common/acc_conf.proto:
--------------------------------------------------------------------------------
1 | package blaze;
2 |
3 | enum AccType {
4 | CPU = 0;
5 | OpenCL = 1;
6 | CUDA = 2;
7 | ZYNQ = 3;
8 | }
9 |
10 | message AccPlatform {
11 | message KeyValue {
12 | required string key = 1;
13 | required string value = 2;
14 | }
15 |
16 | optional string id = 1 [default = "cpu"];
17 | optional string path = 2 [default = ""];
18 | optional string cache_loc = 3 [default = "cpu"];
19 | optional int32 cache_limit = 4 [default = 16];
20 | optional int32 scratch_limit = 5 [default = 4];
21 | repeated KeyValue param = 6;
22 | repeated AccWorker acc = 7;
23 | }
24 |
25 | message AccWorker {
26 | message KeyValue {
27 | required string key = 1;
28 | required string value = 2;
29 | }
30 |
31 | required string id = 1;
32 | required string path = 2;
33 | repeated KeyValue param = 3;
34 | optional string kernel_name = 4;
35 | optional string kernel_path = 5;
36 | }
37 |
38 | message ManagerConf {
39 | optional int32 app_port = 1 [default = 1027];
40 | optional int32 gam_port = 2 [default = 1028];
41 | optional int32 verbose = 3 [default = 0];
42 | optional string local_dir = 4 [default = "/tmp"];
43 | repeated AccPlatform platform = 5;
44 | }
45 |
--------------------------------------------------------------------------------
/common/msgGamNam.proto:
--------------------------------------------------------------------------------
1 | package blaze;
2 |
3 | option java_package = "org.apache.hadoop.fcs";
4 |
5 | message Gam2NamRequest {
6 | enum MsgType {
7 | ACCNAMES = 0;
8 | SHARERATE = 1;
9 | }
10 | required MsgType type = 1;
11 | optional bool pull = 2 [default = false];
12 | }
13 |
14 | message Accelerator {
15 | optional string acc_name = 1;
16 | optional string device_name = 2;
17 | }
18 |
19 | message Nam2GamAccNames {
20 | optional bool isUpdated = 1 [default = false];
21 | repeated Accelerator acc_names = 2;
22 | }
23 |
--------------------------------------------------------------------------------
/common/python/.gitignore:
--------------------------------------------------------------------------------
1 | # Ignore everything in this directory
2 | *
3 | # Except this file
4 | !.gitignore
5 |
--------------------------------------------------------------------------------
/common/task.proto:
--------------------------------------------------------------------------------
1 | package blaze;
2 |
3 | option java_package = "org.apache.spark.blaze";
4 | option java_outer_classname = "AccMessage";
5 |
6 | enum MsgType {
7 | ACCREQUEST = 0;
8 | ACCGRANT = 1;
9 | ACCREJECT = 2;
10 | ACCFINISH = 3;
11 | ACCDATA = 4;
12 | ACCFAILURE = 5;
13 | ACCBROADCAST = 6;
14 | ACCTERM = 7;
15 | ACCREGISTER = 8;
16 | ACCDELETE = 9;
17 | }
18 |
19 | message DataMsg {
20 | optional int64 partition_id = 1;
21 | optional bool cached = 2;
22 | optional bool sampled = 3;
23 | optional int32 element_length = 4;
24 | optional int32 element_size = 5;
25 | optional int32 num_elements = 6;
26 | optional int64 scalar_value = 7;
27 | optional string file_path = 8;
28 | optional int64 file_size = 9;
29 | optional int64 file_offset = 10;
30 | optional string mask_path = 11;
31 | }
32 |
33 | message TaskMsg {
34 | required MsgType type = 1;
35 | optional string app_id = 2;
36 | optional string acc_id = 3;
37 | repeated DataMsg data = 4;
38 | optional AccMsg acc = 5;
39 | optional string msg = 6;
40 | }
41 |
42 | message AccMsg {
43 | message KeyValue {
44 | required string key = 1;
45 | required bytes value = 2;
46 | }
47 | required string acc_id = 1;
48 | required string platform_id = 2;
49 | optional bytes task_impl = 3;
50 | repeated KeyValue param = 4;
51 | }
52 |
--------------------------------------------------------------------------------
/examples/.gitignore:
--------------------------------------------------------------------------------
1 | *.so
2 | *.o
3 | log
4 |
--------------------------------------------------------------------------------
/examples/arrayTest/app/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | fcs_runtime.example
4 | arraytest
5 | jar
6 | "ArrayTest"
7 | 1.0
8 | 4.0.0
9 |
10 |
11 |
12 | scala-tools.org
13 | Scala-tools Maven2 Repository
14 | http://scala-tools.org/repo-releases
15 |
16 |
17 | fcs-mvn.repo
18 | Falcon Computing Solutions Repository
19 | http://support.falcon-computing.com/mvn-repo
20 |
21 |
22 |
23 |
24 | 1.7
25 | 2.10.4
26 | 2.10
27 | 2.10
28 | 1.5.1
29 | UTF-8
30 | UTF-8
31 |
32 |
33 |
34 |
35 | scala-tools.org
36 | Scala-tools Maven2 Repository
37 | http://scala-tools.org/repo-releases
38 |
39 |
40 |
41 |
42 |
43 | org.scala-lang
44 | scala-library
45 | ${scala.version}
46 |
47 |
48 | org.apache.spark
49 | spark-core_${scala.binary.version}
50 | ${spark.version}
51 |
52 |
53 | org.apache.spark
54 | spark-mllib_${scala.binary.version}
55 | ${spark.version}
56 |
57 |
58 | org.apache.spark
59 | blaze_${scala.binary.version}
60 | 1.0
61 |
62 |
63 |
64 |
65 |
66 |
67 | org.scala-tools
68 | maven-scala-plugin
69 | 2.15.2
70 |
71 |
72 |
73 | compile
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
--------------------------------------------------------------------------------
/examples/arrayTest/app/run-local.sh.template:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | SPARK_HOME=
4 | $SPARK_HOME/spark-submit --class LoopBack \
5 | --master local[*] \
6 | target/LoopBack-0.0.0.jar $@
7 |
8 |
--------------------------------------------------------------------------------
/examples/arrayTest/app/src/main/scala/fcs_runtime/examples/ArrayTest.scala:
--------------------------------------------------------------------------------
1 | import Array._
2 | import scala.math._
3 | import scala.util.Random
4 | import java.net._
5 | import org.apache.spark.rdd._
6 | import org.apache.spark.SparkContext
7 | import org.apache.spark.SparkConf
8 | import org.apache.spark.blaze._
9 |
10 | class ArrayTest(v: BlazeBroadcast[Array[Double]])
11 | extends Accelerator[Array[Double], Array[Double]] {
12 |
13 | val id: String = "ArrayTest"
14 |
15 | def getArgNum(): Int = 1
16 |
17 | def getArg(idx: Int): Option[_] = {
18 | if (idx == 0)
19 | Some(v)
20 | else
21 | None
22 | }
23 |
24 | override def call(in: Array[Double]): Array[Double] = {
25 | (in, v.data).zipped.map(_ + _)
26 | }
27 | }
28 |
29 | object TestApp {
30 | def main(args : Array[String]) {
31 |
32 | val conf = new SparkConf()
33 | conf.setAppName("TestApp")
34 |
35 | val sc = new SparkContext(conf)
36 | val acc = new BlazeRuntime(sc)
37 |
38 | var num_sample = 32
39 | var num_part = 4
40 |
41 | if (args.size == 2) {
42 | num_sample = args(0).toInt
43 | num_part = args(1).toInt
44 | }
45 |
46 | val data = Array.fill(num_sample)(Array.fill(8)(Random.nextDouble));
47 | val offset = Array.fill(8)(Random.nextDouble);
48 |
49 | val rdd = sc.parallelize(data, num_part)
50 | val rdd_acc = acc.wrap(rdd)
51 |
52 | val bc_data = sc.broadcast(offset)
53 | val bc_data_acc = acc.wrap(bc_data)
54 |
55 | val res_acc = rdd_acc.map_acc(new ArrayTest(bc_data_acc)).collect
56 | val res_cpu = rdd.map(e => (e, bc_data.value).zipped.map(_ + _)).collect
57 |
58 | // compare results
59 | if (res_acc.deep != res_cpu.deep)
60 | {
61 | println("Result incorrect")
62 | println("map result: \n" + res_cpu.deep.mkString("\n"))
63 | println("map_acc results: \n" + res_acc.deep.mkString("\n"))
64 | }
65 | else {
66 | println("result correct")
67 | }
68 | acc.stop()
69 | }
70 | }
71 |
72 |
--------------------------------------------------------------------------------
/examples/arrayTest/cpp_app/.gitignore:
--------------------------------------------------------------------------------
1 | app
2 |
--------------------------------------------------------------------------------
/examples/arrayTest/cpp_app/Makefile:
--------------------------------------------------------------------------------
1 | MANAGER_DIR=../../../manager
2 | include ../../../Makefile.config
3 |
4 | # check all variables
5 | ifeq ($(BOOST_DIR),)
6 | $(error BOOST_DIR not set properly in Makefile.config)
7 | endif
8 | ifeq ($(PROTOBUF_DIR),)
9 | $(error PROTOBUF_DIR not set properly in Makefile.config)
10 | endif
11 | ifeq ($(GLOG_DIR),)
12 | $(error GLOG_DIR not set properly in Makefile.config)
13 | endif
14 |
15 | PP=g++
16 | CC=gcc
17 |
18 | CFLAGS := -shared -fPIC -std=c++0x -m64 -g
19 |
20 | COMPILE := $(CFLAGS) \
21 | -I$(MANAGER_DIR)/include \
22 | -I$(BOOST_DIR)/include \
23 | -I$(PROTOBUF_DIR)/include \
24 | -I$(GFLAGS_DIR)/include \
25 | -I$(GLOG_DIR)/include
26 |
27 | LINK := -L$(MANAGER_DIR)/lib -lblaze \
28 | -L$(BOOST_DIR)/lib \
29 | -lboost_system -lboost_thread -lboost_iostreams -lboost_filesystem -lboost_regex \
30 | -L$(PROTOBUF_DIR)/lib -lprotobuf \
31 | -L$(GLOG_DIR)/lib -lglog \
32 | -lpthread -lm -ldl
33 |
34 | all: app
35 |
36 | app: main.o
37 | $(PP) -o $@ main.o $(LINK)
38 |
39 | %.o: %.cpp
40 | $(PP) $(COMPILE) $< -o $@
41 |
42 | clean:
43 | rm -rf *.o
44 | rm -rf app
45 |
--------------------------------------------------------------------------------
/examples/arrayTest/cpp_app/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | #include "blaze/Client.h"
4 |
5 | using namespace blaze;
6 |
7 | class ArrayTestClient: public Client {
8 | public:
9 | ArrayTestClient(): Client("ArrayTest", 2, 1) {;}
10 |
11 | void compute() {
12 | int num_samples = getInputNumItems(0);
13 | int feature_size = getInputLength(1);
14 |
15 | double* data_ptr = (double*)getInputPtr(0);
16 | double* weight_ptr = (double*)getInputPtr(1);
17 | double* output_ptr = (double*)createOutput(0,
18 | num_samples, feature_size, sizeof(double));
19 |
20 | for (int i = 0; i < num_samples; i++) {
21 | for (int j = 0; j < feature_size; j++) {
22 | output_ptr[i * feature_size + j] =
23 | data_ptr[i * feature_size +j] +
24 | weight_ptr[j];
25 | }
26 | }
27 | }
28 | };
29 |
30 | int main(int argc, char** argv) {
31 |
32 | // GLOG configuration
33 | google::InitGoogleLogging(argv[0]);
34 | FLAGS_logtostderr = 1;
35 | FLAGS_v = 2;
36 |
37 | if (argc < 2) {
38 | printf("USAGE: %s \n", argv[0]);
39 | return -1;
40 | }
41 |
42 | int num_samples = atoi(argv[1]);
43 | int feature_size = 1024;
44 |
45 | if (argc > 3) {
46 | feature_size = atoi(argv[2]);
47 | }
48 | int data_size = num_samples*feature_size;
49 |
50 | try {
51 | ArrayTestClient client;
52 |
53 | double* data_ptr = (double*)client.createInput(0, num_samples, feature_size, sizeof(double), BLAZE_INPUT);
54 | double* weight_ptr = (double*)client.createInput(1, feature_size, 1, sizeof(double), BLAZE_INPUT);
55 |
56 | double* output_base = new double[num_samples*feature_size];
57 |
58 | // setup input with random data
59 | for (int i=0; i max_diff) {
88 | max_diff = diff;
89 | }
90 |
91 | diff_total += diff;
92 | if (output_base[k]!=0) {
93 | diff_ratio += diff / std::abs(output_base[k]);
94 | }
95 |
96 | if (diff / std::abs(output_base[k]) > 0.05 &&
97 | k<10)
98 | {
99 | printf("%d: %f|%f, ratio=%f\n",
100 | k,
101 | output_base[k],
102 | output_ptr[k],
103 | diff/std::abs(output_base[k]));
104 | }
105 | }
106 | if (diff_total < 1e-6) {
107 | printf("Result correct\n");
108 | }
109 | else {
110 | printf("Result incorrect\n");
111 | }
112 | }
113 | catch (std::exception &e) {
114 | printf("%s\n", e.what());
115 | return -1;
116 | }
117 |
118 | return 0;
119 | }
120 |
--------------------------------------------------------------------------------
/examples/arrayTest/cpu_task/ArrayTest.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | #include "blaze/Task.h"
5 |
6 | using namespace blaze;
7 |
8 | class ArrayTest : public Task {
9 | public:
10 |
11 | // extends the base class constructor
12 | // to indicate how many input blocks
13 | // are required
14 | ArrayTest(): Task(2) {;}
15 |
16 | // overwrites the compute function
17 | virtual void compute() {
18 |
19 | // get input data dimensions
20 | int total_length = getInputLength(0);
21 | int num_vectors = getInputNumItems(0);
22 | int vector_length = total_length / num_vectors;
23 |
24 | // get the pointer to input/output data
25 | double* a = (double*)getInput(0);
26 | double* b = (double*)getInput(1);
27 | double* c = (double*)getOutput(0,
28 | vector_length, num_vectors, sizeof(double));
29 |
30 | // perform computation
31 | // c[i] = a[i] + b
32 | for (int i=0; i
2 |
3 | #include "blaze/Task.h"
4 | #include "blaze/nv_opencl/OpenCLEnv.h"
5 |
6 | using namespace blaze;
7 |
8 | class ArrayTest : public Task {
9 | public:
10 |
11 | // extends the base class constructor
12 | // to indicate how many input blocks
13 | // are required
14 | ArrayTest(): Task(2) {;}
15 |
16 | // overwrites the compute function
17 | virtual void compute() {
18 |
19 | struct timeval t1, t2, tr;
20 |
21 | // dynamically cast the TaskEnv to OpenCLEnv
22 | OpenCLTaskEnv* ocl_env = (OpenCLTaskEnv*)getEnv();
23 |
24 | // get input data length
25 | int total_length = getInputLength(0);
26 | int num_vectors = getInputNumItems(0);
27 | int vector_length = total_length / num_vectors;
28 |
29 | // get OpenCL context
30 | cl_context context = ocl_env->getContext();
31 | cl_command_queue command = ocl_env->getCmdQueue();
32 | cl_program program = ocl_env->getProgram();
33 |
34 | int err;
35 | cl_event event;
36 |
37 | cl_kernel kernel = clCreateKernel(program, "arrayTest", &err);
38 | if (!kernel || err != CL_SUCCESS) {
39 | throw std::runtime_error(
40 | "Failed to create compute kernel arrayTest");
41 | }
42 |
43 | // get the pointer to input/output data
44 | cl_mem ocl_a = *((cl_mem*)getInput(0));
45 | cl_mem ocl_b = *((cl_mem*)getInput(1));
46 | cl_mem ocl_c = *((cl_mem*)getOutput(
47 | 0, vector_length, num_vectors,
48 | sizeof(double)));
49 |
50 | if (!ocl_a || !ocl_b || !ocl_c) {
51 | throw std::runtime_error("Buffer are not allocated");
52 | }
53 |
54 | // Set the arguments to our compute kernel
55 | err |= clSetKernelArg(kernel, 0, sizeof(int), &num_vectors);
56 | err |= clSetKernelArg(kernel, 1, sizeof(int), &vector_length);
57 | err |= clSetKernelArg(kernel, 2, sizeof(cl_mem), &ocl_a);
58 | err |= clSetKernelArg(kernel, 3, sizeof(cl_mem), &ocl_b);
59 | err |= clSetKernelArg(kernel, 4, sizeof(cl_mem), &ocl_c);
60 | if (err != CL_SUCCESS) {
61 | throw std::runtime_error("Cannot set kernel args\n");
62 | }
63 |
64 | size_t work_lsize[1] = {128};
65 | size_t work_gsize[1];
66 | work_gsize[0] = 64*work_lsize[0];
67 |
68 | err = clEnqueueNDRangeKernel(command, kernel, 1, NULL, work_gsize, work_lsize, 0, NULL, &event);
69 | clWaitForEvents(1, &event);
70 | }
71 | };
72 |
73 | extern "C" Task* create() {
74 | return new ArrayTest();
75 | }
76 |
77 | extern "C" void destroy(Task* p) {
78 | delete p;
79 | }
80 |
--------------------------------------------------------------------------------
/examples/arrayTest/gpu_task/Makefile:
--------------------------------------------------------------------------------
1 | MANAGER_DIR=../../../manager
2 | include ../../../Makefile.config
3 |
4 | # check all variables
5 | ifeq ($(BOOST_DIR),)
6 | $(error BOOST_DIR not set properly in Makefile.config)
7 | endif
8 | ifeq ($(PROTOBUF_DIR),)
9 | $(error PROTOBUF_DIR not set properly in Makefile.config)
10 | endif
11 | ifeq ($(GLOG_DIR),)
12 | $(error GLOG_DIR not set properly in Makefile.config)
13 | endif
14 | ifeq ($(CUDA_DIR),)
15 | $(error CUDA_DIR not set properly in Makefile.config)
16 | endif
17 |
18 | CFLAGS := -shared -fPIC -std=c++0x -m64
19 | COMPILE := $(CFLAGS) \
20 | -I$(MANAGER_DIR)/include \
21 | -I$(MANAGER_DIR)/../platforms/nv_opencl/include \
22 | -I$(BOOST_DIR)/include \
23 | -I$(PROTOBUF_DIR)/include \
24 | -I$(GLOG_DIR)/include \
25 | -I$(GFLAGS_DIR)/include \
26 | -I$(CUDA_DIR)/include
27 |
28 | LINK := -L$(CUDA_DIR)/lib64 -lOpenCL
29 |
30 | ACC := ArrayTest
31 |
32 | OBJS := $(addsuffix .so, $(ACC))
33 |
34 | all: $(OBJS)
35 |
36 | %.so: %.cpp Makefile
37 | $(PP) $(COMPILE) $(LINK) $< -o $@
38 |
39 | clean:
40 | rm -rf $(OBJS)
41 |
--------------------------------------------------------------------------------
/examples/loopback/aal_task/conf.prototxt:
--------------------------------------------------------------------------------
1 | port : 1027
2 | verbose : 2
3 | platform : {
4 | type: CPU
5 | cache_limit: 16384
6 | scratch_limit: 4096
7 | acc : {
8 | id : "NLB"
9 | path : "examples/loopback/acc_task/libNLB.so"
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/examples/loopback/aal_task/src/Makefile:
--------------------------------------------------------------------------------
1 | include ../../../../Makefile.config
2 | MANAGER_DIR=../../../../manager
3 |
4 | CPPFLAGS ?=
5 | CXX ?= g++
6 | LDFLAGS ?=
7 |
8 | ifneq (,$(ndebug))
9 | else
10 | CPPFLAGS += -DENABLE_DEBUG=1
11 | endif
12 | ifneq (,$(nassert))
13 | else
14 | CPPFLAGS += -DENABLE_ASSERT=1
15 | endif
16 |
17 | CPPFLAGS += -fPIC -std=c++0x -m64 -g \
18 | -I$(AAL_DIR)/include \
19 | -I$(MANAGER_DIR)/include \
20 | -I$(BOOST_DIR)/include \
21 | -I$(PROTOBUF_DIR)/include
22 |
23 | LDFLAGS += -L$(AAL_DIR)/lib -Wl,-rpath-link \
24 | -Wl,$(AAL_DIR)/lib -Wl,-rpath \
25 | -Wl,$(AAL_DIR)/lib \
26 | -L$(AAL_DIR)/lib64 -Wl,-rpath-link \
27 | -Wl,$(AAL_DIR)/lib64 -Wl,-rpath \
28 | -Wl,$(AAL_DIR)/lib64
29 |
30 | all: ../libNLB.so
31 |
32 | ../libNLB.so: NLB.o
33 | $(CXX) -g -shared -o $@ $< $(LDFLAGS) -lOSAL -lAAS -lxlrt
34 |
35 | NLB.o: NLB.cpp *.h Makefile
36 | $(CXX) $(CPPFLAGS) -D__AAL_USER__=1 -c -o $@ NLB.cpp
37 |
38 | clean:
39 | $(RM) ../libNLB.so *.o
40 |
41 | .PHONY:all clean
42 |
--------------------------------------------------------------------------------
/examples/loopback/aal_task/src/NLB.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include
5 | #include
6 | #include
7 | #include
8 | #include
9 |
10 | #include "blaze.h"
11 | using namespace blaze;
12 |
13 | #include "NLBApp.h"
14 |
15 | class NLB : public Task {
16 | public:
17 |
18 | // extends the base class constructor
19 | // to indicate how many input blocks
20 | // are required
21 | NLB(): Task(1) {;}
22 |
23 | // overwrites the compute function
24 | virtual void compute() {
25 |
26 | // get input data length
27 | int data_length = getInputLength(0);
28 |
29 | if (data_length == 0 ||
30 | data_length % 8 != 0)
31 | {
32 | throw std::runtime_error("Invalid data length");
33 | }
34 |
35 | // get the pointer to input/output data
36 | int n_items = getInputNumItems(0);
37 | double* src_data = (double*)getInput(0);
38 | double* dst_data = (double*)getOutput(
39 | 0, data_length/n_items, n_items,
40 | sizeof(double));
41 |
42 | printf("#items=%d, length=%d\n", n_items, data_length/n_items);
43 |
44 | if (!src_data || !dst_data) {
45 | throw std::runtime_error("Cannot get data pointers");
46 | }
47 |
48 | RuntimeClient runtimeClient;
49 | if(!runtimeClient.isOK()){
50 | throw std::runtime_error("Runtime Failed to Start");
51 | }
52 |
53 | HelloCCINLBApp theApp(
54 | dst_data, src_data,
55 | data_length*sizeof(double), &runtimeClient);
56 |
57 | // start real computation
58 | theApp.run();
59 | }
60 | };
61 |
62 | extern "C" Task* create() {
63 | return new NLB();
64 | }
65 |
66 | extern "C" void destroy(Task* p) {
67 | delete p;
68 | }
69 |
--------------------------------------------------------------------------------
/examples/loopback/app/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | LoopBack
4 | LoopBack
5 | jar
6 | "LoopBack"
7 | 4.0.0
8 | 0.0.0
9 |
10 |
11 |
12 | scala-tools.org
13 | Scala-tools Maven2 Repository
14 | http://scala-tools.org/repo-releases
15 |
16 |
17 | maven-hadoop
18 | Hadoop Releases
19 | https://repository.cloudera.com/content/repositories/releases/
20 |
21 |
22 | cloudera-repos
23 | Cloudera Repos
24 | https://repository.cloudera.com/artifactory/cloudera-repos/
25 |
26 |
27 |
28 |
29 |
30 | scala-tools.org
31 | Scala-tools Maven2 Repository
32 | http://scala-tools.org/repo-releases
33 |
34 |
35 |
36 |
37 | UTF-8
38 | UTF-8
39 | 1.7
40 | 2.10.4
41 | 2.10
42 | 2.10
43 | 1.5.1
44 | 2.6.0
45 | 1.0.1
46 |
47 |
48 |
49 |
50 |
51 | org.scala-tools
52 | maven-scala-plugin
53 | 2.15.2
54 |
55 |
56 |
57 | compile
58 |
59 |
60 |
61 |
62 |
63 | maven-compiler-plugin
64 | 3.1
65 |
66 | 1.6
67 | 1.6
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 | org.scala-lang
76 | scala-library
77 | ${scala.version}
78 |
79 |
80 | org.apache.spark
81 | spark-core_${scala.binary.version}
82 | ${spark.version}
83 |
84 |
85 | org.apache.spark
86 | blaze_${scala.binary.version}
87 | ${blaze.version}
88 |
89 |
90 |
91 |
--------------------------------------------------------------------------------
/examples/loopback/app/run-local.sh.template:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | SPARK_HOME=
4 | $SPARK_HOME/spark-submit --class LoopBack \
5 | --master local[*] \
6 | target/LoopBack-0.0.0.jar $@
7 |
8 |
--------------------------------------------------------------------------------
/examples/loopback/app/src/main/scala/LoopBack/LoopBack.scala:
--------------------------------------------------------------------------------
1 | import org.apache.spark.SparkContext
2 | import org.apache.spark.SparkContext._
3 | import org.apache.spark.SparkConf
4 | import Array._
5 | import org.apache.spark.rdd._
6 |
7 | import scala.math._
8 | import java.util._
9 |
10 | import org.apache.spark.blaze._
11 |
12 | class LoopBack()
13 | extends Accelerator[Array[Double], Array[Double]] {
14 |
15 | val id: String = "LoopBack"
16 | def getArg(idx: Int): Option[BlazeBroadcast[_]] = None
17 | def getArgNum(): Int = 0
18 |
19 | override def call(data: Array[Double]): Array[Double] = {
20 | val out = new Array[Double](data.size)
21 | Array.copy(data, 0, out, 0, data.size)
22 | out
23 | }
24 | }
25 |
26 | object LoopBack {
27 |
28 | def genData(
29 | sc: SparkContext,
30 | n_elements: Int,
31 | n_parts: Int): RDD[Array[Double]] = {
32 |
33 | val data = sc.parallelize(0 until n_elements, n_parts).map{ idx =>
34 | val rand = new Random(42 + idx)
35 | val x = Array.fill[Double](8) { rand.nextGaussian() }
36 | x
37 | }
38 | data
39 | }
40 |
41 | def main(args : Array[String]) {
42 |
43 | if (args.length != 2) {
44 | System.err.println("Usage: LoopBack <#elements> <#parts>")
45 | System.exit(1)
46 | }
47 | val sc = get_spark_context("LoopBack")
48 | val acc = new BlazeRuntime(sc)
49 |
50 | val n_elements = args(0).toInt
51 | val n_parts = args(1).toInt
52 |
53 | val data = acc.wrap(genData(sc, n_elements, n_parts))
54 |
55 | val src = data.collect
56 | val dst = data.map_acc(new LoopBack).collect
57 |
58 |
59 | if (src.deep == dst.deep) {
60 | println("results correct")
61 | acc.stop()
62 | }
63 | else {
64 | println("results incorrect")
65 | println(src.deep.mkString("\n"))
66 | println(dst.deep.mkString("\n"))
67 | acc.stop()
68 | System.exit(1)
69 | }
70 | }
71 |
72 | def get_spark_context(appName : String) : SparkContext = {
73 | val conf = new SparkConf()
74 | conf.setAppName(appName)
75 |
76 | return new SparkContext(conf)
77 | }
78 | }
79 |
80 |
--------------------------------------------------------------------------------
/examples/pi/app/.gitignore:
--------------------------------------------------------------------------------
1 | map-bin
2 | reduce-bin
3 | *.txt
4 |
--------------------------------------------------------------------------------
/examples/pi/app/Makefile:
--------------------------------------------------------------------------------
1 | MANAGER_DIR=../../../manager
2 | include ../../../Makefile.config
3 |
4 | # check all variables
5 | ifeq ($(BOOST_DIR),)
6 | $(error BOOST_DIR not set properly in Makefile.config)
7 | endif
8 | ifeq ($(PROTOBUF_DIR),)
9 | $(error PROTOBUF_DIR not set properly in Makefile.config)
10 | endif
11 | ifeq ($(GLOG_DIR),)
12 | $(error GLOG_DIR not set properly in Makefile.config)
13 | endif
14 |
15 | PP=g++
16 | CC=gcc
17 | AR=ar
18 |
19 | CFLAGS = -c -fPIC -std=c++0x -g
20 | COMPILE := $(CFLAGS) \
21 | -I$(MANAGER_DIR)/include \
22 | -I$(BOOST_DIR)/include \
23 | -I$(PROTOBUF_DIR)/include \
24 | -I$(GLOG_DIR)/include \
25 | -I$(GFLAGS_DIR)/include
26 |
27 | LINK := -L$(MANAGER_DIR)/lib -lblaze \
28 | -L$(BOOST_DIR)/lib \
29 | -lboost_system -lboost_thread -lboost_iostreams -lboost_filesystem -lboost_regex \
30 | -L$(PROTOBUF_DIR)/lib -lprotobuf \
31 | -L$(GLOG_DIR)/lib -lglog \
32 | -L$(GFLAGS_DIR)/lib -lgflags \
33 | -lpthread -lm -ldl
34 |
35 | all: map-bin reduce-bin
36 |
37 | map-bin: map.o
38 | $(PP) -o $@ $< $(LINK)
39 |
40 | reduce-bin: reduce.o
41 | $(PP) -o $@ $< $(LINK)
42 |
43 | %.o: %.cpp Makefile
44 | $(PP) $(COMPILE) $< -o $@
45 |
46 | clean:
47 | rm -rf *.o
48 | rm -rf map-bin
49 | rm -rf reduce-bin
50 |
--------------------------------------------------------------------------------
/examples/pi/app/map.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include
5 | #include
6 | #include
7 |
8 | int main(int argc, char** argv) {
9 |
10 | srand(time(NULL));
11 |
12 | for (std::string line; std::getline(std::cin, line); )
13 | {
14 | printf("key\t%f,%f\n",
15 | (double)rand()/RAND_MAX,
16 | (double)rand()/RAND_MAX);
17 | }
18 |
19 | return 0;
20 | }
21 |
--------------------------------------------------------------------------------
/examples/pi/app/map.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | source ../../../setup.sh
4 | ./map-bin $@
5 |
--------------------------------------------------------------------------------
/examples/pi/app/reduce.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include
5 | #include
6 | #include
7 | #include
8 |
9 | #include
10 |
11 | #include "blaze/Client.h"
12 |
13 | using namespace blaze;
14 |
15 | class PiClient: public Client {
16 | public:
17 |
18 | PiClient(): Client("Pi", 1, 1) {;}
19 |
20 | void compute() {
21 | int num_pairs = getInputNumItems(0);
22 | double* data_ptr = (double*)getInputPtr(0);
23 | uint32_t* output_ptr = (uint32_t*)createOutput(0,
24 | 2, 1, sizeof(uint32_t));
25 |
26 | uint32_t inside_count = 0;
27 | uint32_t outside_count = 0;
28 |
29 | for (int i=0; i > all_data;
53 | for (std::string line; std::getline(std::cin, line); )
54 | {
55 | std::stringstream ss(line);
56 |
57 | std::string key;
58 | std::string value;
59 | double x = 0;
60 | double y = 0;
61 |
62 | std::getline(ss, key, '\t');
63 |
64 | try {
65 | std::getline(ss, value, ',');
66 | x = std::stof(value);
67 | std::getline(ss, value);
68 | y = std::stof(value);
69 | } catch (std::exception &e) {
70 | continue;
71 | }
72 | all_data.push_back(std::make_pair(x, y));
73 | }
74 |
75 | // send to accelerator
76 | PiClient client;
77 |
78 | // prepare input data
79 | double* data_ptr = (double*)client.createInput(0, all_data.size(), 2, sizeof(double), BLAZE_INPUT);
80 |
81 | for (int i=0; i> $TMP_FILE
13 | hdfs dfs -copyFromLocal `pwd`/$TMP_FILE $WORK_DIR
14 |
15 | if [[ "$?" != "0" ]]; then
16 | echo "Task failed"
17 | exit 1
18 | fi
19 |
20 | $HADOOP_HOME/bin/hadoop jar $HADOOP_HOME/share/hadoop/tools/lib/hadoop-streaming-2.6.0.jar \
21 | -files file:`pwd`/map-bin,file:`pwd`/reduce-bin,file:`pwd`/map.sh,file:`pwd`/reduce.sh \
22 | -D mapreduce.label=cpu \
23 | -input $WORK_DIR/$TMP_FILE \
24 | -output $WORK_DIR/output_pi \
25 | -mapper map.sh \
26 | -reducer reduce.sh &> hadoop.log
27 |
28 | hdfs dfs -ls $WORK_DIR/output_pi/_SUCCESS &> /dev/null
29 |
30 | if [[ "$?" != "0" ]]; then
31 | echo "Task failed"
32 | else
33 | ret=`hdfs dfs -cat $WORK_DIR/output_pi/* | head -n 1 | cut -f2`
34 | pi=`echo 4*$ret/$N | bc -l`
35 | echo "PI is estimated to be: $pi"
36 | fi
37 |
38 | # clean up
39 | rm $TMP_FILE
40 | hdfs dfs -rm $WORK_DIR/$TMP_FILE &> /dev/null
41 | hdfs dfs -rm -r $WORK_DIR/output_pi &> /dev/null
42 |
--------------------------------------------------------------------------------
/examples/pi/cpu_task/Makefile:
--------------------------------------------------------------------------------
1 | MANAGER_DIR=../../../manager
2 | include ../../../Makefile.config
3 |
4 | # check all variables
5 | ifeq ($(BOOST_DIR),)
6 | $(error BOOST_DIR not set properly in Makefile.config)
7 | endif
8 | ifeq ($(PROTOBUF_DIR),)
9 | $(error PROTOBUF_DIR not set properly in Makefile.config)
10 | endif
11 | ifeq ($(GLOG_DIR),)
12 | $(error GLOG_DIR not set properly in Makefile.config)
13 | endif
14 |
15 | PP=g++
16 | CC=gcc
17 |
18 | CFLAGS := -shared -fPIC -std=c++0x -m64
19 | COMPILE := $(CFLAGS) \
20 | -I$(MANAGER_DIR)/include \
21 | -I$(BOOST_DIR)/include \
22 | -I$(PROTOBUF_DIR)/include \
23 | -I$(GLOG_DIR)/include \
24 | -I$(GFLAGS_DIR)/include
25 |
26 | ACC = Pi
27 |
28 | OBJS= $(addsuffix .so, $(ACC))
29 |
30 | all: $(OBJS)
31 |
32 | %.so: %.cpp Makefile
33 | $(PP) $(COMPILE) $< -o $@
34 |
35 | clean:
36 | rm -rf $(OBJS)
37 |
--------------------------------------------------------------------------------
/examples/pi/cpu_task/Pi.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 |
5 | #include "blaze/Task.h"
6 |
7 | using namespace blaze;
8 |
9 | class Pi : public Task {
10 | public:
11 |
12 | // extends the base class constructor
13 | // to indicate how many input blocks
14 | // are required
15 | Pi(): Task(1) {;}
16 |
17 | // overwrites the compute function
18 | virtual void compute() {
19 |
20 | // get input data dimensions
21 | int num_data = getInputNumItems(0);
22 |
23 | // get the pointer to input/output data
24 | double* input = (double*)getInput(0);
25 | uint32_t* output = (uint32_t*)getOutput(
26 | 0, 1, 2, sizeof(uint32_t));
27 |
28 | uint32_t inside_count = 0;
29 | uint32_t outside_count = 0;
30 |
31 | // perform computation
32 | for (int i=0; i
2 | #include
3 |
4 | #include "blaze.h"
5 |
6 | using namespace blaze;
7 |
8 | class ArrayTest : public Task {
9 | public:
10 |
11 | // extends the base class constructor
12 | // to indicate how many input blocks
13 | // are required
14 | ArrayTest(): Task(2) {;}
15 |
16 | // overwrites the compute function
17 | virtual void compute() {
18 |
19 | // get input data length
20 | int data_length = getInputLength(0);
21 | int num_samples = getInputNumItems(0);
22 | int item_length = data_length / num_samples;
23 | int weight_length = getInputLength(1);
24 |
25 | // get the pointer to input/output data
26 | double* a = (double*)getInput(0);
27 | double* val = (double*)getInput(1);
28 | double* b = (double*)getOutput(0, item_length, num_samples, sizeof(double));
29 |
30 | // perform computation
31 | double val_sum = 0.0;
32 | for (int k = 0; k < weight_length; k++) {
33 | val_sum += val[k];
34 | }
35 |
36 | for (int i = 0; i < num_samples; i++) {
37 | for (int j = 0; j < item_length; j++) {
38 | b[i * item_length + j] = a[i * item_length +j] + val_sum;
39 | }
40 | }
41 | // if there is any error, throw exceptions
42 | }
43 | };
44 |
45 | extern "C" Task* create() {
46 | return new ArrayTest();
47 | }
48 |
49 | extern "C" void destroy(Task* p) {
50 | delete p;
51 | }
52 |
--------------------------------------------------------------------------------
/functional-test/arraytest/acc_task/Makefile:
--------------------------------------------------------------------------------
1 | MANAGER_DIR=../../../manager
2 | include ../../../Makefile.config
3 |
4 | # check all variables
5 | ifeq ($(BOOST_DIR),)
6 | $(error BOOST_DIR not set properly in Makefile.config)
7 | endif
8 | ifeq ($(PROTOBUF_DIR),)
9 | $(error PROTOBUF_DIR not set properly in Makefile.config)
10 | endif
11 | PP=g++
12 | CC=gcc
13 |
14 | CFLAGS= -shared -fPIC -std=c++0x -m64 -g
15 | COMPILE= $(CFLAGS) \
16 | -I$(MANAGER_DIR)/include \
17 | -I$(BOOST_DIR)/include \
18 | -I$(PROTOBUF_DIR)/include
19 |
20 | ACC = ArrayTest
21 |
22 | OBJS= $(addsuffix .so, $(addprefix ../, $(ACC)))
23 |
24 | all: $(OBJS)
25 |
26 | ../%.so: %.cpp Makefile
27 | $(PP) $(COMPILE) $< -o $@
28 |
29 | clean:
30 | rm -rf $(OBJS)
31 |
--------------------------------------------------------------------------------
/functional-test/arraytest/app/.gitignore:
--------------------------------------------------------------------------------
1 | *.sh
2 |
--------------------------------------------------------------------------------
/functional-test/arraytest/app/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
16 |
17 | test
18 | test
19 | jar
20 | "ArrayTest"
21 | 4.0.0
22 | 0.0.0
23 |
24 |
25 |
26 | scala-tools.org
27 | Scala-tools Maven2 Repository
28 | http://scala-tools.org/repo-releases
29 |
30 |
31 | maven-hadoop
32 | Hadoop Releases
33 | https://repository.cloudera.com/content/repositories/releases/
34 |
35 |
36 | cloudera-repos
37 | Cloudera Repos
38 | https://repository.cloudera.com/artifactory/cloudera-repos/
39 |
40 |
41 |
42 |
43 |
44 | scala-tools.org
45 | Scala-tools Maven2 Repository
46 | http://scala-tools.org/repo-releases
47 |
48 |
49 |
50 |
51 | UTF-8
52 | UTF-8
53 |
54 |
55 |
56 |
57 | org.scala-lang
58 | scala-library
59 | 2.10.4
60 |
61 |
62 | org.apache.spark
63 | spark-core_2.10
64 | 1.5.1
65 |
66 |
67 | org.apache.spark
68 | blaze_2.10
69 | 1.0
70 |
71 |
72 |
73 |
74 |
75 |
76 | org.scala-tools
77 | maven-scala-plugin
78 | 2.15.2
79 |
80 |
81 |
82 | compile
83 |
84 |
85 |
86 |
87 |
88 | maven-compiler-plugin
89 | 3.1
90 |
91 | 1.6
92 | 1.6
93 |
94 |
95 |
96 | org.apache.maven.plugins
97 | maven-shade-plugin
98 | 2.3
99 |
100 |
101 | package
102 |
103 | shade
104 |
105 |
106 |
107 |
108 | org.apache.spark:blaze_2.10
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
--------------------------------------------------------------------------------
/functional-test/arraytest/app/run.sh.template:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | SPARK_HOME=
4 | --driver-memory 8G \
5 | --executor-memory 8G \
6 | --master local[*] \
7 | target/test-0.0.0.jar $@
8 |
9 |
--------------------------------------------------------------------------------
/functional-test/arraytest/app/src/main/scala/testapp/ArrayTest.scala:
--------------------------------------------------------------------------------
1 | import Array._
2 | import scala.math._
3 | import java.net._
4 | import org.apache.spark.rdd._
5 | import org.apache.spark.SparkContext
6 | import org.apache.spark.SparkContext._
7 | import org.apache.spark.SparkConf
8 | import org.apache.spark.util.random._
9 | import org.apache.spark.blaze._
10 |
11 | class ArrayTest(v: BlazeBroadcast[Array[Double]])
12 | extends Accelerator[Array[Double], Array[Double]] {
13 |
14 | val id: String = "ArrayTest"
15 |
16 | def getArgNum(): Int = 1
17 |
18 | def getArg(idx: Int): Option[_] = {
19 | if (idx == 0)
20 | Some(v)
21 | else
22 | None
23 | }
24 |
25 | override def call(in: Array[Double]): Array[Double] = {
26 | val ary = new Array[Double](in.length)
27 | val s = v.data.sum
28 | for (i <- 0 until in.length) {
29 | ary(i) = in(i) + s
30 | }
31 | ary
32 | }
33 |
34 | override def call(in: Iterator[Array[Double]]): Iterator[Array[Double]] = {
35 | val inAry = in.toArray
36 | val length: Int = inAry.length
37 | val itemLength: Int = inAry(0).length
38 | val outAry = new Array[Array[Double]](length)
39 | val s = v.data.sum
40 |
41 | for (i <- 0 until length) {
42 | outAry(i) = new Array[Double](itemLength)
43 | for (j <- 0 until itemLength)
44 | outAry(i)(j) = inAry(i)(j) + s
45 | }
46 |
47 | outAry.iterator
48 | }
49 | }
50 |
51 | object TestApp {
52 | def main(args : Array[String]) {
53 |
54 | val conf = new SparkConf()
55 | conf.setAppName("TestApp")
56 |
57 | val sc = new SparkContext(conf)
58 | val acc = new BlazeRuntime(sc)
59 |
60 | val v = Array(1.1, 2.2, 3.3)
61 |
62 | println("Functional test: array type AccRDD with array type broadcast value")
63 |
64 | val data = new Array[Array[Double]](256)
65 | for (i <- 0 until 256) {
66 | data(i) = new Array[Double](2).map(e => random)
67 | }
68 | val rdd = sc.parallelize(data, 256)
69 |
70 | val rdd_acc = acc.wrap(rdd)
71 | val brdcst_v = acc.wrap(sc.broadcast(v))
72 |
73 | val res0 = rdd_acc.map_acc(new ArrayTest(brdcst_v)).collect
74 | val res1 = rdd_acc.mapPartitions_acc(new ArrayTest(brdcst_v)).collect
75 | val res2 = rdd.map(e => e.map(ee => ee + v.sum)).collect
76 |
77 | // compare results
78 | if (res0.deep != res1.deep ||
79 | res1.deep != res2.deep ||
80 | res0.deep != res2.deep)
81 | {
82 | println("input: \n" + data.deep.mkString("\n"))
83 | println("map result: \n" + res2.deep.mkString("\n"))
84 | println("map_acc results: \n" + res0.deep.mkString("\n"))
85 | println("mapParititions_acc results: \n" + res1.deep.mkString("\n"))
86 | }
87 | else {
88 | println("result correct")
89 | }
90 | acc.stop()
91 | }
92 | }
93 |
94 |
--------------------------------------------------------------------------------
/functional-test/arraytest/client/Makefile:
--------------------------------------------------------------------------------
1 | MANAGER_DIR=../../../manager
2 | include ../../../Makefile.config
3 |
4 | # check all variables
5 | ifeq ($(BOOST_DIR),)
6 | $(error BOOST_DIR not set properly in Makefile.config)
7 | endif
8 | ifeq ($(PROTOBUF_DIR),)
9 | $(error PROTOBUF_DIR not set properly in Makefile.config)
10 | endif
11 |
12 | PP=g++
13 |
14 | CFLAGS= -c -fPIC -std=c++0x -g
15 | COMPILE= $(CFLAGS) \
16 | -I$(MANAGER_DIR)/src \
17 | -I$(BOOST_DIR)/include \
18 | -I$(PROTOBUF_DIR)/include
19 |
20 | LINK= -L$(MANAGER_DIR)/lib -lblaze \
21 | -L$(BOOST_DIR)/lib \
22 | -lboost_system -lboost_thread -lboost_iostreams -lboost_filesystem -lboost_regex \
23 | -L$(PROTOBUF_DIR)/lib -lprotobuf \
24 | -L$(GLOG_DIR)/lib -lglog \
25 | -lpthread -lm -ldl
26 |
27 | all: app
28 |
29 | app: main.o
30 | $(PP) -o $@ main.o $(LINK)
31 |
32 | %.o: %.cpp Makefile
33 | $(PP) $(COMPILE) $< -o $@
34 |
35 | clean:
36 | rm -rf *.o
37 | rm -rf app
38 |
--------------------------------------------------------------------------------
/functional-test/arraytest/client/main.cpp:
--------------------------------------------------------------------------------
1 |
2 | #include "Client.h"
3 |
4 | #define LABEL_SIZE 10
5 | #define FEATURE_SIZE 784
6 |
7 | using namespace blaze;
8 |
9 | int main(int argc, char** argv) {
10 |
11 | if (argc < 2) {
12 | printf("USAGE: %s \n", argv[0]);
13 | return -1;
14 | }
15 |
16 | int num_samples = atoi(argv[1]);
17 | int feature_size = 1024;
18 |
19 | if (argc > 3) {
20 | feature_size = atoi(argv[2]);
21 | }
22 | int data_size = num_samples*feature_size;
23 |
24 | try {
25 | Client client("ArrayTest", "C++");
26 |
27 | double* data_ptr = (double*)client.alloc(num_samples, feature_size, feature_size*sizeof(double), BLAZE_INPUT);
28 | double* weight_ptr = (double*)client.alloc(feature_size, 1, sizeof(double), BLAZE_INPUT);
29 | double* output_ptr = (double*)client.alloc(num_samples, feature_size, feature_size*sizeof(double), BLAZE_OUTPUT);
30 |
31 | double* output_base = new double[num_samples*feature_size];
32 |
33 | // setup input with random data
34 | for (int i=0; i max_diff) {
62 | max_diff = diff;
63 | }
64 |
65 | diff_total += diff;
66 | if (output_base[k]!=0) {
67 | diff_ratio += diff / std::abs(output_base[k]);
68 | }
69 |
70 | if (diff / std::abs(output_base[k]) > 0.05) {
71 | printf("%d: %f|%f, ratio=%f\n",
72 | k,
73 | output_base[k],
74 | output_ptr[k],
75 | diff/std::abs(output_base[k]));
76 | }
77 | }
78 | if (diff_total < 1e-6) {
79 | printf("Result correct\n");
80 | }
81 | else {
82 | printf("Result incorrect\n");
83 | printf("diff: %f max, %f/point, %f%/point\n",
84 | max_diff,
85 | diff_total/(data_size+1),
86 | diff_ratio/(data_size+1)*100.0);
87 | }
88 | }
89 | catch (std::exception &e) {
90 | printf("%s\n", e.what());
91 | return -1;
92 | }
93 |
94 | return 0;
95 | }
96 |
--------------------------------------------------------------------------------
/functional-test/arraytest/ocl_task/ArrayTest.cl:
--------------------------------------------------------------------------------
1 | __kernel
2 | void arrayTest(
3 | int weight_length,
4 | int num_items,
5 | int item_length,
6 | __global double* val,
7 | __global double* a,
8 | __global double* b
9 | )
10 | {
11 | int gid = get_group_id(0);
12 | int gdim = get_num_groups(0);
13 |
14 | int tid = get_local_id(0);
15 | int tdim = get_local_size(0);
16 |
17 | for (int i = gid; i < num_items; i+=gdim) {
18 | for (int j = tid; j < item_length; j+=tdim) {
19 | double val_sum = 0.0;
20 | for (int k = 0; k < weight_length; k++) {
21 | val_sum += val[k];
22 | }
23 | b[i * item_length + j] = a[i * item_length +j] + val_sum;
24 | }
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/functional-test/arraytest/ocl_task/ArrayTest.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include
5 | #include
6 | #include
7 | #include
8 | #include
9 |
10 | #include "Task.h"
11 | #include "OpenCLEnv.h"
12 |
13 | using namespace blaze;
14 |
15 | class ArrayTest : public Task {
16 | public:
17 |
18 | // extends the base class constructor
19 | // to indicate how many input blocks
20 | // are required
21 | ArrayTest(): Task(2) {;}
22 |
23 | // overwrites the compute function
24 | // Input data:
25 | // - data: layout as num_samples x [double label, double[] feature]
26 | // - weight: (num_labels-1) x feature_length
27 | // Output data:
28 | // - gradient plus loss: [double[] gradient, double loss]
29 | virtual void compute() {
30 |
31 | struct timeval t1, t2, tr;
32 |
33 | try {
34 | // dynamically cast the TaskEnv to OpenCLEnv
35 | OpenCLTaskEnv* ocl_env = (OpenCLTaskEnv*)getEnv();
36 |
37 | // get input data length
38 | int data_length = getInputLength(0);
39 | int num_samples = getInputNumItems(0);
40 | int weight_length = getInputLength(1);
41 | int item_length = data_length / num_samples;
42 |
43 | // get OpenCL context
44 | cl_context context = ocl_env->getContext();
45 | cl_command_queue command = ocl_env->getCmdQueue();
46 | cl_program program = ocl_env->getProgram();
47 |
48 | int err;
49 | cl_event event;
50 |
51 | // get corresponding cl_kernel
52 | cl_kernel kernel = clCreateKernel(
53 | program, "arrayTest", &err);
54 |
55 | if (!kernel || err != CL_SUCCESS) {
56 | throw std::runtime_error(
57 | "Failed to create compute kernel arrayTest");
58 | }
59 |
60 | // get the pointer to input/output data
61 | cl_mem ocl_data = *((cl_mem*)getInput(0));
62 | cl_mem ocl_weights = *((cl_mem*)getInput(1));
63 | cl_mem ocl_output = *((cl_mem*)getOutput(
64 | 0, item_length, num_samples,
65 | sizeof(double)));
66 |
67 | if (!ocl_data || !ocl_weights || !ocl_output) {
68 | throw std::runtime_error("Buffer are not allocated");
69 | }
70 |
71 | // Set the arguments to our compute kernel
72 | err = clSetKernelArg(kernel, 0, sizeof(int), &weight_length);
73 | err |= clSetKernelArg(kernel, 1, sizeof(int), &num_samples);
74 | err |= clSetKernelArg(kernel, 2, sizeof(int), &item_length);
75 | err |= clSetKernelArg(kernel, 3, sizeof(cl_mem), &ocl_weights);
76 | err |= clSetKernelArg(kernel, 4, sizeof(cl_mem), &ocl_data);
77 | err |= clSetKernelArg(kernel, 5, sizeof(cl_mem), &ocl_output);
78 | if (err != CL_SUCCESS) {
79 | throw std::runtime_error("Cannot set kernel args\n");
80 | }
81 |
82 | size_t work_lsize[1] = {128};
83 | size_t work_gsize[1];
84 | work_gsize[0] = 64*work_lsize[0];
85 |
86 | err = clEnqueueNDRangeKernel(command, kernel, 1, NULL, work_gsize, work_lsize, 0, NULL, &event);
87 | clWaitForEvents(1, &event);
88 | }
89 | catch (std::runtime_error &e) {
90 | throw e;
91 | }
92 | }
93 | };
94 |
95 | extern "C" Task* create() {
96 | return new ArrayTest();
97 | }
98 |
99 | extern "C" void destroy(Task* p) {
100 | delete p;
101 | }
102 |
--------------------------------------------------------------------------------
/functional-test/arraytest/ocl_task/Makefile:
--------------------------------------------------------------------------------
1 | ifeq ($(BLAZE_HOME),)
2 | $(error BLAZE_HOME not set)
3 | endif
4 | ifeq ("$(wildcard $(BLAZE_HOME)/Makefile.config)","")
5 | $(error BLAZE_HOME not set properly in environment)
6 | endif
7 |
8 | MANAGER_DIR=$(BLAZE_HOME)/manager
9 | PLATFORM_DIR=$(BLAZE_HOME)/platforms/gpu-ocl
10 | include $(BLAZE_HOME)/Makefile.config
11 |
12 | PP=g++
13 | CC=gcc
14 |
15 | CFLAGS= -shared -fPIC -std=c++0x -m64 -g
16 | COMPILE= $(CFLAGS) \
17 | -I$(MANAGER_DIR)/include \
18 | -I$(MANAGER_DIR)/src \
19 | -I$(PLATFORM_DIR) \
20 | -I$(BOOST_DIR)/include \
21 | -I$(PROTOBUF_DIR)/include \
22 | -I$(HADOOP_DIR)/include \
23 | -I$(CUDA_DIR)/include
24 |
25 | ACC = ArrayTest
26 |
27 | OBJS= $(addsuffix .so, $(ACC))
28 |
29 | all: $(OBJS)
30 |
31 | %.so: %.cpp Makefile
32 | $(PP) $(COMPILE) $< -o $@
33 |
34 | clean:
35 | rm -rf $(OBJS)
36 |
--------------------------------------------------------------------------------
/functional-test/conf.prototxt:
--------------------------------------------------------------------------------
1 | port : 1027
2 | verbose : 2
3 | platform : {
4 | cache_limit: 16384
5 | scratch_limit: 4096
6 | acc : {
7 | id : "Tuple2Test"
8 | path : "functional-test/tuple2test/Tuple2Test.so"
9 | }
10 | acc : {
11 | id : "MaskTest"
12 | path : "functional-test/masktest/MaskTest.so"
13 | }
14 | acc : {
15 | id : "MapTest"
16 | path : "functional-test/maptest/MapTest.so"
17 | }
18 | acc : {
19 | id : "ArrayTest"
20 | path : "functional-test/arraytest/ArrayTest.so"
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/functional-test/masktest/acc_task/Makefile:
--------------------------------------------------------------------------------
1 | MANAGER_DIR=../../../manager
2 | include ../../../Makefile.config
3 |
4 | # check all variables
5 | ifeq ($(BOOST_DIR),)
6 | $(error BOOST_DIR not set properly in Makefile.config)
7 | endif
8 | ifeq ($(PROTOBUF_DIR),)
9 | $(error PROTOBUF_DIR not set properly in Makefile.config)
10 | endif
11 | PP=g++
12 | CC=gcc
13 |
14 | CFLAGS= -shared -fPIC -std=c++0x -m64 -g
15 | COMPILE= $(CFLAGS) \
16 | -I$(MANAGER_DIR)/include \
17 | -I$(BOOST_DIR)/include \
18 | -I$(PROTOBUF_DIR)/include
19 |
20 | ACC = MaskTest
21 |
22 | OBJS= $(addsuffix .so, $(addprefix ../, $(ACC)))
23 |
24 | all: $(OBJS)
25 |
26 | ../%.so: %.cpp Makefile
27 | $(PP) $(COMPILE) $< -o $@
28 |
29 | clean:
30 | rm -rf $(OBJS)
31 |
--------------------------------------------------------------------------------
/functional-test/masktest/acc_task/MaskTest.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | #include "blaze.h"
5 |
6 | using namespace blaze;
7 |
8 | class MaskTest : public Task {
9 | public:
10 |
11 | // extends the base class constructor
12 | // to indicate how many input blocks
13 | // are required
14 | MaskTest(): Task(1) {;}
15 |
16 | // overwrites the compute function
17 | virtual void compute() {
18 |
19 | // get input data length
20 | int data_length = getInputLength(0);
21 |
22 | // get the pointer to input/output data
23 | double* a = (double*)getInput(0);
24 | double* b = (double*)getOutput(0, 1, data_length, sizeof(double));
25 |
26 | // perform computation
27 | for (int i=0; i
2 |
16 |
17 | test
18 | test
19 | jar
20 | "MaskTest"
21 | 4.0.0
22 | 0.0.0
23 |
24 |
25 |
26 | scala-tools.org
27 | Scala-tools Maven2 Repository
28 | http://scala-tools.org/repo-releases
29 |
30 |
31 | maven-hadoop
32 | Hadoop Releases
33 | https://repository.cloudera.com/content/repositories/releases/
34 |
35 |
36 | cloudera-repos
37 | Cloudera Repos
38 | https://repository.cloudera.com/artifactory/cloudera-repos/
39 |
40 |
41 |
42 |
43 |
44 | scala-tools.org
45 | Scala-tools Maven2 Repository
46 | http://scala-tools.org/repo-releases
47 |
48 |
49 |
50 |
51 | UTF-8
52 | UTF-8
53 |
54 |
55 |
56 |
57 |
58 | org.scala-tools
59 | maven-scala-plugin
60 | 2.15.2
61 |
62 |
63 |
64 | compile
65 |
66 |
67 |
68 |
69 |
70 | maven-compiler-plugin
71 | 3.1
72 |
73 | 1.6
74 | 1.6
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 | org.scala-lang
83 | scala-library
84 | 2.11.4
85 |
86 |
87 | org.apache.spark
88 | spark-core_2.10
89 | 1.3.1
90 |
91 |
92 | org.apache.spark
93 | blaze
94 | 1.0-SNAPSHOT
95 |
96 |
97 |
98 |
--------------------------------------------------------------------------------
/functional-test/masktest/app/src/main/scala/testapp/MaskTest.scala:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | import org.apache.spark.SparkContext
19 | import org.apache.spark.SparkContext._
20 | import org.apache.spark.SparkConf
21 | import org.apache.spark.util.random._
22 | import Array._
23 | import scala.util.Random
24 | import org.apache.spark.rdd._
25 | import java.net._
26 |
27 | // comaniac: Import extended package
28 | import org.apache.spark.blaze._
29 |
30 | class MaskTest() extends Accelerator[Double, Double] {
31 | val id: String = "MaskTest"
32 |
33 | def getArg(idx: Int): Option[_] = None
34 | def getArgNum(): Int = 0
35 |
36 | override def call(in: Iterator[Double]): Iterator[Double] = {
37 | in.filter { a =>
38 | a > 0.5
39 | }
40 | }
41 | }
42 |
43 | object TestApp {
44 | def main(args : Array[String]) {
45 | val sc = get_spark_context("Test App")
46 | println("Functional test: AccRDD.sample_acc")
47 | println("Set random seed as 904401792 to activate TestSampler.")
48 |
49 | val rander = new Random(0)
50 | val data = new Array[Double](150).map(e => rander.nextDouble)
51 | val rdd = sc.parallelize(data, 4).cache()
52 |
53 | val acc = new BlazeRuntime(sc)
54 | val rdd_acc = acc.wrap(rdd)
55 |
56 | val rdd_sampled = rdd_acc.sample_acc(true, 0.4, 904401792)
57 | val res_acc = rdd_sampled.mapPartitions_acc(new MaskTest()).collect
58 | val res_cpu = rdd.mapPartitions(iter => {
59 | val out = new Array[Double](30)
60 | for (i <- 0 until 30)
61 | out(i) = iter.next
62 | out.iterator
63 | }).collect
64 |
65 | if (res_cpu.deep != res_acc.deep) {
66 | println("CPU result: \n" + res_cpu.deep.mkString("\n"))
67 | println("ACC result: \n" + res_acc.deep.mkString("\n"))
68 | } else {
69 | println("result correct")
70 | }
71 |
72 | acc.stop()
73 | }
74 |
75 | def get_spark_context(appName : String) : SparkContext = {
76 | val conf = new SparkConf()
77 | conf.setAppName(appName)
78 |
79 | return new SparkContext(conf)
80 | }
81 | }
82 |
83 |
--------------------------------------------------------------------------------
/functional-test/tuple2test/acc_task/Makefile:
--------------------------------------------------------------------------------
1 | ifeq ($(BLAZE_HOME),)
2 | $(error BLAZE_HOME not set)
3 | endif
4 | ifeq ("$(wildcard $(BLAZE_HOME)/Makefile.config)","")
5 | $(error BLAZE_HOME not set properly in environment)
6 | endif
7 |
8 | MANAGER_DIR=$(BLAZE_HOME)/manager
9 | include $(BLAZE_HOME)/Makefile.config
10 |
11 | # check all variables
12 | ifeq ($(BOOST_DIR),)
13 | $(error BOOST_DIR not set properly in Makefile.config)
14 | endif
15 | ifeq ($(PROTOBUF_DIR),)
16 | $(error PROTOBUF_DIR not set properly in Makefile.config)
17 | endif
18 | PP=g++
19 | CC=gcc
20 |
21 | CFLAGS= -shared -fPIC -std=c++0x -m64 -g
22 | COMPILE= $(CFLAGS) \
23 | -I$(MANAGER_DIR)/include \
24 | -I$(BOOST_DIR)/include \
25 | -I$(PROTOBUF_DIR)/include
26 |
27 | ACC = Tuple2Test
28 |
29 | OBJS= $(addsuffix .so, $(addprefix ../, $(ACC)))
30 |
31 | all: $(OBJS)
32 |
33 | ../%.so: %.cpp Makefile
34 | $(PP) $(COMPILE) $< -o $@
35 |
36 | clean:
37 | rm -rf $(OBJS)
38 |
--------------------------------------------------------------------------------
/functional-test/tuple2test/acc_task/Tuple2Test.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | #include "blaze.h"
5 |
6 | using namespace blaze;
7 |
8 | class Tuple2Test : public Task {
9 | public:
10 |
11 | // extends the base class constructor
12 | // to indicate how many input blocks
13 | // are required
14 | Tuple2Test(): Task(2) {;}
15 |
16 | // overwrites the compute function
17 | virtual void compute() {
18 |
19 | // get input data length
20 | int data_length = getInputLength(0);
21 |
22 | // get the pointer to input/output data
23 | int* a = (int*)getInput(0);
24 | int* b = (int*)getInput(1);
25 | int* c = (int*)getOutput(0, 1, data_length, sizeof(int));
26 |
27 | // perform computation
28 | for (int i=0; i
2 |
16 |
17 | test
18 | test
19 | jar
20 | "Tuple2Test"
21 | 4.0.0
22 | 0.0.0
23 |
24 |
25 |
26 | scala-tools.org
27 | Scala-tools Maven2 Repository
28 | http://scala-tools.org/repo-releases
29 |
30 |
31 | maven-hadoop
32 | Hadoop Releases
33 | https://repository.cloudera.com/content/repositories/releases/
34 |
35 |
36 | cloudera-repos
37 | Cloudera Repos
38 | https://repository.cloudera.com/artifactory/cloudera-repos/
39 |
40 |
41 |
42 |
43 |
44 | scala-tools.org
45 | Scala-tools Maven2 Repository
46 | http://scala-tools.org/repo-releases
47 |
48 |
49 |
50 |
51 | UTF-8
52 | UTF-8
53 |
54 |
55 |
56 |
57 |
58 | org.scala-tools
59 | maven-scala-plugin
60 | 2.15.2
61 |
62 |
63 |
64 | compile
65 |
66 |
67 |
68 |
69 |
70 | maven-compiler-plugin
71 | 3.1
72 |
73 | 1.6
74 | 1.6
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 | org.scala-lang
83 | scala-library
84 | 2.11.4
85 |
86 |
87 | org.apache.spark
88 | spark-core_2.10
89 | 1.3.1
90 |
91 |
92 | org.apache.spark
93 | blaze
94 | 1.0-SNAPSHOT
95 |
96 |
97 |
98 |
--------------------------------------------------------------------------------
/functional-test/tuple2test/app/src/main/scala/testapp/Tuple2Test.scala:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | import org.apache.spark.SparkContext
19 | import org.apache.spark.SparkContext._
20 | import org.apache.spark.SparkConf
21 | import org.apache.spark.util.random._
22 | import Array._
23 | import scala.math._
24 | import org.apache.spark.rdd._
25 | import java.net._
26 |
27 | // comaniac: Import extended package
28 | import org.apache.spark.blaze._
29 |
30 | // TODO: Tuple2 as an output type
31 | class Tuple2Test extends Accelerator[Tuple2[Double, Double], Double] {
32 | val id: String = "Tuple2Test"
33 |
34 | def getArgNum(): Int = 0
35 |
36 | def getArg(idx: Int): Option[_] = None
37 |
38 | override def call(in: Tuple2[Double, Double]): Double = {
39 | in._1 + in._2
40 | }
41 |
42 | override def call(in: Iterator[Tuple2[Double, Double]]): Iterator[Double] = {
43 | val inAry = in.toArray
44 | val length: Int = inAry.length
45 | val outAry = new Array[Double](length)
46 |
47 | for (i <- 0 until length)
48 | outAry(i) = inAry(i)._1 + inAry(i)._2
49 |
50 | outAry.iterator
51 | }
52 | }
53 |
54 | object TestApp {
55 | def main(args : Array[String]) {
56 | val sc = get_spark_context("Tuple2 Test")
57 | val data = new Array[Double](1024).map(e => (random, random))
58 | val rdd = sc.parallelize(data, 8).cache
59 |
60 | val acc = new BlazeRuntime(sc)
61 | val rdd_acc = acc.wrap(rdd)
62 |
63 | println("map Result: " + rdd_acc.map_acc(new Tuple2Test).reduce((a, b) => (a + b)))
64 | println("mapPartition Result: " + rdd_acc.mapPartitions_acc(new Tuple2Test).reduce((a, b) => (a + b)))
65 | println("CPU Result: " + rdd_acc.map({case (a, b) => (a + b)}).reduce((a, b) => (a + b)))
66 |
67 | acc.stop()
68 | }
69 |
70 | def get_spark_context(appName : String) : SparkContext = {
71 | val conf = new SparkConf()
72 | conf.setAppName(appName)
73 |
74 | return new SparkContext(conf)
75 | }
76 | }
77 |
78 |
--------------------------------------------------------------------------------
/manager/bin/.gitignore:
--------------------------------------------------------------------------------
1 | # Ignore everything in this directory
2 | *
3 | # Except this file
4 | !.gitignore
5 |
--------------------------------------------------------------------------------
/manager/include/blaze/AccAgent.h:
--------------------------------------------------------------------------------
1 | #ifndef ACCAGENT_H
2 | #define ACCAGENT_H
3 |
4 | #include
5 |
6 | #include "Common.h"
7 | #include "Task.h"
8 | #include "proto/acc_conf.pb.h"
9 |
10 | namespace blaze {
11 |
12 | typedef boost::shared_ptr ManagerConf_ptr;
13 |
14 | class AccAgent {
15 | public:
16 | AccAgent(const char* conf_path);
17 |
18 | Task_ptr createTask(std::string acc_id);
19 |
20 | void writeInput(Task_ptr task,
21 | std::string acc_id,
22 | void* data_ptr,
23 | int num_items,
24 | int item_length,
25 | int data_width);
26 |
27 | void readOutput(Task_ptr task,
28 | void* data_ptr,
29 | size_t data_size);
30 |
31 | private:
32 | ManagerConf_ptr conf_;
33 | PlatformManager_ptr platform_manager_;
34 | };
35 |
36 | } // namespace blaze
37 | #endif
38 |
--------------------------------------------------------------------------------
/manager/include/blaze/Admin.h:
--------------------------------------------------------------------------------
1 | #ifndef ADMIN_H
2 | #define ADMIN_H
3 |
4 | #include "proto/task.pb.h"
5 | #include "proto/acc_conf.pb.h"
6 |
7 | #include "Common.h"
8 |
9 | // for testing purpose
10 | #ifndef TEST_FRIENDS_LIST
11 | #define TEST_FRIENDS_LIST
12 | #endif
13 |
14 | namespace blaze {
15 |
16 | class Admin {
17 | TEST_FRIENDS_LIST
18 | public:
19 | Admin(std::string _ip = "127.0.0.1");
20 |
21 | bool registerAcc(ManagerConf &conf);
22 | bool deleteAcc(ManagerConf &conf);
23 |
24 | private:
25 |
26 | void sendMessage(TaskMsg &msg);
27 |
28 | // data structures for socket connection
29 | int nam_port;
30 | std::string ip_address;
31 | ios_ptr ios;
32 | endpoint_ptr endpoint;
33 | };
34 |
35 | } // namespace blaze
36 | #endif
37 |
--------------------------------------------------------------------------------
/manager/include/blaze/Block.h:
--------------------------------------------------------------------------------
1 | #ifndef BLOCK_H
2 | #define BLOCK_H
3 |
4 | #include
5 |
6 | #include "Common.h"
7 |
8 | /*
9 | * base class extendable to manage memory block
10 | * on other memory space (e.g. FPGA device memory)
11 | */
12 |
13 | namespace blaze {
14 |
15 | class DataBlock
16 | : public boost::basic_lockable_adapter
17 | {
18 |
19 | public:
20 |
21 | // create basic data block
22 | DataBlock(int _num_items,
23 | int _item_length,
24 | int _item_size,
25 | int _align_width = 0,
26 | int _flag = BLAZE_INPUT_BLOCK);
27 |
28 | DataBlock(const DataBlock &block);
29 |
30 | ~DataBlock();
31 |
32 | // allocate data aligned to a given width
33 | void alloc(int _align_width);
34 |
35 | // allocate data
36 | virtual void alloc();
37 |
38 | // copy data from an array
39 | virtual void writeData(void* src, size_t _size);
40 |
41 | // copy data from an array with offset
42 | virtual void writeData(void* src, size_t _size, size_t offset);
43 |
44 | // write data to an array
45 | virtual void readData(void* dst, size_t size);
46 |
47 | // get the pointer to data
48 | virtual char* getData();
49 |
50 | // sample the items in the block by a mask
51 | virtual boost::shared_ptr sample(char* mask);
52 |
53 | virtual void readFromMem(std::string path);
54 | virtual void writeToMem(std::string path);
55 |
56 | int getNumItems() { return num_items; }
57 | int getItemLength() { return item_length; }
58 | int getItemSize() { return item_size; }
59 | int getLength() { return length; }
60 | int getSize() { return size; }
61 | int getFlag() { return flag; }
62 |
63 | // status check of DataBlock needs to be exclusive
64 | bool isAllocated();
65 | bool isReady();
66 |
67 | protected:
68 | int flag; /* enum: input, shared, output */
69 | int item_length; /* number of elements per data item */
70 | int item_size; /* byte size per data item */
71 | int num_items; /* number of data items per data block */
72 | int data_width; /* byte size per element */
73 | int align_width; /* align data width per data item */
74 | int length; /* total number of elements */
75 | int64_t size; /* total byte size of the data block */
76 |
77 | bool allocated;
78 | bool aligned;
79 | bool ready;
80 | bool copied;
81 |
82 | private:
83 | char* data;
84 | };
85 |
86 | const DataBlock_ptr NULL_DATA_BLOCK;
87 |
88 | }
89 | #endif
90 |
--------------------------------------------------------------------------------
/manager/include/blaze/BlockManager.h:
--------------------------------------------------------------------------------
1 | #ifndef BLOCK_MANAGER_H
2 | #define BLOCK_MANAGER_H
3 |
4 | #include