Difference between revisions of "Ghost Cells"
(→Profiles) |
(→Assignment 3) |
||
Line 1,190: | Line 1,190: | ||
=== Assignment 3 === | === Assignment 3 === | ||
+ | {| class="wikitable mw-collapsible mw-collapsed" | ||
+ | ! PCIe Optimization | ||
+ | |- | ||
+ | | | ||
+ | <source> | ||
+ | /* | ||
+ | * Poisson Method using two arrays. | ||
+ | * Non-Ghost Cells Method | ||
+ | * Multiple PCIe Calls made, once per iteration | ||
+ | * by Tony Sim | ||
+ | */ | ||
+ | #include <cstring> | ||
+ | #include <cstdlib> | ||
+ | #include <iomanip> | ||
+ | #include <iostream> | ||
+ | #include <string> | ||
+ | #include <cuda_runtime.h> | ||
+ | #include "poisson.cuh" | ||
+ | |||
+ | namespace DPS{ | ||
+ | |||
+ | Poisson::Poisson(std::ifstream& ifs) { | ||
+ | std::string line; | ||
+ | nColumns = 0; | ||
+ | bufferSide = 0; | ||
+ | nRowsTotal = 0; | ||
+ | /* find number of columns */ | ||
+ | std::getline(ifs,line); | ||
+ | for (size_t i = 0 ; i < line.size() ; i++){ | ||
+ | if(line[i]==' ') nColumns++; | ||
+ | } | ||
+ | nColumns++; | ||
+ | |||
+ | /* find number of rows */ | ||
+ | nRowsTotal++; /* already fetched one */ | ||
+ | while(std::getline(ifs,line)) | ||
+ | nRowsTotal++; | ||
+ | ifs.clear(); | ||
+ | |||
+ | try{ | ||
+ | for (size_t i = 0 ; i < 2 ; i++) | ||
+ | h_data[i] = new float[ (nColumns+2) * (nRowsTotal+2)]; /* add edge buffers */ | ||
+ | } | ||
+ | catch (...){ | ||
+ | throw std::runtime_error("Failed to Allocate Memory"); | ||
+ | } | ||
+ | |||
+ | /* readin data */ | ||
+ | std::cout <<"Reading in data"<<std::endl; | ||
+ | ifs.seekg(0,ifs.beg); | ||
+ | /* allocate memory to all but the edge buffer, index 0 and max for each row and column */ | ||
+ | for (size_t i = 0 ; i < nRowsTotal+2 ; i++){ | ||
+ | for (size_t j = 0 ; j < nColumns+2 ; j++){ | ||
+ | float val = 0; | ||
+ | if(!(i == 0 || i == nRowsTotal + 1 || j == 0 || j == nColumns + 1)) | ||
+ | ifs >> val; | ||
+ | h_data[0][i*(nColumns+2)+j] = val; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | std::cout <<"Setting buffer"<<std::endl; | ||
+ | std::memset(h_data[1],0,(nRowsTotal+2)*(nColumns+2)*sizeof(float)); | ||
+ | bool state = devMemSet(); | ||
+ | /* DEBUG */ std::cout << state << std::endl; | ||
+ | |||
+ | } | ||
+ | |||
+ | Poisson::Poisson(const size_t r, const size_t c, float* d) { | ||
+ | bufferSide = 0; | ||
+ | nRowsTotal = r; | ||
+ | nColumns = c; | ||
+ | try{ | ||
+ | h_data[0] = new float[(r+2)*(c+2)]; | ||
+ | h_data[1] = new float[(r+2)*(c+2)]; | ||
+ | } | ||
+ | catch (...){ | ||
+ | throw std::runtime_error("Failed to Allocate Memory"); | ||
+ | } | ||
+ | std::memcpy(h_data[0],d,(r+2)*(c+2)*sizeof(float)); | ||
+ | std::memset(h_data[1],0,(r+2)*(c+2)*sizeof(float)); | ||
+ | devMemSet(); | ||
+ | } | ||
+ | |||
+ | Poisson::~Poisson(){ | ||
+ | for( size_t i = 0 ; i < 2 ; i++){ | ||
+ | delete [] h_data[i]; | ||
+ | cudaFree(d_data[i]); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | bool Poisson::devMemSet(){ | ||
+ | for(size_t i = 0 ; i < 2 ; i++){ | ||
+ | cudaMalloc(&d_data[i],(nColumns+2)*(nRowsTotal+2)*sizeof(float)); | ||
+ | if(d_data[i] != nullptr){ | ||
+ | cudaError_t state = cudaMemcpy((void*)d_data[i],(const void*)h_data[i],(nColumns+2)*(nRowsTotal+2)*sizeof(float),cudaMemcpyHostToDevice); | ||
+ | if(state != cudaSuccess) | ||
+ | std::cerr << "ERROR on devMemSet for : " << i <<" with : " << cudaGetErrorString(state)<< std::endl; | ||
+ | } | ||
+ | } | ||
+ | return d_data[0]&&d_data[1]; | ||
+ | } | ||
+ | |||
+ | |||
+ | float* Poisson::operator()(const size_t nIterations, const float wx, const float wy){ | ||
+ | |||
+ | /* calculate the grid, block, where block has 1024 threads total */ | ||
+ | unsigned int blockx = 32; | ||
+ | unsigned int blocky = 32; | ||
+ | unsigned int gridx = ((nRowsTotal+2)+blockx-1)/blockx; | ||
+ | unsigned int gridy = ((nRowsTotal+2)+blocky-1)/blocky; | ||
+ | |||
+ | /* create dim3 */ | ||
+ | dim3 dBlock= {blockx,blocky}; | ||
+ | dim3 dGrid = {gridx,gridy}; | ||
+ | |||
+ | /* run iterations */ | ||
+ | for (size_t i = 0; i < nIterations; i++) { | ||
+ | update<<<dGrid,dBlock>>>(d_data[1-bufferSide],d_data[bufferSide],nColumns, nRowsTotal, wx, wy); | ||
+ | bufferSwitch(); | ||
+ | } | ||
+ | |||
+ | /* DEBUG */ h_data[bufferSide][1*(nColumns+2) + 1] = 100.0f; | ||
+ | /* output results from device to host */ | ||
+ | cudaError_t state = cudaMemcpy(h_data[bufferSide],d_data[bufferSide],(nColumns+2)*(nRowsTotal+2)*sizeof(float),cudaMemcpyDeviceToHost); | ||
+ | if(state != cudaSuccess) | ||
+ | std::cout << "ERROR on () when copying data back to host" <<" with : " << cudaGetErrorString(state)<< std::endl; | ||
+ | |||
+ | return h_data[bufferSide]; | ||
+ | } | ||
+ | |||
+ | void Poisson::show(std::ostream& ofs) const{ | ||
+ | ofs << std::fixed << std::setprecision(1); | ||
+ | for (size_t j = 1; j <= nColumns ; j++) { | ||
+ | for (size_t i = 1 ; i <= nRowsTotal ; i++) | ||
+ | ofs << std::setw(8) << h_data[bufferSide][i * (nColumns+2) + j]<<","; | ||
+ | ofs << std::endl; | ||
+ | } | ||
+ | } | ||
+ | __global__ void update (float* newD, const float* currD, int nCol, int nRow, const float wx, const float wy){ | ||
+ | size_t j = blockDim.x * blockIdx.x + threadIdx.x + 1; /* for x axis */ | ||
+ | size_t i = blockDim.y * blockIdx.y + threadIdx.y + 1; /* for y axis */ | ||
+ | float curr = currD[i * (nCol+2)+ j]; | ||
+ | float dir1 = currD[(i+1) * (nCol+2) +j]; | ||
+ | float dir2 = currD[(i-1) * (nCol+2) +j]; | ||
+ | float dir3 = currD[i * (nCol+2) +j+1]; | ||
+ | float dir4 = currD[i * (nCol+2) +j-1]; | ||
+ | newD[i*(nCol+2)+j] = curr + wx * (dir1+dir2-2.0f*curr) + wy * (dir3+dir4-2.0f*curr); | ||
+ | __syncthreads(); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | </source> | ||
+ | |} |
Revision as of 01:37, 7 April 2019
GPU610/DPS915 | Student List | Group and Project Index | Student Resources | Glossary
Contents
[hide]Ghost Cells
Team Members
- Tony Sim, Issue Dumper
- Robert Dittrich, Issue Collector
- Inna Zhogova, Issue Resolver
Progress
Assignment 1
Tony
Subject: Jacobi's method for Poisson's equation
Source Code
[Expand] poissan.h |
---|
[Expand] poissan.cpp |
---|
[Expand] main.cpp |
---|
Introduction
The presented code simulates heat map using Jacobi's method for Poisson's equation. It is represented in a 2D array, and each element updates its value based on the adjacent elements at a given moment. Each iteration represent one instance in time. By repeating the calculation over the entire array through multiple iterations, we can estimate the state of the heat transfer after a given time interval.
Profiling
The profiling was conducted using a data set of 79 rows and 205 columns over 150000 iterations.
[Expand] Flat profile |
---|
[Expand] Call graph |
---|
Analysis
given 98.57 percent of time is spent on the update() function, it is considered the hotspot. Total time taken was 2.75.
If we consider a GPU environment with 1000 cores, we can estimate the following speedup: S1000 = 1/(1-.9857 + .9857/1000) = 65.00 In fact, the speed will decrease from 2.75 seconds to 0.0450 seconds.
As each iteration depends on the product of the previous iteration, there is a dependency resolution that might hamper the parallel process. Consideration may also be extended to resolving ghost cells across different SMX while using the device global memory as the transfer pipeline.
Robert
Multi Sampling Anti Aliasing
Source Files
[Expand] main.cpp |
---|
[Expand] vec3.h |
---|
Introduction
For my selection I chose to do Anti Aliasing since I see it a lot in video games but I never really knew how it worked. There are other anti aliasing methods like FXAA which is fast approximate anti aliasing but it seemed a lot more complicated than MSAA. The way I approached this problem is by getting the color of the pixels around a pixel. In you can specify the distance it will search in the application flags. In my implementation you specify an input file, output file, the radius of pixels to sample and how many passes to take on the image. In my tests the command line options I used was an image I made in paint with 4 sample size and 4 passes.
[Expand] Before |
---|
[Expand] After |
---|
Profiling
[Expand] Profiling |
---|
Conclusion
Since the msaa
function I wrote is a hotspot of the program I would suggest offloading part of it to a GPU, more specifically the part that finds the average of colors of the nearby pixels. That part also does not depend on previous iterations to finish so it is a prime candidate for parallelization.
Inna
Subject: Data compression - LWZ algorithm.
Source: http://www.cplusplus.com/articles/iL18T05o/#Version1
I tested the following source code for a compression and decompression of .txt files and a gif.
[Expand] lwz.cpp( ... ) |
---|
Tested data
1. book.txt - a 343 kilobyte text file.
2. words.txt - a 4.7 megabyte text file.
3. fire.gif - a 309 kilobyte graphical image.
Flat Profiles
Book
Flat profile for compression:
Flat profile for decompression:
Text
Flat profile for compression:
Flat profile for decompression:
GIF
Flat profile for compression:
Flat profile for decompression:
Assignment 2
Source Files
[Expand] poisson-pcie.cu |
---|
[Expand] poisson-alt.cu |
---|
Profiles
[Expand] Poisson PCIe Profile |
---|
[Expand] Poisson AltProfile |
---|
GPU Offload Vs CPU
Assignment 3
[Expand] PCIe Optimization |
---|