cuda实战chapter4_julia

来源:互联网 发布:stm32串口读取数据 编辑:程序博客网 时间:2024/06/09 15:44

结果

参考:书本。自己写了一遍。方法参考书本 

疑问:1:

运行书本例子显示the launch timed out and was terminated,参考网址

http://stackoverflow.com/questions/6185117/cudamemcpy-errorthe-launch-timed-out-and-was-terminated好像是单个网格数目不能负担1000*1000的并行数目??  修改成200*200后,可以。  应该可以修改成多个GPU运行??但现在不知道。不过直接增加每个线程块负担的数目应该可以。

2:参考http://blog.csdn.net/xizhibei/article/details/6664184,修改julia数据,但是显示的并不好。是不是颜色搭配问题??
3:刚开始invalid argument,应该是数组没有设置好。a[(i*width+j)*6+m]才是正确的,但我a[i*width+y*6+m],只检测了i=0的情况,是正确的。。悲剧
4:那开始用vbo绘制也不对,应该也是数组问题??


5. 对C的指针不熟悉。
   怎么开多个线程网格?并运用?
   数组,最好多举例,实现。

6 opengl不熟悉。  把 glutCreateWindow()放在glutInitPosition前面了,一直显示初始化内容??


直接cpu实现:
     
#include<iostream>#include <time.h>#include <gl/GLee.h>#include <gl/glut.h>#include <gl/GL.h>#include<stdlib.h>#include <math.h>#define  width 1000#define  height 1000using namespace std;void init(void ){glClearColor(0,0,0,0);glShadeModel(GL_SMOOTH);}struct cuComplex {float   r;float   i;cuComplex( float a, float b ) : r(a), i(b)  {}float magnitude2( void ) { return r * r + i * i; }cuComplex operator*(const cuComplex& a) {return cuComplex(r*a.r - i*a.i, i*a.r + r*a.i);}cuComplex operator+(const cuComplex& a) {return cuComplex(r+a.r, i+a.i);}};int julia(int x,int y){const float scale = 1.5;float jx = scale * (float)(width/2 - x)/(width/2);float jy = scale * (float)(height/2 - y)/(height/2);cuComplex c( -0.8, 0.156);cuComplex a(jx, jy);  //cuComplex a(x, y);int i = 0;for (i=0; i<200; i++) {a = a * a + c;if (a.magnitude2() > 1000)return 0;}return 1;}void reshap(int w,int h){glViewport(0, 0, (GLsizei) w, (GLsizei) h);glMatrixMode(GL_PROJECTION);glLoadIdentity();//gluOrtho2D(-(GLdouble) w/2, (GLdouble) w/2, (GLdouble) -h/2, (GLdouble) h/2);gluOrtho2D(0, (GLdouble) w, 0, (GLdouble) h);glMatrixMode(GL_MODELVIEW);glLoadIdentity();}void keyboard(unsigned char key,int x,int y){switch(key){case 27:exit(0);break;}}static float vertex_list[width*height][6];static int index_list[width*height];void kernel(){for (int i=0;i!=width;++i){for (int j=0;j!=height;++j){vertex_list[i*width+j][0]=julia(i,j);vertex_list[i*width+j][1]=0;vertex_list[i*width+j][2]=0;//前三为颜色vertex_list[i*width+j][3]=i;vertex_list[i*width+j][4]=j;vertex_list[i*width+j][5]=0;//后三为坐标}}for (int i=0;i!=width*height;++i){index_list[i]=i;}}void display(void ){glClear(GL_COLOR_BUFFER_BIT);kernel();GLuint vbuffer=0;GLuint ibuffer=0;kernel();glGenBuffers(1,&vbuffer);glGenBuffers(1,&ibuffer);glBindBuffer(GL_ARRAY_BUFFER,vbuffer);glBufferData(GL_ARRAY_BUFFER,sizeof(vertex_list),vertex_list,GL_STATIC_DRAW);glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,ibuffer);glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(index_list),index_list,GL_STATIC_DRAW);glEnableClientState(GL_COLOR_ARRAY);glEnableClientState(GL_VERTEX_ARRAY);glInterleavedArrays(GL_C3F_V3F,0,0);glDrawElements(GL_POINTS,width*height,GL_UNSIGNED_INT,NULL);      glDrawElements(GL_POINTS,width*height,GL_UNSIGNED_INT,vertex_list);glutSwapBuffers();}int main(int argc,char** argv){clock_t start, end;start = clock();glutInit(&argc,argv);glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE);glutInitWindowSize(width,height);glutCreateWindow(argv[0]);init();glutReshapeFunc(reshap);glutDisplayFunc(display);glutKeyboardFunc(keyboard);end = clock();cout<<"Run time: "<<(double)(end - start) / CLOCKS_PER_SEC<<"S"<<endl;    glutMainLoop();return 0;}


