cuda中的blockIdx,threadIdx,blockDim以及gridDim的使用

news/2024/10/17 14:23:08/

转载于:DSP Tian的博客

threadIdx是一个uint3类型,表示一个线程的索引。

blockIdx是一个uint3类型,表示一个线程块的索引,一个线程块中通常有多个线程。

blockDim是一个dim3类型,表示线程块的大小。

gridDim是一个dim3类型,表示网格的大小,一个网格中通常有多个线程块。

下面这张图比较清晰的表示的几个概念的关系:

cuda 通过<<< >>>符号来分配索引线程的方式,我知道的一共有15种索引方式。

下面程序展示了这15种索引方式:

复制代码
#include "cuda_runtime.h"
#include "device_launch_parameters.h"

#include <stdio.h>
#include <stdlib.h>
#include <iostream>

using namespace std;

//thread 1D
global void testThread1(int *c, const int *a, const int *b)
{
int i = threadIdx.x;
c[i] = b[i] - a[i];
}

//thread 2D
global void testThread2(int *c, const int *a, const int b)
{
int i = threadIdx.x + threadIdx.y
blockDim.x;
c[i] = b[i] - a[i];
}

//thread 3D
global void testThread3(int c, const int a, const int b)
{
int i = threadIdx.x + threadIdx.y
blockDim.x + threadIdx.z
blockDim.x
blockDim.y;
c[i] = b[i] - a[i];
}

//block 1D
global void testBlock1(int *c, const int *a, const int *b)
{
int i = blockIdx.x;
c[i] = b[i] - a[i];
}

//block 2D
global void testBlock2(int *c, const int *a, const int b)
{
int i = blockIdx.x + blockIdx.y
gridDim.x;
c[i] = b[i] - a[i];
}

//block 3D
global void testBlock3(int c, const int a, const int b)
{
int i = blockIdx.x + blockIdx.y
gridDim.x + blockIdx.z
gridDim.x
gridDim.y;
c[i] = b[i] - a[i];
}

//block-thread 1D-1D
global void testBlockThread1(int *c, const int *a, const int b)
{
int i = threadIdx.x + blockDim.x
blockIdx.x;
c[i] = b[i] - a[i];
}

//block-thread 1D-2D
global void testBlockThread2(int c, const int a, const int b)
{
int threadId_2D = threadIdx.x + threadIdx.y
blockDim.x;
int i = threadId_2D+ (blockDim.x
blockDim.y)
blockIdx.x;
c[i] = b[i] - a[i];
}

//block-thread 1D-3D
global void testBlockThread3(int c, const int a, const int b)
{
int threadId_3D = threadIdx.x + threadIdx.y
blockDim.x + threadIdx.z
blockDim.x
blockDim.y;
int i = threadId_3D + (blockDim.xblockDim.yblockDim.z)*blockIdx.x;
c[i] = b[i] - a[i];
}

//block-thread 2D-1D
global void testBlockThread4(int *c, const int a, const int b)
{
int blockId_2D = blockIdx.x + blockIdx.y
gridDim.x;
int i = threadIdx.x + blockDim.x
blockId_2D;
c[i] = b[i] - a[i];
}

//block-thread 3D-1D
global void testBlockThread5(int c, const int a, const int b)
{
int blockId_3D = blockIdx.x + blockIdx.y
gridDim.x + blockIdx.z
gridDim.x
gridDim.y;
int i = threadIdx.x + blockDim.x*blockId_3D;
c[i] = b[i] - a[i];
}

//block-thread 2D-2D
global void testBlockThread6(int c, const int a, const int b)
{
int threadId_2D = threadIdx.x + threadIdx.y
blockDim.x;
int blockId_2D = blockIdx.x + blockIdx.y
gridDim.x;
int i = threadId_2D + (blockDim.x
blockDim.y)*blockId_2D;
c[i] = b[i] - a[i];
}

//block-thread 2D-3D
global void testBlockThread7(int c, const int a, const int b)
{
int threadId_3D = threadIdx.x + threadIdx.y
blockDim.x + threadIdx.z
blockDim.x
blockDim.y;
int blockId_2D = blockIdx.x + blockIdx.y*gridDim.x;
int i = threadId_3D + (blockDim.xblockDim.yblockDim.z)*blockId_2D;
c[i] = b[i] - a[i];
}

