sure, sure. here goes:
cyst.h:
/* Copyright (c) 2009 Kevin Daley The following code is part of cYst. cYst is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. cYst is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with cYst. If not, see <http://www.gnu.org/licenses/>. */ #include <cuda_gl_interop.h> #include <cstdio> #include <cstdlib> #include <cmath> __global__ void initialize(float*** hd, unsigned char* file, int i, int j, int k); extern int resx, resy; namespace cyst{ struct simParams{ float dT; float n; float4 dx; dim3 grid; dim3 block; float max; unsigned int width, height, depth; int nstreams; }; class cystGPUMsim{ protected: cudaPitchedPtr *dp; float dT; float4 dx; float n; dim3 grid; dim3 block; cudaStream_t *stream; int nstreams; float max; unsigned int width, height, depth; cudaExtent ca_extent; public: __host__ explicit cystGPUMsim(struct simParams *params, const char* buff); __host__ virtual ~cystGPUMsim(); __host__ void callGPUsim(int frames); __host__ float* retrieveGPUsim(); }; }
cyst.cu
/* Copyright (c) 2009 Kevin Daley The following code is part of cYst. cYst is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. cYst is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with cYst. If not, see <http://www.gnu.org/licenses/>. */ #include "cyst.cu" __global__ void initialize(float*** hd, unsigned char* file, int i, int j, int k){ uint3 pg=make_uint3(threadIdx.x+blockIdx.x*blockDim.x+i,threadIdx.x+blockIdx .y*blockDim.y+j,threadIdx.z+blockIdx.z*blockDim.z+k); hd[pg.x][pg.y][pg.z]=3; } void demo(){ cudaSetDevice(0); struct simParams *parms= new simParams; parms->dT=10; parms->n=.5; parms->dx=make_float4(0.1,0.1,0.1,0.1); dim3 grid (2,1); dim3 block (2,2,2); parms->width=128; parms->height=128; parms->depth=64; parms->max=50.0f; parms->nstreams=2; cudaSetDevice(0); parms->grid=grid; parms->block=block; cyst::cystGPUMsim* sim=new cyst::cystGPUMsim(parms, "k"); sim->callGPUsim(1); float* H=sim->retrieveGPUsim(); delete sim; FILE* f=fopen("out.dat","rw+"); for(int i=0; i<256; i++){ for(int j=0; j<256; j++){ for(int k=0; k<256; k++){ fprintf(f, "%f %f %f %f %f %f %f %f\n", i*parms->dx.x, j*parms->dx.y,k*parms->dx.z, k*parms->dx.w, H[(i+j*256+k*256*1024)+3], H[(i+j*256+k*256*1024)], H[(i+j*256+k*256*1024)+1], H[(i+j*256+k*256*1024)+2]); }}} fclose(f); exit(0); }
and demo.cu
/* Copyright (c) 2009 Kevin Daley The following code is part of cYst. cYst is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. cYst is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with cYst. If not, see <http://www.gnu.org/licenses/>. */ #include "cyst.cu" __global__ void initialize(float*** hd, unsigned char* file, int i, int j, int k){ uint3 pg=make_uint3(threadIdx.x+blockIdx.x*blockDim.x+i,threadIdx.x+blockIdx .y*blockDim.y+j,threadIdx.z+blockIdx.z*blockDim.z+k); hd[pg.x][pg.y][pg.z]=3; } void demo(){ cudaSetDevice(0); struct simParams *parms= new simParams; parms->dT=10; parms->n=.5; parms->dx=make_float4(0.1,0.1,0.1,0.1); dim3 grid (2,1); dim3 block (2,2,2); parms->width=128; parms->height=128; parms->depth=64; parms->max=50.0f; parms->nstreams=2; cudaSetDevice(0); parms->grid=grid; parms->block=block; cyst::cystGPUMsim* sim=new cyst::cystGPUMsim(parms, "k"); sim->callGPUsim(1); float* H=sim->retrieveGPUsim(); delete sim; FILE* f=fopen("out.dat","rw+"); for(int i=0; i<256; i++){ for(int j=0; j<256; j++){ for(int k=0; k<256; k++){ fprintf(f, "%f %f %f %f %f %f %f %f\n", i*parms->dx.x, j*parms->dx.y,k*parms->dx.z, k*parms->dx.w, H[(i+j*256+k*256*1024)+3], H[(i+j*256+k*256*1024)], H[(i+j*256+k*256*1024)+1], H[(i+j*256+k*256*1024)+2]); }}} fclose(f); exit(0); }
and kernel.cu:
/* Copyright (c) 2009 Kevin Daley The following code is part of cYst. cYst is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. cYst is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with cYst. If not, see <http://www.gnu.org/licenses/>. */ //planck units #define const_pi 3.1415927f #define kC 1.0f #define kB 1.0f #define lightspeed 1.0f #define finestructure 1.0f #include <sm_11_atomic_functions.h> __global__ void kernelGPU(float ***H, float dT, float4 dx, float n, float max, int i, int j, int k){ float mg=0; uint3 pl=make_uint3(threadIdx.x, threadIdx.y, threadIdx.z); uint3 pg=make_uint3(threadIdx.x+blockIdx.x*blockDim.x+i,threadIdx.x+blockIdx .y*blockDim.y+j,threadIdx.z+blockIdx.z*blockDim.z+k); float x=0.0f, y=0.0f, z=0.0f; __shared__ float shared[2][2][2], E[2][2][2], E2[2][2][2]; E[pl.x][pl.y][pl.z]=H[pg.x][pg.y][pg.z]; E2[pl.x][pl.y][pl.z]=0.0f; for(float i=0; i<max && i<E[pl.x][pl.y][pl.z]; i+=n){ if((threadIdx.z+1)%4==0){ shared[pl.x][pl.y][pl.z]=i; mg=sqrtf(pow(shared[pl.x][pl.y][pl.z-3],2.0f)+pow(shared[pl.x][pl.y][pl.z-2],2.0f)+pow(shared[pl.x][pl.y][pl.z-1],2.0f)); x+=shared[pl.x][pl.y][pl.z]*(cosf(mg)-1/mg*shared[pl.x][pl.y][pl.z]*sinf(mg)); } else if((threadIdx.z+1)%3==0){ shared[pl.x][pl.y][pl.z]=i; mg=sqrtf(pow(shared[pl.x][pl.y][pl.z+1],2.0f)+pow(shared[pl.x][pl.y][pl.z-2],2.0f)+pow(shared[pl.x][pl.y][pl.z-1],2.0f)); x+=shared[pl.x][pl.y][pl.z]*(cosf(mg)-1/mg*shared[pl.x][pl.y][pl.z]*sinf(mg)); } else if((threadIdx.z+1)%2==0){ shared[pl.x][pl.y][pl.z]=i; mg=sqrtf(pow(shared[pl.x][pl.y][pl.z+1],2.0f)+pow(shared[pl.x][pl.y][pl.z+2],2.0f)+pow(shared[pl.x][pl.y][pl.z-1],2.0f)); x+=shared[pl.x][pl.y][pl.z]*(cosf(mg)-1/mg*shared[pl.x][pl.y][pl.z]*sinf(mg)); } else{ shared[pl.x][pl.y][pl.z]=i; mg=sqrtf(pow(shared[pl.x][pl.y][pl.z+1],2.0f)+pow(shared[pl.x][pl.y][pl.z+2],2.0f)+pow(shared[pl.x][pl.y][pl.z+3],2.0f)); x+=shared[pl.x][pl.y][pl.z]*(cosf(mg)-1/mg*shared[pl.x][pl.y][pl.z]*sinf(mg)); } shared[pl.x][pl.y][pl.z]=x; __syncthreads(); __syncthreads(); if((threadIdx.z+1)%4==0){ z=shared[pl.x][pl.y][pl.z-1]; y=shared[pl.x][pl.y][pl.z-2]; x=shared[pl.x][pl.y][pl.z-3]; } __syncthreads(); if((threadIdx.z+1)%4==0 && int(fdividef(x,dx.x))>=0 && int(fdividef(x,dx.x))){ E2[pl.x+int(fdividef(x,dx.x))][pl.y+int(fdividef(y,dx.y))][pl.z+4*int(fdividef(z,dx.z))]+=shared[pl.x][pl.y][pl.z]; } else if((threadIdx.z+1)%4==0){ H[int(fdividef(x,dx.x))+pg.x][int(fdividef(y,dx.y))+pg.y][(int(fdividef(z,dx.z))*4+pg.z)]+=i; } E2[pl.x][pl.y][pl.z]-=i; __syncthreads(); } H[pg.x][pg.y][pg.z]=E[pl.x][pl.y][pl.z]+E2[pl.x][pl.y][pl.z]; }
Finally, main.cpp:
/* Copyright (c) 2009 Kevin Daley The following code is part of cYst. cYst is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. cYst is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with cYst. If not, see <http://www.gnu.org/licenses/>. */ extern void demo(); int main(int argc, char** argv){ demo(); }
There you go.