\begin{figure*}[t]
\begin{center}
\includegraphics[width=6in]{fig/flow.pdf}
\end{center}
\caption{Compilation Flow and Runtime}
\label{fig:flow}
\end{figure*}

This section describes features of the Habanero-Java (HJ) parallel programming language
which are added or supported by HJ-OpenCL.

\subsection{Overview of HJ language}
The Habanero Java (HJ) parallel programming language~\cite{hj-pppj11} provides an execution model for multicore processors
that builds on four orthogonal constructs:

\begin{enumerate}
\item
Lightweight {\em dynamic task creation and termination} using {\em async} and {\em finish constructs}~\cite{wf-hf-ipdps09}.
\item
{\em Locality control} with task and data distributions using the {\em place} construct~\cite{CSSB08}.
\item
Mutual exclusion and isolation among tasks using the {\em isolated} construct~\cite{LZBC11}.
\item
Collective and point-to-point synchronization using the {\em phasers}
construct~\cite{SPSS08}.
\end{enumerate}

\noindent
%The HJ language derives from initial versions of the X10 language (up to v1.5) that used Java as the underlying sequential language~\cite{x10-oopsla2005}.
%In contrast, more recent versions of X10 starting with v1.7 have moved to a Scala-like syntax~\cite{x10v17}.
Since HJ is based on Java, the use of certain primitives from the Java
Concurrency Utilities~\cite{JCIP} is also permitted in HJ programs.
%
%In this section, we briefly recapitulate {\em async} and {\em finish}
%constructs for task creation and termination, and {\em array view} to
%provide a multi-dimensional view of one-dimensional array.  The whole
%language specification is available in \cite{hjlang}.

HJ-OpenCL only supports OpenCL execution for a subset of the parallel
language features of Habanero-Java, though the full HJ language can still be
used on multiple CPU cores within the JVM.
Work to date on HJ-OpenCL has focused on supporting \hjforall{} parallel loops and \nextOp{} barrier statements for OpenCL execution:
\begin{enumerate}
\item {\bf forall:}
The statement ``\hjforall{}{\tt \small (point p : }{\em
region}{\tt \small)} $\langle${\em stmt}$\rangle$'' indicates a
parallel loop whose iteration space is defined by a {\em region}.  The
region can be one- or multi-dimensional space, e.g.,
{\tt \small [0:M-1,0:N-1]} for a 2-D iteration space. Each iteration instance
executes the loop body $\langle${\em stmt}$\rangle$ given a different $point$ in the iteration space, i.e., region.  
All \hjforall{} loops end with an implicit barrier (finish scope).

\item {\bf next:}
The statement \nextOp{} represents an all-to-all barrier synchronization\footnote{HJ-OpenCL only supports the all-to-all barrier synchronization in phasers due to OpenCL constraints. (See Section~\ref{subsec:barrier}.)}
point with both signal and wait semantics in a parallel \hjforall{} loop.
\end{enumerate}

\subsection{The \safe{} Construct}\label{subsec:safe}

We propose the  addition of a \safe{} language construct to Habanero-Java.
Programmers can use the \safe{} language feature to assert conditions
which ensure no exception will be thrown in a code region.
The ``\safe{} (boolean cond) $\langle${\em stmt}$\rangle$'' construct indicates that 
$\langle${\em stmt}$\rangle$ is asserted to not throw an exception if {\tt cond} is true.

To illustrate this, let us again consider the ArraySum example from Section~\ref{sec:motivation}.
To check the non-nullness of \textit{result}, \textit{arrays}, and \textit{arrayViews}, a programmer would put 
\textit{isSafe = (result != null \&\& arrays != null \&\& arraysView != null)}.
It would also be necessary to add \textit{N - 1 $\le$ result.length
  \&\& N*M-1 $\le$ arrays.length} for array bounds checking.
The schema for this approach is shown in Figure~\ref{fig:hjopencl-example}.
The HJ-OpenCL compiler and runtime can then use these conditions to safely generate and execute code on OpenCL devices.
Section~\ref{subsec:flow} describes the detail of code generation for
the \safe{} constructs.  Note that this approach can be used to
accelerate performance both on CPUs and on GPUs due to the use of
OpenCL instead of Java, while still preserving exception semantics.

\subsection{Support for Rectangular Multi-Dimensional Arrays}
\label{sec:hj-multi}

The OpenCL specification~\cite{opencl-spec} does not support the use of
Java-style and C-style multi-dimensional arrays 
in OpenCL kernels. However, computationally intensive Java programs
often use rectangular multi-dimensional arrays as a basic data structure. Therefore, it is important to provide software
developers with language support in HJ-OpenCL for rectangular multi-dimensional arrays while enabling efficient translation and transfer of those
arrays for use in OpenCL kernels.

In the HJ-OpenCL language,  HJ's array views are used to provide the software developer with what syntactically appears
to be a multi-dimensional array but which is actually backed by a
one-dimensional array at the Java level.
The statement ``{\em dataType}{\tt \small [.] v = new arrayView(}{\em
baseArr}{\tt,}{\em offset}{\tt,}{\em region}{\tt)}'' declares a view
{\tt v} on a one-dimensional array {\em baseArr} with starting offset
{\em offset}.  The created view {\tt v} can then be referenced with a
Fortran-like comma-separate multi-dimensional array syntax, with bounds defined by {\em region}. 
The details of supporting array views at compile- and run-time in HJ-OpenCL 
are discussed in Section~\ref{subsec:multidimension}.
