\documentclass[11pt]{article}

\usepackage[utf8]{inputenc}

\usepackage{geometry}
\geometry{a4paper}

\usepackage{verbatim}
\usepackage{hyperref}
\usepackage{amsmath}
\usepackage{mathpartir}
\usepackage{semantic}

\title{HW3 -- Language Memory Models}
\author{CS 554, Fall 2025\\ Due October 27}
\date{}

\begin{document}
\maketitle
\section{Rules}

\subsection{Sequentially Consistent Atomics and Non-Atomics (SC+NA)}
\begin{enumerate}
\item There is a total order $\mathsf{to}$ on all \emph{atomic} operations, consistent with $\mathsf{po}$.
\item There is a partial reads-from order $\mathsf{rf}$ that orders each write $W\, \ell\ v$ before any reads that read from it. The synchronizes-with order $\mathsf{sw}$ is exactly the $\mathsf{rf}$ order on atomic operations.
\item The happens-before order $\mathsf{hb} := \mathsf{po} \cup \mathsf{sw}$ is acyclic.
\item Each atomic read $R_{\mathrm{at}}\ \ell\ v$ reads from the immediately preceding write to $\ell$ in $\mathsf{to}$.
\item ``No intervening writes'': if $w_1 \xrightarrow{\mathsf{rf}} r$ and there is another write $w_2$ to the same location such that $w_1 <_\mathsf{hb} w_2$, then it is not the case that $w_2 <_\mathsf{hb} r$. Equivalently, if $w_1 \xrightarrow{\mathsf{rf}} r$ and there is another write $w_2$ to the same location $\ell$ such that $w_1 <_\mathsf{hb} w_2$, we can draw a from-read edge $r \xrightarrow{\mathsf{rf}_\ell} w_2$, and $\mathsf{hb} \cup \mathsf{fr}_\ell$ is acyclic. (Note that for atomic locations, the ``no intervening writes'' condition follows from the previous rule.)
\item If $a$ and $b$ are non-atomic operations on the same location, at least one of them is a write, and neither $a <_\mathsf{hb} b$ nor $b <_\mathsf{hb} a$, then $a$ and $b$ are a data race.
\end{enumerate}

\subsection{Release-Acquire Atomics and Non-Atomics (RA+NA)}
\begin{enumerate}
\item There is a partial reads-from order $\mathsf{rf}$ that orders each write $W\, \ell\ v$ before any reads that read from it. The synchronizes-with order $\mathsf{sw}$ is exactly the $\mathsf{rf}$ order on atomic operations.
\item The happens-before order $\mathsf{hb} := \mathsf{po} \cup \mathsf{sw}$ is acyclic.
\item ``No intervening writes'': for each location $\ell$, there is a total order $\mathsf{mo}_\ell$ on all writes to $\ell$ that is consistent with $\mathsf{po}$. If $w_1 \xrightarrow{\mathsf{rf}} r$ and there is another write $w_2$ such that $w_1 <_{\mathsf{mo}_\ell} w_2$, then it is not the case that $w_2 <_\mathsf{hb}' r$, where $\mathsf{hb}'_\ell := \mathsf{hb} \cup \mathsf{mo}_\ell$. Equivalently, if $w_1 \xrightarrow{\mathsf{rf}} r$ and there is another write $w_2$ to the same location $\ell$ such that $w_1 <_{\mathsf{mo}_\ell} w_2$, we can draw a from-read edge $r \xrightarrow{\mathsf{rf}_\ell} w_2$, and $\mathsf{hb} \cup \mathsf{mo}_\ell \cup \mathsf{fr}_\ell$ is acyclic.
\item If $a$ and $b$ are non-atomic operations on the same location, at least one of them is a write, and neither $a <_\mathsf{hb} b$ nor $b <_\mathsf{hb} a$, then $a$ and $b$ are a data race.
\end{enumerate}

\section{Problems}
This is a written homework with 3 problems, each of which has multiple parts.

\begin{enumerate}
\item
\begin{enumerate}
\item Is SC+NA a stronger model or a weaker one than RA+NA?

\vspace{12em}

\item Write a program that has an allowed behavior under one of the two models that is disallowed under the other model. Draw the execution graph for the behavior under each model, and show why the behavior is disallowed in the stronger model. (Hint: The differences will be clearest in a program where all memory operations are atomic.)

\vspace{12em}

\end{enumerate}

\newpage
\item Consider the following C program:

\begin{tabular}{l || l}
\texttt{*y = 1;} & \ \texttt{while(atomic\_load(x) != 1);} \\
\texttt{atomic\_store(x, 1);} & \  \texttt{b = *y;} \\
\texttt{*y = 2;} &
\end{tabular}

\begin{enumerate}
\item Draw an execution of this program under RA+NA that has a data race. Be sure to indicate which two operations race with each other. You may check your answer using CppMem.

\vspace{16em}

\item How could we change the program to remove this data race?

\end{enumerate}

\newpage
\item Suppose we were trying to write a highly optimizing compiler that performed optimizations on atomic operations, and allowed it to reorder atomic writes past non-atomic writes to other locations.
\begin{enumerate}
\item Intuitively, why would this mess up the behavior of programs?

\vspace{12em}

\item Give an example of a program whose allowed behaviors would be changed by this optimization.

\vspace{14em}

\item Is there any safe optimization a compiler could perform involving atomic operations? Why or why not? (This will be very generously graded---just write out your thoughts.)
\end{enumerate}

\end{enumerate}

\end{document}
