Untitled

 avatar
unknown
c_cpp
2 years ago
1.8 kB
4
Indexable
int main( void ) {


    FILE *fptr;
    fptr = fopen("programGPU.txt","a");


    cudaEvent_t start,stop;
    cudaEventCreate(&start);
    cudaEventCreate(&stop);
    cudaEventRecord(start,0);
    
    DataBlock data;
    unsigned char *bitmap = (unsigned char*)malloc(DIM*DIM*4*sizeof(unsigned char));
    int image_size = DIM*DIM*4;
    unsigned char *dev_bitmap;

    cudaMalloc((void**)&dev_bitmap,image_size);
    data.dev_bitmap=dev_bitmap;
    Sphere *s;
    

    // allocate temp memory for the Sphere dataset on CPU
    cudaMalloc((void**)&s,sizeof(Sphere)*SPHERES);

    Sphere *temp_s = (Sphere*)malloc( sizeof(Sphere) * SPHERES );

    // initialize the Sphere dataset
    for (int i=0; i<SPHERES; i++) {
        temp_s[i].r = rnd( 1.0f );
        temp_s[i].g = rnd( 1.0f );
        temp_s[i].b = rnd( 1.0f );
        temp_s[i].x = rnd( 1000.0f ) - 500;
        temp_s[i].y = rnd( 1000.0f ) - 500;
        temp_s[i].z = rnd( 1000.0f ) - 500;
        temp_s[i].radius = rnd( 100.0f ) + 20;
    }
    cudaMemcpy(s,temp_s,sizeof(Sphere) * SPHERES, cudaMemcpyHostToDevice);
    // free CPU memory
    free( temp_s );
    // generate a bitmap from our sphere data
    dim3    grids(DIM/16,DIM/16);
    dim3    threads(16,16);
    kernel<<<grids,threads>>>( s, dev_bitmap );

    cudaMemcpy( bitmap, dev_bitmap,image_size,cudaMemcpyDeviceToHost );

    cudaEventRecord( stop, 0 );
    cudaEventSynchronize( stop );
    float elapsedTime;
    cudaEventElapsedTime( &elapsedTime,start, stop );
    printf( "Time to generate:  %3.1f ms\n", elapsedTime );
    cudaEventDestroy( start );
    cudaEventDestroy( stop );

    cudaFree( dev_bitmap );
    cudaFree( s );
    save_to_file(bitmap);

    fprintf(fptr,"%d %f\n",DIM,elapsedTime);
    fclose(fptr);
}
Editor is loading...