Commit 0bcbafdf authored by Guilherme Tegoni Goedert's avatar Guilherme Tegoni Goedert
Browse files

Adicionando exemplos

parent 636c1616
function fft_ex1_CPU()
tic;
% generate signal
sampleFreq = 1000;
sampleTime = 1/sampleFreq;
numSamples = 2^23;
timeVec = (0:numSamples-1) * sampleTime ;
freq1 = 2 * pi * 50;
freq2 = 2 * pi * 120;
signal = sin( freq1 .* timeVec ) + sin( freq2 .* timeVec );
% add noise
signal = signal + 2 * randn( size( timeVec ));
plot( signal(1:100) );
title( 'Noisy time-domain signal' );
xlabel('t')
% perform spectral analysis (in GPU, since signal is a gpuArray!)
transformedSignal = fft( signal );
%Compute the Power Spectral Density
powerSpectrum = transformedSignal .* conj(transformedSignal) ./ numSamples;
% Display the power spectral density
frequencyVector = sampleFreq/2 * linspace( 0, 1, numSamples/2 + 1 );
figure
plot( frequencyVector, real(powerSpectrum(1:numSamples/2+1)) );
title( 'Power spectral density' );
xlabel( 'Frequency (Hz)' );
GPUtime = toc;
fprintf('Total time taken with GPU %2.3fs \n',GPUtime)
\ No newline at end of file
function fft_ex1_CPUtoGPU()
tic;
% generate signal
sampleFreq = 1000;
sampleTime = 1/sampleFreq;
numSamples = 2^23;
timeVec = (0:numSamples-1) * sampleTime ;
freq1 = 2 * pi * 50;
freq2 = 2 * pi * 120;
signalcpu = sin( freq1 .* timeVec ) + sin( freq2 .* timeVec );
% add noise
signalcpu = signalcpu + 2 * randn( size( timeVec ));
signal = gpuArray(signalcpu);
plot( signalcpu(1:100) );
title( 'Noisy time-domain signal' );
xlabel('t')
% perform spectral analysis (in GPU, since signal is a gpuArray!)
transformedSignal = fft( signal );
%Compute the Power Spectral Density
powerSpectrum = transformedSignal .* conj(transformedSignal) ./ numSamples;
% Display the power spectral density
frequencyVector = sampleFreq/2 * linspace( 0, 1, numSamples/2 + 1 );
figure
plot( frequencyVector, real(powerSpectrum(1:numSamples/2+1)) );
title( 'Power spectral density' );
xlabel( 'Frequency (Hz)' );
GPUtime = toc;
fprintf('Total time taken with GPU when copying from CPU %2.3fs \n',GPUtime)
\ No newline at end of file
function fft_ex1()
tic;
% generate signal
sampleFreq = 1000;
sampleTime = 1/sampleFreq;
numSamples = 2^23;
timeVec = gpuArray( (0:numSamples-1) * sampleTime );
freq1 = 2 * pi * 50;
freq2 = 2 * pi * 120;
signal = sin( freq1 .* timeVec ) + sin( freq2 .* timeVec );
% add noise
signal = signal + 2 * randn( size( timeVec ), 'gpuArray' );
plot( signal(1:100) );
title( 'Noisy time-domain signal' );
xlabel('t')
% perform spectral analysis (in GPU, since signal is a gpuArray!)
transformedSignal = fft( signal );
%Compute the Power Spectral Density
powerSpectrum = transformedSignal .* conj(transformedSignal) ./ numSamples;
% Display the power spectral density
frequencyVector = sampleFreq/2 * linspace( 0, 1, numSamples/2 + 1 );
figure
plot( frequencyVector, real(powerSpectrum(1:numSamples/2+1)) );
title( 'Power spectral density' );
xlabel( 'Frequency (Hz)' );
GPUtime = toc;
fprintf('Total time taken with GPU %2.3fs \n',GPUtime)
\ No newline at end of file
function Double_Slit_CPU()
tic;
N2 = 1024;
[gx, gy] = meshgrid( colon( -1, 1/N2, (N2-1)/N2 ) );
aperture = ( abs(gx) < 4/N2 ) .* ( abs(gy) < 2/N2 );
lightsource = double( aperture );
farfieldsignal = fft2( lightsource );
farfieldintensity = real( farfieldsignal .* conj( farfieldsignal ) );
imagesc( fftshift( farfieldintensity ) );
axis( 'equal' ); axis( 'off' );
title( 'Rectangular aperture far-field diffraction pattern' );
slits = (abs( gx ) <= 10/N2) .* (abs( gx ) >= 8/N2);
aperture = slits .* (abs(gy) < 20/N2);
lightsource = double( aperture );
farfieldsignal = fft2( lightsource );
farfieldintensity = real( farfieldsignal .* conj( farfieldsignal ) );
imagesc( fftshift( farfieldintensity ) );
axis( 'equal' ); axis( 'off' );
title( 'Double slit far-field diffraction pattern' );
TimeCPU = toc;
fprintf('Time taken with CPU computation %fs \n',TimeCPU)
\ No newline at end of file
function Double_Slit_GPU()
tic;
N2 = 1024;
[gx, gy] = meshgrid( gpuArray.colon( -1, 1/N2, (N2-1)/N2 ) );
aperture = ( abs(gx) < 4/N2 ) .* ( abs(gy) < 2/N2 );
lightsource = double( aperture );
farfieldsignal = fft2( lightsource );
farfieldintensity = real( farfieldsignal .* conj( farfieldsignal ) );
imagesc( fftshift( farfieldintensity ) );
axis( 'equal' ); axis( 'off' );
title( 'Rectangular aperture far-field diffraction pattern' );
slits = (abs( gx ) <= 10/N2) .* (abs( gx ) >= 8/N2);
aperture = slits .* (abs(gy) < 20/N2);
lightsource = double( aperture );
farfieldsignal = fft2( lightsource );
farfieldintensity = real( farfieldsignal .* conj( farfieldsignal ) );
imagesc( fftshift( farfieldintensity ) );
axis( 'equal' ); axis( 'off' );
title( 'Double slit far-field diffraction pattern' );
TimeGPU = toc;
fprintf('Time taken with CPU computation %fs \n',TimeGPU)
\ No newline at end of file
% GAME OF LIFE in CPU
% Generate a Grid; in every consecutive generation, look at the number N of "live" neighboors of each cell
% if N = 3, cell turns alive in next step
% if N = 2, cell stays alive in next step
% else, cell dies
function GameOfLife_CPU()
% Measure avarage time?
measure = 0;
% Initialize Grid
gridSize = 500;
numGenerations = 100;
initialGrid = (rand(gridSize,gridSize) > .75);
% Draw the initial grid
hold off
imagesc(initialGrid);
colormap([1 1 1;0 0.5 0]);
title('Initial Grid');
grid = initialGrid;
if measure == 1
timer = tic();
for generation = 1:numGenerations
grid = updateGrid(grid, gridSize);
end
cpuTime = toc(timer);
imagesc(grid);
title(num2str(generation));
drawnow;
fprintf('Average time on the CPU: %2.3fms per generation.\n', ...
1000*cpuTime/numGenerations);
elseif measure == 0
% Loop through each generation updating the grid and displaying it
for generation = 1:numGenerations
grid = updateGrid(grid, gridSize);
imagesc(grid);
title(num2str(generation));
drawnow;
end
end
%%%%%%>>>>> Evolve grid acording to Game of Life
function X = updateGrid(X, N)
p = [1 1:N-1];
q = [2:N N];
% Count how many of the eight neighbors are alive.
neighbors = X(:,p) + X(:,q) + X(p,:) + X(q,:) + ...
X(p,p) + X(q,q) + X(p,q) + X(q,p);
% A live cell with two live neighbors, or any cell with
% three live neighbors, is alive at the next step.
X = (X & (neighbors == 2)) | (neighbors == 3);
end
end
\ No newline at end of file
% GAME OF LIFE in GPU
% Generate a Grid; in every consecutive generation, look at the number N of "live" neighboors of each cell
% if N = 3, cell turns alive in next step
% if N = 2, cell stays alive in next step
% else, cell dies
function GameOfLife_GPU()
gridSize = 500;
numGenerations = 100;
initialGrid = (rand(gridSize,gridSize) > .75);
gpu = gpuDevice();
% Draw the initial grid
hold off
imagesc(initialGrid);
colormap([1 1 1;0 0.5 0]);
title('Initial Grid');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%>>>>>>>>>>>>>>>>>> CPU <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
grid = initialGrid;
timer = tic();
for generation = 1:numGenerations
grid = updateGrid(grid, gridSize);
end
cpuTime = toc(timer);
%fprintf('Average time on the CPU: %2.3fms per generation.\n', ...
% 1000*cpuTime/numGenerations);
expectedResult = grid;
function X = updateGrid(X, N)
p = [1 1:N-1];
q = [2:N N];
% Count how many of the eight neighbors are alive.
neighbors = X(:,p) + X(:,q) + X(p,:) + X(q,:) + ...
X(p,p) + X(q,q) + X(p,q) + X(q,p);
% A live cell with two live neighbors, or any cell with
% three live neighbors, is alive at the next step.
X = (X & (neighbors == 2)) | (neighbors == 3);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%>>>>>>>>>>>>>>>>>> GPU v1 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
grid = gpuArray(initialGrid);
timer = tic();
for generation = 1:numGenerations
grid = updateGrid(grid, gridSize);
end
wait(gpu); % Only needed to ensure accurate timing
gpuSimpleTime = toc(timer);
% Print out the average computation time and check the result is unchanged.
%fprintf(['Average time on the GPU: %2.3fms per generation ', ...
% '(%1.1fx faster).\n'], ...
% 1000*gpuSimpleTime/numGenerations, cpuTime/gpuSimpleTime);
assert(isequal(grid, expectedResult));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%>>>>>>>>>>>>>>>>>> GPU v2 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
grid = gpuArray(initialGrid);
function X = updateParentGrid(row, col, N)
% Take account of boundary effects
rowU = max(1,row-1); rowD = min(N,row+1);
colL = max(1,col-1); colR = min(N,col+1);
% Count neighbors
neighbors ...
= grid(rowU,colL) + grid(row,colL) + grid(rowD,colL) ...
+ grid(rowU,col) + grid(rowD,col) ...
+ grid(rowU,colR) + grid(row,colR) + grid(rowD,colR);
% A live cell with two live neighbors, or any cell with
% three live neighbors, is alive at the next step.
X = (grid(row,col) & (neighbors == 2)) | (neighbors == 3);
end
timer2 = tic();
rows = gpuArray.colon(1, gridSize)';
cols = gpuArray.colon(1, gridSize);
for generation = 1:numGenerations
grid = arrayfun(@updateParentGrid, rows, cols, gridSize);
end
wait(gpu); % Only needed to ensure accurate timing
gpuArrayfunTime = toc(timer2);
% Print out the average computation time and check the result is unchanged.
%fprintf(['Average time using GPU arrayfun: %2.3fms per generation ', ...
% '(%1.1fx faster).\n'], ...
% 1000*gpuArrayfunTime/numGenerations, cpuTime/gpuArrayfunTime);
assert(isequal(grid, expectedResult));
%> Conclusion
fprintf('CPU: %2.3fms per generation.\n', ...
1000*cpuTime/numGenerations);
fprintf('Simple GPU: %2.3fms per generation (%1.1fx faster).\n', ...
1000*gpuSimpleTime/numGenerations, cpuTime/gpuSimpleTime);
fprintf('Arrayfun GPU: %2.3fms per generation (%1.1fx faster).\n', ...
1000*gpuArrayfunTime/numGenerations, cpuTime/gpuArrayfunTime);
end
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment