zoukankan      html  css  js  c++  java
  • 向量在同一个块中用多个线程执行

     项目打包下载

    向量在同一个块中用多个线程执行。

    本实验是启动一个线程块,这个线程块中启动的线程个数为10个。

     1 /*
     2 * Copyright 1993-2010 NVIDIA Corporation.  All rights reserved.
     3 *
     4 * NVIDIA Corporation and its licensors retain all intellectual property and
     5 * proprietary rights in and to this software and related documentation.
     6 * Any use, reproduction, disclosure, or distribution of this software
     7 * and related documentation without an express license agreement from
     8 * NVIDIA Corporation is strictly prohibited.
     9 *
    10 * Please refer to the applicable NVIDIA end user license agreement (EULA)
    11 * associated with this source code for terms and conditions that govern
    12 * your use of this NVIDIA software.
    13 *
    14 */
    15 
    16 
    17 #include "../common/book.h"
    18 #include "cuda.h"
    19 #include "cuda_runtime.h"
    20 #include "device_launch_parameters.h"
    21 #define N   10
    22 
    23 __global__ void add(int *a, int *b, int *c) {
    24     int tid = threadIdx.x;
    25     if (tid < N)
    26         c[tid] = a[tid] + b[tid];
    27 }
    28 
    29 int main(void) {
    30     int a[N], b[N], c[N];
    31     int *dev_a, *dev_b, *dev_c;
    32 
    33     // allocate the memory on the GPU
    34     HANDLE_ERROR(cudaMalloc((void**)&dev_a, N * sizeof(int)));
    35     HANDLE_ERROR(cudaMalloc((void**)&dev_b, N * sizeof(int)));
    36     HANDLE_ERROR(cudaMalloc((void**)&dev_c, N * sizeof(int)));
    37 
    38     // fill the arrays 'a' and 'b' on the CPU
    39     for (int i = 0; i<N; i++) {
    40         a[i] = i;
    41         b[i] = i * i;
    42     }
    43 
    44     // copy the arrays 'a' and 'b' to the GPU
    45     HANDLE_ERROR(cudaMemcpy(dev_a, a, N * sizeof(int), cudaMemcpyHostToDevice));
    46     HANDLE_ERROR(cudaMemcpy(dev_b, b, N * sizeof(int), cudaMemcpyHostToDevice));
    47 
    48     /*
    49     第一个参数1表示启动一个执行副本,也就是启动一个线程块
    50     第二个参数N表示每个线程块中启动的线程个数为N个
    51     */
    52     add << <1, N >> >(dev_a, dev_b, dev_c);
    53 
    54     // copy the array 'c' back from the GPU to the CPU
    55     HANDLE_ERROR(cudaMemcpy(c, dev_c, N * sizeof(int), cudaMemcpyDeviceToHost));
    56 
    57     // display the results
    58     for (int i = 0; i<N; i++) {
    59         printf("%d + %d = %d
    ", a[i], b[i], c[i]);
    60     }
    61 
    62     // free the memory allocated on the GPU
    63     HANDLE_ERROR(cudaFree(dev_a));
    64     HANDLE_ERROR(cudaFree(dev_b));
    65     HANDLE_ERROR(cudaFree(dev_c));
    66 
    67     return 0;
    68 }

     

  • 相关阅读:
    html笔记3
    html笔记2
    html学习第一天
    用Vue中的指令写一个对表格进行增加和删除
    Vue中的列表渲染
    Vue中的计算属性(computed)和侦听器(watch)
    Vue的模块语法
    vue-cli的搭建
    Vue的概念介绍
    React中函数组件和类组件的区别
  • 原文地址:https://www.cnblogs.com/liangliangdetianxia/p/3984981.html
Copyright © 2011-2022 走看看