0
我在Cuda C中做了一個分形,並且我已經爲1024 * 1024的圖像做了我的程序,但我想要2048 * 2048的更大圖像,我有關於圖像映射如何幫助我的問題附件我的兩個代碼1024 * 1024和我是什麼要做如何映射2048 * 2048的圖像?
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <cuda.h>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ctime>
#define MAX_ITER 5000
#define N 1024
#define BLOCKS 32
#define THREAD 1
using namespace cv;
using namespace std;
__global__ void mul(unsigned char *imagen){
int i=blockIdx.y*gridDim.x+blockIdx.x;
int j=threadIdx.y*blockDim.x+threadIdx.x;
double x,y,a,b,xnew,ynew,sq;
double iter;
iter=0;
x=0;
y=0;
a=((3.0/(N))*j-2);
b=((2.0/(N))*i-1);
sq=abs(sqrt(pow(x,2)+pow(y,2)));
while((sq<2)&&(iter<MAX_ITER))
{
xnew=((x*x)-(y*y))+a;
ynew=(2*x*y)+b;
x=xnew;
y=ynew;
sq=abs(sqrt(pow(x,2)+pow(y,2)));
iter=iter+1;
}
if(iter==MAX_ITER)
{
imagen[i*(N)+j]=255;
}
else
{
imagen[i*(N)+j]=0;
}
}
int main(){
dim3 bloques (32,32);
dim3 threads(32,32);
unsigned char *matriz_a;
unsigned char *matriz_dev_a;
matriz_a = (unsigned char *)malloc(sizeof(unsigned char) * N*N);
cudaMalloc((void **)&matriz_dev_a, N*N*sizeof(unsigned char));
cudaMemcpy(matriz_dev_a, matriz_a, sizeof(unsigned char) *N*N, cudaMemcpyHostToDevice);
/**************************************************************/
mul<<<bloques, threads>>>(matriz_dev_a);
cudaMemcpy(matriz_a, matriz_dev_a, sizeof(unsigned char) *N*N, cudaMemcpyDeviceToHost);
/**************************************************************************/
/************************************************************************/
/***********************************************************************/
const cv::Mat img(cv::Size(N, N), CV_8U, matriz_a);
cv::namedWindow("foobar");
cv::imshow("foobar", img);
cv::waitKey(0);
free(matriz_a);
cudaFree(matriz_dev_a);
}
好做映射只改變了幾行例如
#define N 2048
dim3 bloques (45,45);
mul<<<bloques, 1>>>(matriz_dev_a);
想想發送每塊線程的,但在運行時不沒有做任何事情會花費我一點時間來考慮映射的可能性。 對不起,我的英語 晚上好,我希望無論如何說謝謝
噢謝謝你,我想我要學習更多,我會讓它工作 謝謝! –