2. CUDA C
Язык C
void add (int* a, int* b, int* c)
{
int tid = 0;
while (tid < N)
{
c[tid]=a[tid]+b[tid];
tid++;
}
}
CUDA C
__device__ void add (int* a, int*
b, int* c)
{
int tid = blockIdx.x;
if (tid < N)
{
c[tid]=a[tid]+b[tid];
}
}
2
Сложение векторов длиной N
6. Виды памяти
6
Память Находится
на чипе
Кэширов
ание
Доступ Область
видимости
Время жизни
Register Да - R/W 1 нить Нить
Local Нет * R/W 1 нить Нить
Shared Да - R/W Все нити в
блоке
Блок
Global Нет * R/W Все нити+хост Определяется
хостом
Constant Нет Да R Все нити+хост Определяется
хостом
Texture Нет Да R Все нити+хост Определяется
хостом
* - кэшируется только на устройствах с CC 2.x
7. Что не реализовано в CUDAfy.NET?
Zero Copy
Texture Memory
Jugged Array
7
8. HelloWorld
Скачать и установить CUDAfy.NET
Компилятор cl v9.0 или v10.0
C:Program Files (x86)Microsoft Visual Studio 10.0VCbinvcvars32.bat
8
9. ErrorLaunchTimeOut
TdrDelay
Specifies the number of seconds that the GPU can delay the preempt
request from the GPU scheduler. This is effectively the timeout
threshold. The default value is 2 seconds.
KeyPath :
HKEY_LOCAL_MACHINESystemCurrentControlSetControlGraphicsDrivers
KeyValue : TdrDelay
ValueType : REG_DWORD
ValueData : Number of seconds to delay. 2 seconds is the default
value.
9
10. Про Context
Exception ErrorInvalidContext
GPGPU gpu = CudafyHost.GetDevice(CudafyModes.Target, CudafyModes.DeviceId);
gpu.LoadModule(km);
...
if (!gpu.IsCurrentContext)
{
gpu.SetCurrentContext();
}
10
11. Атрибуты CUDAfy
[Cudafy]
public static void Add(GThread thread, int[] a, int[] b, int[] c)
{
int tid = thread.threadIdx.x + thread.blockIdx.x * thread.blockDim.x;
while (tid < N)
{
CplusSquaredA(a, c, tid);
Cplus3rdB(b, c, tid);
tid += thread.blockDim.x*thread.gridDim.x;
}
}
[Cudafy(eCudafyType.Device)]
private static void CplusSquaredA(int[] a, int[] c, int tid)
{
c[tid] += a[tid] * a[tid];
}
[Cudafy(eCudafyType.Device)]
private static void Cplus3rdB(int[] b, int[] c, int tid)
{
c[tid] += b[tid] * b[tid] * b[tid];
}
11
12. Интеграция с Nvidia NSight
http://www.hybriddsp.com/Support/CudafyTutorials/UsingtheNVIDIANSightDebu
gger.aspx
- полное описание процесса подключения Nvidia Nsight.
12
13. Особенности алгоритмов,
выполняемых на видеокарте
Минимум обмена данными между хостом и устройством
Легковесный код
Одно «узкое место» в коде, которое можно разбить на блоки,
исполняемые одновременно
13