zoukankan      html  css  js  c++  java
  • CUDA 实例练习(一)

    题目:将1000000个线程写入到10个数组。

    #include <stdio.h>
    #include <cuda_runtime.h>
    #include "device_launch_parameters.h"
    #include "gputimer.h"
    #define num_threads 1000000
    #define block_width 1000
    #define array_size 10
    void print_array(int * array, int size);
    __global__ void increment_atomic(int * g)
    {
        int i = blockIdx.x * blockDim.x + threadIdx.x;
        i = i % array_size;
        atomicAdd(&g[i], 1);
    }
    
    void print_array(int * array, int size)
    {
        for (int i = 0; i < size; i++) {
            printf("%d ", array[i]);
        }
    }
    
    int main()
    {
        GpuTimer timer;
        printf("%d total threads in %d blocks writing into %d arrays
    ", num_threads, num_threads / block_width, array_size);
    
        int h_array[array_size];
        const int array_bytes = array_size * sizeof(int);
    
        int * d_array;
        cudaMalloc((void **)&d_array, array_bytes);
        cudaMemset((void *)d_array, 0, array_bytes);
    
        timer.Start();
        increment_atomic << <num_threads / block_width, block_width >> >(d_array);
        timer.Stop();
    
        cudaMemcpy(h_array, d_array, array_bytes, cudaMemcpyDeviceToHost);
        print_array(h_array, array_size);
        printf("
    Time elapsed = %g ms
    ", timer.Elapsed());
        cudaFree(d_array);
    
        return 0;
    }

    编译环境:visual studio 2013

  • 相关阅读:
    Django终端打印SQL语句
    Django之缓存
    Django总结三
    Django之信号
    Django之Form的ModelForm
    git下
    Ajax实现文件的上传
    operator模块
    Django之Form自定义验证规则
    New Concept English Two 32 88
  • 原文地址:https://www.cnblogs.com/zhangshuwen/p/7145609.html
Copyright © 2011-2022 走看看