Skip to content

Commit b010198

Browse files
author
Denis Jelovina
committed
Clarify element-wise operations in Numerical Linear Algebra section and correct variable references in examples
1 parent d110c39 commit b010198

File tree

1 file changed

+32
-48
lines changed

1 file changed

+32
-48
lines changed

ALP_Tutorial.tex

Lines changed: 32 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -261,10 +261,14 @@ \subsection{Copying, Masking, and Standard Matrices}
261261
\subsection{Numerical Linear Algebra}
262262

263263
GraphBLAS, as the name implies, provides canonical BLAS-like functionalities on sparse matrices, sparse vectors, and dense vectors. These include \texttt{grb::dot}, \texttt{grb::eWiseAdd}, \texttt{grb::eWiseMul}, \texttt{grb::mxv}, \texttt{grb::vxm}, and \texttt{grb::mxm}. The former three scalar/vector primitives are dubbed \emph{level 1}, the following two matrix--vector primitives \emph{level 2}, and the latter matrix--matrix primitive \emph{level 3}. Their functionalities are summarised as follows:\newline
264-
264+
265265
\textbf{grb::dot} – Compute the dot product of two vectors, $\alpha$\textit{+=}$u^Tv$ or $\alpha$\textit{+=}$\sum_i (u_i \times v_i)$, in essence combining element-wise multiplication with a reduction. The output $\alpha$ is a scalar, usually a primitive type such as \texttt{double}. Unlike the out-of-place \texttt{grb::set}, the \texttt{grb::dot} updates the output scalar in-place.\newline
266266

267-
\textbf{grb::eWiseMul, grb::eWiseAdd} - These primitives combine two containers element-by-element, the former using element-wise multiplication, and the latter using element-wise addition. Different from \texttt{grb::set}, these primitives are in-place, meaning the result of the element-wise operations are added to any elements already in the output container; i.e., \texttt{grb::eWiseMul} computes $z$\textit{+=}$x\odot y$, where $\odot$ denotes element-wise multiplication. In case of sparse vectors and an initially-empty output container, the primitives separate themselves in terms of the structure of the output vector, which is composed either of an intersection or union of the input structures.
267+
\textbf{grb::eWiseMul, grb::eWiseAdd} - These primitives combine two containers element-by-element, the former using element-wise multiplication, and the latter using element-wise addition. Different from \texttt{grb::set},
268+
these primitives are in-place, meaning the result of the element-wise operations are added to any elements already in the output container; i.e., \texttt{grb::eWiseMul} computes $z$\textit{+=}$x\odot y$, where $\odot$ denotes element-wise multiplication.
269+
In case of sparse vectors and an initially-empty output container, the primitives separate themselves in terms of the structure of the output vector, which is composed either of an intersection or union of the input structures.
270+
Since \textbf{grb} primitives are unvaware of conventional multiplication and addition operations, they are provedied
271+
as correspond monoids from the semiring argument, the multiplicative and additive monoids.
268272
\begin{itemize}
269273
\item \textbf{intersection (eWiseMul):} The primitive will compute only an element-wise multiplication for those positions where \emph{both} input containers have entries. This is since any missing entries are assumed to have value zero, and are therefore ignored under multiplication.
270274
\item \textbf{union (eWiseAdd):} The primitive will compute element-wise addition for those positions where \emph{any} of the input containers have entries. This is again because a missing entry in one of the containers is assumed to have a zero value, meaning the result of the addition simply equals the value of the entry present in the other container.
@@ -289,7 +293,7 @@ \subsection{Numerical Linear Algebra}
289293
\item use \texttt{grb::set} and \texttt{grb::setElement} to assign values (see below for a suggestion);
290294
\item perform a matrix--vector multiplication $y = Ax$ (using \texttt{grb::mxv} with the plus-times semiring);
291295
\item compute a dot product $d = x^Tx$ (using \texttt{grb::dot} with the same semiring);
292-
\item perform an element-wise multiplication $z = x \odot x$ (using \texttt{grb::eWiseMul} with the same semiring); and
296+
\item perform an element-wise multiplication $z = x \odot y$ (using \texttt{grb::eWiseMul} with the same semiring); and
293297
\item print the results.
294298
\end{enumerate}
295299
One example $A, x$ could be:
@@ -307,11 +311,11 @@ \subsection{Numerical Linear Algebra}
307311
Step 1: Constructing a 3x3 sparse matrix A.
308312
Step 2: Creating vector x = [1, 2, 3]^T.
309313
Step 3: Computing y = A·x under plus‐times semiring.
310-
Step 4: Computing z = x ⊙ x (element‐wise multiply).
314+
Step 4: Computing z = x ⊙ y (element‐wise multiply).
311315
Step 5: Computing dot_val = xᵀ·x under plus‐times semiring.
312316
x = [ 1, 2, 3 ]
313-
y = A·x = [ 7, 18, 17 ]
314-
z = x ⊙ x = [ 1, 4, 9 ]
317+
y = A·x = [ 8, 18, 17 ]
318+
z = x ⊙ y = [ 8, 36, 51 ]
315319
dot(x,x) = 14
316320
\end{lstlisting}
317321

