gpt4 book ai didi

c++ - 如何映射2048 * 2048的图像?

转载 作者:行者123 更新时间:2023-11-27 22:44:40 25 4
gpt4 key购买 nike

我正在 Cuda C 中做分形,我已经为 1024 * 1024 的图像编写了我的程序,但我想要 2048 * 2048 的更大图像,我有关于图像映射如何帮助我的问题附件我的两个编码 1024 * 1024 和我想做什么

#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);

考虑在每个 block 中发送一个线程,但是当运行时不执行任何操作时,我需要花费一些时间来考虑映射可能是什么样子。对不起我的英语不好晚上好,无论如何我都想说声谢谢

最佳答案

当前代码有两个问题。

  1. 代码不可扩展,因为 block 数是固定的。
  2. 内核中的索引不正确。全局索引 j 不会随着 block 数的变化而缩放。

问题可以通过以下方式解决:

使 block 数动态变化,即取决于输入数据大小:

dim3 threads(32,32);
dim3 bloques;
bloques.x = (N + threads.x - 1)/threads.x;
bloques.y = (N + threads.y - 1)/threads.y;

标准化内核内部的索引:

int i= blockIdx.y * blockDim.y + threadIdx.y;
int j= blockIdx.x * blockDim.x + threadIdx.x;

修改后的代码在分形尺寸 2048 x 2048 上运行良好。

关于c++ - 如何映射2048 * 2048的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44580324/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com