Skip to content

Commit 37afc7a

Browse files
committed
stuff
1 parent a8bfee0 commit 37afc7a

File tree

4 files changed

+65
-16
lines changed

4 files changed

+65
-16
lines changed

bib.bib

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2506,3 +2506,22 @@ @BOOK{algdesign:parseq2016
25062506
MONTH={September},
25072507
PUBLISHER = {Carnegie Mellon University}
25082508
}
2509+
2510+
@article{Schwartz:1980:ULT:357114.357116,
2511+
author = {Schwartz, Jacob T.},
2512+
title = {Ultracomputers},
2513+
journal = {ACM Trans. Program. Lang. Syst.},
2514+
issue_date = {Oct. 1980},
2515+
volume = {2},
2516+
number = {4},
2517+
month = oct,
2518+
year = {1980},
2519+
issn = {0164-0925},
2520+
pages = {484--521},
2521+
numpages = {38},
2522+
url = {http://doi.acm.org/10.1145/357114.357116},
2523+
doi = {10.1145/357114.357116},
2524+
acmid = {357116},
2525+
publisher = {ACM},
2526+
address = {New York, NY, USA},
2527+
}

main.tex

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1881,6 +1881,35 @@ \subsection{Radix-Sort in Futhark}
18811881
that $S(\kw{rsort}~\kw{v}) = O(\log\,\kw{n})$, dominated by the
18821882
\fop{scan} SOAC calls in $\kw{rsort\_step}$.
18831883

1884+
\section{Counting Primes}
1885+
A variant of a contraction algorithm is an algorithm that first solves
1886+
a smaller problem, recursively, and then uses this result to provide a
1887+
solution to the larger problem. One such algorithm is a version of the
1888+
Sieve of Eratosthenes that, to find the primes smaller than some $n$,
1889+
first calculates the primes smaller than $\sqrt n$. It then uses this
1890+
intermediate result for sieving away the integers in the range $\sqrt
1891+
n$ up to $n$ that are multiples of the primes smaller than $\sqrt n$.
1892+
1893+
A Futhark program calculating the number of primes below some number
1894+
$n$, also denoted in the literature as the $\pi$ function, is shown in
1895+
Figure~\ref{fig:primes}.
1896+
1897+
\begin{figure}
1898+
\begin{wrap}
1899+
\lstinputlisting{src/primes.fut}
1900+
\end{wrap}
1901+
\caption{An Implementation of the Sieve of Eratosthenes.}
1902+
\label{fig:primes}
1903+
\end{figure}
1904+
1905+
Notice that the algorithm applies a parallel sieve for each step,
1906+
using a combination of maps and reductions. The work done by the
1907+
algorithm is $O(n\,\log\,\log\,n)$ and the span is
1908+
$O(\log\,\log\,n)$. The $\log\,\log\,n$ factor appears because the
1909+
size of the problem is reduced at each step by applying the square
1910+
root, which is identical to halving the exponent size at each
1911+
step (i.e., the sequence $n, \ldots, 2^{16}, 2^8, 2^4, 2^2$, where $n=2^{2^m}$, for some positive $m$, has $m = \log\,\log\,n$ elements.)
1912+
18841913
\chapter{Algebraic Properties of SOACs}
18851914
\label{chap:soac-algebra}
18861915

@@ -1917,17 +1946,16 @@ \section{Segmented Scan}
19171946
\label{sec:sgmscan}
19181947

19191948
The segmented scan operator is quite essential as we shall see
1920-
demonstrated in many of the algorithms explained later. The segmented scan
1921-
operator can be implemented with a simple scan using an associative
1922-
function that operates on pairs of values. Here is the definition of
1923-
the segmented scan operation:
1949+
demonstrated in many of the algorithms explained later. The segmented
1950+
scan operator can be implemented with a simple scan using an
1951+
associative function that operates on pairs of values
1952+
\cite{Schwartz:1980:ULT:357114.357116,blelloch1990vector}. Here is the
1953+
definition of the segmented scan operation:
19241954

19251955
\begin{wrap}
19261956
\lstinputlisting[firstline=7]{src/sgm_scan.fut}
19271957
\end{wrap}
19281958

1929-
1930-
19311959
\section{Parallel Utility Functions}
19321960
For use by other algorithms, a set of utility functions for
19331961
manipulating and managing arrays is an important part of the tool

src/maxidx.ok

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
1212i32
2+
17i32

src/primes.fut

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
2-
-- Finding the first n primes
1+
-- Find the first n primes
32
fun primes (n:i32) : []i32 =
4-
if n <= 3 then [2,3]
5-
else let sqrtn = i32(sqrt32(f32(n)))
6-
let first = primes (sqrtn+1)
3+
if n == 2 then empty(i32)
4+
else let sqrtn = i32(sqrt32(f32(n)))+1
5+
let first = primes sqrtn
76
let is = map (+sqrtn) (iota(n-sqrtn))
87
let fs = map (fn i =>
9-
let xs = map (fn p => if i%p == 0 then 1 else 0) first
10-
in reduce (+) 0 xs) is
8+
let xs = map (fn p => if i%p==0 then 1
9+
else 0) first
10+
in reduce (+) 0 xs) is
11+
-- apply the sieve
1112
let new = filter (fn i => 0 == unsafe fs[i-sqrtn]) is
1213
in concat first new
1314

14-
-- main(n) returns the number of primes less than n
15+
-- Return the number of primes less than n
1516
fun main (n:i32) : i32 =
16-
let ps = primes n
17-
in (shape ps)[0]
17+
let ps = primes n in (shape ps)[0]

0 commit comments

Comments
 (0)