私は、cuda by example book で与えられた非常に単純なコードを書いていました。これは、グラフィックを作成する cuda openGL 相互運用です。プログラムは正常にビルドされていますが、プログラムを実行していると、ウィンドウに次のように表示されます。アプリケーションを正しく起動できませんでした。[OK] をクリックして、アプリケーションを閉じます。openglサンプルプログラムと同様に正常に実行されているサンプルcudaプログラムがほとんどないため、なぜそれが発生するのかわかりません。正常に実行されている NVidia サンプル プログラムからサンプルの cuda openGL 相互運用プログラムを実行することもできます。ここで、すべての lib ファイルを追加のライブラリに含めたこと、および追加のインクルード ディレクトリに含まれているファイルを含めたことに言及する必要があります。これは、相互運用に使用しているピクセル バッファーが原因で、通常の openGL と cuda プログラムが正常に実行されているために発生していると考えられます。また、Visual Studio intellisense がバッファー API (glGenBuffers など) をプログラムに含めようとしているときにそれらを表示していることにも言及する必要がありますが、プログラムで宣言した後、識別子が未定義であり、その下に赤い線が表示されています。 . しかし、これは NVidia のサンプル OpenGL コードでは発生していません。
以下のコードを貼り付けています:
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <GL/glew.h>
#include <GL/glut.h>
#include <GL/glaux.h>
#include <cuda.h>
#include <cuda_runtime.h>
#include <cutil.h>
#include <cuda_gl_interop.h>
#define DIM 512
GLuint bufferObj;
cudaGraphicsResource *resource;
// based on ripple code, but uses uchar4 which is the type of data
// graphic inter op uses. see screenshot - basic2.png
__global__ void kernel( uchar4 *ptr ) {
int x = threadIdx.x + blockIdx.x * blockDim.x;
int y = threadIdx.y + blockIdx.y * blockDim.y;
int offset = x + y * blockDim.x * gridDim.x;
// now calculate the value at that position
float fx = x/(float)DIM - 0.5f;
float fy = y/(float)DIM - 0.5f;
unsigned char green = 128 + 127 *
sin( abs(fx*100) - abs(fy*100) );
// accessing uchar4 vs unsigned char*
ptr[offset].x = 0;
ptr[offset].y = green;
ptr[offset].z = 0;
ptr[offset].w = 255;
}
static void key_func( unsigned char key, int x, int y ) {
switch (key) {
case 27:
// clean up OpenGL and CUDA
cudaGraphicsUnregisterResource( resource );
glBindBuffer( GL_PIXEL_UNPACK_BUFFER_ARB, 0 );
glDeleteBuffers( 1, &bufferObj );
exit(0);
}
}
static void draw_func( void ) {
glDrawPixels( DIM, DIM, GL_RGBA, GL_UNSIGNED_BYTE, 0 );
glutSwapBuffers();
}
int main( int argc, char **argv ) {
cudaDeviceProp prop;
int dev;
memset( &prop, 0, sizeof( cudaDeviceProp ) );
prop.major = 1;
prop.minor = 0;
cudaChooseDevice( &dev, &prop );
cudaGLSetGLDevice( dev ) ;
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGBA );
glutInitWindowSize( DIM, DIM );
glutCreateWindow( "bitmap" );
glGenBuffers( 1, &bufferObj );
glBindBuffer( GL_PIXEL_UNPACK_BUFFER_ARB, bufferObj );
glBufferData( GL_PIXEL_UNPACK_BUFFER_ARB, DIM * DIM * 4,NULL, GL_DYNAMIC_DRAW_ARB);
cudaGraphicsGLRegisterBuffer( &resource,
bufferObj,
cudaGraphicsMapFlagsNone ) ;
// do work with the memory dst being on the GPU, gotten via mapping
cudaGraphicsMapResources( 1, &resource, NULL ) ;
uchar4* devPtr;
size_t size;
cudaGraphicsResourceGetMappedPointer( (void**)&devPtr,
&size,
resource) ;
dim3 grids(DIM/16,DIM/16);
dim3 threads(16,16);
kernel<<<grids,threads>>>( devPtr );
cudaGraphicsUnmapResources( 1, &resource, NULL ) ;
glutKeyboardFunc( key_func );
glutDisplayFunc( draw_func );
glutMainLoop();
}