//block-thread 3D-2D
global void testBlockThread8(int c, const int a, const int b)
{
int threadId_2D = threadIdx.x + threadIdx.y
blockDim.x;
int blockId_3D = blockIdx.x + blockIdx.y
gridDim.x + blockIdx.z
gridDim.x*gridDim.y;
int i = threadId_2D + (blockDim.xblockDim.y)blockId_3D;
c[i] = b[i] - a[i];
}

//block-thread 3D-3D
global void testBlockThread9(int c, const int a, const int b)
{
int threadId_3D = threadIdx.x + threadIdx.y
blockDim.x + threadIdx.z
blockDim.x
blockDim.y;
int blockId_3D = blockIdx.x + blockIdx.ygridDim.x + blockIdx.zgridDim.x*gridDim.y;
int i = threadId_3D + (blockDim.xblockDim.yblockDim.z)*blockId_3D;
c[i] = b[i] - a[i];
}

void addWithCuda(int *c, const int *a, const int *b, unsigned int size)
{
int *dev_a = 0;
int *dev_b = 0;
int *dev_c = 0;

cudaSetDevice(</span><span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">);cudaMalloc((</span><span style="color: rgba(0, 0, 255, 1)">void</span>**)&amp;dev_c, size * <span style="color: rgba(0, 0, 255, 1)">sizeof</span>(<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)">));
cudaMalloc((</span><span style="color: rgba(0, 0, 255, 1)">void</span>**)&amp;dev_a, size * <span style="color: rgba(0, 0, 255, 1)">sizeof</span>(<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)">));
cudaMalloc((</span><span style="color: rgba(0, 0, 255, 1)">void</span>**)&amp;dev_b, size * <span style="color: rgba(0, 0, 255, 1)">sizeof</span>(<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)">));cudaMemcpy(dev_a, a, size </span>* <span style="color: rgba(0, 0, 255, 1)">sizeof</span>(<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)">), cudaMemcpyHostToDevice);
cudaMemcpy(dev_b, b, size </span>* <span style="color: rgba(0, 0, 255, 1)">sizeof</span>(<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)">), cudaMemcpyHostToDevice);</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">testThread1&lt;&lt;&lt;1, size&gt;&gt;&gt;(dev_c, dev_a, dev_b);</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">uint3 s;s.x = size/5;s.y = 5;s.z = 1;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">testThread2 &lt;&lt;&lt;1,s&gt;&gt;&gt;(dev_c, dev_a, dev_b);</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">uint3 s; s.x = size / 10; s.y = 5; s.z = 2;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">testThread3&lt;&lt;&lt;1, s &gt;&gt;&gt;(dev_c, dev_a, dev_b);</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">testBlock1&lt;&lt;&lt;size,1 &gt;&gt;&gt;(dev_c, dev_a, dev_b);</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">uint3 s; s.x = size / 5; s.y = 5; s.z = 1;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">testBlock2&lt;&lt;&lt;s, 1 &gt;&gt;&gt;(dev_c, dev_a, dev_b);</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">uint3 s; s.x = size / 10; s.y = 5; s.z = 2;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">testBlock3&lt;&lt;&lt;s, 1 &gt;&gt;&gt;(dev_c, dev_a, dev_b);</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">testBlockThread1&lt;&lt;&lt;size/10, 10&gt;&gt;&gt;(dev_c, dev_a, dev_b);</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">uint3 s1; s1.x = size / 100; s1.y = 1; s1.z = 1;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">uint3 s2; s2.x = 10; s2.y = 10; s2.z = 1;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">testBlockThread2 &lt;&lt; &lt;s1, s2 &gt;&gt; &gt;(dev_c, dev_a, dev_b);</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">uint3 s1; s1.x = size / 100; s1.y = 1; s1.z = 1;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">uint3 s2; s2.x = 10; s2.y = 5; s2.z = 2;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">testBlockThread3 &lt;&lt; &lt;s1, s2 &gt;&gt; &gt;(dev_c, dev_a, dev_b);</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">uint3 s1; s1.x = 10; s1.y = 10; s1.z = 1;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">uint3 s2; s2.x = size / 100; s2.y = 1; s2.z = 1;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">testBlockThread4 &lt;&lt; &lt;s1, s2 &gt;&gt; &gt;(dev_c, dev_a, dev_b);</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">uint3 s1; s1.x = 10; s1.y = 5; s1.z = 2;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">uint3 s2; s2.x = size / 100; s2.y = 1; s2.z = 1;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">testBlockThread5 &lt;&lt; &lt;s1, s2 &gt;&gt; &gt;(dev_c, dev_a, dev_b);</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">uint3 s1; s1.x = size / 100; s1.y = 10; s1.z = 1;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">uint3 s2; s2.x = 5; s2.y = 2; s2.z = 1;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">testBlockThread6 &lt;&lt; &lt;s1, s2 &gt;&gt; &gt;(dev_c, dev_a, dev_b);</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">uint3 s1; s1.x = size / 100; s1.y = 5; s1.z = 1;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">uint3 s2; s2.x = 5; s2.y = 2; s2.z = 2;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">testBlockThread7 &lt;&lt; &lt;s1, s2 &gt;&gt; &gt;(dev_c, dev_a, dev_b);</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">uint3 s1; s1.x = 5; s1.y = 2; s1.z = 2;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">uint3 s2; s2.x = size / 100; s2.y = 5; s2.z = 1;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">testBlockThread8 &lt;&lt;&lt;s1, s2 &gt;&gt;&gt;(dev_c, dev_a, dev_b);</span>
uint3 s1; s1.x = 5; s1.y = 2; s1.z = 2 ; uint3 s2; s2.x = size / 200; s2.y = 5; s2.z = 2 ; testBlockThread9<<<s1, s2 >>> (dev_c, dev_a, dev_b);
cudaMemcpy(c, dev_c, size</span>*<span style="color: rgba(0, 0, 255, 1)">sizeof</span>(<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)">), cudaMemcpyDeviceToHost);cudaFree(dev_a);
cudaFree(dev_b);
cudaFree(dev_c);cudaGetLastError();

}

int main()
{
const int n = 1000;

</span><span style="color: rgba(0, 0, 255, 1)">int</span> *a = <span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)">[n];
</span><span style="color: rgba(0, 0, 255, 1)">int</span> *b = <span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)">[n];
</span><span style="color: rgba(0, 0, 255, 1)">int</span> *c = <span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)">[n];
</span><span style="color: rgba(0, 0, 255, 1)">int</span> *cc = <span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)">[n];</span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i = <span style="color: rgba(128, 0, 128, 1)">0</span>; i &lt; n; i++<span style="color: rgba(0, 0, 0, 1)">)
{a[i] </span>= rand() % <span style="color: rgba(128, 0, 128, 1)">100</span><span style="color: rgba(0, 0, 0, 1)">;b[i] </span>= rand() % <span style="color: rgba(128, 0, 128, 1)">100</span><span style="color: rgba(0, 0, 0, 1)">;c[i] </span>= b[i] -<span style="color: rgba(0, 0, 0, 1)"> a[i];
}addWithCuda(cc, a, b, n);FILE </span>*fp = fopen(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">out.txt</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">w</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i = <span style="color: rgba(128, 0, 128, 1)">0</span>; i &lt; n; i++<span style="color: rgba(0, 0, 0, 1)">)fprintf(fp, </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">%d %d\n</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">, c[i], cc[i]);
fclose(fp);</span><span style="color: rgba(0, 0, 255, 1)">bool</span> flag = <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i = <span style="color: rgba(128, 0, 128, 1)">0</span>; i &lt; n; i++<span style="color: rgba(0, 0, 0, 1)">)
{</span><span style="color: rgba(0, 0, 255, 1)">if</span> (c[i] !=<span style="color: rgba(0, 0, 0, 1)"> cc[i]){flag </span>= <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;</span><span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">;}
}</span><span style="color: rgba(0, 0, 255, 1)">if</span> (flag == <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">)printf(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">no pass</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">printf(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">pass</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);cudaDeviceReset();</span><span style="color: rgba(0, 0, 255, 1)">delete</span><span style="color: rgba(0, 0, 0, 1)">[] a;
</span><span style="color: rgba(0, 0, 255, 1)">delete</span><span style="color: rgba(0, 0, 0, 1)">[] b;
</span><span style="color: rgba(0, 0, 255, 1)">delete</span><span style="color: rgba(0, 0, 0, 1)">[] c;
</span><span style="color: rgba(0, 0, 255, 1)">delete</span><span style="color: rgba(0, 0, 0, 1)">[] cc;getchar();
</span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">;

}

