Eu gostaria de cercar algum código-fonte na minha apresentação do beamer com uma caixa levemente sombreada.
Mais especificamente no código-fonte C++ fornecido abaixo, gostaria de sombrear o bloco de código da vecAddKernel
função com uma caixa cinza claro. Como eu faço isso.
Aqui está o meu código para o meu quadro de projetor
\begin{frame}[fragile]{Simple CUDA Code: A Vector Sum}
\lstset{ language=C++,
basicstyle=\ttfamily\scriptsize,
keywordstyle=\color{blue}\ttfamily,
stringstyle=\color{red}\ttfamily,
commentstyle=\color{green}\ttfamily
}
\begin{lstlisting}
// Compute vector sum C = A+B
// Each thread performs one pair-wise addition
__global__
void vecAddKernel(float* A_d, float* B_d, float* C_d, int n)
{
int i = threadIdx.x + blockDim.x * blockIdx.x;
if(i<n) C_d[i] = A_d[i] + B_d[i];
}
int vecAdd(float* A, float* B, float* C, int n)
{
// A_d, B_d, C_d allocations and copies omitted
// Run ceil(n/256) blocks of 256 threads each
dim3 DimGrid(ceil(n/256.0), 1, 1);
dim3 DimBlock(256, 1, 1);
vecAddKernnel<<<DimGrid,DimBlock>>>(A_d, B_d, C_d, n);
}
\end{lstlisting}
\textbf{Note: } Calls to kernels are \textbf{\color{orange}asynchronous}.
\end{frame}
Aqui está o resultado da compilação.
Responder1
Aqui está uma opção usando o lstlinebgrd
pacote:
\documentclass{beamer}
\usepackage{listings}
\usepackage{lstlinebgrd}
\lstset{ language=C++,
basicstyle=\ttfamily\scriptsize,
keywordstyle=\color{blue}\ttfamily,
stringstyle=\color{red}\ttfamily,
commentstyle=\color{green!60!black}\ttfamily,
escapeinside=||,
linebackgroundcolor={\ifnum\value{lstnumber}>3 \ifnum\value{lstnumber}<9\color{gray!30}\fi\fi}
}
\begin{document}
\begin{frame}[fragile]{Simple CUDA Code: A Vector Sum}
\begin{lstlisting}
// Compute vector sum C = A+B
// Each thread performs one pair-wise addition
__global__
void vecAddKernel(float* A_d, float* B_d, float* C_d, int n)
{
int i = threadIdx.x + blockDim.x * blockIdx.x;
if(i<n) C_d[i] = A_d[i] + B_d[i];
}
int vecAdd(float* A, float* B, float* C, int n)
{
// A_d, B_d, C_d allocations and copies omitted
// Run ceil(n/256) blocks of 256 threads each
dim3 DimGrid(ceil(n/256.0), 1, 1);
dim3 DimBlock(256, 1, 1);
vecAddKernnel<<<DimGrid,DimBlock>>>(A_d, B_d, C_d, n);
}
\end{lstlisting}
\textbf{Note: } Calls to kernels are \textbf{\color{orange}asynchronous}.
\end{frame}
\end{document}
Para molduras mais sofisticadas, há outra opção usando a versão melhorada \tizmark
(graças a Andrew Stacey); a ideia é colocar algumas marcas e depois usar as marcas para desenhar o fundo colorido:
\documentclass{beamer}
\usepackage{listings}
\usepackage{tikz}
\usetikzlibrary{calc}
\makeatletter
\tikzset{%
remember picture with id/.style={%
remember picture,
overlay,
save picture id=#1,
},
save picture id/.code={%
\edef\pgf@temp{#1}%
\immediate\write\pgfutil@auxout{%
\noexpand\savepointas{\pgf@temp}{\pgfpictureid}}%
},
if picture id/.code args={#1#2#3}{%
\@ifundefined{save@pt@#1}{%
\pgfkeysalso{#3}%
}{
\pgfkeysalso{#2}%
}
}
}
\def\savepointas#1#2{%
\expandafter\gdef\csname save@pt@#1\endcsname{#2}%
}
\def\tmk@labeldef#1,#2\@nil{%
\def\tmk@label{#1}%
\def\tmk@def{#2}%
}
\tikzdeclarecoordinatesystem{pic}{%
\pgfutil@in@,{#1}%
\ifpgfutil@in@%
\tmk@labeldef#1\@nil
\else
\tmk@labeldef#1,(0pt,0pt)\@nil
\fi
\@ifundefined{save@pt@\tmk@label}{%
\tikz@scan@one@point\pgfutil@firstofone\tmk@def
}{%
\pgfsys@getposition{\csname save@pt@\tmk@label\endcsname}\save@orig@pic%
\pgfsys@getposition{\pgfpictureid}\save@this@pic%
\pgf@process{\pgfpointorigin\save@this@pic}%
\pgf@xa=\pgf@x
\pgf@ya=\pgf@y
\pgf@process{\pgfpointorigin\save@orig@pic}%
\advance\pgf@x by -\pgf@xa
\advance\pgf@y by -\pgf@ya
}%
}
\newcommand\tikzmark[2][]{%
\tikz[remember picture with id=#2] #1;}
\makeatother
\newcommand<>\MyBox[2]{%
\tikz[remember picture,overlay]
\filldraw[draw=gray,fill=gray!40,line width=1pt,rectangle,rounded corners]
( $ (pic cs:#1) + (-2pt,1.3ex) $ ) rectangle ( $ (pic cs:#2) +
(\textwidth,-0.5ex) $ )
;}%
\lstset{ language=C++,
basicstyle=\ttfamily\scriptsize,
keywordstyle=\color{blue}\ttfamily,
stringstyle=\color{red}\ttfamily,
commentstyle=\color{green!60!black}\ttfamily,
escapeinside=||,
}
\begin{document}
\begin{frame}[fragile]{Simple CUDA Code: A Vector Sum}
\MyBox{start}{end}
\begin{lstlisting}
// Compute vector sum C = A+B
// Each thread performs one pair-wise addition
__global__
|\tikzmark{start}|void vecAddKernel(float* A_d, float* B_d, float* C_d, int n)
{
int i = threadIdx.x + blockDim.x * blockIdx.x;
if(i<n) C_d[i] = A_d[i] + B_d[i];
}|\tikzmark{end}|
int vecAdd(float* A, float* B, float* C, int n)
{
// A_d, B_d, C_d allocations and copies omitted
// Run ceil(n/256) blocks of 256 threads each
dim3 DimGrid(ceil(n/256.0), 1, 1);
dim3 DimBlock(256, 1, 1);
vecAddKernnel<<<DimGrid,DimBlock>>>(A_d, B_d, C_d, n);
}
\end{lstlisting}
\textbf{Note: } Calls to kernels are \textbf{\color{orange}asynchronous}.
\end{frame}
\end{document}