main.cu
资源名称:CUDAseg.zip [点击查看]
上传用户:liuping58
上传日期:2022-06-05
资源大小:105k
文件大小:4k
源码类别:
3D图形编程
开发平台:
Visual C++
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <float.h>
- #include <GL/glew.h>
- #include <cuda_runtime.h>
- #include <cutil.h>
- #include <math.h>
- #include <GL/glut.h>
- #define ITERATIONS 1000
- #define THRESHOLD 150
- #define EPSILON 50
- #define BLOCKDIM_X 11
- #define BLOCKDIM_Y 11
- #define BLOCKDIM_Z 1
- char *volumeFilename = "brain.raw";
- char *maskFilename = "phi.raw";
- char *outputFilename= "output.raw";
- //cudaExtent volumeSize = make_cudaExtent(86, 86, 22);
- cudaExtent volumeSize = make_cudaExtent(181,217,181);
- //cudaExtent volumeSize = make_cudaExtent(10,10,10);
- float *phi, *D, *contour;
- char *path;
- size_t size, pitchbytes;
- unsigned char *input,*output;
- int imageW, imageH, imageD, N, pitch;
- float *d_phi, *d_phi1, *d_D;
- int its=0;
- unsigned int Timer = 0;
- unsigned int IterationTimer = 0;
- int i,j,k;
- __global__ void updatephi( float *d_phi, float *d_phi1, float *d_D, int imageW, int imageH, int imageD, int pitch);
- unsigned char* loadRawFile(char *filename, size_t size){
- FILE *fp = fopen(filename, "rb");
- if (!fp) {
- fprintf(stderr, "Error opening file '%s'n", filename);
- exit(EXIT_FAILURE);
- }
- unsigned char *data = (unsigned char *) malloc(size);
- size_t read = fread(data, 1, size, fp);
- fclose(fp);
- printf("Read '%s', %d bytesn", filename, read);
- return data;
- }
- float *loadMask(char *filename, size_t size){
- FILE *fp = fopen(filename, "rb");
- if (!fp) {
- fprintf(stderr, "Error opening file '%s'n", filename);
- exit(EXIT_FAILURE);
- }
- float *data = (float *) malloc(size*sizeof(float));
- size_t read = fread(data, 4, size, fp);
- fclose(fp);
- printf("Read '%s', %d elementsn", filename, read);
- return data;
- }
- void cuda_update(){
- dim3 dimGrid( ((imageW-1)/BLOCKDIM_X) + 1, ((imageH-1)/BLOCKDIM_Y) +1);
- dim3 dimBlock(BLOCKDIM_X, BLOCKDIM_Y, BLOCKDIM_Z);
- updatephi<<< dimGrid, dimBlock>>>(d_phi, d_phi1, d_D, imageW, imageH, imageD, pitch);
- d_phi1=d_phi;
- CUT_CHECK_ERROR("Kernel execution failedn");
- cudaThreadSynchronize();
- }
- int main(int argc, char** argv){
- size = volumeSize.width*volumeSize.height*volumeSize.depth;
- input = loadRawFile(volumeFilename, size);
- phi = loadMask(maskFilename, size);
- imageW=volumeSize.width;
- imageH=volumeSize.height;
- imageD=volumeSize.depth;
- N=imageW*imageH*imageD;
- if((D = (float *)malloc(imageW*imageH*imageD*sizeof(float)))==NULL)printf("ME_Dn");
- for(i=0;i<N;i++){
- D[i] = EPSILON - abs(input[i] - THRESHOLD);
- }
- // Set up CUDA Timer
- cutCreateTimer(&Timer);
- cutCreateTimer(&IterationTimer);
- cutStartTimer(Timer);
- // Allocate Memory on Device
- cudaMallocPitch((void**)&d_D, &pitchbytes, sizeof(float)*imageW, imageH*imageD);
- cudaMallocPitch((void**)&d_phi, &pitchbytes, sizeof(float)*imageW, imageH*imageD);
- cudaMallocPitch((void**)&d_phi1, &pitchbytes, sizeof(float)*imageW, imageH*imageD);
- pitch=pitchbytes/sizeof(float);
- // Copy Host Thresholding Data to Device Memory
- cudaMemcpy2D(d_D, pitchbytes, D, sizeof(float)*imageW, sizeof(float)*imageW, imageH*imageD, cudaMemcpyHostToDevice);
- cudaMemcpy2D(d_phi1, pitchbytes, phi, sizeof(float)*imageW, sizeof(float)*imageW, imageH*imageD, cudaMemcpyHostToDevice);
- for(its=0;its<ITERATIONS;its++){
- cuda_update();
- if(its%50==0){
- printf("Iteration %3d Total Time: %3.2f ReInit Time: %3.2fn", its, 0.001*cutGetTimerValue(Timer), 0.001*cutGetTimerValue(IterationTimer));}
- }
- if((output = (unsigned char *) malloc(imageW*imageH*imageD))==NULL)printf("ME_OUTPUTn");
- cudaMemcpy2D(phi, sizeof(float)*imageW, d_phi1, pitchbytes, sizeof(float)*imageW, imageH*imageD, cudaMemcpyDeviceToHost);
- for(i=0;i<N;i++){
- if(phi[i]>0){output[i]=0;} else { output[i]=255; }
- }
- FILE *fp = fopen(outputFilename, "wb");
- size_t write = fwrite(output, 1, size, fp);
- fclose(fp);
- printf("Write '%s', %d bytesn", outputFilename, write);
- }