当前位置: 首页 > news >正文

深圳网站建设加盟百度上搜不到网站

深圳网站建设加盟,百度上搜不到网站,网上投资项目的平台有哪些,成都定制网站建目录 1. 解决的问题 2. 分析 3. 方法 4. 代码示例 1. 解决的问题 利用块和线程索引,从全局内存中访问指定的数据。 2. 分析 通常情况下,矩阵是用行优先的方法在全局内存中线性存储的。如下。 8列6行矩阵(nx,ny)(…

目录

1. 解决的问题

2. 分析

3. 方法

4. 代码示例


1. 解决的问题

利用块和线程索引,从全局内存中访问指定的数据。

2. 分析

通常情况下,矩阵是用行优先的方法在全局内存中线性存储的。如下。

8列6行矩阵(nx,ny)=(8,6)。

3. 方法

这里建立二维网格(2,3)+二维块(4,2)为例,使用其块和线程索引映射矩阵索引。

(1)第一步,可以用以下公式把线程和块索引映射到矩阵坐标上;

(2)第二步,可以用以下公式把矩阵坐标映射到全局内存中的索引/存储单元上;

比如要获取矩阵元素(col,row) = (2,4) ,其全局索引是34,映射到矩阵坐标上,

ix = 2 + 0*3=2; iy = 0 + 2*2=4. 然后再映射到全局内存idx = 4*8 + 2 = 34.

4. 代码示例

#include "cuda_runtime.h"
#include "device_launch_parameters.h"  // threadIdx#include <stdio.h>    // io
#include <time.h>     // time_t
#include <stdlib.h>  // rand
#include <memory.h>  //memset#define CHECK(call)                                   \
{                                                     \const cudaError_t error_code = call;              \if (error_code != cudaSuccess)                    \{                                                 \printf("CUDA Error:\n");                      \printf("    File:       %s\n", __FILE__);     \printf("    Line:       %d\n", __LINE__);     \printf("    Error code: %d\n", error_code);   \printf("    Error text: %s\n",                \cudaGetErrorString(error_code));          \exit(1);                                      \}                                                 \
}void initiaInt(int* p, const int N)
{for (int i = 0; i < N; i++){p[i] = i;}
}/// <summary>
/// 
/// </summary>
/// <param name="c">全局内存中线性存储的二维矩阵</param>
/// <param name="nx">列</param>
/// <param name="ny"></param>
void printMatrix(int* c, const int nx, const int ny)
{int* ic = c;printf("\n matrix: [%d, %d] \n", nx, ny);for (int i = 0; i < ny; i++){for (int j = 0; j < nx; j++){int cur_ele = ic[i * nx + j];printf("%d ", cur_ele);}printf("\n");}printf("\n");
}/// <summary>
/// 
/// </summary>
/// <param name="a">全局内存中是线性存储的</param>
/// <param name="nx">col</param>
/// <param name="ny"></param>
/// <returns></returns>
__global__ void printThreadIdx(int* a, const int nx, const int ny)
{// 矩阵行列int ix = threadIdx.x + blockIdx.x * blockDim.x;int iy = threadIdx.y + blockIdx.y * blockDim.y;  // 全局索引unsigned int idx = iy * nx + ix;  // 前面有iy行,每行有nx个数。printf("thread_idx (%d, %d) block_idx (%d, %d) coordinate (%d, %d) global index %d val %d\n",threadIdx.x, threadIdx.y, blockIdx.x, blockIdx.y, ix, iy, idx, a[idx]);
}int main(void)
{// get device infoint device = 0;cudaDeviceProp deviceProp;CHECK(cudaGetDeviceProperties(&deviceProp, device));printf("Using device: %d %s", device, deviceProp.name);  // 卡号0的显卡名称。CHECK(cudaSetDevice(device));  // 设置显卡号// set matrix dimensionint nx = 8, ny =6, nxy = nx * ny;int nBytes = nxy * sizeof(int);// malloc host memoryint* h_a;h_a = (int*)malloc(nBytes);// init datainitiaInt(h_a, nxy);printMatrix(h_a, nx, ny);// malloc device memoryint* d_Mat_a;cudaMalloc((void**)&d_Mat_a, nBytes);// transfer data from host to devicecudaMemcpy(d_Mat_a, h_a, nBytes, cudaMemcpyHostToDevice);// configdim3 block(4, 2);  // 二维线程块(x,y)=(4,2)dim3 grid((nx+block.x-1) / block.x, (ny+block.y-1)/block.y); // 二维网格(2,3)// 直接nx/block.x = 8/4=2. (8+4-1)/4=2.// invoke kernelprintThreadIdx << <grid, block >> > (d_Mat_a, nx, ny);cudaDeviceSynchronize();// free memorycudaFree(d_Mat_a);free(h_a);// reset devicecudaDeviceReset();return 0;
}

可以看到,全局索引值就是矩阵中存储的值。 

http://www.yayakq.cn/news/721919/

相关文章:

  • 怎么在网站做浮动图标武陟外贸英文网站建设
  • 网站建设中 敬请期待 源码中国信息网官网查询系统
  • 怎么找到要做网站推广的客户资源C 做的窗体怎么变成网站
  • 良品铺子网站建设目标ai做网站步骤
  • 优质的中小型网站建设网络舆情监测制度
  • 网站建设顶层设计食品厂招男女工5000
  • 上海浦东新区科技网站建设《奖励自己的网站》
  • 成都高端网站开发互站网站源码
  • 哪个网站可以做教师招聘题目湛江在线网
  • 巴中交通建设有限公司网站手机版做网站
  • 网站建设图标素材免费深圳最便宜的物流公司
  • 网站建设促销文案高端建网站
  • 湖南怀化市住房城乡建设局网站wordpress整站生成html
  • 如何做贷款网站windows优化大师电脑版
  • 杨凌网站建设推广百度关键词排名代发
  • 安徽省住房建设工程信息网站找大连做企业网站的公司
  • 公司推广发帖网站怎么做天辰建设工程信息网
  • 谷歌系平台推广seo计费系统源码
  • 一比一高仿手表网站建立自我追求无我是什么意思
  • 做网站和做新媒体运营网站 防攻击
  • 网站子目录设计做公司网站的企业
  • 国外私人网站2024年新冠会消失吗
  • linux建设网站php打开提示404手机网站用户体验
  • 大气 网站源码微信营销要怎么做
  • 网站工作沟通及建设网站建设板块如何分类
  • 那家公司做网站会议网站定制
  • 网站备案和服务器备案如何在网上推广网站
  • 公司网站建设的相关费用入账模拟建设官方网站
  • 考研培训太原网站优化多少钱
  • 做外贸有哪些免费的网站有哪些北京度seo排名