Skip to contentSkip to conversation

count: Use new scheme for updated 2 snippets

PATCH In review Akira Yokosawa 2 participants 7 messages Sep 15, 2018 – Sep 17, 2018

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 count: Use new scheme for updated 2 snippets

From e8d3761680aa3d5a978ddccf2b9c00b5920cd39e Mon Sep 17 00:00:00 2001
From: Akira Yokosawa <[email protected]>
Date: Fri, 14 Sep 2018 22:11:18 +0900
Subject: [PATCH 1/2] count: Use new scheme for updated 2 snippets

Also use "static __inline__" for inlined function definitions.

Commit b1bab635598b ("Making the counter implementations safer")
introduced the __inline__ keyword. Inline functions defined in CodeSamples/api_pthreads/api-gcc.h have "static __inline__" keywords. Such functions can not be used in functions which have
only __inline__. The commit avoided the mismatch by not adding
__inline__ to inc_count() in count_atomic.c.

Consistently using "static __inline__" should be a better approach in this case.

Side effect: This change slightly increases height of code snippets, and the "[bp]" placement specifier to count_nonatomic.c originally appeared in commit 216744f9918e ("Add non-optimized counter material to the count chapter.") back in 2009, which repeatedly swung among "[hp]", [tp], and "[bp]" during 2016 and 2017, has finally become moot. We can settle with the default "[tbp]".

---
Imre,