gpu实现,但怎么修改,使1000*1000的可以/
#include<iostream>#include <time.h>#include<cuda.h>//#include<gl/glee.h>#include <gl/glut.h>#include<stdlib.h>#include <math.h>#define  width 200#define  height 200//static float vertex_list[width*height][6]; float vertex_list[width*height*6];// float index_list[width*height];using namespace std;void init(void ){glClearColor(0,0,0,0);glShadeModel(GL_SMOOTH);}struct cuComplex {float   r;float   i; __device__cuComplex( float a, float b ) : r(a), i(b)  {} __device__float magnitude2( void ) { return r * r + i * i; } __device__cuComplex operator*(const cuComplex& a) {return cuComplex(r*a.r - i*a.i, i*a.r + r*a.i);} __device__cuComplex operator+(const cuComplex& a) {return cuComplex(r+a.r, i+a.i);}};__device__ int julia(int x,int y){const float scale = 1.5;float jx = scale * (float)(width/2 - x)/(width/2);float jy = scale * (float)(height/2 - y)/(height/2);cuComplex c( -0.8, 0.156);cuComplex a(jx, jy);  //cuComplex a(x, y);int i = 0;for (i=0; i<200; i++) {a = a * a + c;if (a.magnitude2() > 1000)return 0;}return 1;}void reshap(int w,int h){glViewport(0, 0, (GLsizei) w, (GLsizei) h);glMatrixMode(GL_PROJECTION);glLoadIdentity();//gluOrtho2D(-(GLdouble) w/2, (GLdouble) w/2, (GLdouble) -h/2, (GLdouble) h/2);gluOrtho2D(0, (GLdouble) w, 0, (GLdouble) h);glMatrixMode(GL_MODELVIEW);glLoadIdentity();}void keyboard(unsigned char key,int x,int y){switch(key){case 27:exit(0);break;}}//__global__ void kernel1(float a[width*height][6])__global__ void kernel1(float *a){int i=blockIdx.x;int j=blockIdx.y;a[(i*width+j)*6]=julia(i,j);a[(i*width+j)*6+1]=0;a[(i*width+j)*6+2]=0;//前三为颜色a[(i*width+j)*6+3]=i;a[(i*width+j)*6+4]=j;a[(i*width+j)*6+5]=0;//后三为坐标}static void HandleError( cudaError_t err,                         const char *file,                         int line ) {    if (err != cudaSuccess) {        printf( "%s in %s at line %d\n", cudaGetErrorString( err ),                file, line );        exit( EXIT_FAILURE );    }}#define HANDLE_ERROR( err ) (HandleError( err, __FILE__, __LINE__ ))void display();int main(int argc,char** argv){clock_t start, end;start = clock();    float *cuda_vertex;//static float cuda_vertex[width*height][6]; //float cuda_index_list[width*height];HANDLE_ERROR(cudaMalloc((void**)&cuda_vertex,6*width*height*sizeof(float))); //HANDLE_ERROR(cudaMalloc((void**)&cuda_vertex,sizeof(cuda_vertex)));dim3 grid(width,height);kernel1<<<grid,1>>>(cuda_vertex);HANDLE_ERROR(cudaMemcpy(vertex_list,cuda_vertex,sizeof(float)*width*height*6,cudaMemcpyDeviceToHost));glutInit(&argc,argv);glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE);glutInitWindowSize(width,height);glutCreateWindow(argv[0]);init();glutReshapeFunc(reshap);glutDisplayFunc(display);glutKeyboardFunc(keyboard); HANDLE_ERROR(cudaFree(cuda_vertex));end = clock();cout<<"Run time: "<<(double)(end - start) / CLOCKS_PER_SEC<<"S"<<endl;    glutMainLoop();return 0;}void display(){glClear(GL_COLOR_BUFFER_BIT);glBegin(GL_POINTS);for(int i=0;i!=width;++i){for(int j=0;j!=height;++j){//glColor3f(1,0,0);             //glColor3f(vertex_list[i*width+6*j+0],0,0);               glColor3f(vertex_list[(i*width+j)*6+0],vertex_list[(i*width+j)*6+1],vertex_list[(i*width+j)*6+2]);               glVertex2i(vertex_list[(i*width+j)*6+3],vertex_list[(i*width+j)*6+4]);   //glVertex2i(i,j);}}glEnd();glutSwapBuffers();}



原创粉丝点击