复制代码

这里只保留了3D-3D方式,注释了其余14种方式,所有索引方式均测试通过。

还是能看出一些规律的:)


http://www.ppmy.cn/news/191441.html

相关文章

win10 + NVIDIA GeForce RTX 2080 Ti + CUDA10.0 + cuDNN v7.6.5

win10 NVIDIA GeForce RTX 2080 Ti CUDA10.0 cuDNN v7.6.5 配置教程 参考文献&#xff1a; 本文参考了一下文献&#xff1a; 显卡驱动版本号一定要与cuda版本号想对应,要不然tensorflow运行会报错&#xff1a; https://blog.csdn.net/qxqsunshine/article/details/9690195…

tensorflow2.10.0+CUDA11.2+cuDNN8.1 for cuda11.2

下载前记得查看tensorflow-gpu与CUDA、cuDNN对应的版本 https://tensorflow.google.cn/install/source_windows?hl=en#gpu tensorflow-gpu2.10.0 pip install tensorflow-gpu 后面也可以跟上对应的版本号,如果下载失败可能是网不好,多试几次,也可以找找其他教程。 CUD…

Geforce GTX 1660Ti + Ubuntu18.04 LTS + Nvidia显卡驱动 +CUDA10 配置安装

一、安装环境介绍 操作系统&#xff1a;Ubuntu 18.04.2 LTS 系统内核&#xff1a;linux-image-4.18.0-25-generic CPU&#xff1a;Intel Core i7-9750H 独立显卡&#xff1a;Geforce GTX 1660Ti 二、安装Nvidia显卡驱动 sudo apt remove --purge nvidia* …

