93
edits
Changes
GPUSquad
,→Idea 1 - Jacobi Method for 2D Poisson Problem
The following code is based on the jacobi.cc and common.cc code at the bottom of the source PDF, but there are some changes for these tests. The files use .cpp extensions instead of .cc, the code in common.cc was added to jacobi.cpp so it's a single file, and code for carrying out the Jacobi iterations in the main were placed into a seperate function called dojacobi() for gprof profiling.
The std::cout line inside the output_and_error function was commented out to avoid excessive printing to the terminal. The error_and_output function was rewritten as outputImage to get rid of terminal and file I/O during calculations, and only output the final image at the end of the calculations (this would be necessary for the parallel versions, so the changes were applied to the serial code as well to make things fair).
<source>
#include <fstream>
#include <cmath>
#include <chrono>
using namespace std;
// Set grid size and number of iterations
const int save_iters=20;const int total_iters=5000;const int error_every=2;const int m=33032,n=3301024;const double float xmin=-1,xmax=1;const double float ymin=-1,ymax=1;
// Compute useful constants
const double float pi=3.1415926535897932384626433832795;const double float omega=2/(1+sin(2*pi/n));const double float dx=(xmax-xmin)/(m-1);const double float dy=(ymax-ymin)/(n-1);const double float dxxinv=1/(dx*dx);const double float dyyinv=1/(dy*dy);const double float dcent=1/(2*(dxxinv+dyyinv));
// Input function
inline double float f(int i,int j) { double float x=xmin+i*dx,y=ymin+j*dy; return abs(x)>0.5||abs(y)>0.5?0:1;
}
// Common output and error routine
void output_and_error(char* filename,double float *a,const int sn) { // Computes the error if sn%error every==0 if(sn%error_every==0) { double float z,error=0;int ij; for(int j=1;j<n-1;j++) { for(int i=1;i<m-1;i++) { ij=i+m*j; z=f(i,j)-a[ij]*(2*dxxinv+2*dyyinv) +dxxinv*(a[ij-1]+a[ij+1]) +dyyinv*(a[ij-m]+a[ij+m]); error+=z*z; } } //cout << sn << " " << error*dx*dy << endl; }
}
void dojacobioutputImage(int i, int j, int ij, int k, double error, double u[]char* filename, double v[], double z, doublefloat * a, double* b) { // Computes the error if sn%error every==0
}
int main() {
}
</source>