利用Block和Thread进行并行加速

_global_ void add(int *a, int *b, int *c)
{
int index = threadIdx.x + blockIdx.x * blockDim.x;
c[index] = a[index] + b[index];
} #define N (2048*2048)
#define THREAD_PER_BLOCK 512 int main()
{
int *a, *b, *c; //host copies of a, b, c
int *d_a, *d_b, *d_c; //device copies of a, b, c
int size = N * sizeof(int); //Alloc space for device copies of a, b, c
cudaMalloc((void **)&d_a, size);
cudaMalloc((void **)&d_b, size);
cudaMalloc((void **)&d_c, size); //Alloc space for host copies of a, b, c and setup input values
a = (int *)malloc(size); random_ints(a, N);
b = (int *)malloc(size); random_ints(b, N);
c = (int *)malloc(size); //Copy the data into device
cudeMemcpy(d_a, a, size, cudaMemcpyHostToDevice);
cudaMemcpy(d_b, b, size, cudaMemcpyHostToDevice); //Launch add() kernel on GPU with N blocks
add<<<N/THREADS_PER_BLOCK,THREADS_PER_BLOCK>>>(d_a, d_b, d_c); //Copy result back to host
cudaMemcpy(c, d_c, size, cudaMemcpyDeviceToHost); //Cleanup
free(a); free(b); free(c);
cudeFree(d_a); cudaFree(d_b); cudaFree(d_c);
return ;
} /**** What's the function of random_ints****/
void random_ints(int* a, int N)
{
int i;
for (i = ; i < N; ++i)
a[i] = rand();
}

CUDA编程学习(四)的更多相关文章

  1. CUDA编程学习笔记1

    CUDA编程模型是一个异构模型,需要CPU和GPU协同工作. host和device host和device是两个重要的概念 host指代CPU及其内存 device指代GPU及其内存 __globa ...

  2. CUDA编程学习相关

    1. CUDA编程之快速入门:https://www.cnblogs.com/skyfsm/p/9673960.html 2. CUDA编程入门极简教程:https://blog.csdn.net/x ...

  3. CUDA编程学习笔记2

    第二章 cuda代码写在.cu/.cuh里面 cuda 7.0 / 9.0开始,NVCC就支持c++11 / 14里面绝大部分的语言特性了. Dim3 __host__ __device__ dim3 ...

  4. CUDA编程学习(一)

    /****c code****/ #include<stdio.h> int main() { printf("Hello world!\n); ; } /****CUDA co ...

  5. cuda编程学习6——点积dot

    __shared__ float cache[threadPerBlock];//声明共享内存缓冲区,__shared__ __syncthreads();//对线程块中的线程进行同步,只有都完成前面 ...

  6. cuda编程学习5——波纹ripple

    /共有DIM×DIM个像素,每个像素对应一个线程dim3 blocks(DIM/16,DIM/16);//2维dim3 threads(16,16);//2维kernel<<<blo ...

  7. cuda编程学习4——Julia

    书上的例子编译会有错误,修改一下行即可. __device__ cuComplex(float a,float b):r(a),i(b){} /* ========================== ...

  8. cuda编程学习3——VectorSum

    这个程序是把两个向量相加 add<<<N,1>>>(dev_a,dev_b,dev_c);//<N,1>,第一个参数N代表block的数量,第二个参数1 ...

  9. cuda编程学习2——add

    cudaMalloc()分配的指针有使用限制,设备指针的使用限制总结如下: 1.可以将其传递给在设备上执行的函数 2.可以在设备代码中使用其进行内存的读写操作 3.可以将其传递给在主机上执行的函数 4 ...

随机推荐

  1. AFTER触发器与INSTEAD OF触发器

    在对表进行操作时,总会产生 INSERTED 和(或)DELETED表,不管这个操作是否已经进行.这里的和/或,要看进行的什么操作,插入,产生 INSERTED 表,删除,产生DELETED表,而up ...

  2. Effective Java 68 Prefer executors and tasks to threads

    Principle The general mechanism for executing tasks is the executor service. If you think in terms o ...

  3. System Center的一些资料收集

    MS 的 system center 中文首页 http://www.microsoft.com/zh-cn/server-cloud/system-center/default.aspx 英文首页 ...

  4. IIS管理

    1.缓存的处理 http://www.cnblogs.com/dudu/p/iis_user-mode_caching_cache-control_public.html 2.负载均衡的使用 ARR ...

  5. LightSpeed的批量更新和批量删除

    1.Update对于批量操作 无论是Update还是Remove  都是使用LightSpeed的Query对象来完成. 注:Student是要进行Update的表(实体),StuName是表Stud ...

  6. spring中的 classpath* 存在可移植性问题

    classpath* 的可移植性问题,许多人都应该遇到过了.下面就是一个例子(使用的是spring4.1.5和mybatis3.2.8): <bean id="sqlSessionFa ...

  7. hibernate一对多映射实现

    Junit4方法详解 setUpBeforeClass()类初始化前调用 tearDownAfterClass()类初始化后调用 setUp()在测试方法前调用 tearDown()在测试方法后调用 ...

  8. CListCtrl

    CListCtrl CCmdTarget     └CListCtrl CListCtrl类封装"列表视图控件"功能,显示每个包含图标(列表视图中)和标签的收集.除图标和标签外,每 ...

  9. Hadoop Yarn core concepts

    The fundamental idea of YARN is to split the two major responsibilities of the JobTracker—that is, r ...

  10. 自定义input[type="file"]的样式

    input[type="file"]的样式在各个浏览器中的表现不尽相同: 1. chrome: 2. firefox: 3. opera: 4. ie: 5. edge: 另外,当 ...