Changes in count_atomic.c and count_nonatomic.c should not affect their performance so much. So all we need for now is the performance data of count_nonatomic after applying the previous patch (now commit c1e1273b3e17 in the repository.

Paul,

My plan is to take care of the other code under CodeSamples/count/ and update code snippets in Chapter 5 after the Figure 5.1 fix.

Thanks, Akira
--

 CodeSamples/count/count_atomic.c    | 16 +++++++-----
 CodeSamples/count/count_nonatomic.c | 16 +++++++-----
 count/count.tex                     | 51 +++++++++----------------------------
 3 files changed, 30 insertions(+), 53 deletions(-)
CodeSamples/count/count_atomic.c +9 -7
index fc73717..298f104 100644
@@ -20,23 +20,25 @@
 
 #include "../api.h"
 
-atomic_t counter = ATOMIC_INIT(0);
+//\begin{snippet}[labelbase=ln:count:count_atomic:inc-read,commandchars=\\\[\]]
+atomic_t counter = ATOMIC_INIT(0);		//\lnlbl{counter}
 
-void inc_count(void)
+static __inline__ void inc_count(void)
 {
-	atomic_inc(&counter);
+	atomic_inc(&counter);			//\lnlbl{inc}
 }
 
-__inline__ long read_count(void)
+static __inline__ long read_count(void)
 {
-	return atomic_read(&counter);
+	return atomic_read(&counter);		//\lnlbl{read}
 }
+//\end{snippet}
 
-__inline__ void count_init(void)
+static __inline__ void count_init(void)
 {
 }
 
-__inline__ void count_cleanup(void)
+static __inline__ void count_cleanup(void)
 {
 }
 
CodeSamples/count/count_nonatomic.c +9 -7
index 50df475..d5582b1 100644
@@ -21,23 +21,25 @@
 
 #include "../api.h"
 
-unsigned long counter = 0;
+//\begin{snippet}[labelbase=ln:count:count_nonatomic:inc-read,commandchars=\\\[\]]
+unsigned long counter = 0;				//\lnlbl{counter}
 
-__inline__ void inc_count(void)
+static __inline__ void inc_count(void)
 {
-	WRITE_ONCE(counter, READ_ONCE(counter) + 1);
+	WRITE_ONCE(counter, READ_ONCE(counter) + 1);	//\lnlbl{inc}
 }
 
-__inline__ unsigned long read_count(void)
+static __inline__ unsigned long read_count(void)
 {
-	return READ_ONCE(counter);
+	return READ_ONCE(counter);			//\lnlbl{read}
 }
+//\end{snippet}
 
-__inline__ void count_init(void)
+static __inline__ void count_init(void)
 {
 }
 
-__inline__ void count_cleanup(void)
+static __inline__ void count_cleanup(void)
 {
 }
 
count/count.tex +12 -39

76 lines not on this page · raw message

Signature · 1 line
2.7.4
#1 lore raw

PATCH 2/2 count: Reword Quick Quiz 5.6 to adjust context

From 7bf9ee029829a53009e29ea23b287bb874bd8ee4 Mon Sep 17 00:00:00 2001
From: Akira Yokosawa <[email protected]>
Date: Fri, 14 Sep 2018 22:50:04 +0900
Subject: [PATCH 2/2] count: Reword Quick Quiz 5.6 to adjust context

count_nonatomic.c has lost the explicit "++" operator, so this is my humble attempt to update the question.

---
Paul,

Does this change look reasonable to you?
I'm quite sure you will come up with something better. ;-)

Thanks, Akira
--

 count/count.tex | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
count/count.tex +3 -1
index cfdacb5..6a48b99 100644
@@ -193,7 +193,9 @@ Although approximate values do have their place in computing,
 accuracies far greater than 50\,\% are almost always necessary.
 
 \QuickQuiz{}
-	But doesn't the \co{++} operator produce an x86 add-to-memory
+	But doesn't a smart compiler prove
+        line~\ref{ln:count:count_nonatomic:inc-read:inc} is equivalent
+        to the \co{++} operator and produce an x86 add-to-memory
 	instruction?
 	And won't the CPU cache cause this to be atomic?
 \QuickQuizAnswer{
Signature · 1 line
2.7.4
#2 lore raw

Re: [PATCH 2/2] count: Reword Quick Quiz 5.6 to adjust context

Quoted 9 lines · Akira Yokosawa
Quoted 1 line

From 7bf9ee029829a53009e29ea23b287bb874bd8ee4 Mon Sep 17 00:00:00 2001

From: Akira Yokosawa <[email protected]>
Date: Fri, 14 Sep 2018 22:50:04 +0900
Subject: [PATCH 2/2] count: Reword Quick Quiz 5.6 to adjust context

count_nonatomic.c has lost the explicit "++" operator, so this is my humble attempt to update the question.

I adjusted it slightly, then applied and pushed it, thank you!

Could you please double check that I didn't mess something up?

Thanx, Paul

Quoted 30 lines

---
Paul,

Does this change look reasonable to you?
I'm quite sure you will come up with something better. ;-)

Thanks, Akira
--

 count/count.tex | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
count/count.tex +3 -1
index cfdacb5..6a48b99 100644
@@ -193,7 +193,9 @@ Although approximate values do have their place in computing,
 accuracies far greater than 50\,\% are almost always necessary.
 
 \QuickQuiz{}
-	But doesn't the \co{++} operator produce an x86 add-to-memory
+	But doesn't a smart compiler prove
+        line~\ref{ln:count:count_nonatomic:inc-read:inc} is equivalent
+        to the \co{++} operator and produce an x86 add-to-memory
 	instruction?
 	And won't the CPU cache cause this to be atomic?
 \QuickQuizAnswer{
Signature · 1 line
2.7.4
#3 lore raw

Re: [PATCH 2/2] count: Reword Quick Quiz 5.6 to adjust context

Quoted 6 lines · Paul E. McKenney
Quoted 5 lines · Akira Yokosawa
Quoted 1 line

From 7bf9ee029829a53009e29ea23b287bb874bd8ee4 Mon Sep 17 00:00:00 2001

From: Akira Yokosawa <[email protected]>
Date: Fri, 14 Sep 2018 22:50:04 +0900
Subject: [PATCH 2/2] count: Reword Quick Quiz 5.6 to adjust context

Quoted 8 lines
Quoted 4 lines

count_nonatomic.c has lost the explicit "++" operator, so this is my humble attempt to update the question.

I adjusted it slightly, then applied and pushed it, thank you!

Could you please double check that I didn't mess something up?

Hyperlinks in PDF from \ref{} to line labels by their full name doesn't work at the moment. They jump to somewhere near the Listing.

Actually speaking, I've experimented accurate anchors to line labels. My plan was to submit the change in a pull request to cover litmus tests and other types of code samples in the new scheme. I'll submit a cherry-picked patch as a reply.

(I'll stop CCing Imre in the reply.)

Thanks, Akira

Quoted 34 lines

Thanx, Paul

Quoted 30 lines

---
Paul,

Does this change look reasonable to you?
I'm quite sure you will come up with something better. ;-)

Thanks, Akira
--

 count/count.tex | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
count/count.tex +3 -1
index cfdacb5..6a48b99 100644
@@ -193,7 +193,9 @@ Although approximate values do have their place in computing,
 accuracies far greater than 50\,\% are almost always necessary.
 
 \QuickQuiz{}
-	But doesn't the \co{++} operator produce an x86 add-to-memory
+	But doesn't a smart compiler prove
+        line~\ref{ln:count:count_nonatomic:inc-read:inc} is equivalent
+        to the \co{++} operator and produce an x86 add-to-memory
 	instruction?
 	And won't the CPU cache cause this to be atomic?
 \QuickQuizAnswer{
Signature · 1 line
2.7.4
#4 lore raw

PATCH 1/1 Enable hyperlink to line label in code snippet

From d189669d1f093262b522ab012e04a55da871421a Mon Sep 17 00:00:00 2001
From: Akira Yokosawa <[email protected]>
Date: Mon, 17 Sep 2018 07:57:40 +0900
Subject: [PATCH] Enable hyperlink to line label in code snippet

Now you can jump to the exact line by cliking a line number in the PDF generated by \lnref{} and \ref{} with full label name.

---
 perfbook.tex | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
perfbook.tex +2 -2
index fb35c5a..1dc72c2 100644
@@ -241,9 +241,9 @@
 \setcounter{secnumdepth}{3}
 %\fvset{fontsize=\scriptsize,numbers=left,numbersep=5pt,xleftmargin=9pt,obeytabs=true,tabsize=2}
 \newcommand{\lnlblbase}{}
-\newcommand{\lnlbl}[1]{\label{\lnlblbase:#1}}
+\newcommand{\lnlbl}[1]{\phantomsection\label{\lnlblbase:#1}}
 \newcommand{\lnrefbase}{}
-\newcommand{\lnref}[1]{\ref*{\lnrefbase:#1}}
+\newcommand{\lnref}[1]{\ref{\lnrefbase:#1}}
 
 \newenvironment{linelabel}[1][]{\renewcommand{\lnlblbase}{#1}%
 \ignorespaces}{\ignorespacesafterend}
Signature · 1 line
2.7.4
#5 lore raw

Re: [PATCH] Enable hyperlink to line label in code snippet

Quoted 9 lines · Akira Yokosawa
Quoted 1 line

From d189669d1f093262b522ab012e04a55da871421a Mon Sep 17 00:00:00 2001

From: Akira Yokosawa <[email protected]>
Date: Mon, 17 Sep 2018 07:57:40 +0900
Subject: [PATCH] Enable hyperlink to line label in code snippet

Now you can jump to the exact line by cliking a line number in the PDF generated by \lnref{} and \ref{} with full label name.

Queued and pushed, thank you very much!

Thanx, Paul

Quoted 24 lines
---
 perfbook.tex | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
perfbook.tex +2 -2
index fb35c5a..1dc72c2 100644
@@ -241,9 +241,9 @@
 \setcounter{secnumdepth}{3}
 %\fvset{fontsize=\scriptsize,numbers=left,numbersep=5pt,xleftmargin=9pt,obeytabs=true,tabsize=2}
 \newcommand{\lnlblbase}{}
-\newcommand{\lnlbl}[1]{\label{\lnlblbase:#1}}
+\newcommand{\lnlbl}[1]{\phantomsection\label{\lnlblbase:#1}}
 \newcommand{\lnrefbase}{}
-\newcommand{\lnref}[1]{\ref*{\lnrefbase:#1}}
+\newcommand{\lnref}[1]{\ref{\lnrefbase:#1}}
 
 \newenvironment{linelabel}[1][]{\renewcommand{\lnlblbase}{#1}%
 \ignorespaces}{\ignorespacesafterend}
Signature · 1 line
2.7.4
#6 lore raw
In reply to Akira Yokosawa
Quoted 24 lines · Akira Yokosawa
Quoted 1 line

From e8d3761680aa3d5a978ddccf2b9c00b5920cd39e Mon Sep 17 00:00:00 2001

From: Akira Yokosawa <[email protected]>
Date: Fri, 14 Sep 2018 22:11:18 +0900
Subject: [PATCH 1/2] count: Use new scheme for updated 2 snippets

Also use "static __inline__" for inlined function definitions.

Commit b1bab635598b ("Making the counter implementations safer")
introduced the __inline__ keyword. Inline functions defined in CodeSamples/api_pthreads/api-gcc.h have "static __inline__" keywords. Such functions can not be used in functions which have
only __inline__. The commit avoided the mismatch by not adding
__inline__ to inc_count() in count_atomic.c.

Consistently using "static __inline__" should be a better approach in this case.

Side effect: This change slightly increases height of code snippets, and the "[bp]" placement specifier to count_nonatomic.c originally appeared in commit 216744f9918e ("Add non-optimized counter material to the count chapter.") back in 2009, which repeatedly swung among "[hp]", [tp], and "[bp]" during 2016 and 2017, has finally become moot. We can settle with the default "[tbp]".

Quoted 2 lines

Applied and pushed, thank you!

Quoted 8 lines

---
Imre,

Changes in count_atomic.c and count_nonatomic.c should not affect their performance so much. So all we need for now is the performance data of count_nonatomic after applying the previous patch (now commit c1e1273b3e17 in the repository.

Quoted 4 lines

Paul,

My plan is to take care of the other code under CodeSamples/count/ and update code snippets in Chapter 5 after the Figure 5.1 fix.

That would be extremely helpful, thank you!

Thanx, Paul

Quoted 168 lines

Thanks, Akira
--

 CodeSamples/count/count_atomic.c    | 16 +++++++-----
 CodeSamples/count/count_nonatomic.c | 16 +++++++-----
 count/count.tex                     | 51 +++++++++----------------------------
 3 files changed, 30 insertions(+), 53 deletions(-)
CodeSamples/count/count_atomic.c +9 -7
index fc73717..298f104 100644
@@ -20,23 +20,25 @@
 
 #include "../api.h"
 
-atomic_t counter = ATOMIC_INIT(0);
+//\begin{snippet}[labelbase=ln:count:count_atomic:inc-read,commandchars=\\\[\]]
+atomic_t counter = ATOMIC_INIT(0);		//\lnlbl{counter}
 
-void inc_count(void)
+static __inline__ void inc_count(void)
 {
-	atomic_inc(&counter);
+	atomic_inc(&counter);			//\lnlbl{inc}
 }
 
-__inline__ long read_count(void)
+static __inline__ long read_count(void)
 {
-	return atomic_read(&counter);
+	return atomic_read(&counter);		//\lnlbl{read}
 }
+//\end{snippet}
 
-__inline__ void count_init(void)
+static __inline__ void count_init(void)
 {
 }
 
-__inline__ void count_cleanup(void)
+static __inline__ void count_cleanup(void)
 {
 }
 
CodeSamples/count/count_nonatomic.c +9 -7
index 50df475..d5582b1 100644
@@ -21,23 +21,25 @@
 
 #include "../api.h"
 
-unsigned long counter = 0;
+//\begin{snippet}[labelbase=ln:count:count_nonatomic:inc-read,commandchars=\\\[\]]
+unsigned long counter = 0;				//\lnlbl{counter}
 
-__inline__ void inc_count(void)
+static __inline__ void inc_count(void)
 {
-	WRITE_ONCE(counter, READ_ONCE(counter) + 1);
+	WRITE_ONCE(counter, READ_ONCE(counter) + 1);	//\lnlbl{inc}
 }
 
-__inline__ unsigned long read_count(void)
+static __inline__ unsigned long read_count(void)
 {
-	return READ_ONCE(counter);
+	return READ_ONCE(counter);			//\lnlbl{read}
 }
+//\end{snippet}
 
-__inline__ void count_init(void)
+static __inline__ void count_init(void)
 {
 }
 
-__inline__ void count_cleanup(void)
+static __inline__ void count_cleanup(void)
 {
 }
 
count/count.tex +12 -39
index b9cd888..cfdacb5 100644
@@ -165,24 +165,8 @@ are more appropriate for advanced students.
 \section{Why Isn't Concurrent Counting Trivial?}
 \label{sec:count:Why Isn't Concurrent Counting Trivial?}
 
-\begin{listing}[bp]
-{ \scriptsize
-\begin{verbbox}
-  1 long counter = 0;
-  2 
-  3 void inc_count(void)
-  4 {
-  5   counter++;
-  6 }
-  7 
-  8 long read_count(void)
-  9 {
- 10   return counter;
- 11 }
-\end{verbbox}
-}
-\centering
-\theverbbox
+\begin{listing}[tbp]
+\input{CodeSamples/count/[email protected]}
 \caption{Just Count!}
 \label{lst:count:Just Count!}
 \end{listing}
@@ -190,9 +174,11 @@ are more appropriate for advanced students.
 Let's start with something simple, for example, the straightforward
 use of arithmetic shown in
 Listing~\ref{lst:count:Just Count!} (\path{count_nonatomic.c}).
-Here, we have a counter on line~1, we increment it on line~5, and we
-read out its value on line~10.
+\begin{lineref}[ln:count:count_nonatomic:inc-read]
+Here, we have a counter on line~\lnref{counter}, we increment it on
+line~\lnref{inc}, and we read out its value on line~\lnref{read}.
 What could be simpler?
+\end{lineref}
 
 This approach has the additional advantage of being blazingly fast if
 you are doing lots of reading and almost no incrementing, and on small
@@ -243,23 +229,7 @@ accuracies far greater than 50\,\% are almost always necessary.
 } \QuickQuizEnd
 
 \begin{listing}[tbp]
-{ \scriptsize
-\begin{verbbox}
-  1 atomic_t counter = ATOMIC_INIT(0);
-  2 
-  3 void inc_count(void)
-  4 {
-  5   atomic_inc(&counter);
-  6 }
-  7 
-  8 long read_count(void)
-  9 {
- 10   return atomic_read(&counter);
- 11 }
-\end{verbbox}
-}
-\centering
-\theverbbox
+\input{CodeSamples/count/[email protected]}
 \caption{Just Count Atomically!}
 \label{lst:count:Just Count Atomically!}
 \end{listing}
@@ -274,8 +244,11 @@ accuracies far greater than 50\,\% are almost always necessary.
 The straightforward way to count accurately is to use atomic operations,
 as shown in
 Listing~\ref{lst:count:Just Count Atomically!} (\path{count_atomic.c}).
-Line~1 defines an atomic variable, line~5 atomically increments it, and
-line~10 reads it out.
+\begin{lineref}[ln:count:count_atomic:inc-read]
+Line~\lnref{counter} defines an atomic variable,
+line~\lnref{inc} atomically increments it, and
+line~\lnref{read} reads it out.
+\end{lineref}
 Because this is atomic, it keeps perfect count.
 However, it is slower: on a Intel Core Duo laptop, it is about
 six times slower than non-atomic increment
Signature · 1 line
2.7.4