cuda统计直方图
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
typedef struct
{
BYTE r;
BYTE b;
BYTE g;
}RGB;
__global__ void func(BYTE *gray, RGB **img, int width)
{
int i = threadIdx.x;
for (int j = 0; j < width; j++)
{
gray[(img[i][j].r * 38 + img[i][j].g * 75 + img[i][j].b * 15) >> 7]++;
}
}
int main()
{
BITMAPFILEHEADER fileHeader;
BITMAPINFOHEADER infoHeader;
FILE* pfin;
BYTE gray[256] = { 0 };
RGB img[256][256] = { 0 };
cudaError_t cudaStatus;
pfin = fopen("1.bmp", "rb");
//ReadtheBitmapfileheader;
fread(&fileHeader, sizeof(BITMAPFILEHEADER), 1, pfin);
//ReadtheBitmapinfoheader;
fread(&infoHeader, sizeof(BITMAPINFOHEADER), 1, pfin);
//the size of the picture
int size = infoHeader.biWidth*infoHeader.biHeight;
//read the file
fread(img, sizeof(RGB), size, pfin);
int width = infoHeader.biWidth;
BYTE *dev_gray;
RGB **dev_img;
cudaStatus=cudaMalloc((void **)&dev_gray,width*sizeof(BYTE));
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaMalloc failed!");
}
cudaStatus=cudaMalloc((void **)&dev_img,sizeof(img[256][256]));
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaMalloc failed!");
}
cudaMemcpy(dev_gray, gray, width*sizeof(BYTE), cudaMemcpyHostToDevice);
cudaMemcpy(dev_img, img,sizeof(img[256][256]),cudaMemcpyHostToDevice);
func<<<1,width>>>(dev_gray,dev_img, width);
cudaMemcpy(gray, dev_gray, width*sizeof(BYTE),cudaMemcpyDeviceToHost);
cudaFree(dev_gray);
cudaFree(dev_img);
getchar();
return 0;
}
这是我代码 为什么最后结果gray数组里面什么都没有啊