目录

共享内存和线程同步计算
功能:对于长度为10的数组,用10个线程同步计算当前元素之前所有元素的平均值。
#include <stdio.h>
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
__global__ void gpu_shared_memory(float *d_a)
{
// Defining local variables which are private to each thread
int i, index = threadIdx.x;
float average, sum = 0.0f;
//Define shared memory
__shared__ float sh_arr[10];
sh_arr[index] = d_a[index];
__syncthreads()<

本文记录了一次CUDA编程经验,利用共享内存和线程同步计算数组中前i个元素的平均值。在GPU上,10个线程同步执行,每个线程在完成相应次数的循环后执行求均值操作,确保了计算的正确性。
最低0.47元/天 解锁文章
1692

被折叠的 条评论
为什么被折叠?