@@ -403,6 +407,22 @@ \section{Solution to Exercise 8}\label{sec:simple_example}
403407

404408
using namespace grb;
405409

410+
% A =
411+
% \begin{bmatrix}
412+
% 0 & 1 & 2 \\
413+
% 0 & 3 & 4 \\
414+
% 5 & 6 & 0
415+
% \end{bmatrix},\quad
416+
% Step 1: Constructing a 3x3 sparse matrix A.
417+
% Step 2: Creating vector x = [1, 2, 3]^T.
418+
% Step 3: Computing y = A·x under plus‐times semiring.
419+
% Step 4: Computing z = x ⊙ y (element‐wise multiply).
420+
% Step 5: Computing dot_val = xᵀ·x under plus‐times semiring.
421+
% x = [ 1, 2, 3 ]
422+
% y = A·x = [ 8, 18, 17 ]
423+
% z = x ⊙ y = [ 8, 36, 51 ]
424+
% dot(x,x) = 14
425+
406426
// Indices and values for our sparse 3x3 matrix A:
407427
//
408428
// A = [ 1 0 2 ]
@@ -472,16 +492,15 @@ \section{Solution to Exercise 8}\label{sec:simple_example}
472492
}
473493

474494
//------------------------------
475-
// 6) Compute z = x ⊙ x (element‐wise multiply) via eWiseApply with mul
495+
// 6) Compute z = x ⊙ y (element‐wise multiply) via eWiseMul with semiring
476496
//------------------------------
477-
std::printf("Step 4: Computing z = x ⊙ x (element‐wise multiply).\n");
497+
std::printf("Step 4: Computing z = x ⊙ y (element‐wise multiply).\n");
478498
{
479-
RC rc = eWiseApply<descriptors::no_operation>(
480-
z, x, x,
481-
grb::operators::mul<double>() // plain multiplication ⊙
499+
RC rc = eWiseMul<descriptors::no_operation>(
500+
z, x, y, plusTimes
482501
);
483502
if(rc != SUCCESS) {
484-
std::cerr << "Error: eWiseApply(z,x,x,mul) failed with code " << toString(rc) << "\n";
503+
std::cerr << "Error: eWiseMul(z,x,y,plusTimes) failed with code " << toString(rc) << "\n";
485504
return (int)rc;
486505
}
487506
}
@@ -523,48 +542,13 @@ \section{Solution to Exercise 8}\label{sec:simple_example}
523542
std::printf("\n-- Results --\n");
524543
printVector(x, "x");
525544
printVector(y, "y = A·x");
526-
printVector(z, "z = x ⊙ x");
545+
printVector(z, "z = x ⊙ y");
527546
std::printf("dot(x,x) = %g\n\n", dot_val);
528547

529548
return EXIT_SUCCESS;
530549
}
531550
\end{lstlisting}
532551

533-
In this program, we manually set up a $3\times3$ matrix $A$:
534-
\[
535-
A = \begin{pmatrix}
536-
1 & 0 & 2 \\
537-
0 & 3 & 4 \\
538-
5 & 6 & 0
539-
\end{pmatrix},
540-
\]
541-
542-
543-
and a vector $x = [1,2,3]^T$. The code multiplies $A$ by $x$, producing $y = A \times x$. Given the above $A$ and $x$, the result should be:
544-
545-
\[
546-
y = \begin{pmatrix}
547-
7 \\
548-
18 \\
549-
17
550-
\end{pmatrix},
551-
\]
552-
553-
because
554-
$y_0 = 1\cdot 1 + 0\cdot 2 + 2\cdot 3 = 7$,
555-
$y_1 = 0\cdot1 + 3\cdot2 + 4\cdot3 = 18$,
556-
$y_2 = 5\cdot1 + 6\cdot2 + 0\cdot3 = 17$.
557-
558-
We also compute the dot product $x \cdot x = 1^2 + 2^2 + 3^2 = 14$ and the element-wise product $z = x * x = [1,4,9]^T$. The program then extracts the results with \texttt{grb::extractElement} (to get values from the containers) and prints them. Running this program would produce output similar to:
559-
560-
\begin{verbatim}
561-
y = [7, 18, 17]
562-
x . x = 14
563-
z (element-wise product of x with x) = [1, 4, 9]
564-
\end{verbatim}
565-
566-
This confirms that our ALP operations worked as expected. The code demonstrates setting values, performing an \texttt{mxv} multiplication, an element-wise multiply, and a dot product, covering several fundamental ALP/GraphBLAS operations.
567-
568552

569553
\section{Makefile and CMake Instructions}\label{sec:build_instructions}
570554

0 commit comments

Comments
 (0)