- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下两个几乎相同的示例代码。 code1.cu 使用 cudaMalloc
和 cudaMemcpy
处理设备/主机变量值交换。
code2.cu使用cudaMallocManaged
,因此不需要cudaMemcpy
。当使用 cudaMallocManaged 时,我必须包含 cudaDeviceSynchronize()
才能获得正确的结果,而对于使用 cudaMalloc 的结果,则不需要这样做。我希望能得到一些关于为什么会发生这种情况的提示
code2.cu
#include <iostream>
#include <math.h>
#include <vector>
//
using namespace std;
// Kernel function to do nested loops
__global__
void add(int max_x, int max_y, float *tot, float *x, float *y)
{
int i = blockIdx.x*blockDim.x + threadIdx.x;
int j = blockIdx.y*blockDim.y + threadIdx.y;
if(i < max_x && j<max_y) {
atomicAdd(tot, x[i] + y[j]);
}
}
int main(void)
{
int Nx = 1<<15;
int Ny = 1<<15;
float *d_x = NULL, *d_y = NULL;
float *d_tot = NULL;
cudaMalloc((void **)&d_x, sizeof(float)*Nx);
cudaMalloc((void **)&d_y, sizeof(float)*Ny);
cudaMallocManaged((void **)&d_tot, sizeof(float));
// Allocate Unified Memory – accessible from CPU or GPU
vector<float> vx;
vector<float> vy;
// initialize x and y arrays on the host
for (int i = 0; i < Nx; i++)
vx.push_back(i);
for (int i = 0; i < Ny; i++)
vy.push_back(i*10);
//
float tot = 0;
for(int i = 0; i<vx.size(); i++)
for(int j = 0; j<vy.size(); j++)
tot += vx[i] + vy[j];
cout<<"CPU: tot: "<<tot<<endl;
//
cudaMemcpy(d_x, vx.data(), vx.size()*sizeof(float), cudaMemcpyHostToDevice);
cudaMemcpy(d_y, vy.data(), vy.size()*sizeof(float), cudaMemcpyHostToDevice);
//
int blockSize; // The launch configurator returned block size
int minGridSize; // The minimum grid size needed to achieve the
cudaOccupancyMaxPotentialBlockSize( &minGridSize, &blockSize, add, 0, Nx+Ny);
//.. bx*by can not go beyond the blockSize, or hardware limit, which is 1024;
//.. bx*bx = blockSize && bx/by=Nx/Ny, solve the equation
int bx = sqrt(blockSize*Nx/(float)Ny);
int by = bx*Ny/(float)Nx;
dim3 blockSize_3D(bx, by);
dim3 gridSize_3D((Nx+bx-1)/bx, (Ny+by+1)/by);
cout<<"blockSize: "<<blockSize<<endl;
cout<<"bx: "<<bx<<" by: "<<by<<" gx: "<<gridSize_3D.x<<" gy: "<<gridSize_3D.y<<endl;
// calculate theoretical occupancy
int maxActiveBlocks;
cudaOccupancyMaxActiveBlocksPerMultiprocessor( &maxActiveBlocks, add, blockSize, 0);
int device;
cudaDeviceProp props;
cudaGetDevice(&device);
cudaGetDeviceProperties(&props, device);
float occupancy = (maxActiveBlocks * blockSize / props.warpSize) /
(float)(props.maxThreadsPerMultiProcessor /
props.warpSize);
printf("Launched blocks of size %d. Theoretical occupancy: %f\n",
blockSize, occupancy);
// Run kernel on 1M elements on the GPU
tot = 0;
add<<<gridSize_3D, blockSize_3D>>>(Nx, Ny, d_tot, d_x, d_y);
// Wait for GPU to finish before accessing on host
//cudaDeviceSynchronize();
tot =*d_tot;
//
//
cout<<" GPU: tot: "<<tot<<endl;
// Free memory
cudaFree(d_x);
cudaFree(d_y);
cudaFree(d_tot);
return 0;
}
code1.cu
#include <iostream>
#include <math.h>
#include <vector>
//
using namespace std;
// Kernel function to do nested loops
__global__
void add(int max_x, int max_y, float *tot, float *x, float *y)
{
int i = blockIdx.x*blockDim.x + threadIdx.x;
int j = blockIdx.y*blockDim.y + threadIdx.y;
if(i < max_x && j<max_y) {
atomicAdd(tot, x[i] + y[j]);
}
}
int main(void)
{
int Nx = 1<<15;
int Ny = 1<<15;
float *d_x = NULL, *d_y = NULL;
float *d_tot = NULL;
cudaMalloc((void **)&d_x, sizeof(float)*Nx);
cudaMalloc((void **)&d_y, sizeof(float)*Ny);
cudaMalloc((void **)&d_tot, sizeof(float));
// Allocate Unified Memory – accessible from CPU or GPU
vector<float> vx;
vector<float> vy;
// initialize x and y arrays on the host
for (int i = 0; i < Nx; i++)
vx.push_back(i);
for (int i = 0; i < Ny; i++)
vy.push_back(i*10);
//
float tot = 0;
for(int i = 0; i<vx.size(); i++)
for(int j = 0; j<vy.size(); j++)
tot += vx[i] + vy[j];
cout<<"CPU: tot: "<<tot<<endl;
//
cudaMemcpy(d_x, vx.data(), vx.size()*sizeof(float), cudaMemcpyHostToDevice);
cudaMemcpy(d_y, vy.data(), vy.size()*sizeof(float), cudaMemcpyHostToDevice);
//
int blockSize; // The launch configurator returned block size
int minGridSize; // The minimum grid size needed to achieve the
cudaOccupancyMaxPotentialBlockSize( &minGridSize, &blockSize, add, 0, Nx+Ny);
//.. bx*by can not go beyond the blockSize, or hardware limit, which is 1024;
//.. bx*bx = blockSize && bx/by=Nx/Ny, solve the equation
int bx = sqrt(blockSize*Nx/(float)Ny);
int by = bx*Ny/(float)Nx;
dim3 blockSize_3D(bx, by);
dim3 gridSize_3D((Nx+bx-1)/bx, (Ny+by+1)/by);
cout<<"blockSize: "<<blockSize<<endl;
cout<<"bx: "<<bx<<" by: "<<by<<" gx: "<<gridSize_3D.x<<" gy: "<<gridSize_3D.y<<endl;
// calculate theoretical occupancy
int maxActiveBlocks;
cudaOccupancyMaxActiveBlocksPerMultiprocessor( &maxActiveBlocks, add, blockSize, 0);
int device;
cudaDeviceProp props;
cudaGetDevice(&device);
cudaGetDeviceProperties(&props, device);
float occupancy = (maxActiveBlocks * blockSize / props.warpSize) /
(float)(props.maxThreadsPerMultiProcessor /
props.warpSize);
printf("Launched blocks of size %d. Theoretical occupancy: %f\n",
blockSize, occupancy);
// Run kernel on 1M elements on the GPU
tot = 0;
add<<<gridSize_3D, blockSize_3D>>>(Nx, Ny, d_tot, d_x, d_y);
// Wait for GPU to finish before accessing on host
//cudaDeviceSynchronize();
//
cudaMemcpy(&tot, d_tot, sizeof(float), cudaMemcpyDeviceToHost);
//
cout<<" GPU: tot: "<<tot<<endl;
// Free memory
cudaFree(d_x);
cudaFree(d_y);
cudaFree(d_tot);
return 0;
}
//Code2.cu has the following output:
//
//CPU: tot: 8.79609e+12
//blockSize: 1024
//bx: 32 by: 32 gx: 1024 gy: 1025
//Launched blocks of size 1024. Theoretical occupancy: 1.000000
//GPU: tot: 0
删除cudaDeviceSynchronize()
上的注释后,
GPU: tot: 8.79609e+12
最佳答案
CUDA 内核启动是异步的。这意味着它们的执行独立于启动它们的 CPU 线程。
由于这种异步启动,当您的 CPU 线程代码开始测试结果时,不能保证 CUDA 内核已完成(甚至启动)。
因此需要等待GPU内核完成,cudaDeviceSynchronize()
正是这样做的。 cudaMemcpy
还具有同步效果,因此当您删除cudaMemcpy
时操作,您会失去同步,但是 cudaDeviceSynchronize()
恢复它。
关于cudaMallocManaged 和 cudaDeviceSynchronize(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58695262/
我遇到了一个非常奇怪的错误,在运行特定大小的 Heat 2D 模拟时出现“非法内存访问”错误,但如果我运行完全相同的模拟,模拟运行良好,只是元素更少。 是否有增加数组大小会导致此异常的原因?我使用的是
我是 CUDA 的新手,对 cudaEvent 有点困惑。我现在有一个代码示例,如下所示: float elapsedTime; cudaEvent_t start, stop; CUDA_ERR_C
我有以下两个几乎相同的示例代码。 code1.cu 使用 cudaMalloc 和 cudaMemcpy 处理设备/主机变量值交换。 code2.cu使用cudaMallocManaged,因此不需要
什么时候真正需要调用cudaDeviceSynchronize函数? 据我从 CUDA 文档中了解到,CUDA 内核是异步的,因此我们似乎应该在每次内核启动后调用 cudaDeviceSynchron
我正在具有统一内存的 TegraK1 板上实现共轭梯度求解器。我面临的问题是在循环中我必须执行 cudaDeviceSynchronize();两次更新变量,与 TI Keystone-II 相比,这
我最近发现了 cuda-memcheck 的 racecheck 工具,可用于 CUDA 5.0(cuda-memcheck --tool racecheck,请参阅 NVIDIA doc)。该工具可
我在使用并发 CUDA 时遇到了一些问题。看看附图。内核在标记点启动,即 0.395 秒。然后是一些绿色的 CpuWork。最后,调用 cudaDeviceSynchronize。在 CpuWork
我在使用并发 CUDA 时遇到了一些问题。看看附图。内核在标记点启动,即 0.395 秒。然后是一些绿色的 CpuWork。最后,调用 cudaDeviceSynchronize。在 CpuWork
CUDA 版本 10.1。帕斯卡GPU。所有命令都发布到默认流: void * ptr; cudaMalloc(&ptr, ...); launch_kernel>>(ptr); cudaDevice
我用 CUDA 6.5 和 4 x GPU Kepler . 我使用多线程、CUDA 运行时 API 并从不同的 CPU 线程访问 CUDA 上下文(通过使用 OpenMP - 但它并不重要)。 当我
这三个函数有什么区别,尤其是最后两个?图书馆手册说 Note that this function is deprecated because its name does not reflect it
非常感谢您阅读我的帖子。 我正在做 CUDA 工作,但一直收到 cudaDeviceSynchronize() 错误代码 77:cudaErrorIllegalAddress,不知道为什么。我搜索了代
__global__ void helloCUDA(float f) { printf("Hello thread %d, f=%f\n", threadIdx.x, f); } int ma
我是一名优秀的程序员,十分优秀!