mirror of
https://github.com/NVIDIA/cuda-samples.git
synced 2026-10-25 22:28:17 +08:00
Apply consistent code formatting across the repo. Add clang-format and pre-commit hooks.
This commit is contained in:
@@ -28,21 +28,20 @@
|
||||
#ifndef DEFINES_H
|
||||
#define DEFINES_H
|
||||
|
||||
#define DIM 512 // Square size of solver domain
|
||||
#define DS (DIM * DIM) // Total domain size
|
||||
#define CPADW (DIM / 2 + 1) // Padded width for real->complex in-place FFT
|
||||
#define RPADW \
|
||||
(2 * (DIM / 2 + 1)) // Padded width for real->complex in-place FFT
|
||||
#define PDS (DIM * CPADW) // Padded total domain size
|
||||
#define DIM 512 // Square size of solver domain
|
||||
#define DS (DIM * DIM) // Total domain size
|
||||
#define CPADW (DIM / 2 + 1) // Padded width for real->complex in-place FFT
|
||||
#define RPADW (2 * (DIM / 2 + 1)) // Padded width for real->complex in-place FFT
|
||||
#define PDS (DIM * CPADW) // Padded total domain size
|
||||
|
||||
#define DT 0.09f // Delta T for interative solver
|
||||
#define VIS 0.0025f // Viscosity constant
|
||||
#define FORCE (5.8f * DIM) // Force scale factor
|
||||
#define FR 4 // Force update radius
|
||||
#define DT 0.09f // Delta T for interative solver
|
||||
#define VIS 0.0025f // Viscosity constant
|
||||
#define FORCE (5.8f * DIM) // Force scale factor
|
||||
#define FR 4 // Force update radius
|
||||
|
||||
#define TILEX 64 // Tile width
|
||||
#define TILEY 64 // Tile height
|
||||
#define TIDSX 64 // Tids in X
|
||||
#define TIDSY 4 // Tids in Y
|
||||
#define TILEX 64 // Tile width
|
||||
#define TILEY 64 // Tile height
|
||||
#define TIDSX 64 // Tids in X
|
||||
#define TIDSY 4 // Tids in Y
|
||||
|
||||
#endif
|
||||
|
||||
@@ -39,21 +39,21 @@
|
||||
#endif
|
||||
|
||||
// Includes
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
// CUDA standard includes
|
||||
#include <cuda_runtime.h>
|
||||
#include <cuda_gl_interop.h>
|
||||
#include <cuda_runtime.h>
|
||||
|
||||
// CUDA FFT Libraries
|
||||
#include <cufft.h>
|
||||
|
||||
// CUDA helper functions
|
||||
#include <helper_cuda.h>
|
||||
#include <helper_functions.h>
|
||||
#include <rendercheck_gl.h>
|
||||
#include <helper_cuda.h>
|
||||
|
||||
#include "defines.h"
|
||||
#include "fluidsGL_kernels.h"
|
||||
@@ -73,34 +73,34 @@ void cleanup(void);
|
||||
void reshape(int x, int y);
|
||||
|
||||
// CUFFT plan handle
|
||||
cufftHandle planr2c;
|
||||
cufftHandle planc2r;
|
||||
cufftHandle planr2c;
|
||||
cufftHandle planc2r;
|
||||
static cData *vxfield = NULL;
|
||||
static cData *vyfield = NULL;
|
||||
|
||||
cData *hvfield = NULL;
|
||||
cData *dvfield = NULL;
|
||||
static int wWidth = MAX(512, DIM);
|
||||
cData *hvfield = NULL;
|
||||
cData *dvfield = NULL;
|
||||
static int wWidth = MAX(512, DIM);
|
||||
static int wHeight = MAX(512, DIM);
|
||||
|
||||
static int clicked = 0;
|
||||
static int fpsCount = 0;
|
||||
static int fpsLimit = 1;
|
||||
StopWatchInterface *timer = NULL;
|
||||
static int clicked = 0;
|
||||
static int fpsCount = 0;
|
||||
static int fpsLimit = 1;
|
||||
StopWatchInterface *timer = NULL;
|
||||
|
||||
// Particle data
|
||||
GLuint vbo = 0; // OpenGL vertex buffer object
|
||||
struct cudaGraphicsResource *cuda_vbo_resource; // handles OpenGL-CUDA exchange
|
||||
static cData *particles = NULL; // particle positions in host memory
|
||||
static int lastx = 0, lasty = 0;
|
||||
GLuint vbo = 0; // OpenGL vertex buffer object
|
||||
struct cudaGraphicsResource *cuda_vbo_resource; // handles OpenGL-CUDA exchange
|
||||
static cData *particles = NULL; // particle positions in host memory
|
||||
static int lastx = 0, lasty = 0;
|
||||
|
||||
// Texture pitch
|
||||
size_t tPitch = 0; // Now this is compatible with gcc in 64-bit
|
||||
size_t tPitch = 0; // Now this is compatible with gcc in 64-bit
|
||||
|
||||
char *ref_file = NULL;
|
||||
bool g_bQAAddTestForce = true;
|
||||
int g_iFrameToCompare = 100;
|
||||
int g_TotalErrors = 0;
|
||||
char *ref_file = NULL;
|
||||
bool g_bQAAddTestForce = true;
|
||||
int g_iFrameToCompare = 100;
|
||||
int g_TotalErrors = 0;
|
||||
|
||||
bool g_bExitESC = false;
|
||||
|
||||
@@ -109,389 +109,388 @@ CheckRender *g_CheckRender = NULL;
|
||||
|
||||
void autoTest(char **);
|
||||
|
||||
extern "C" void addForces(cData *v, int dx, int dy, int spx, int spy, float fx,
|
||||
float fy, int r);
|
||||
extern "C" void advectVelocity(cData *v, float *vx, float *vy, int dx, int pdx,
|
||||
int dy, float dt);
|
||||
extern "C" void diffuseProject(cData *vx, cData *vy, int dx, int dy, float dt,
|
||||
float visc);
|
||||
extern "C" void updateVelocity(cData *v, float *vx, float *vy, int dx, int pdx,
|
||||
int dy);
|
||||
extern "C" void addForces(cData *v, int dx, int dy, int spx, int spy, float fx, float fy, int r);
|
||||
extern "C" void advectVelocity(cData *v, float *vx, float *vy, int dx, int pdx, int dy, float dt);
|
||||
extern "C" void diffuseProject(cData *vx, cData *vy, int dx, int dy, float dt, float visc);
|
||||
extern "C" void updateVelocity(cData *v, float *vx, float *vy, int dx, int pdx, int dy);
|
||||
extern "C" void advectParticles(GLuint vbo, cData *v, int dx, int dy, float dt);
|
||||
|
||||
void simulateFluids(void) {
|
||||
// simulate fluid
|
||||
advectVelocity(dvfield, (float *)vxfield, (float *)vyfield, DIM, RPADW, DIM,
|
||||
DT);
|
||||
diffuseProject(vxfield, vyfield, CPADW, DIM, DT, VIS);
|
||||
updateVelocity(dvfield, (float *)vxfield, (float *)vyfield, DIM, RPADW, DIM);
|
||||
advectParticles(vbo, dvfield, DIM, DIM, DT);
|
||||
void simulateFluids(void)
|
||||
{
|
||||
// simulate fluid
|
||||
advectVelocity(dvfield, (float *)vxfield, (float *)vyfield, DIM, RPADW, DIM, DT);
|
||||
diffuseProject(vxfield, vyfield, CPADW, DIM, DT, VIS);
|
||||
updateVelocity(dvfield, (float *)vxfield, (float *)vyfield, DIM, RPADW, DIM);
|
||||
advectParticles(vbo, dvfield, DIM, DIM, DT);
|
||||
}
|
||||
|
||||
void display(void) {
|
||||
if (!ref_file) {
|
||||
sdkStartTimer(&timer);
|
||||
simulateFluids();
|
||||
}
|
||||
|
||||
// render points from vertex buffer
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
glColor4f(0, 1, 0, 0.5f);
|
||||
glPointSize(1);
|
||||
glEnable(GL_POINT_SMOOTH);
|
||||
glEnable(GL_BLEND);
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
glEnableClientState(GL_VERTEX_ARRAY);
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
glDisable(GL_CULL_FACE);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
||||
glVertexPointer(2, GL_FLOAT, 0, NULL);
|
||||
glDrawArrays(GL_POINTS, 0, DS);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glDisableClientState(GL_VERTEX_ARRAY);
|
||||
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
||||
glDisable(GL_TEXTURE_2D);
|
||||
|
||||
if (ref_file) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Finish timing before swap buffers to avoid refresh sync
|
||||
sdkStopTimer(&timer);
|
||||
glutSwapBuffers();
|
||||
|
||||
fpsCount++;
|
||||
|
||||
if (fpsCount == fpsLimit) {
|
||||
char fps[256];
|
||||
float ifps = 1.f / (sdkGetAverageTimerValue(&timer) / 1000.f);
|
||||
sprintf(fps, "Cuda/GL Stable Fluids (%d x %d): %3.1f fps", DIM, DIM, ifps);
|
||||
glutSetWindowTitle(fps);
|
||||
fpsCount = 0;
|
||||
fpsLimit = (int)MAX(ifps, 1.f);
|
||||
sdkResetTimer(&timer);
|
||||
}
|
||||
|
||||
glutPostRedisplay();
|
||||
}
|
||||
|
||||
void autoTest(char **argv) {
|
||||
CFrameBufferObject *fbo =
|
||||
new CFrameBufferObject(wWidth, wHeight, 4, false, GL_TEXTURE_2D);
|
||||
g_CheckRender = new CheckFBO(wWidth, wHeight, 4, fbo);
|
||||
g_CheckRender->setPixelFormat(GL_RGBA);
|
||||
g_CheckRender->setExecPath(argv[0]);
|
||||
g_CheckRender->EnableQAReadback(true);
|
||||
|
||||
fbo->bindRenderPath();
|
||||
|
||||
reshape(wWidth, wHeight);
|
||||
|
||||
for (int count = 0; count < g_iFrameToCompare; count++) {
|
||||
simulateFluids();
|
||||
|
||||
// add in a little force so the automated testing is interesting.
|
||||
if (ref_file) {
|
||||
int x = wWidth / (count + 1);
|
||||
int y = wHeight / (count + 1);
|
||||
float fx = (x / (float)wWidth);
|
||||
float fy = (y / (float)wHeight);
|
||||
int nx = (int)(fx * DIM);
|
||||
int ny = (int)(fy * DIM);
|
||||
|
||||
int ddx = 35;
|
||||
int ddy = 35;
|
||||
fx = ddx / (float)wWidth;
|
||||
fy = ddy / (float)wHeight;
|
||||
int spy = ny - FR;
|
||||
int spx = nx - FR;
|
||||
|
||||
addForces(dvfield, DIM, DIM, spx, spy, FORCE * DT * fx, FORCE * DT * fy,
|
||||
FR);
|
||||
lastx = x;
|
||||
lasty = y;
|
||||
void display(void)
|
||||
{
|
||||
if (!ref_file) {
|
||||
sdkStartTimer(&timer);
|
||||
simulateFluids();
|
||||
}
|
||||
}
|
||||
|
||||
display();
|
||||
// render points from vertex buffer
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
glColor4f(0, 1, 0, 0.5f);
|
||||
glPointSize(1);
|
||||
glEnable(GL_POINT_SMOOTH);
|
||||
glEnable(GL_BLEND);
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
glEnableClientState(GL_VERTEX_ARRAY);
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
glDisable(GL_CULL_FACE);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
||||
glVertexPointer(2, GL_FLOAT, 0, NULL);
|
||||
glDrawArrays(GL_POINTS, 0, DS);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glDisableClientState(GL_VERTEX_ARRAY);
|
||||
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
||||
glDisable(GL_TEXTURE_2D);
|
||||
|
||||
fbo->unbindRenderPath();
|
||||
if (ref_file) {
|
||||
return;
|
||||
}
|
||||
|
||||
// compare to official reference image, printing PASS or FAIL.
|
||||
printf("> (Frame %d) Readback BackBuffer\n", 100);
|
||||
g_CheckRender->readback(wWidth, wHeight);
|
||||
g_CheckRender->savePPM("fluidsGL.ppm", true, NULL);
|
||||
// Finish timing before swap buffers to avoid refresh sync
|
||||
sdkStopTimer(&timer);
|
||||
glutSwapBuffers();
|
||||
|
||||
if (!g_CheckRender->PPMvsPPM("fluidsGL.ppm", ref_file, MAX_EPSILON_ERROR,
|
||||
0.25f)) {
|
||||
g_TotalErrors++;
|
||||
}
|
||||
fpsCount++;
|
||||
|
||||
if (fpsCount == fpsLimit) {
|
||||
char fps[256];
|
||||
float ifps = 1.f / (sdkGetAverageTimerValue(&timer) / 1000.f);
|
||||
sprintf(fps, "Cuda/GL Stable Fluids (%d x %d): %3.1f fps", DIM, DIM, ifps);
|
||||
glutSetWindowTitle(fps);
|
||||
fpsCount = 0;
|
||||
fpsLimit = (int)MAX(ifps, 1.f);
|
||||
sdkResetTimer(&timer);
|
||||
}
|
||||
|
||||
glutPostRedisplay();
|
||||
}
|
||||
|
||||
void autoTest(char **argv)
|
||||
{
|
||||
CFrameBufferObject *fbo = new CFrameBufferObject(wWidth, wHeight, 4, false, GL_TEXTURE_2D);
|
||||
g_CheckRender = new CheckFBO(wWidth, wHeight, 4, fbo);
|
||||
g_CheckRender->setPixelFormat(GL_RGBA);
|
||||
g_CheckRender->setExecPath(argv[0]);
|
||||
g_CheckRender->EnableQAReadback(true);
|
||||
|
||||
fbo->bindRenderPath();
|
||||
|
||||
reshape(wWidth, wHeight);
|
||||
|
||||
for (int count = 0; count < g_iFrameToCompare; count++) {
|
||||
simulateFluids();
|
||||
|
||||
// add in a little force so the automated testing is interesting.
|
||||
if (ref_file) {
|
||||
int x = wWidth / (count + 1);
|
||||
int y = wHeight / (count + 1);
|
||||
float fx = (x / (float)wWidth);
|
||||
float fy = (y / (float)wHeight);
|
||||
int nx = (int)(fx * DIM);
|
||||
int ny = (int)(fy * DIM);
|
||||
|
||||
int ddx = 35;
|
||||
int ddy = 35;
|
||||
fx = ddx / (float)wWidth;
|
||||
fy = ddy / (float)wHeight;
|
||||
int spy = ny - FR;
|
||||
int spx = nx - FR;
|
||||
|
||||
addForces(dvfield, DIM, DIM, spx, spy, FORCE * DT * fx, FORCE * DT * fy, FR);
|
||||
lastx = x;
|
||||
lasty = y;
|
||||
}
|
||||
}
|
||||
|
||||
display();
|
||||
|
||||
fbo->unbindRenderPath();
|
||||
|
||||
// compare to official reference image, printing PASS or FAIL.
|
||||
printf("> (Frame %d) Readback BackBuffer\n", 100);
|
||||
g_CheckRender->readback(wWidth, wHeight);
|
||||
g_CheckRender->savePPM("fluidsGL.ppm", true, NULL);
|
||||
|
||||
if (!g_CheckRender->PPMvsPPM("fluidsGL.ppm", ref_file, MAX_EPSILON_ERROR, 0.25f)) {
|
||||
g_TotalErrors++;
|
||||
}
|
||||
}
|
||||
|
||||
// very simple von neumann middle-square prng. can't use rand() in -qatest
|
||||
// mode because its implementation varies across platforms which makes testing
|
||||
// for consistency in the important parts of this program difficult.
|
||||
float myrand(void) {
|
||||
static int seed = 72191;
|
||||
char sq[22];
|
||||
float myrand(void)
|
||||
{
|
||||
static int seed = 72191;
|
||||
char sq[22];
|
||||
|
||||
if (ref_file) {
|
||||
seed *= seed;
|
||||
sprintf(sq, "%010d", seed);
|
||||
// pull the middle 5 digits out of sq
|
||||
sq[8] = 0;
|
||||
seed = atoi(&sq[3]);
|
||||
if (ref_file) {
|
||||
seed *= seed;
|
||||
sprintf(sq, "%010d", seed);
|
||||
// pull the middle 5 digits out of sq
|
||||
sq[8] = 0;
|
||||
seed = atoi(&sq[3]);
|
||||
|
||||
return seed / 99999.f;
|
||||
} else {
|
||||
return rand() / (float)RAND_MAX;
|
||||
}
|
||||
}
|
||||
|
||||
void initParticles(cData *p, int dx, int dy) {
|
||||
int i, j;
|
||||
|
||||
for (i = 0; i < dy; i++) {
|
||||
for (j = 0; j < dx; j++) {
|
||||
p[i * dx + j].x = (j + 0.5f + (myrand() - 0.5f)) / dx;
|
||||
p[i * dx + j].y = (i + 0.5f + (myrand() - 0.5f)) / dy;
|
||||
return seed / 99999.f;
|
||||
}
|
||||
else {
|
||||
return rand() / (float)RAND_MAX;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void keyboard(unsigned char key, int x, int y) {
|
||||
switch (key) {
|
||||
void initParticles(cData *p, int dx, int dy)
|
||||
{
|
||||
int i, j;
|
||||
|
||||
for (i = 0; i < dy; i++) {
|
||||
for (j = 0; j < dx; j++) {
|
||||
p[i * dx + j].x = (j + 0.5f + (myrand() - 0.5f)) / dx;
|
||||
p[i * dx + j].y = (i + 0.5f + (myrand() - 0.5f)) / dy;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void keyboard(unsigned char key, int x, int y)
|
||||
{
|
||||
switch (key) {
|
||||
case 27:
|
||||
g_bExitESC = true;
|
||||
g_bExitESC = true;
|
||||
#if defined(__APPLE__) || defined(MACOSX)
|
||||
exit(EXIT_SUCCESS);
|
||||
exit(EXIT_SUCCESS);
|
||||
#else
|
||||
glutDestroyWindow(glutGetWindow());
|
||||
return;
|
||||
glutDestroyWindow(glutGetWindow());
|
||||
return;
|
||||
#endif
|
||||
break;
|
||||
break;
|
||||
|
||||
case 'r':
|
||||
memset(hvfield, 0, sizeof(cData) * DS);
|
||||
cudaMemcpy(dvfield, hvfield, sizeof(cData) * DS, cudaMemcpyHostToDevice);
|
||||
memset(hvfield, 0, sizeof(cData) * DS);
|
||||
cudaMemcpy(dvfield, hvfield, sizeof(cData) * DS, cudaMemcpyHostToDevice);
|
||||
|
||||
initParticles(particles, DIM, DIM);
|
||||
initParticles(particles, DIM, DIM);
|
||||
|
||||
cudaGraphicsUnregisterResource(cuda_vbo_resource);
|
||||
cudaGraphicsUnregisterResource(cuda_vbo_resource);
|
||||
|
||||
getLastCudaError("cudaGraphicsUnregisterBuffer failed");
|
||||
getLastCudaError("cudaGraphicsUnregisterBuffer failed");
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(cData) * DS, particles,
|
||||
GL_DYNAMIC_DRAW_ARB);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(cData) * DS, particles, GL_DYNAMIC_DRAW_ARB);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
|
||||
cudaGraphicsGLRegisterBuffer(&cuda_vbo_resource, vbo,
|
||||
cudaGraphicsMapFlagsNone);
|
||||
cudaGraphicsGLRegisterBuffer(&cuda_vbo_resource, vbo, cudaGraphicsMapFlagsNone);
|
||||
|
||||
getLastCudaError("cudaGraphicsGLRegisterBuffer failed");
|
||||
break;
|
||||
getLastCudaError("cudaGraphicsGLRegisterBuffer failed");
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void click(int button, int updown, int x, int y) {
|
||||
lastx = x;
|
||||
lasty = y;
|
||||
clicked = !clicked;
|
||||
void click(int button, int updown, int x, int y)
|
||||
{
|
||||
lastx = x;
|
||||
lasty = y;
|
||||
clicked = !clicked;
|
||||
}
|
||||
|
||||
void motion(int x, int y) {
|
||||
// Convert motion coordinates to domain
|
||||
float fx = (lastx / (float)wWidth);
|
||||
float fy = (lasty / (float)wHeight);
|
||||
int nx = (int)(fx * DIM);
|
||||
int ny = (int)(fy * DIM);
|
||||
void motion(int x, int y)
|
||||
{
|
||||
// Convert motion coordinates to domain
|
||||
float fx = (lastx / (float)wWidth);
|
||||
float fy = (lasty / (float)wHeight);
|
||||
int nx = (int)(fx * DIM);
|
||||
int ny = (int)(fy * DIM);
|
||||
|
||||
if (clicked && nx < DIM - FR && nx > FR - 1 && ny < DIM - FR && ny > FR - 1) {
|
||||
int ddx = x - lastx;
|
||||
int ddy = y - lasty;
|
||||
fx = ddx / (float)wWidth;
|
||||
fy = ddy / (float)wHeight;
|
||||
int spy = ny - FR;
|
||||
int spx = nx - FR;
|
||||
addForces(dvfield, DIM, DIM, spx, spy, FORCE * DT * fx, FORCE * DT * fy,
|
||||
FR);
|
||||
lastx = x;
|
||||
lasty = y;
|
||||
}
|
||||
if (clicked && nx < DIM - FR && nx > FR - 1 && ny < DIM - FR && ny > FR - 1) {
|
||||
int ddx = x - lastx;
|
||||
int ddy = y - lasty;
|
||||
fx = ddx / (float)wWidth;
|
||||
fy = ddy / (float)wHeight;
|
||||
int spy = ny - FR;
|
||||
int spx = nx - FR;
|
||||
addForces(dvfield, DIM, DIM, spx, spy, FORCE * DT * fx, FORCE * DT * fy, FR);
|
||||
lastx = x;
|
||||
lasty = y;
|
||||
}
|
||||
|
||||
glutPostRedisplay();
|
||||
glutPostRedisplay();
|
||||
}
|
||||
|
||||
void reshape(int x, int y) {
|
||||
wWidth = x;
|
||||
wHeight = y;
|
||||
glViewport(0, 0, x, y);
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
glLoadIdentity();
|
||||
glOrtho(0, 1, 1, 0, 0, 1);
|
||||
glMatrixMode(GL_MODELVIEW);
|
||||
glLoadIdentity();
|
||||
glutPostRedisplay();
|
||||
void reshape(int x, int y)
|
||||
{
|
||||
wWidth = x;
|
||||
wHeight = y;
|
||||
glViewport(0, 0, x, y);
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
glLoadIdentity();
|
||||
glOrtho(0, 1, 1, 0, 0, 1);
|
||||
glMatrixMode(GL_MODELVIEW);
|
||||
glLoadIdentity();
|
||||
glutPostRedisplay();
|
||||
}
|
||||
|
||||
void cleanup(void) {
|
||||
cudaGraphicsUnregisterResource(cuda_vbo_resource);
|
||||
void cleanup(void)
|
||||
{
|
||||
cudaGraphicsUnregisterResource(cuda_vbo_resource);
|
||||
|
||||
deleteTexture();
|
||||
deleteTexture();
|
||||
|
||||
// Free all host and device resources
|
||||
free(hvfield);
|
||||
free(particles);
|
||||
cudaFree(dvfield);
|
||||
cudaFree(vxfield);
|
||||
cudaFree(vyfield);
|
||||
cufftDestroy(planr2c);
|
||||
cufftDestroy(planc2r);
|
||||
// Free all host and device resources
|
||||
free(hvfield);
|
||||
free(particles);
|
||||
cudaFree(dvfield);
|
||||
cudaFree(vxfield);
|
||||
cudaFree(vyfield);
|
||||
cufftDestroy(planr2c);
|
||||
cufftDestroy(planc2r);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glDeleteBuffers(1, &vbo);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glDeleteBuffers(1, &vbo);
|
||||
|
||||
sdkDeleteTimer(&timer);
|
||||
sdkDeleteTimer(&timer);
|
||||
}
|
||||
|
||||
int initGL(int *argc, char **argv) {
|
||||
glutInit(argc, argv);
|
||||
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
|
||||
glutInitWindowSize(wWidth, wHeight);
|
||||
glutCreateWindow("Compute Stable Fluids");
|
||||
glutDisplayFunc(display);
|
||||
glutKeyboardFunc(keyboard);
|
||||
glutMouseFunc(click);
|
||||
glutMotionFunc(motion);
|
||||
glutReshapeFunc(reshape);
|
||||
int initGL(int *argc, char **argv)
|
||||
{
|
||||
glutInit(argc, argv);
|
||||
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
|
||||
glutInitWindowSize(wWidth, wHeight);
|
||||
glutCreateWindow("Compute Stable Fluids");
|
||||
glutDisplayFunc(display);
|
||||
glutKeyboardFunc(keyboard);
|
||||
glutMouseFunc(click);
|
||||
glutMotionFunc(motion);
|
||||
glutReshapeFunc(reshape);
|
||||
|
||||
if (!isGLVersionSupported(1, 5)) {
|
||||
fprintf(stderr, "ERROR: Support for OpenGL 1.5 is missing");
|
||||
fflush(stderr);
|
||||
return false;
|
||||
}
|
||||
if (!isGLVersionSupported(1, 5)) {
|
||||
fprintf(stderr, "ERROR: Support for OpenGL 1.5 is missing");
|
||||
fflush(stderr);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!areGLExtensionsSupported("GL_ARB_vertex_buffer_object")) {
|
||||
fprintf(stderr, "ERROR: Support for necessary OpenGL extensions missing.");
|
||||
fflush(stderr);
|
||||
return false;
|
||||
}
|
||||
if (!areGLExtensionsSupported("GL_ARB_vertex_buffer_object")) {
|
||||
fprintf(stderr, "ERROR: Support for necessary OpenGL extensions missing.");
|
||||
fflush(stderr);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
return true;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
int devID;
|
||||
cudaDeviceProp deviceProps;
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int devID;
|
||||
cudaDeviceProp deviceProps;
|
||||
|
||||
#if defined(__linux__)
|
||||
char *Xstatus = getenv("DISPLAY");
|
||||
if (Xstatus == NULL) {
|
||||
printf("Waiving execution as X server is not running\n");
|
||||
exit(EXIT_WAIVED);
|
||||
}
|
||||
setenv("DISPLAY", ":0", 0);
|
||||
char *Xstatus = getenv("DISPLAY");
|
||||
if (Xstatus == NULL) {
|
||||
printf("Waiving execution as X server is not running\n");
|
||||
exit(EXIT_WAIVED);
|
||||
}
|
||||
setenv("DISPLAY", ":0", 0);
|
||||
#endif
|
||||
|
||||
printf("%s Starting...\n\n", sSDKname);
|
||||
printf("%s Starting...\n\n", sSDKname);
|
||||
|
||||
printf(
|
||||
"NOTE: The CUDA Samples are not meant for performance measurements. "
|
||||
"Results may vary when GPU Boost is enabled.\n\n");
|
||||
printf("NOTE: The CUDA Samples are not meant for performance measurements. "
|
||||
"Results may vary when GPU Boost is enabled.\n\n");
|
||||
|
||||
// First initialize OpenGL context, so we can properly set the GL for CUDA.
|
||||
// This is necessary in order to achieve optimal performance with OpenGL/CUDA
|
||||
// interop.
|
||||
if (false == initGL(&argc, argv)) {
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
// First initialize OpenGL context, so we can properly set the GL for CUDA.
|
||||
// This is necessary in order to achieve optimal performance with OpenGL/CUDA
|
||||
// interop.
|
||||
if (false == initGL(&argc, argv)) {
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
// use command-line specified CUDA device, otherwise use device with highest
|
||||
// Gflops/s
|
||||
devID = findCudaDevice(argc, (const char **)argv);
|
||||
// use command-line specified CUDA device, otherwise use device with highest
|
||||
// Gflops/s
|
||||
devID = findCudaDevice(argc, (const char **)argv);
|
||||
|
||||
// get number of SMs on this GPU
|
||||
checkCudaErrors(cudaGetDeviceProperties(&deviceProps, devID));
|
||||
printf("CUDA device [%s] has %d Multi-Processors\n", deviceProps.name,
|
||||
deviceProps.multiProcessorCount);
|
||||
// get number of SMs on this GPU
|
||||
checkCudaErrors(cudaGetDeviceProperties(&deviceProps, devID));
|
||||
printf("CUDA device [%s] has %d Multi-Processors\n", deviceProps.name, deviceProps.multiProcessorCount);
|
||||
|
||||
// automated build testing harness
|
||||
if (checkCmdLineFlag(argc, (const char **)argv, "file")) {
|
||||
getCmdLineArgumentString(argc, (const char **)argv, "file", &ref_file);
|
||||
}
|
||||
// automated build testing harness
|
||||
if (checkCmdLineFlag(argc, (const char **)argv, "file")) {
|
||||
getCmdLineArgumentString(argc, (const char **)argv, "file", &ref_file);
|
||||
}
|
||||
|
||||
// Allocate and initialize host data
|
||||
GLint bsize;
|
||||
// Allocate and initialize host data
|
||||
GLint bsize;
|
||||
|
||||
sdkCreateTimer(&timer);
|
||||
sdkResetTimer(&timer);
|
||||
sdkCreateTimer(&timer);
|
||||
sdkResetTimer(&timer);
|
||||
|
||||
hvfield = (cData *)malloc(sizeof(cData) * DS);
|
||||
memset(hvfield, 0, sizeof(cData) * DS);
|
||||
hvfield = (cData *)malloc(sizeof(cData) * DS);
|
||||
memset(hvfield, 0, sizeof(cData) * DS);
|
||||
|
||||
// Allocate and initialize device data
|
||||
cudaMallocPitch((void **)&dvfield, &tPitch, sizeof(cData) * DIM, DIM);
|
||||
// Allocate and initialize device data
|
||||
cudaMallocPitch((void **)&dvfield, &tPitch, sizeof(cData) * DIM, DIM);
|
||||
|
||||
cudaMemcpy(dvfield, hvfield, sizeof(cData) * DS, cudaMemcpyHostToDevice);
|
||||
// Temporary complex velocity field data
|
||||
cudaMalloc((void **)&vxfield, sizeof(cData) * PDS);
|
||||
cudaMalloc((void **)&vyfield, sizeof(cData) * PDS);
|
||||
cudaMemcpy(dvfield, hvfield, sizeof(cData) * DS, cudaMemcpyHostToDevice);
|
||||
// Temporary complex velocity field data
|
||||
cudaMalloc((void **)&vxfield, sizeof(cData) * PDS);
|
||||
cudaMalloc((void **)&vyfield, sizeof(cData) * PDS);
|
||||
|
||||
setupTexture(DIM, DIM);
|
||||
setupTexture(DIM, DIM);
|
||||
|
||||
// Create particle array
|
||||
particles = (cData *)malloc(sizeof(cData) * DS);
|
||||
memset(particles, 0, sizeof(cData) * DS);
|
||||
// Create particle array
|
||||
particles = (cData *)malloc(sizeof(cData) * DS);
|
||||
memset(particles, 0, sizeof(cData) * DS);
|
||||
|
||||
initParticles(particles, DIM, DIM);
|
||||
initParticles(particles, DIM, DIM);
|
||||
|
||||
// Create CUFFT transform plan configuration
|
||||
checkCudaErrors(cufftPlan2d(&planr2c, DIM, DIM, CUFFT_R2C));
|
||||
checkCudaErrors(cufftPlan2d(&planc2r, DIM, DIM, CUFFT_C2R));
|
||||
// Create CUFFT transform plan configuration
|
||||
checkCudaErrors(cufftPlan2d(&planr2c, DIM, DIM, CUFFT_R2C));
|
||||
checkCudaErrors(cufftPlan2d(&planc2r, DIM, DIM, CUFFT_C2R));
|
||||
|
||||
glGenBuffers(1, &vbo);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(cData) * DS, particles,
|
||||
GL_DYNAMIC_DRAW_ARB);
|
||||
glGenBuffers(1, &vbo);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(cData) * DS, particles, GL_DYNAMIC_DRAW_ARB);
|
||||
|
||||
glGetBufferParameteriv(GL_ARRAY_BUFFER, GL_BUFFER_SIZE, &bsize);
|
||||
glGetBufferParameteriv(GL_ARRAY_BUFFER, GL_BUFFER_SIZE, &bsize);
|
||||
|
||||
if (bsize != (sizeof(cData) * DS)) goto EXTERR;
|
||||
if (bsize != (sizeof(cData) * DS))
|
||||
goto EXTERR;
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
|
||||
checkCudaErrors(cudaGraphicsGLRegisterBuffer(&cuda_vbo_resource, vbo,
|
||||
cudaGraphicsMapFlagsNone));
|
||||
getLastCudaError("cudaGraphicsGLRegisterBuffer failed");
|
||||
checkCudaErrors(cudaGraphicsGLRegisterBuffer(&cuda_vbo_resource, vbo, cudaGraphicsMapFlagsNone));
|
||||
getLastCudaError("cudaGraphicsGLRegisterBuffer failed");
|
||||
|
||||
if (ref_file) {
|
||||
autoTest(argv);
|
||||
cleanup();
|
||||
if (ref_file) {
|
||||
autoTest(argv);
|
||||
cleanup();
|
||||
|
||||
printf("[fluidsGL] - Test Results: %d Failures\n", g_TotalErrors);
|
||||
exit(g_TotalErrors == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
|
||||
|
||||
} else {
|
||||
printf("[fluidsGL] - Test Results: %d Failures\n", g_TotalErrors);
|
||||
exit(g_TotalErrors == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
|
||||
}
|
||||
else {
|
||||
#if defined(__APPLE__) || defined(MACOSX)
|
||||
atexit(cleanup);
|
||||
atexit(cleanup);
|
||||
#else
|
||||
glutCloseFunc(cleanup);
|
||||
glutCloseFunc(cleanup);
|
||||
#endif
|
||||
glutMainLoop();
|
||||
}
|
||||
glutMainLoop();
|
||||
}
|
||||
|
||||
if (!ref_file) {
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
if (!ref_file) {
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
|
||||
EXTERR:
|
||||
printf("Failed to initialize GL extensions.\n");
|
||||
printf("Failed to initialize GL extensions.\n");
|
||||
|
||||
exit(EXIT_FAILURE);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
@@ -25,13 +25,12 @@
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <cuda_runtime.h>
|
||||
#include <cufft.h> // CUDA FFT Libraries
|
||||
#include <helper_cuda.h> // Helper functions for CUDA Error handling
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <cuda_runtime.h>
|
||||
#include <cufft.h> // CUDA FFT Libraries
|
||||
#include <helper_cuda.h> // Helper functions for CUDA Error handling
|
||||
|
||||
// OpenGL Graphics includes
|
||||
#define HELPERGL_EXTERN_GL_FUNC_IMPLEMENTATION
|
||||
#include <helper_gl.h>
|
||||
@@ -41,51 +40,52 @@
|
||||
|
||||
// Texture object for reading velocity field
|
||||
cudaTextureObject_t texObj;
|
||||
static cudaArray *array = NULL;
|
||||
static cudaArray *array = NULL;
|
||||
|
||||
// Particle data
|
||||
extern GLuint vbo; // OpenGL vertex buffer object
|
||||
extern struct cudaGraphicsResource
|
||||
*cuda_vbo_resource; // handles OpenGL-CUDA exchange
|
||||
extern GLuint vbo; // OpenGL vertex buffer object
|
||||
extern struct cudaGraphicsResource *cuda_vbo_resource; // handles OpenGL-CUDA exchange
|
||||
|
||||
// Texture pitch
|
||||
extern size_t tPitch;
|
||||
extern size_t tPitch;
|
||||
extern cufftHandle planr2c;
|
||||
extern cufftHandle planc2r;
|
||||
cData *vxfield = NULL;
|
||||
cData *vyfield = NULL;
|
||||
cData *vxfield = NULL;
|
||||
cData *vyfield = NULL;
|
||||
|
||||
void setupTexture(int x, int y) {
|
||||
cudaChannelFormatDesc desc = cudaCreateChannelDesc<float2>();
|
||||
void setupTexture(int x, int y)
|
||||
{
|
||||
cudaChannelFormatDesc desc = cudaCreateChannelDesc<float2>();
|
||||
|
||||
cudaMallocArray(&array, &desc, y, x);
|
||||
getLastCudaError("cudaMalloc failed");
|
||||
cudaMallocArray(&array, &desc, y, x);
|
||||
getLastCudaError("cudaMalloc failed");
|
||||
|
||||
cudaResourceDesc texRes;
|
||||
memset(&texRes, 0, sizeof(cudaResourceDesc));
|
||||
cudaResourceDesc texRes;
|
||||
memset(&texRes, 0, sizeof(cudaResourceDesc));
|
||||
|
||||
texRes.resType = cudaResourceTypeArray;
|
||||
texRes.res.array.array = array;
|
||||
texRes.resType = cudaResourceTypeArray;
|
||||
texRes.res.array.array = array;
|
||||
|
||||
cudaTextureDesc texDescr;
|
||||
memset(&texDescr, 0, sizeof(cudaTextureDesc));
|
||||
cudaTextureDesc texDescr;
|
||||
memset(&texDescr, 0, sizeof(cudaTextureDesc));
|
||||
|
||||
texDescr.normalizedCoords = false;
|
||||
texDescr.filterMode = cudaFilterModeLinear;
|
||||
texDescr.addressMode[0] = cudaAddressModeWrap;
|
||||
texDescr.readMode = cudaReadModeElementType;
|
||||
texDescr.normalizedCoords = false;
|
||||
texDescr.filterMode = cudaFilterModeLinear;
|
||||
texDescr.addressMode[0] = cudaAddressModeWrap;
|
||||
texDescr.readMode = cudaReadModeElementType;
|
||||
|
||||
checkCudaErrors(cudaCreateTextureObject(&texObj, &texRes, &texDescr, NULL));
|
||||
checkCudaErrors(cudaCreateTextureObject(&texObj, &texRes, &texDescr, NULL));
|
||||
}
|
||||
|
||||
void updateTexture(cData *data, size_t wib, size_t h, size_t pitch) {
|
||||
checkCudaErrors(cudaMemcpy2DToArray(array, 0, 0, data, pitch, wib, h,
|
||||
cudaMemcpyDeviceToDevice));
|
||||
void updateTexture(cData *data, size_t wib, size_t h, size_t pitch)
|
||||
{
|
||||
checkCudaErrors(cudaMemcpy2DToArray(array, 0, 0, data, pitch, wib, h, cudaMemcpyDeviceToDevice));
|
||||
}
|
||||
|
||||
void deleteTexture(void) {
|
||||
checkCudaErrors(cudaDestroyTextureObject(texObj));
|
||||
checkCudaErrors(cudaFreeArray(array));
|
||||
void deleteTexture(void)
|
||||
{
|
||||
checkCudaErrors(cudaDestroyTextureObject(texObj));
|
||||
checkCudaErrors(cudaFreeArray(array));
|
||||
}
|
||||
|
||||
// Note that these kernels are designed to work with arbitrary
|
||||
@@ -99,54 +99,61 @@ void deleteTexture(void) {
|
||||
|
||||
// This method adds constant force vectors to the velocity field
|
||||
// stored in 'v' according to v(x,t+1) = v(x,t) + dt * f.
|
||||
__global__ void addForces_k(cData *v, int dx, int dy, int spx, int spy,
|
||||
float fx, float fy, int r, size_t pitch) {
|
||||
int tx = threadIdx.x;
|
||||
int ty = threadIdx.y;
|
||||
cData *fj = (cData *)((char *)v + (ty + spy) * pitch) + tx + spx;
|
||||
__global__ void addForces_k(cData *v, int dx, int dy, int spx, int spy, float fx, float fy, int r, size_t pitch)
|
||||
{
|
||||
int tx = threadIdx.x;
|
||||
int ty = threadIdx.y;
|
||||
cData *fj = (cData *)((char *)v + (ty + spy) * pitch) + tx + spx;
|
||||
|
||||
cData vterm = *fj;
|
||||
tx -= r;
|
||||
ty -= r;
|
||||
float s = 1.f / (1.f + tx * tx * tx * tx + ty * ty * ty * ty);
|
||||
vterm.x += s * fx;
|
||||
vterm.y += s * fy;
|
||||
*fj = vterm;
|
||||
cData vterm = *fj;
|
||||
tx -= r;
|
||||
ty -= r;
|
||||
float s = 1.f / (1.f + tx * tx * tx * tx + ty * ty * ty * ty);
|
||||
vterm.x += s * fx;
|
||||
vterm.y += s * fy;
|
||||
*fj = vterm;
|
||||
}
|
||||
|
||||
// This method performs the velocity advection step, where we
|
||||
// trace velocity vectors back in time to update each grid cell.
|
||||
// That is, v(x,t+1) = v(p(x,-dt),t). Here we perform bilinear
|
||||
// interpolation in the velocity space.
|
||||
__global__ void advectVelocity_k(cData *v, float *vx, float *vy, int dx,
|
||||
int pdx, int dy, float dt, int lb,
|
||||
cudaTextureObject_t texObject) {
|
||||
int gtidx = blockIdx.x * blockDim.x + threadIdx.x;
|
||||
int gtidy = blockIdx.y * (lb * blockDim.y) + threadIdx.y * lb;
|
||||
int p;
|
||||
__global__ void advectVelocity_k(cData *v,
|
||||
float *vx,
|
||||
float *vy,
|
||||
int dx,
|
||||
int pdx,
|
||||
int dy,
|
||||
float dt,
|
||||
int lb,
|
||||
cudaTextureObject_t texObject)
|
||||
{
|
||||
int gtidx = blockIdx.x * blockDim.x + threadIdx.x;
|
||||
int gtidy = blockIdx.y * (lb * blockDim.y) + threadIdx.y * lb;
|
||||
int p;
|
||||
|
||||
cData vterm, ploc;
|
||||
float vxterm, vyterm;
|
||||
cData vterm, ploc;
|
||||
float vxterm, vyterm;
|
||||
|
||||
// gtidx is the domain location in x for this thread
|
||||
if (gtidx < dx) {
|
||||
for (p = 0; p < lb; p++) {
|
||||
// fi is the domain location in y for this thread
|
||||
int fi = gtidy + p;
|
||||
// gtidx is the domain location in x for this thread
|
||||
if (gtidx < dx) {
|
||||
for (p = 0; p < lb; p++) {
|
||||
// fi is the domain location in y for this thread
|
||||
int fi = gtidy + p;
|
||||
|
||||
if (fi < dy) {
|
||||
int fj = fi * pdx + gtidx;
|
||||
vterm = tex2D<cData>(texObject, (float)gtidx, (float)fi);
|
||||
ploc.x = (gtidx + 0.5f) - (dt * vterm.x * dx);
|
||||
ploc.y = (fi + 0.5f) - (dt * vterm.y * dy);
|
||||
vterm = tex2D<cData>(texObject, ploc.x, ploc.y);
|
||||
vxterm = vterm.x;
|
||||
vyterm = vterm.y;
|
||||
vx[fj] = vxterm;
|
||||
vy[fj] = vyterm;
|
||||
}
|
||||
if (fi < dy) {
|
||||
int fj = fi * pdx + gtidx;
|
||||
vterm = tex2D<cData>(texObject, (float)gtidx, (float)fi);
|
||||
ploc.x = (gtidx + 0.5f) - (dt * vterm.x * dx);
|
||||
ploc.y = (fi + 0.5f) - (dt * vterm.y * dy);
|
||||
vterm = tex2D<cData>(texObject, ploc.x, ploc.y);
|
||||
vxterm = vterm.x;
|
||||
vyterm = vterm.y;
|
||||
vx[fj] = vxterm;
|
||||
vy[fj] = vyterm;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// This method performs velocity diffusion and forces mass conservation
|
||||
@@ -157,204 +164,197 @@ __global__ void advectVelocity_k(cData *v, float *vx, float *vy, int dx,
|
||||
// and k is the wavenumber. The projection step forces the Fourier
|
||||
// velocity vectors to be orthogonal to the vectors for each
|
||||
// wavenumber: v(k,t) = v(k,t) - ((k dot v(k,t) * k) / k^2.
|
||||
__global__ void diffuseProject_k(cData *vx, cData *vy, int dx, int dy, float dt,
|
||||
float visc, int lb) {
|
||||
int gtidx = blockIdx.x * blockDim.x + threadIdx.x;
|
||||
int gtidy = blockIdx.y * (lb * blockDim.y) + threadIdx.y * lb;
|
||||
int p;
|
||||
__global__ void diffuseProject_k(cData *vx, cData *vy, int dx, int dy, float dt, float visc, int lb)
|
||||
{
|
||||
int gtidx = blockIdx.x * blockDim.x + threadIdx.x;
|
||||
int gtidy = blockIdx.y * (lb * blockDim.y) + threadIdx.y * lb;
|
||||
int p;
|
||||
|
||||
cData xterm, yterm;
|
||||
cData xterm, yterm;
|
||||
|
||||
// gtidx is the domain location in x for this thread
|
||||
if (gtidx < dx) {
|
||||
for (p = 0; p < lb; p++) {
|
||||
// fi is the domain location in y for this thread
|
||||
int fi = gtidy + p;
|
||||
// gtidx is the domain location in x for this thread
|
||||
if (gtidx < dx) {
|
||||
for (p = 0; p < lb; p++) {
|
||||
// fi is the domain location in y for this thread
|
||||
int fi = gtidy + p;
|
||||
|
||||
if (fi < dy) {
|
||||
int fj = fi * dx + gtidx;
|
||||
xterm = vx[fj];
|
||||
yterm = vy[fj];
|
||||
if (fi < dy) {
|
||||
int fj = fi * dx + gtidx;
|
||||
xterm = vx[fj];
|
||||
yterm = vy[fj];
|
||||
|
||||
// Compute the index of the wavenumber based on the
|
||||
// data order produced by a standard NN FFT.
|
||||
int iix = gtidx;
|
||||
int iiy = (fi > dy / 2) ? (fi - (dy)) : fi;
|
||||
// Compute the index of the wavenumber based on the
|
||||
// data order produced by a standard NN FFT.
|
||||
int iix = gtidx;
|
||||
int iiy = (fi > dy / 2) ? (fi - (dy)) : fi;
|
||||
|
||||
// Velocity diffusion
|
||||
float kk = (float)(iix * iix + iiy * iiy); // k^2
|
||||
float diff = 1.f / (1.f + visc * dt * kk);
|
||||
xterm.x *= diff;
|
||||
xterm.y *= diff;
|
||||
yterm.x *= diff;
|
||||
yterm.y *= diff;
|
||||
// Velocity diffusion
|
||||
float kk = (float)(iix * iix + iiy * iiy); // k^2
|
||||
float diff = 1.f / (1.f + visc * dt * kk);
|
||||
xterm.x *= diff;
|
||||
xterm.y *= diff;
|
||||
yterm.x *= diff;
|
||||
yterm.y *= diff;
|
||||
|
||||
// Velocity projection
|
||||
if (kk > 0.f) {
|
||||
float rkk = 1.f / kk;
|
||||
// Real portion of velocity projection
|
||||
float rkp = (iix * xterm.x + iiy * yterm.x);
|
||||
// Imaginary portion of velocity projection
|
||||
float ikp = (iix * xterm.y + iiy * yterm.y);
|
||||
xterm.x -= rkk * rkp * iix;
|
||||
xterm.y -= rkk * ikp * iix;
|
||||
yterm.x -= rkk * rkp * iiy;
|
||||
yterm.y -= rkk * ikp * iiy;
|
||||
// Velocity projection
|
||||
if (kk > 0.f) {
|
||||
float rkk = 1.f / kk;
|
||||
// Real portion of velocity projection
|
||||
float rkp = (iix * xterm.x + iiy * yterm.x);
|
||||
// Imaginary portion of velocity projection
|
||||
float ikp = (iix * xterm.y + iiy * yterm.y);
|
||||
xterm.x -= rkk * rkp * iix;
|
||||
xterm.y -= rkk * ikp * iix;
|
||||
yterm.x -= rkk * rkp * iiy;
|
||||
yterm.y -= rkk * ikp * iiy;
|
||||
}
|
||||
|
||||
vx[fj] = xterm;
|
||||
vy[fj] = yterm;
|
||||
}
|
||||
}
|
||||
|
||||
vx[fj] = xterm;
|
||||
vy[fj] = yterm;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// This method updates the velocity field 'v' using the two complex
|
||||
// arrays from the previous step: 'vx' and 'vy'. Here we scale the
|
||||
// real components by 1/(dx*dy) to account for an unnormalized FFT.
|
||||
__global__ void updateVelocity_k(cData *v, float *vx, float *vy, int dx,
|
||||
int pdx, int dy, int lb, size_t pitch) {
|
||||
int gtidx = blockIdx.x * blockDim.x + threadIdx.x;
|
||||
int gtidy = blockIdx.y * (lb * blockDim.y) + threadIdx.y * lb;
|
||||
int p;
|
||||
__global__ void updateVelocity_k(cData *v, float *vx, float *vy, int dx, int pdx, int dy, int lb, size_t pitch)
|
||||
{
|
||||
int gtidx = blockIdx.x * blockDim.x + threadIdx.x;
|
||||
int gtidy = blockIdx.y * (lb * blockDim.y) + threadIdx.y * lb;
|
||||
int p;
|
||||
|
||||
float vxterm, vyterm;
|
||||
cData nvterm;
|
||||
float vxterm, vyterm;
|
||||
cData nvterm;
|
||||
|
||||
// gtidx is the domain location in x for this thread
|
||||
if (gtidx < dx) {
|
||||
for (p = 0; p < lb; p++) {
|
||||
// fi is the domain location in y for this thread
|
||||
int fi = gtidy + p;
|
||||
// gtidx is the domain location in x for this thread
|
||||
if (gtidx < dx) {
|
||||
for (p = 0; p < lb; p++) {
|
||||
// fi is the domain location in y for this thread
|
||||
int fi = gtidy + p;
|
||||
|
||||
if (fi < dy) {
|
||||
int fjr = fi * pdx + gtidx;
|
||||
vxterm = vx[fjr];
|
||||
vyterm = vy[fjr];
|
||||
if (fi < dy) {
|
||||
int fjr = fi * pdx + gtidx;
|
||||
vxterm = vx[fjr];
|
||||
vyterm = vy[fjr];
|
||||
|
||||
// Normalize the result of the inverse FFT
|
||||
float scale = 1.f / (dx * dy);
|
||||
nvterm.x = vxterm * scale;
|
||||
nvterm.y = vyterm * scale;
|
||||
// Normalize the result of the inverse FFT
|
||||
float scale = 1.f / (dx * dy);
|
||||
nvterm.x = vxterm * scale;
|
||||
nvterm.y = vyterm * scale;
|
||||
|
||||
cData *fj = (cData *)((char *)v + fi * pitch) + gtidx;
|
||||
*fj = nvterm;
|
||||
}
|
||||
} // If this thread is inside the domain in Y
|
||||
} // If this thread is inside the domain in X
|
||||
cData *fj = (cData *)((char *)v + fi * pitch) + gtidx;
|
||||
*fj = nvterm;
|
||||
}
|
||||
} // If this thread is inside the domain in Y
|
||||
} // If this thread is inside the domain in X
|
||||
}
|
||||
|
||||
// This method updates the particles by moving particle positions
|
||||
// according to the velocity field and time step. That is, for each
|
||||
// particle: p(t+1) = p(t) + dt * v(p(t)).
|
||||
__global__ void advectParticles_k(cData *part, cData *v, int dx, int dy,
|
||||
float dt, int lb, size_t pitch) {
|
||||
int gtidx = blockIdx.x * blockDim.x + threadIdx.x;
|
||||
int gtidy = blockIdx.y * (lb * blockDim.y) + threadIdx.y * lb;
|
||||
int p;
|
||||
__global__ void advectParticles_k(cData *part, cData *v, int dx, int dy, float dt, int lb, size_t pitch)
|
||||
{
|
||||
int gtidx = blockIdx.x * blockDim.x + threadIdx.x;
|
||||
int gtidy = blockIdx.y * (lb * blockDim.y) + threadIdx.y * lb;
|
||||
int p;
|
||||
|
||||
// gtidx is the domain location in x for this thread
|
||||
cData pterm, vterm;
|
||||
// gtidx is the domain location in x for this thread
|
||||
cData pterm, vterm;
|
||||
|
||||
if (gtidx < dx) {
|
||||
for (p = 0; p < lb; p++) {
|
||||
// fi is the domain location in y for this thread
|
||||
int fi = gtidy + p;
|
||||
if (gtidx < dx) {
|
||||
for (p = 0; p < lb; p++) {
|
||||
// fi is the domain location in y for this thread
|
||||
int fi = gtidy + p;
|
||||
|
||||
if (fi < dy) {
|
||||
int fj = fi * dx + gtidx;
|
||||
pterm = part[fj];
|
||||
if (fi < dy) {
|
||||
int fj = fi * dx + gtidx;
|
||||
pterm = part[fj];
|
||||
|
||||
int xvi = ((int)(pterm.x * dx));
|
||||
int yvi = ((int)(pterm.y * dy));
|
||||
vterm = *((cData *)((char *)v + yvi * pitch) + xvi);
|
||||
int xvi = ((int)(pterm.x * dx));
|
||||
int yvi = ((int)(pterm.y * dy));
|
||||
vterm = *((cData *)((char *)v + yvi * pitch) + xvi);
|
||||
|
||||
pterm.x += dt * vterm.x;
|
||||
pterm.x = pterm.x - (int)pterm.x;
|
||||
pterm.x += 1.f;
|
||||
pterm.x = pterm.x - (int)pterm.x;
|
||||
pterm.y += dt * vterm.y;
|
||||
pterm.y = pterm.y - (int)pterm.y;
|
||||
pterm.y += 1.f;
|
||||
pterm.y = pterm.y - (int)pterm.y;
|
||||
pterm.x += dt * vterm.x;
|
||||
pterm.x = pterm.x - (int)pterm.x;
|
||||
pterm.x += 1.f;
|
||||
pterm.x = pterm.x - (int)pterm.x;
|
||||
pterm.y += dt * vterm.y;
|
||||
pterm.y = pterm.y - (int)pterm.y;
|
||||
pterm.y += 1.f;
|
||||
pterm.y = pterm.y - (int)pterm.y;
|
||||
|
||||
part[fj] = pterm;
|
||||
}
|
||||
} // If this thread is inside the domain in Y
|
||||
} // If this thread is inside the domain in X
|
||||
part[fj] = pterm;
|
||||
}
|
||||
} // If this thread is inside the domain in Y
|
||||
} // If this thread is inside the domain in X
|
||||
}
|
||||
|
||||
// These are the external function calls necessary for launching fluid
|
||||
// simulation
|
||||
extern "C" void addForces(cData *v, int dx, int dy, int spx, int spy, float fx,
|
||||
float fy, int r) {
|
||||
dim3 tids(2 * r + 1, 2 * r + 1);
|
||||
extern "C" void addForces(cData *v, int dx, int dy, int spx, int spy, float fx, float fy, int r)
|
||||
{
|
||||
dim3 tids(2 * r + 1, 2 * r + 1);
|
||||
|
||||
addForces_k<<<1, tids>>>(v, dx, dy, spx, spy, fx, fy, r, tPitch);
|
||||
getLastCudaError("addForces_k failed.");
|
||||
addForces_k<<<1, tids>>>(v, dx, dy, spx, spy, fx, fy, r, tPitch);
|
||||
getLastCudaError("addForces_k failed.");
|
||||
}
|
||||
|
||||
extern "C" void advectVelocity(cData *v, float *vx, float *vy, int dx, int pdx,
|
||||
int dy, float dt) {
|
||||
dim3 grid((dx / TILEX) + (!(dx % TILEX) ? 0 : 1),
|
||||
(dy / TILEY) + (!(dy % TILEY) ? 0 : 1));
|
||||
extern "C" void advectVelocity(cData *v, float *vx, float *vy, int dx, int pdx, int dy, float dt)
|
||||
{
|
||||
dim3 grid((dx / TILEX) + (!(dx % TILEX) ? 0 : 1), (dy / TILEY) + (!(dy % TILEY) ? 0 : 1));
|
||||
|
||||
dim3 tids(TIDSX, TIDSY);
|
||||
dim3 tids(TIDSX, TIDSY);
|
||||
|
||||
updateTexture(v, DIM * sizeof(cData), DIM, tPitch);
|
||||
advectVelocity_k<<<grid, tids>>>(v, vx, vy, dx, pdx, dy, dt, TILEY / TIDSY,
|
||||
texObj);
|
||||
updateTexture(v, DIM * sizeof(cData), DIM, tPitch);
|
||||
advectVelocity_k<<<grid, tids>>>(v, vx, vy, dx, pdx, dy, dt, TILEY / TIDSY, texObj);
|
||||
|
||||
getLastCudaError("advectVelocity_k failed.");
|
||||
getLastCudaError("advectVelocity_k failed.");
|
||||
}
|
||||
|
||||
extern "C" void diffuseProject(cData *vx, cData *vy, int dx, int dy, float dt,
|
||||
float visc) {
|
||||
// Forward FFT
|
||||
checkCudaErrors(cufftExecR2C(planr2c, (cufftReal *)vx, (cufftComplex *)vx));
|
||||
checkCudaErrors(cufftExecR2C(planr2c, (cufftReal *)vy, (cufftComplex *)vy));
|
||||
extern "C" void diffuseProject(cData *vx, cData *vy, int dx, int dy, float dt, float visc)
|
||||
{
|
||||
// Forward FFT
|
||||
checkCudaErrors(cufftExecR2C(planr2c, (cufftReal *)vx, (cufftComplex *)vx));
|
||||
checkCudaErrors(cufftExecR2C(planr2c, (cufftReal *)vy, (cufftComplex *)vy));
|
||||
|
||||
uint3 grid = make_uint3((dx / TILEX) + (!(dx % TILEX) ? 0 : 1),
|
||||
(dy / TILEY) + (!(dy % TILEY) ? 0 : 1), 1);
|
||||
uint3 tids = make_uint3(TIDSX, TIDSY, 1);
|
||||
uint3 grid = make_uint3((dx / TILEX) + (!(dx % TILEX) ? 0 : 1), (dy / TILEY) + (!(dy % TILEY) ? 0 : 1), 1);
|
||||
uint3 tids = make_uint3(TIDSX, TIDSY, 1);
|
||||
|
||||
diffuseProject_k<<<grid, tids>>>(vx, vy, dx, dy, dt, visc, TILEY / TIDSY);
|
||||
getLastCudaError("diffuseProject_k failed.");
|
||||
diffuseProject_k<<<grid, tids>>>(vx, vy, dx, dy, dt, visc, TILEY / TIDSY);
|
||||
getLastCudaError("diffuseProject_k failed.");
|
||||
|
||||
// Inverse FFT
|
||||
checkCudaErrors(cufftExecC2R(planc2r, (cufftComplex *)vx, (cufftReal *)vx));
|
||||
checkCudaErrors(cufftExecC2R(planc2r, (cufftComplex *)vy, (cufftReal *)vy));
|
||||
// Inverse FFT
|
||||
checkCudaErrors(cufftExecC2R(planc2r, (cufftComplex *)vx, (cufftReal *)vx));
|
||||
checkCudaErrors(cufftExecC2R(planc2r, (cufftComplex *)vy, (cufftReal *)vy));
|
||||
}
|
||||
|
||||
extern "C" void updateVelocity(cData *v, float *vx, float *vy, int dx, int pdx,
|
||||
int dy) {
|
||||
dim3 grid((dx / TILEX) + (!(dx % TILEX) ? 0 : 1),
|
||||
(dy / TILEY) + (!(dy % TILEY) ? 0 : 1));
|
||||
dim3 tids(TIDSX, TIDSY);
|
||||
extern "C" void updateVelocity(cData *v, float *vx, float *vy, int dx, int pdx, int dy)
|
||||
{
|
||||
dim3 grid((dx / TILEX) + (!(dx % TILEX) ? 0 : 1), (dy / TILEY) + (!(dy % TILEY) ? 0 : 1));
|
||||
dim3 tids(TIDSX, TIDSY);
|
||||
|
||||
updateVelocity_k<<<grid, tids>>>(v, vx, vy, dx, pdx, dy, TILEY / TIDSY,
|
||||
tPitch);
|
||||
getLastCudaError("updateVelocity_k failed.");
|
||||
updateVelocity_k<<<grid, tids>>>(v, vx, vy, dx, pdx, dy, TILEY / TIDSY, tPitch);
|
||||
getLastCudaError("updateVelocity_k failed.");
|
||||
}
|
||||
|
||||
extern "C" void advectParticles(GLuint vbo, cData *v, int dx, int dy,
|
||||
float dt) {
|
||||
dim3 grid((dx / TILEX) + (!(dx % TILEX) ? 0 : 1),
|
||||
(dy / TILEY) + (!(dy % TILEY) ? 0 : 1));
|
||||
dim3 tids(TIDSX, TIDSY);
|
||||
extern "C" void advectParticles(GLuint vbo, cData *v, int dx, int dy, float dt)
|
||||
{
|
||||
dim3 grid((dx / TILEX) + (!(dx % TILEX) ? 0 : 1), (dy / TILEY) + (!(dy % TILEY) ? 0 : 1));
|
||||
dim3 tids(TIDSX, TIDSY);
|
||||
|
||||
cData *p;
|
||||
cudaGraphicsMapResources(1, &cuda_vbo_resource, 0);
|
||||
getLastCudaError("cudaGraphicsMapResources failed");
|
||||
cData *p;
|
||||
cudaGraphicsMapResources(1, &cuda_vbo_resource, 0);
|
||||
getLastCudaError("cudaGraphicsMapResources failed");
|
||||
|
||||
size_t num_bytes;
|
||||
cudaGraphicsResourceGetMappedPointer((void **)&p, &num_bytes,
|
||||
cuda_vbo_resource);
|
||||
getLastCudaError("cudaGraphicsResourceGetMappedPointer failed");
|
||||
size_t num_bytes;
|
||||
cudaGraphicsResourceGetMappedPointer((void **)&p, &num_bytes, cuda_vbo_resource);
|
||||
getLastCudaError("cudaGraphicsResourceGetMappedPointer failed");
|
||||
|
||||
advectParticles_k<<<grid, tids>>>(p, v, dx, dy, dt, TILEY / TIDSY, tPitch);
|
||||
getLastCudaError("advectParticles_k failed.");
|
||||
advectParticles_k<<<grid, tids>>>(p, v, dx, dy, dt, TILEY / TIDSY, tPitch);
|
||||
getLastCudaError("advectParticles_k failed.");
|
||||
|
||||
cudaGraphicsUnmapResources(1, &cuda_vbo_resource, 0);
|
||||
getLastCudaError("cudaGraphicsUnmapResources failed");
|
||||
cudaGraphicsUnmapResources(1, &cuda_vbo_resource, 0);
|
||||
getLastCudaError("cudaGraphicsUnmapResources failed");
|
||||
}
|
||||
|
||||
@@ -39,16 +39,14 @@ void deleteTexture(void);
|
||||
|
||||
// This method adds constant force vectors to the velocity field
|
||||
// stored in 'v' according to v(x,t+1) = v(x,t) + dt * f.
|
||||
__global__ void addForces_k(cData *v, int dx, int dy, int spx, int spy,
|
||||
float fx, float fy, int r, size_t pitch);
|
||||
__global__ void addForces_k(cData *v, int dx, int dy, int spx, int spy, float fx, float fy, int r, size_t pitch);
|
||||
|
||||
// This method performs the velocity advection step, where we
|
||||
// trace velocity vectors back in time to update each grid cell.
|
||||
// That is, v(x,t+1) = v(p(x,-dt),t). Here we perform bilinear
|
||||
// interpolation in the velocity space.
|
||||
__global__ void advectVelocity_k(cData *v, float *vx, float *vy, int dx,
|
||||
int pdx, int dy, float dt, int lb,
|
||||
cudaTextureObject_t tex);
|
||||
__global__ void
|
||||
advectVelocity_k(cData *v, float *vx, float *vy, int dx, int pdx, int dy, float dt, int lb, cudaTextureObject_t tex);
|
||||
|
||||
// This method performs velocity diffusion and forces mass conservation
|
||||
// in the frequency domain. The inputs 'vx' and 'vy' are complex-valued
|
||||
@@ -58,19 +56,16 @@ __global__ void advectVelocity_k(cData *v, float *vx, float *vy, int dx,
|
||||
// and k is the wavenumber. The projection step forces the Fourier
|
||||
// velocity vectors to be orthogonal to the wave wave vectors for each
|
||||
// wavenumber: v(k,t) = v(k,t) - ((k dot v(k,t) * k) / k^2.
|
||||
__global__ void diffuseProject_k(cData *vx, cData *vy, int dx, int dy, float dt,
|
||||
float visc, int lb);
|
||||
__global__ void diffuseProject_k(cData *vx, cData *vy, int dx, int dy, float dt, float visc, int lb);
|
||||
|
||||
// This method updates the velocity field 'v' using the two complex
|
||||
// arrays from the previous step: 'vx' and 'vy'. Here we scale the
|
||||
// real components by 1/(dx*dy) to account for an unnormalized FFT.
|
||||
__global__ void updateVelocity_k(cData *v, float *vx, float *vy, int dx,
|
||||
int pdx, int dy, int lb, size_t pitch);
|
||||
__global__ void updateVelocity_k(cData *v, float *vx, float *vy, int dx, int pdx, int dy, int lb, size_t pitch);
|
||||
|
||||
// This method updates the particles by moving particle positions
|
||||
// according to the velocity field and time step. That is, for each
|
||||
// particle: p(t+1) = p(t) + dt * v(p(t)).
|
||||
__global__ void advectParticles_k(cData *part, cData *v, int dx, int dy,
|
||||
float dt, int lb, size_t pitch);
|
||||
__global__ void advectParticles_k(cData *part, cData *v, int dx, int dy, float dt, int lb, size_t pitch);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -37,16 +37,14 @@ void deleteTexture(void);
|
||||
|
||||
// This method adds constant force vectors to the velocity field
|
||||
// stored in 'v' according to v(x,t+1) = v(x,t) + dt * f.
|
||||
__global__ void addForces_k(cData *v, int dx, int dy, int spx, int spy,
|
||||
float fx, float fy, int r, size_t pitch);
|
||||
__global__ void addForces_k(cData *v, int dx, int dy, int spx, int spy, float fx, float fy, int r, size_t pitch);
|
||||
|
||||
// This method performs the velocity advection step, where we
|
||||
// trace velocity vectors back in time to update each grid cell.
|
||||
// That is, v(x,t+1) = v(p(x,-dt),t). Here we perform bilinear
|
||||
// interpolation in the velocity space.
|
||||
__global__ void advectVelocity_k(cData *v, float *vx, float *vy, int dx,
|
||||
int pdx, int dy, float dt, int lb,
|
||||
cudaTextureObject_t tex);
|
||||
__global__ void
|
||||
advectVelocity_k(cData *v, float *vx, float *vy, int dx, int pdx, int dy, float dt, int lb, cudaTextureObject_t tex);
|
||||
|
||||
// This method performs velocity diffusion and forces mass conservation
|
||||
// in the frequency domain. The inputs 'vx' and 'vy' are complex-valued
|
||||
@@ -56,19 +54,16 @@ __global__ void advectVelocity_k(cData *v, float *vx, float *vy, int dx,
|
||||
// and k is the wavenumber. The projection step forces the Fourier
|
||||
// velocity vectors to be orthogonal to the wave wave vectors for each
|
||||
// wavenumber: v(k,t) = v(k,t) - ((k dot v(k,t) * k) / k^2.
|
||||
__global__ void diffuseProject_k(cData *vx, cData *vy, int dx, int dy, float dt,
|
||||
float visc, int lb);
|
||||
__global__ void diffuseProject_k(cData *vx, cData *vy, int dx, int dy, float dt, float visc, int lb);
|
||||
|
||||
// This method updates the velocity field 'v' using the two complex
|
||||
// arrays from the previous step: 'vx' and 'vy'. Here we scale the
|
||||
// real components by 1/(dx*dy) to account for an unnormalized FFT.
|
||||
__global__ void updateVelocity_k(cData *v, float *vx, float *vy, int dx,
|
||||
int pdx, int dy, int lb, size_t pitch);
|
||||
__global__ void updateVelocity_k(cData *v, float *vx, float *vy, int dx, int pdx, int dy, int lb, size_t pitch);
|
||||
|
||||
// This method updates the particles by moving particle positions
|
||||
// according to the velocity field and time step. That is, for each
|
||||
// particle: p(t+1) = p(t) + dt * v(p(t)).
|
||||
__global__ void advectParticles_k(cData *part, cData *v, int dx, int dy,
|
||||
float dt, int lb, size_t pitch);
|
||||
__global__ void advectParticles_k(cData *part, cData *v, int dx, int dy, float dt, int lb, size_t pitch);
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user