串行代码片段如下所示:
int i, j;
for(j=0; j<ny; j++)
{
for(i=0; i<nx; i++)
{
x[i + j*nx] *= y[i];
}
}
Run Code Online (Sandbox Code Playgroud)
我使用这个内核将其转换为CUDA:
int tid = blockIdx.x * blockDim.x + threadIdx.x;
int i,j;
for(tid = 0; tid <nx*ny; tid++)
{
j = tid/nx;
i = tid - j*nx;
x[tid] *= y[i];
}
Run Code Online (Sandbox Code Playgroud)
但是GPU内核没有提供任何加速改进?关于更好解决方案的任何建议?提前致谢
我们有以下串行C代码在运行
两个向量a []和b []:
double a[20000],b[20000],r=0.9;
for(int i=1;i<=10000;++i)
{
a[i]=r*a[i]+(1-r)*b[i]];
errors=max(errors,fabs(a[i]-b[i]);
b[i]=a[i];
}
Run Code Online (Sandbox Code Playgroud)
请告诉我们如何将此代码移植到CUDA和Cublas?
所以我正在尝试研究如何在vb.net中使用我的GPU进行处理.我发现了一个看起来很棒的c#教程(对于c#用户来说看起来很简单).无论如何,我对c#的了解非常糟糕,我无法将事情从c#转移到vb.net.
这是链接:
http://www.codeproject.com/Articles/202792/Using-Cudafy-for-GPGPU-Programming-in-NET
如果有人可以将其转换为vb.net代码(这是有道理的)那么那就太好了.否则,如果有人可以给我一个非常简单的vb.net示例,基于本教程(或其他任何东西)在GPU上运行矢量操作,那么这可能会更好!:d
我正在寻找一种在CUDA内核中传递多个重复参数的优雅方法,
众所周知,每个内核参数都位于每个CUDA线程的堆栈中,因此,内核传递给每个线程的参数之间可能存在重复,每个线程都位于每个堆栈上.
为了最大限度地减少传递的重复参数的数量,我正在寻找一种优雅的方式.
为了解释我的担忧:假设我的代码如下:
kernelFunction<<<gridSize,blockSize>>>(UINT imageWidth, UINT imageWidth, UINT imageStride, UINT numberOfElements,x,y,ect...)
Run Code Online (Sandbox Code Playgroud)
UINT imageWidth,UINT imageWidth,UINT imageStride,UINT numberOfElements参数位于每个线程库存中,
我正在寻找一个技巧来发送更少的参数并从其他来源访问数据.
我正在考虑使用常量内存,但由于常量内存位于全局,因此我将其删除.不用说内存位置应该很快.
任何帮助,将不胜感激.
我正在学习CUDA.今天,我在书中尝试了一些代码:CUDA Application Design And Development这让我感到惊讶.为什么CUDA推力如此之慢?这是代码和输出.
#include <iostream>
using namespace std;
#include<thrust/reduce.h>
#include<thrust/sequence.h>
#include<thrust/host_vector.h>
#include<thrust/device_vector.h>
#include <device_launch_parameters.h>
#include "GpuTimer.h"
__global__ void fillKernel(int *a, int n)
{
int tid = blockDim.x * blockIdx.x + threadIdx.x;
if(tid <n) a[tid] = tid;
}
void fill(int *d_a, int n)
{
int nThreadsPerBlock = 512;
int nBlock = n/nThreadsPerBlock + ((n/nThreadsPerBlock)?1:0);
fillKernel<<<nBlock, nThreadsPerBlock>>>(d_a, n);
}
int main()
{
const int N = 500000;
GpuTimer timer1, timer2;
thrust::device_vector<int> a(N);
fill(thrust::raw_pointer_cast(&a[0]), N);
timer1.Start();
int sumA = …Run Code Online (Sandbox Code Playgroud) 我想逐渐计算整个内核执行的线程执行次数.是否有本地计数器或有没有其他方法可以做到这一点?我知道保留一个全局变量并且每个线程的增量都不会起作用,因为全局内存中的变量不保证线程的同步访问.
我尝试使用 CUDA 和 Qt 来模糊图像。我使用 NPP 库,nppiFilterGauss_8u_C1R 效果很好
void cuda_npp_gauss_filter_qt(uchar* pSourceData, uchar* pResultData, const int &ImageLineStep, const int &ImageWidth, const int &ImageHeight)
{
NppiSize oSizeROI;
oSizeROI.width = ImageWidth;
oSizeROI.height = ImageHeight;
Npp32s SourceStep = ImageLineStep;
Npp32s DestinationStep = ImageLineStep;
size_t AllocationSizeInBytes = ImageLineStep * ImageHeight;
Npp8u *pSource, *pDestination;
cudaMalloc<Npp8u>(&pSource,AllocationSizeInBytes);
cudaMalloc<Npp8u>(&pDestination,AllocationSizeInBytes);
cudaMemcpy(pSource, pSourceData, AllocationSizeInBytes, cudaMemcpyHostToDevice);
nppiFilterGauss_8u_C1R(pSource, SourceStep, pDestination, DestinationStep, oSizeROI, NPP_MASK_SIZE_15_X_15);
cudaMemcpy(pResultData, pDestination, AllocationSizeInBytes, cudaMemcpyDeviceToHost);
}
Run Code Online (Sandbox Code Playgroud)
但 nppiFilterGaussAdvanced_8u_C1R 会损坏图像
void cuda_npp_gauss_filter_qt_advanced(uchar* pSourceData, uchar* pResultData, const int &ImageLineStep, const int &ImageWidth, const …Run Code Online (Sandbox Code Playgroud) 我对我的系统上安装的 CUDA 版本以及是否被我的软件有效使用有疑问。\n我在网上做了一些研究,但找不到解决我的疑问的方法。\n这个问题对我的理解有所帮助,并且是与我下面要问的问题最相关的是这个。
\n问题描述:
\n我使用 virtualenvironmentwrapper 创建了一个虚拟环境,然后在其中安装了 pytorch。
\n一段时间后,我意识到我的系统上没有安装 CUDA。
\n您可以通过执行以下操作找到它:
\nnvcc \xe2\x80\x93V
如果没有返回任何内容,则意味着您没有安装 CUDA(据我了解)。
\n因此,我按照这里的说明进行操作
\n我用这个官方链接安装了CUDA。
\n然后,我nvidia-development-kit简单地安装了
sudo apt install nvidia-cuda-toolkit
现在,如果在我的虚拟环境中我这样做:
\nnvcc -V
我得到:
\nnvcc: NVIDIA (R) Cuda compiler driver\nCopyright (c) 2005-2019 NVIDIA Corporation\nBuilt on Sun_Jul_28_19:07:16_PDT_2019\nCuda compilation tools, release 10.1, V10.1.243\nRun Code Online (Sandbox Code Playgroud)\n但是,如果(总是在虚拟环境中)我这样做:
\npython -c "import torch; print(torch.version.cuda)"
我得到:
\n10.2
这是我不明白的第一件事。我在虚拟环境中使用哪个版本的 CUDA?
\n …我尝试使用命令“conda install cudatoolkit = 11.7”安装cudatoolkit 11.7,但失败了。但是,网站https://pytorch.org/get-started/locally/显示 11.7 可用。我该怎么办才能解决它?谢谢
我想编写一个在 CPU 模式和 CUDA 设备模式下使用的 Numba 函数。当然,我可以使用和不使用 cuda.jit 装饰器编写两个相同的函数。例如:
from numba import cuda, njit
@njit("i4(i4, i4)")
def func_cpu(a, b)
return a + b
@cuda.jit("i4(i4, i4)", device=True)
def func_gpu(a, b)
return a + b
Run Code Online (Sandbox Code Playgroud)
但在软件工程中它是丑陋的。有没有一种更优雅的方式,即将代码组合在一个函数中?