dig命令笔记

dig 命令全称域信息搜索器&#xff0c;是一个用于查询 DNS 域名服务器信息的命令行工具。因为dig命令灵活&#xff0c;容易使用&#xff0c;多数DNS管理员使用dig命令来诊断 DNS 问题。 dig 常用命令格式 dig [server] [-p port] [-t type] [-4] [-6] [trace] name 指定 DNS …

ubuntu 18.04 RTX2080(ti) --- tensorflow-gpu + cuda9.0 + cudnn-9.0 (ubuntu 16.04, TITAN XP)

0.下载display driver、cuda和cudnn RTX2080 Display Driver cuda cudnn 版本对应关系 1. 禁止系统默认的显卡驱动 打开系统黑名单 sudo gedit /etc/modprobe.d/blacklist.conf将下列代码填入文件末尾 # for nvidia display driver install blacklist vga16fb blacklist n…

dig命令的介绍与使用

dig命令的使用 Reference&#xff1a; 1.Linux下解析域名命令-dig 命令使用详解 2.ubuntu/debian下安装使用dig 3.dig命令详解 一、基本介绍 dig&#xff08;domain information group&#xff09;是常用的域名查询工具&#xff0c;可以从DNS域名服务器查询主机地址信息&…

win10 安装dig工具与使用dig命令

文章目录 1、安装dig2、添加环境变量3、使用dig windows dig工具的安装与使用 Dig 工具全称为域名信息搜索器&#xff08;Domain Information Groper&#xff09;&#xff0c;能够显示详细的DNS查询过程&#xff0c;是一个非常强大的DNS故障诊断工具。一般Linux和Unix系统都已内…

ubuntu16.04 titan rtx 24g +显卡驱动+cuda10.1+cudnn环境配置

1. 显卡驱动安装 1.1 禁用Nouveau驱动 Nouveau为linux自带驱动&#xff0c;非官方驱动&#xff08;参考这里&#xff09;&#xff0c;若安装nvidia驱动&#xff0c;需将其禁掉&#xff0c;即将其加入黑名单&#xff1a; sudo apt-get updatesudo gedit /etc/modprobe.d/blac…