4 Replies Latest reply on Mar 28, 2010 10:09 AM by nou

    Graphic output getting clipped past two parallel lines!

    ankurdh

      hello,

      i've written a code to simulate a large number of particles in openGL. When i run the program on CPU, it just works fine. but when i run it on the GPU, there are two parallel lines that are coming, and past them, the output is getting clipped!
      The same code is used, only the device type i've changed!

      Can anyone tell why this is happening?

      I'm posting from my school. i'l post the code once i'm back home.

        • Graphic output getting clipped past two parallel lines!
          gaurav.garg

          Can you post your code?

            • Graphic output getting clipped past two parallel lines!
              ankurdh

              yes, here is the code. 

              /* This is the header file i created to contain the classes. */ #include<GL/glut.h> #include<cstdlib> #include<cstdio> #include<iostream> #include<cstdlib> #define __NO_STD_VECTOR #define __NO_STD_STRING #include<SDKUtil/SDKFile.hpp> #include <SDKUtil/SDKCommon.hpp> #include<CL/cl.hpp> using namespace cl; #define MAX_NO_OF_PARTICLES 500 class OpenCLReference{ private: streamsdk::SDKCommon * toolkit; vector<Platform> platforms; cl_context_properties cntxtProps[3]; Context * context; vector<Device> devices; CommandQueue * cmdQueue; streamsdk::SDKFile kernelFile; Program * program; Program::Sources * kernelSource; Kernel * kernel; public: OpenCLReference(); friend void setParticlePositions(); }; class ParticleSystem { private: GLfloat particlesXValues[MAX_NO_OF_PARTICLES][MAX_NO_OF_PARTICLES]; GLfloat particlesYValues[MAX_NO_OF_PARTICLES][MAX_NO_OF_PARTICLES]; public: void initializeParticles(); friend void setParticlePositions(); friend void display(); friend class OpenCLReference; }; void setParticlePositions(); void display(); /* This is the main.cpp file */ #include "particleSystems.h" OpenCLReference * openCLReference; ParticleSystem * particleSystem; OpenCLReference::OpenCLReference(){ toolkit = new streamsdk::SDKCommon(); cl_int err = Platform::get(&platforms); toolkit->checkVal(err,CL_SUCCESS,"Platforms could not be queried"); cntxtProps[0] = CL_CONTEXT_PLATFORM; cntxtProps[1] = (cl_context_properties)(*platforms.begin())(); cntxtProps[2] = 0; context = new Context(CL_DEVICE_TYPE_GPU,cntxtProps,NULL,NULL,&err); toolkit->checkVal(err,CL_SUCCESS,"Context could not be created."); devices = context->getInfo<CL_CONTEXT_DEVICES>(&err); toolkit->checkVal(err,CL_SUCCESS,"Devices could not be queried."); cmdQueue = new CommandQueue(*context,*devices.begin(),NULL,&err); toolkit->checkVal(err,CL_SUCCESS,"Command queue could not be created"); if(!kernelFile.open("particleSystemKernelFile.txt")){ std::cout<<"Could not open kernel file."<<std::endl; std::getchar(); std::exit(-1); } kernelSource = new Program::Sources(1,std::make_pair(kernelFile.source().data(),kernelFile.source().size())); program = new Program(*context,*kernelSource,&err); toolkit->checkVal(err,CL_SUCCESS,"Program could not be created."); err = program->build(devices,NULL,NULL,NULL); toolkit->checkVal(err,CL_SUCCESS,"Program was not allocated the devices."); kernel = new Kernel(*program,"particleSystemKernel",&err); toolkit->checkVal(err,CL_SUCCESS,"Kernel could not be created."); } void ParticleSystem::initializeParticles(){ int x , y = 0; for(int i = 0 ; i < MAX_NO_OF_PARTICLES ; i ++){ x = 0; for(int j = 0 ; j < MAX_NO_OF_PARTICLES ; j ++){ particlesXValues[i][j] = x; particlesYValues[i][j] = y; x+=1; } y+=1; } } void reshape(int width, int height) { const float ar = (float) width / (float) height; glViewport(0, 0, width, height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glFrustum(-1.0f, 1.0f, -1.0, 1.0, 2.0, 2000.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity() ; gluLookAt(250.0f,250.0f,-1900.0f,250.0f,250.0f,0.0f,0.0f,1.0f,0.0f); } void display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glColor3d(1.0,0.50,0.0); glBegin(GL_POINTS); for(int i = 0 ; i < MAX_NO_OF_PARTICLES ; i ++) for(int j = 0 ; j < MAX_NO_OF_PARTICLES ; j ++) glVertex2f(particleSystem->particlesXValues[i][j],particleSystem->particlesYValues[i][j]); glEnd(); glFlush(); glutSwapBuffers(); } void setParticlePositions(){ int err; Buffer particlesXBuffer(*(openCLReference->context), CL_MEM_USE_HOST_PTR | CL_MEM_READ_WRITE , sizeof(GLfloat)*MAX_NO_OF_PARTICLES*MAX_NO_OF_PARTICLES, (void *)(particleSystem->particlesXValues),&err); openCLReference->toolkit->checkVal(err,CL_SUCCESS,"Buffer 1 could not be allocated"); Buffer particlesYBuffer(*(openCLReference->context), CL_MEM_USE_HOST_PTR | CL_MEM_READ_WRITE , sizeof(GLfloat)*MAX_NO_OF_PARTICLES*MAX_NO_OF_PARTICLES, (void *)(particleSystem->particlesYValues),&err); openCLReference->toolkit->checkVal(err,CL_SUCCESS,"Buffer 2 could not be allocated"); Buffer centerXBuffer(*(openCLReference->context), CL_MEM_READ_ONLY , sizeof(float), NULL,&err); openCLReference->toolkit->checkVal(err,CL_SUCCESS,"Buffer 3 could not be allocated"); Buffer centerYBuffer(*(openCLReference->context), CL_MEM_READ_ONLY , sizeof(float), NULL,&err); openCLReference->toolkit->checkVal(err,CL_SUCCESS,"Buffer 4 could not be allocated"); Buffer maxNoOfParticlesBuffer(*(openCLReference->context), CL_MEM_READ_ONLY, sizeof(int), NULL,&err); openCLReference->toolkit->checkVal(err,CL_SUCCESS,"Buffer 4 could not be allocated"); err = openCLReference->cmdQueue->enqueueWriteBuffer(particlesXBuffer,CL_TRUE,NULL, sizeof(GLfloat)*MAX_NO_OF_PARTICLES*MAX_NO_OF_PARTICLES,(void *)particleSystem->particlesXValues); openCLReference->toolkit->checkVal(err,CL_SUCCESS,"Buffer 1 could not be copied"); err = openCLReference->cmdQueue->enqueueWriteBuffer(particlesYBuffer,CL_TRUE,NULL, sizeof(GLfloat)*MAX_NO_OF_PARTICLES*MAX_NO_OF_PARTICLES,(void *)particleSystem->particlesYValues); openCLReference->toolkit->checkVal(err,CL_SUCCESS,"Buffer 2 could not be copied"); int centerX = MAX_NO_OF_PARTICLES/2; int centerY = MAX_NO_OF_PARTICLES/2; int maxNoOfParticles = MAX_NO_OF_PARTICLES; err = openCLReference->cmdQueue->enqueueWriteBuffer(centerXBuffer,CL_TRUE,NULL, sizeof(float),(void *)&centerX,NULL,NULL); openCLReference->toolkit->checkVal(err,CL_SUCCESS,"buffer 3 could not be copied"); err = openCLReference->cmdQueue->enqueueWriteBuffer(centerYBuffer,CL_TRUE,NULL, sizeof(float),(void *)&centerY,NULL,NULL); openCLReference->toolkit->checkVal(err,CL_SUCCESS,"buffer 4 could not be copied"); err = openCLReference->cmdQueue->enqueueWriteBuffer(maxNoOfParticlesBuffer,CL_TRUE,NULL, sizeof(int),(void *)&maxNoOfParticles,NULL,NULL); openCLReference->toolkit->checkVal(err,CL_SUCCESS,"buffer 5 could not be copied"); err = openCLReference->kernel->setArg<Buffer>(0,particlesXBuffer); openCLReference->toolkit->checkVal(err,CL_SUCCESS,"Arg 1 not set"); err = openCLReference->kernel->setArg<Buffer>(1,particlesYBuffer); openCLReference->toolkit->checkVal(err,CL_SUCCESS,"Arg 2 not set"); err = openCLReference->kernel->setArg<Buffer>(2,centerXBuffer); openCLReference->toolkit->checkVal(err,CL_SUCCESS,"Arg 3 not set"); err = openCLReference->kernel->setArg<Buffer>(3,centerYBuffer); openCLReference->toolkit->checkVal(err,CL_SUCCESS,"Arg 4 not set"); err = openCLReference->kernel->setArg<Buffer>(4,maxNoOfParticlesBuffer); openCLReference->toolkit->checkVal(err,CL_SUCCESS,"Arg 5 not set"); err = openCLReference->cmdQueue->enqueueNDRangeKernel(*(openCLReference->kernel),NullRange, NDRange(MAX_NO_OF_PARTICLES,MAX_NO_OF_PARTICLES),NullRange,NULL,NULL); openCLReference->toolkit->checkVal(err,CL_SUCCESS,"Kernels could not be run"); err = openCLReference->cmdQueue->enqueueReadBuffer(particlesXBuffer,CL_TRUE,NULL, sizeof(GLfloat)*MAX_NO_OF_PARTICLES*MAX_NO_OF_PARTICLES,(void *)particleSystem->particlesXValues, NULL,NULL); openCLReference->toolkit->checkVal(err,CL_SUCCESS,"Values could not be read from the kernel."); glutPostRedisplay(); } int main(int argc, char ** argv){ openCLReference = new OpenCLReference(); particleSystem = new ParticleSystem(); particleSystem->initializeParticles(); glutInit(&argc, argv); glutInitWindowSize(640,480); glutInitWindowPosition(10,10); glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); glutCreateWindow("Particle System"); glClearColor(0.0f,0.0f,0.0f,0.0f); glutReshapeFunc(reshape); glutDisplayFunc(display); glutIdleFunc(setParticlePositions); glutMainLoop(); std::cout<<"All fine till now."<<std::endl; std::getchar(); } /* this is the kernel file. The interactions i chose between the particles is random. */ __kernel void particleSystemKernel( __global float * particlesXValues, __global float * particlesYValues, __global float * centerX, __global float * centerY, __global int * maxNoOfParticles) { int x = get_global_id(0); int y = get_global_id(1); int dx = *(centerX) - particlesXValues[y * (*maxNoOfParticles) + x]; int dy = *(centerY) - particlesYValues[y * (*maxNoOfParticles) + x]; particlesXValues[y * (*maxNoOfParticles) + x] -= dx * sin(y * (*maxNoOfParticles) + x) * 0.1f; particlesYValues[y * (*maxNoOfParticles) + x] -= dy * sin(y * (*maxNoOfParticles) + x) * 0.1f; }

                • Graphic output getting clipped past two parallel lines!
                  ankurdh

                  when i run the code on the CPU, i get a swirl of particles. 

                  when i run the code on the GPU, the same swirl of particles i get, but past two lines, they're clipped.

                    • Graphic output getting clipped past two parallel lines!
                      nou

                      for pass one float/int you do not need create buffers.you can change

                      __kernel void particleSystemKernel(__global float * particlesXValues, __global float * particlesYValues, __global float * centerX, __global float * centerY, __global int   * maxNoOfParticles) to

                      __kernel void particleSystemKernel(__global float * particlesXValues, __global float * particlesYValues, float centerX, float centerY,int maxNoOfParticles)

                      and then set set openCLReference->kernel->setArg<float>(2,centerX);

                      and you should vectorize your code by using float2 float4 for better performance