只是简单的把代码改写,没有优化,不知道问题出在哪里,请看一下谢谢了
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<sys/time.h>
#include"cutil_inline.h"
#define x 0.353553390
typedef double DCT_type;
typedef struct{
int width;
int height;
DCT_type*elements;
}Matrix;
global void DCT_kernel(Matrix A){
//int i;
int i=0;
i=blockIdx.x;//在这是编译器总是指出错误 error:expected a member name
DCT_type a,b,c,d,e,f;
a=1.387039842;
b=1.306562962;
c=1.175875600;
d=0.785694957;
e=0.541196099;
f=0.275899378;
DCT_type*array;
array=A.elements;
DCT_type temp0,temp1,temp2,temp3,temp4,temp5,temp6,temp7;
temp0=(array[i*8+0]+array[i*8+7])*x;
temp2=(array[i*8+1]+array[i*8+6])*x;
temp4=(array[i*8+2]+array[i*8+5])*x;
temp6=(array[i*8+3]+array[i*8+4])*x;
temp1=(array[i*8+0]-array[i*8+7])*x;
temp3=(array[i*8+6]-array[i*8+1])*x;
temp5=(array[i*8+2]-array[i*8+5])*x;
temp7=(array[i*8+4]-array[i*8+3])*x;
array[i*8+0]=temp0+temp2+temp4+temp6;
array[i*8+2]=btemp0+etemp2-etemp4-btemp6;
array[i*8+4]=temp0-temp2-temp4+temp6;
array[i*8+6]=etemp0-btemp2+btemp4-etemp6;
array[i*8+1]=atemp1-ctemp3+dtemp5-ftemp7;
array[i*8+3]=ctemp1+ftemp3-atemp5+dtemp7;
array[i*8+5]=dtemp1+atemp3+ftemp5-ctemp7;
array[i*8+7]=ftemp1+dtemp3+ctemp5+atemp7;
__syncthreads();
temp0=(array[08+i]+array[78+i])x;
temp2=(array[18+i]+array[68+i])x;
temp4=(array[28+i]+array[58+i])x;
temp6=(array[38+i]+array[4*8+i])*x;
temp1=(array[08+i]-array[78+i])x;
temp3=(array[68+i]-array[18+i])x;
temp5=(array[28+i]-array[58+i])x;
temp7=(array[48+i]-array[3*8+i])*x;
array[0*8+i]=temp0+temp2+temp4+temp6;
array[28+i]=btemp0+etemp2-etemp4-b*temp6;
array[48+i]=temp0-temp2-temp4+temp6;
array[68+i]=etemp0-btemp2+btemp4-etemp6;
array[18+i]=atemp1-ctemp3+dtemp5-f*temp7;
array[38+i]=ctemp1+ftemp3-atemp5+dtemp7;
array[58+i]=dtemp1+atemp3+ftemp5-ctemp7;
array[78+i]=ftemp1+dtemp3+ctemp5+a*temp7;
__syncthreads();
}
main(){
int i,j,nblocks,nthreads;
nblocks=8;
nthreads=1;
size_t size;
Matrix A;
A.width=8;
A.height=8;
size=A.widthA.heightsizeof(DCT_type);
A.elements=(DCT_type*)malloc(size);
for(i=0;i<A.height;i++){
for(j=0;j<A.width;j++)
A.elements[i*A.width+j]=1;
}
//Initialize Matrix d_A,d_B,d_C
Matrix d_A;
d_A.width=A.width;
d_A.height=A.height;
size=A.widthA.heightsizeof(DCT_type);
cudaMalloc((void**)&d_A.elements,size);
cudaMemcpy(d_A.elements,A.elements,size,cudaMemcpyHostToDevice);
//Invoke kernel
//dim3 dimBlock(8,8);
//dim3 dimGrid(8,8);
DCT_kernel<<<nblocks,nthreads>>>(d_A);
//Read A from device memory
cudaMemcpy(A.elements,d_A.elements,size,cudaMemcpyDeviceToHost);
for(i=0;i<A.width*A.height;i++){
if(i%8==0)
printf(“\n”);
printf("%f ",A.elements[i]);
}
//Free device memory
cudaFree(d_A.elements);
}[/i]