cuda多重for循环中使用同步函数

之前写一道题将多重for循环改编成并行,由于没有使用同步函数,导致CPU端主线程和GPU端的数据传输出现了问题,下面是最终成功的样例代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include <cmath>
#include <cuda_runtime.h>
#include <device_launch_parameters.h>
#define G			9.8f		//gravitational constant 重力
#define dt			0.01f		//time step 时间片的具体时间
#define SOFTENING	2.0f		//softening parameter to help with numerical instability 变量(空气的影响参数?

struct nbody {
   
	float x, y, vx, vy, m;//m=质量
};

typedef struct nbody nbody;

//void print_help();
void step(void);
void d_step(void);
__global__  void d_step3(int* k, float* x_total, float* y_total,const nbody*input_data);
int N = 1000; // Number of Nbody
int D = 3; // dimension  尺寸?体积?
int Iter = 1000; // iteration number 迭代数
nbody* input_data;
float* den_arr; //dencity array 密度 
__device__ float d_x_total = 0;
__device__ float d_y_total = 0;


int main() {
   
	input_data = (nbody*)malloc(sizeof(nbody) * N);
	den_arr = (float*)malloc(sizeof(float) * D * D);
	if (!input_data || !den_arr) {
   
		printf("malloc failed!\n");
		exit(0);
	}
	for (int i = 0; i < N; i++) {
   
		input_data[i].m = 1.0 / N;
		input_data[i].x = (float)rand() / (float)RAND_MAX;
		input_data[i].y = (float)rand() / (float)RAND_MAX;
		input_data[i].vx = 0;
		input_data[i].vy = 0;
	}


	unsigned long long start1, end1;
	//这里是global代码:
	//创建新的device数组保存用于计算:
	start1 = clock();
	//进行device端计算:
	d_step();

	end1 = clock();

	//释放device内存数据


	printf("t2= %lf\n", (double)(end1 - start1) / CLOCKS_PER_SEC);
	
	//host端代码:
	unsigned long long start, end;
	start = clock();
	//step();
	end = clock();
	printf("t1 = %lf\n", (double)(end - start) / CLOCKS_PER_SEC);
	int
CUDA是一种并行计算框架,可以利用GPU进行高效的并行计算。在CUDA中,可以使用CUDA C/C++编程语言来编写CUDA程序。 在CUDA程序中,可以使用for循环来对数据进行迭代计算,并且可以使用CUDA提供的并行计算机制来加速计算过程。通常,使用CUDA进行并行计算的步骤如下: 1. 将数据从主机内存(CPU)传输到设备内存(GPU)。 2. 在GPU上启动一个或多个线程块,每个线程块包含多个线程。 3. 在每个线程中执行计算操作。 4. 将计算结果从设备内存传输回主机内存。 下面是一个使用CUDA进行for循环的示例代码: ```cuda #include <stdio.h> #define N 1000 __global__ void add(int *a, int *b, int *c) { int tid = blockIdx.x * blockDim.x + threadIdx.x; if (tid < N) { c[tid] = a[tid] + b[tid]; } } int main() { int a[N], b[N], c[N]; int *dev_a, *dev_b, *dev_c; cudaMalloc((void**)&dev_a, N * sizeof(int)); cudaMalloc((void**)&dev_b, N * sizeof(int)); cudaMalloc((void**)&dev_c, N * sizeof(int)); for (int i = 0; i < N; i++) { a[i] = i; b[i] = i * i; } cudaMemcpy(dev_a, a, N * sizeof(int), cudaMemcpyHostToDevice); cudaMemcpy(dev_b, b, N * sizeof(int), cudaMemcpyHostToDevice); int threadsPerBlock = 256; int blocksPerGrid = (N + threadsPerBlock - 1) / threadsPerBlock; add<<<blocksPerGrid, threadsPerBlock>>>(dev_a, dev_b, dev_c); cudaMemcpy(c, dev_c, N * sizeof(int), cudaMemcpyDeviceToHost); for (int i = 0; i < N; i++) { printf("%d + %d = %d\n", a[i], b[i], c[i]); } cudaFree(dev_a); cudaFree(dev_b); cudaFree(dev_c); return 0; } ``` 在这个示例代码中,我们定义了一个数组a和b,并且使用for循环对数组a和b进行初始化。然后,我们将数组a和b从主机内存拷贝到设备内存中。 接着,我们定义了一个CUDA核函数add,这个函数接受三个参数:dev_a、dev_b和dev_c。在add函数中,我们使用blockIdx.x和threadIdx.x来计算当前线程的ID,然后使用这个ID来计算数组c的值。 最后,我们在主函数调用了add函数,并且将计算结果从设备内存拷贝回主机内存。最后,我们使用for循环来输出数组c的值。 CUDA中的for循环和普通的C/C++ for循环非常类似,唯一的区别是使用CUDA提供的并行计算机制来加速计算过程。需要注意的是,在使用CUDA进行并行计算时,需要特别注意线程之间的同步和数据传输的开销。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值